tenant: add member management APIs

This commit is contained in:
2025-12-18 17:29:17 +08:00
parent 5029234e47
commit de574bbd9d
11 changed files with 815 additions and 14 deletions

View File

@@ -15,6 +15,8 @@ import (
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"go.ipao.vip/gen"
"go.ipao.vip/gen/types"
"gorm.io/gorm"
)
// tenant implements tenant-related domain operations.
@@ -35,9 +37,27 @@ func (t *tenant) ContainsUserID(ctx context.Context, tenantID, userID int64) (*m
// AddUser
func (t *tenant) AddUser(ctx context.Context, tenantID, userID int64) error {
logrus.WithFields(logrus.Fields{
"tenant_id": tenantID,
"user_id": userID,
}).Info("services.tenant.add_user")
// 幂等:若成员关系已存在,则直接返回成功,避免重复插入触发唯一约束错误。
tbl, query := models.TenantUserQuery.QueryContext(ctx)
_, err := query.Where(tbl.TenantID.Eq(tenantID), tbl.UserID.Eq(userID)).First()
if err == nil {
return nil
}
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return errors.Wrapf(err, "AddUser failed to query existing, tenantID: %d, userID: %d", tenantID, userID)
}
// 关键默认值:加入租户默认成为 member并设置为 verified避免 DB 默认值与枚举不一致导致脏数据)。
tenantUser := &models.TenantUser{
TenantID: tenantID,
UserID: userID,
Role: types.NewArray([]consts.TenantUserRole{consts.TenantUserRoleMember}),
Status: consts.UserStatusVerified,
}
if err := tenantUser.Create(ctx); err != nil {
@@ -69,7 +89,8 @@ func (t *tenant) SetUserRole(ctx context.Context, tenantID, userID int64, role .
return errors.Wrapf(err, "SetUserRole failed to find, tenantID: %d, userID: %d", tenantID, userID)
}
tenantUser.Role = role
// 角色更新:当前约定 role 数组通常只存一个主角色member/tenant_admin
tenantUser.Role = types.NewArray(role)
if _, err := tenantUser.Update(ctx); err != nil {
return errors.Wrapf(err, "SetUserRole failed to update, tenantID: %d, userID: %d", tenantID, userID)
}