2021-06-07 17:22:07 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"context"
|
2023-12-18 22:39:32 +08:00
|
|
|
|
"mayfly-go/internal/sys/consts"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/sys/domain/repository"
|
2024-04-28 23:45:57 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"mayfly-go/pkg/contextx"
|
2023-12-18 22:39:32 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-07-27 16:47:05 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
|
|
|
|
|
"time"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type Role interface {
|
2024-04-28 23:45:57 +08:00
|
|
|
|
base.App[*entity.Role]
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
|
|
|
|
|
|
|
|
|
|
|
ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
SaveRole(ctx context.Context, role *entity.Role) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
DeleteRole(ctx context.Context, id uint64) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
GetRoleResourceIds(roleId uint64) []uint64
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
GetRoleResources(roleId uint64, toEntity any)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2024-11-20 22:43:53 +08:00
|
|
|
|
GetResourceRoles(resourceId uint64) ([]*entity.RoleResource, error)
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 保存角色资源关联记录
|
2024-04-28 23:45:57 +08:00
|
|
|
|
SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
// 关联账号角色
|
|
|
|
|
|
RelateAccountRole(ctx context.Context, accountId, roleId uint64, relateType consts.AccountRoleRelateType) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
// 获取账号关联角色
|
|
|
|
|
|
GetAccountRoles(accountId uint64) ([]*entity.AccountRole, error)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
// 获取角色关联的用户信息
|
|
|
|
|
|
GetRoleAccountPage(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type roleAppImpl struct {
|
2024-04-28 23:45:57 +08:00
|
|
|
|
base.AppImpl[*entity.Role, repository.Role]
|
|
|
|
|
|
|
|
|
|
|
|
accountRoleRepo repository.AccountRole `inject:"AccountRoleRepo"`
|
|
|
|
|
|
roleResourceRepo repository.RoleResource `inject:"RoleResourceRepo"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-20 22:43:53 +08:00
|
|
|
|
var _ (Role) = (*roleAppImpl)(nil)
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
func (m *roleAppImpl) GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
2024-04-28 23:45:57 +08:00
|
|
|
|
return m.GetRepo().GetPageList(condition, pageParam, toEntity, orderBy...)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
func (m *roleAppImpl) ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) {
|
2024-04-28 23:45:57 +08:00
|
|
|
|
return m.GetRepo().ListByQuery(condition)
|
2023-12-18 22:39:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (m *roleAppImpl) SaveRole(ctx context.Context, role *entity.Role) error {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
if role.Id != 0 {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// code不可更改,防止误传
|
|
|
|
|
|
role.Code = ""
|
2024-04-28 23:45:57 +08:00
|
|
|
|
return m.UpdateById(ctx, role)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
role.Status = 1
|
2024-04-28 23:45:57 +08:00
|
|
|
|
return m.Insert(ctx, role)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (m *roleAppImpl) DeleteRole(ctx context.Context, id uint64) error {
|
2023-12-18 22:39:32 +08:00
|
|
|
|
// 删除角色与资源账号的关联关系
|
2024-04-28 23:45:57 +08:00
|
|
|
|
return m.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return m.DeleteById(ctx, id)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
return m.roleResourceRepo.DeleteByCond(ctx, &entity.RoleResource{RoleId: id})
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
return m.accountRoleRepo.DeleteByCond(ctx, &entity.AccountRole{RoleId: id})
|
|
|
|
|
|
})
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (m *roleAppImpl) GetRoleResourceIds(roleId uint64) []uint64 {
|
2024-04-28 23:45:57 +08:00
|
|
|
|
return m.roleResourceRepo.GetRoleResourceIds(roleId)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func (m *roleAppImpl) GetRoleResources(roleId uint64, toEntity any) {
|
2024-04-28 23:45:57 +08:00
|
|
|
|
m.roleResourceRepo.GetRoleResources(roleId, toEntity)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-20 22:43:53 +08:00
|
|
|
|
func (m *roleAppImpl) GetResourceRoles(resourceId uint64) ([]*entity.RoleResource, error) {
|
|
|
|
|
|
return m.roleResourceRepo.SelectByCond(&entity.RoleResource{ResourceId: resourceId})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-28 23:45:57 +08:00
|
|
|
|
func (m *roleAppImpl) SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64) error {
|
2023-07-27 16:47:05 +08:00
|
|
|
|
oIds := m.GetRoleResourceIds(roleId)
|
|
|
|
|
|
|
2024-01-13 13:38:53 +08:00
|
|
|
|
addIds, delIds, _ := collx.ArrayCompare(resourceIds, oIds)
|
2023-07-27 16:47:05 +08:00
|
|
|
|
|
|
|
|
|
|
la := contextx.GetLoginAccount(ctx)
|
|
|
|
|
|
createTime := time.Now()
|
|
|
|
|
|
creator := la.Username
|
|
|
|
|
|
creatorId := la.Id
|
|
|
|
|
|
undeleted := model.ModelUndeleted
|
|
|
|
|
|
|
|
|
|
|
|
addVals := make([]*entity.RoleResource, 0)
|
|
|
|
|
|
for _, v := range addIds {
|
|
|
|
|
|
rr := &entity.RoleResource{RoleId: roleId, ResourceId: v, CreateTime: &createTime, CreatorId: creatorId, Creator: creator}
|
|
|
|
|
|
rr.IsDeleted = undeleted
|
|
|
|
|
|
addVals = append(addVals, rr)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-28 23:45:57 +08:00
|
|
|
|
return m.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
if len(addVals) > 0 {
|
|
|
|
|
|
return m.roleResourceRepo.BatchInsert(ctx, addVals)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
if len(delIds) > 0 {
|
|
|
|
|
|
return m.roleResourceRepo.DeleteByCond(ctx, model.NewCond().In("resource_id", delIds))
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
})
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
func (m *roleAppImpl) RelateAccountRole(ctx context.Context, accountId, roleId uint64, relateType consts.AccountRoleRelateType) error {
|
|
|
|
|
|
accountRole := &entity.AccountRole{AccountId: accountId, RoleId: roleId}
|
|
|
|
|
|
if relateType == consts.AccountRoleUnbind {
|
2024-01-23 19:30:28 +08:00
|
|
|
|
return m.accountRoleRepo.DeleteByCond(ctx, accountRole)
|
2023-12-18 22:39:32 +08:00
|
|
|
|
}
|
2023-07-27 16:47:05 +08:00
|
|
|
|
|
2024-04-28 23:45:57 +08:00
|
|
|
|
err := m.accountRoleRepo.GetByCond(accountRole)
|
2023-12-18 22:39:32 +08:00
|
|
|
|
if err == nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return errorx.NewBiz("The user already owns the role")
|
2023-12-18 22:39:32 +08:00
|
|
|
|
}
|
2023-07-27 16:47:05 +08:00
|
|
|
|
|
|
|
|
|
|
la := contextx.GetLoginAccount(ctx)
|
|
|
|
|
|
createTime := time.Now()
|
2023-12-18 22:39:32 +08:00
|
|
|
|
accountRole.Creator = la.Username
|
|
|
|
|
|
accountRole.CreatorId = la.Id
|
|
|
|
|
|
accountRole.CreateTime = &createTime
|
2024-01-23 19:30:28 +08:00
|
|
|
|
return m.accountRoleRepo.Insert(ctx, accountRole)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
func (m *roleAppImpl) GetAccountRoles(accountId uint64) ([]*entity.AccountRole, error) {
|
2024-05-05 14:53:30 +08:00
|
|
|
|
return m.accountRoleRepo.SelectByCond(&entity.AccountRole{AccountId: accountId})
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
func (m *roleAppImpl) GetRoleAccountPage(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
2024-01-23 19:30:28 +08:00
|
|
|
|
return m.accountRoleRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|