tenant: add member management APIs
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
"quyun/v2/pkg/utils"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
@@ -48,3 +49,52 @@ func (t *TenantTestSuite) Test_TenantUserCount() {
|
||||
t.T().Logf("%s", utils.MustJsonString(result))
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TenantTestSuite) Test_AddUser() {
|
||||
Convey("Tenant.AddUser", t.T(), func() {
|
||||
ctx := t.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
database.Truncate(ctx, t.DB, models.TableNameTenantUser)
|
||||
|
||||
Convey("首次添加成员成功", func() {
|
||||
err := Tenant.AddUser(ctx, tenantID, userID)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
m, err := Tenant.FindTenantUser(ctx, tenantID, userID)
|
||||
So(err, ShouldBeNil)
|
||||
So(m, ShouldNotBeNil)
|
||||
So(m.TenantID, ShouldEqual, tenantID)
|
||||
So(m.UserID, ShouldEqual, userID)
|
||||
})
|
||||
|
||||
Convey("重复添加应幂等返回成功", func() {
|
||||
So(Tenant.AddUser(ctx, tenantID, userID), ShouldBeNil)
|
||||
So(Tenant.AddUser(ctx, tenantID, userID), ShouldBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TenantTestSuite) Test_SetUserRole() {
|
||||
Convey("Tenant.SetUserRole", t.T(), func() {
|
||||
ctx := t.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
database.Truncate(ctx, t.DB, models.TableNameTenantUser)
|
||||
|
||||
So(Tenant.AddUser(ctx, tenantID, userID), ShouldBeNil)
|
||||
|
||||
Convey("设置为 tenant_admin 成功", func() {
|
||||
err := Tenant.SetUserRole(ctx, tenantID, userID, consts.TenantUserRoleTenantAdmin)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
m, err := Tenant.FindTenantUser(ctx, tenantID, userID)
|
||||
So(err, ShouldBeNil)
|
||||
So(m, ShouldNotBeNil)
|
||||
So(len(m.Role), ShouldEqual, 1)
|
||||
So(m.Role[0], ShouldEqual, consts.TenantUserRoleTenantAdmin)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user