1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
authing_tenant.go 4.56 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
package authing_v2
import (
"errors"
"fmt"
"log"
"net/http"
"gitee.com/carlmax_my/console-core-go/pkg/authing/authing_model"
"github.com/Authing/authing-go-sdk/lib/model"
jsoniter "github.com/json-iterator/go"
)
func ConvertTenantModel(m *model.Tenant) *authing_model.Tenant {
return &authing_model.Tenant{
ID: m.ID,
Name: m.Name,
UserPoolID: m.UserPoolID,
}
}
func ConvertTenantDetailModel(m *model.TenantDetails) *authing_model.TenantDetails {
var apps []authing_model.TenantApp
if len(m.Apps) > 0 {
for _, v := range m.Apps {
apps = append(apps, authing_model.TenantApp{
ID: v.ID,
Name: v.Name,
Secret: v.Secret,
})
}
}
return &authing_model.TenantDetails{
Tenant: authing_model.Tenant{
ID: m.ID,
Name: m.Name,
UserPoolID: m.UserPoolID,
},
Apps: apps,
}
}
// 获取用户池下租户列表
func (a *AuthingCli) GetTenantList() (list *[]authing_model.Tenant) {
var tenantList []authing_model.Tenant
resp, _err := a.Cli.GetTenantList(&model.CommonPageRequest{
Page: 1,
Limit: -1,
})
if _err != nil {
log.Println("GetTenantList err:" + _err.Error())
return
}
for _, tenant := range resp.List {
m := ConvertTenantModel(&tenant)
tenantList = append(tenantList, *m)
}
return &tenantList
}
func (a *AuthingCli) GetAppTenantList() (list *[]authing_model.TenantDetails, err error) {
var tenantList []authing_model.TenantDetails
resp, _err := a.Cli.GetTenantList(&model.CommonPageRequest{
Page: 1,
Limit: -1,
})
if _err != nil {
log.Println("GetTenantList err:" + _err.Error())
return nil, err
}
for _, tenant := range resp.List {
if tenantDetail, _err := a.Cli.GetTenantDetails(tenant.ID); _err == nil {
for _, app := range tenantDetail.Apps {
if app.ID == a.AuthCli.AppId {
// log.Println(tenant.ID)
m := ConvertTenantDetailModel(tenantDetail)
tenantList = append(tenantList, *m)
}
}
}
}
return &tenantList, nil
}
func (a *AuthingCli) AddTenantMembers(tenantId, userId *string) error {
_, err := a.Cli.AddTenantMembers(*tenantId, []string{*userId})
return err
}
func (a *AuthingCli) CreateTenant(tenantInfo *authing_model.CreateTenantRequest) (*authing_model.TenantDetails, error) {
req := &model.CreateTenantRequest{
Name: tenantInfo.Name,
AppIds: tenantInfo.AppIds,
Description: tenantInfo.Description,
Logo: tenantInfo.Logo,
}
resp, err := a.Cli.CreateTenant(req)
if err != nil {
return nil, err
}
return &authing_model.TenantDetails{
Tenant: authing_model.Tenant{
ID: resp.ID,
UserPoolID: resp.UserPoolID,
Name: resp.Name,
},
Apps: nil,
}, nil
}
func (a *AuthingCli) UpdateTenant(id *string, tenantInfo *authing_model.CreateTenantRequest) (bool, error) {
req := &model.CreateTenantRequest{
Name: tenantInfo.Name,
AppIds: tenantInfo.AppIds,
Description: tenantInfo.Description,
Logo: tenantInfo.Logo,
}
return a.Cli.UpdateTenant(*id, req)
}
func (a *AuthingCli) UpdateTenant_ExtraInfo(tenantId string, request *authing_model.UpdateTenantExtraReq) (bool, error) {
c := a.Cli
data, _ := jsoniter.Marshal(request)
variables := make(map[string]interface{})
jsoniter.Unmarshal(data, &variables)
url := fmt.Sprintf("%s/api/v2/tenant/%s", c.Host, tenantId)
b, err := c.SendHttpRestRequest(url, http.MethodPost, variables)
if err != nil {
return false, err
}
resp := &struct {
Message string `json:"message"`
Code int64 `json:"code"`
Data bool `json:"data"`
}{}
jsoniter.Unmarshal(b, &resp)
if resp.Code != 200 {
return false, errors.New(resp.Message)
}
return true, nil
}
func (a *AuthingCli) RemoveTenant(tenantId *string) (*string, error) {
return a.Cli.RemoveTenant(*tenantId)
}
func (a *AuthingCli) GetTenantMembers(tenantId *string, userMap map[string]bool, tenantUserMap map[string][]string) (list *[]model.User) {
var userList []model.User
resp, _err := a.Cli.GetTenantMembers(*tenantId, &model.CommonPageRequest{
Page: 1,
Limit: -1,
})
if _err != nil {
log.Println("GetTenantMembers err:" + _err.Error())
return
}
if _, ok := tenantUserMap[*tenantId]; !ok {
tenantUserMap[*tenantId] = make([]string, 0)
}
for _, usr := range resp.List {
uid := usr.User.Id
tid := usr.TenantID
if tid != *tenantId {
continue
}
tenantUserMap[tid] = append(tenantUserMap[tid], uid)
if _, ok := userMap[uid]; !ok {
userList = append(userList, *usr.User)
userMap[uid] = true
}
}
return &userList
}
func (a *AuthingCli) UpdateTenant_loginPage(tenantId string, loginCfg *authing_model.MyAuthingLoginConfig) error {
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.37

搜索帮助