mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 00:10:25 +08:00
refactor: 系统模块角色分配相关优化
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"mayfly-go/internal/sys/api/form"
|
||||
"mayfly-go/internal/sys/api/vo"
|
||||
"mayfly-go/internal/sys/application"
|
||||
"mayfly-go/internal/sys/consts"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/contextx"
|
||||
@@ -91,10 +92,7 @@ func (a *Account) ChangePassword(rc *req.Ctx) {
|
||||
func (a *Account) AccountInfo(rc *req.Ctx) {
|
||||
ap := new(vo.AccountPersonVO)
|
||||
// 角色信息
|
||||
roles := new([]vo.AccountRoleVO)
|
||||
a.RoleApp.GetAccountRoles(rc.GetLoginAccount().Id, roles)
|
||||
|
||||
ap.Roles = *roles
|
||||
ap.Roles = a.getAccountRoles(rc.GetLoginAccount().Id)
|
||||
rc.ResData = ap
|
||||
}
|
||||
|
||||
@@ -173,16 +171,47 @@ func (a *Account) DeleteAccount(rc *req.Ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取账号角色id列表,用户回显角色分配
|
||||
func (a *Account) AccountRoleIds(rc *req.Ctx) {
|
||||
rc.ResData = a.RoleApp.GetAccountRoleIds(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
|
||||
// 获取账号角色信息列表
|
||||
func (a *Account) AccountRoles(rc *req.Ctx) {
|
||||
rc.ResData = a.getAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
|
||||
}
|
||||
|
||||
// 获取账号角色id列表,用户回显角色分配
|
||||
func (a *Account) AccountRoles(rc *req.Ctx) {
|
||||
vos := new([]vo.AccountRoleVO)
|
||||
a.RoleApp.GetAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id")), vos)
|
||||
rc.ResData = vos
|
||||
func (a *Account) getAccountRoles(accountId uint64) []*vo.AccountRoleVO {
|
||||
vos := make([]*vo.AccountRoleVO, 0)
|
||||
|
||||
accountRoles, err := a.RoleApp.GetAccountRoles(accountId)
|
||||
biz.ErrIsNil(err)
|
||||
|
||||
if len(accountRoles) == 0 {
|
||||
return vos
|
||||
}
|
||||
|
||||
// 获取角色信息进行组装
|
||||
roleIds := collx.ArrayMap[*entity.AccountRole, uint64](accountRoles, func(val *entity.AccountRole) uint64 {
|
||||
return val.RoleId
|
||||
})
|
||||
roles, err := a.RoleApp.ListByQuery(&entity.RoleQuery{Ids: roleIds})
|
||||
biz.ErrIsNil(err)
|
||||
roleId2Role := collx.ArrayToMap[*entity.Role, uint64](roles, func(val *entity.Role) uint64 {
|
||||
return val.Id
|
||||
})
|
||||
|
||||
for _, ac := range accountRoles {
|
||||
role := roleId2Role[ac.RoleId]
|
||||
if role == nil {
|
||||
continue
|
||||
}
|
||||
vos = append(vos, &vo.AccountRoleVO{
|
||||
RoleId: ac.RoleId,
|
||||
RoleName: role.Name,
|
||||
Code: role.Code,
|
||||
Status: role.Status,
|
||||
CreateTime: ac.CreateTime, // 分配时间
|
||||
Creator: ac.Creator, // 分配者
|
||||
})
|
||||
}
|
||||
|
||||
return vos
|
||||
}
|
||||
|
||||
func (a *Account) AccountResources(rc *req.Ctx) {
|
||||
@@ -192,19 +221,13 @@ func (a *Account) AccountResources(rc *req.Ctx) {
|
||||
rc.ResData = resources.ToTrees(0)
|
||||
}
|
||||
|
||||
// 保存账号角色信息
|
||||
func (a *Account) SaveRoles(rc *req.Ctx) {
|
||||
// 关联账号角色
|
||||
func (a *Account) RelateRole(rc *req.Ctx) {
|
||||
var form form.AccountRoleForm
|
||||
ginx.BindJsonAndValid(rc.GinCtx, &form)
|
||||
rc.ReqParam = form
|
||||
|
||||
// 将,拼接的字符串进行切割并转换
|
||||
newIds := collx.ArrayMap[string, uint64](strings.Split(form.RoleIds, ","), func(val string) uint64 {
|
||||
id, _ := strconv.Atoi(val)
|
||||
return uint64(id)
|
||||
})
|
||||
|
||||
a.RoleApp.SaveAccountRole(rc.MetaCtx, form.Id, newIds)
|
||||
biz.ErrIsNil(a.RoleApp.RelateAccountRole(rc.MetaCtx, form.Id, form.RoleId, consts.AccountRoleRelateType(form.RelateType)))
|
||||
}
|
||||
|
||||
// 重置otp秘钥
|
||||
|
||||
@@ -17,6 +17,7 @@ type RoleForm struct {
|
||||
|
||||
// 账号分配角色表单
|
||||
type AccountRoleForm struct {
|
||||
Id uint64 `json:"id" binding:"required"`
|
||||
RoleIds string `json:"roleIds"`
|
||||
Id uint64 `json:"id" binding:"required"`
|
||||
RoleId uint64 `json:"roleId" binding:"required"`
|
||||
RelateType int `json:"relateType" binding:"required"`
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/anyx"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -20,8 +21,16 @@ type Role struct {
|
||||
|
||||
func (r *Role) Roles(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
condition := &entity.Role{Name: g.Query("name")}
|
||||
res, err := r.RoleApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Role))
|
||||
cond, pageParam := ginx.BindQueryAndPage(g, new(entity.RoleQuery))
|
||||
|
||||
notIdsStr := g.Query("notIds")
|
||||
if notIdsStr != "" {
|
||||
cond.NotIds = collx.ArrayMap[string, uint64](strings.Split(notIdsStr, ","), func(val string) uint64 {
|
||||
return uint64(anyx.ConvInt(val))
|
||||
})
|
||||
}
|
||||
|
||||
res, err := r.RoleApp.GetPageList(cond, pageParam, new([]entity.Role))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
@@ -77,3 +86,14 @@ func (r *Role) SaveResource(rc *req.Ctx) {
|
||||
|
||||
r.RoleApp.SaveRoleResource(rc.MetaCtx, form.Id, newIds)
|
||||
}
|
||||
|
||||
// 查看角色关联的用户
|
||||
func (r *Role) RoleAccount(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
cond, pageParam := ginx.BindQueryAndPage[*entity.RoleAccountQuery](g, new(entity.RoleAccountQuery))
|
||||
cond.RoleId = uint64(ginx.PathParamInt(g, "id"))
|
||||
var accounts []*vo.AccountRoleVO
|
||||
res, err := r.RoleApp.GetRoleAccountPage(cond, pageParam, &accounts)
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
@@ -16,13 +16,19 @@ type AccountManageVO struct {
|
||||
|
||||
// 账号角色信息
|
||||
type AccountRoleVO struct {
|
||||
Name *string `json:"name"`
|
||||
Status int `json:"status"`
|
||||
CreateTime *time.Time `json:"createTime"`
|
||||
Creator string `json:"creator"`
|
||||
RoleId uint64 `json:"roleId"`
|
||||
RoleName string `json:"roleName"`
|
||||
Code string `json:"code"`
|
||||
Status int `json:"status"`
|
||||
AccountId uint64 `json:"accountId" gorm:"column:accountId"`
|
||||
AccountName string `json:"accountName" gorm:"column:accountName"`
|
||||
Username string `json:"username"`
|
||||
AccountStatus int `json:"accountStatus" gorm:"column:accountStatus"`
|
||||
CreateTime *time.Time `json:"createTime"`
|
||||
Creator string `json:"creator"`
|
||||
}
|
||||
|
||||
// 账号个人信息
|
||||
type AccountPersonVO struct {
|
||||
Roles []AccountRoleVO `json:"roles"` // 角色信息
|
||||
Roles []*AccountRoleVO `json:"roles"` // 角色信息
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ var (
|
||||
accountApp = newAccountApp(persistence.GetAccountRepo())
|
||||
configApp = newConfigApp(persistence.GetConfigRepo())
|
||||
resourceApp = newResourceApp(persistence.GetResourceRepo())
|
||||
roleApp = newRoleApp(persistence.GetRoleRepo())
|
||||
roleApp = newRoleApp(persistence.GetRoleRepo(), persistence.GetAccountRoleRepo())
|
||||
syslogApp = newSyslogApp(persistence.GetSyslogRepo())
|
||||
)
|
||||
|
||||
|
||||
@@ -2,20 +2,23 @@ package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mayfly-go/internal/sys/consts"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/contextx"
|
||||
"mayfly-go/pkg/errorx"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Role interface {
|
||||
GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
|
||||
ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error)
|
||||
|
||||
SaveRole(ctx context.Context, role *entity.Role) error
|
||||
|
||||
@@ -28,36 +31,37 @@ type Role interface {
|
||||
// 保存角色资源关联记录
|
||||
SaveRoleResource(ctx context.Context, roleId uint64, resourceIds []uint64)
|
||||
|
||||
// 删除角色资源关联记录
|
||||
DeleteRoleResource(ctx context.Context, roleId uint64, resourceId uint64)
|
||||
// 关联账号角色
|
||||
RelateAccountRole(ctx context.Context, accountId, roleId uint64, relateType consts.AccountRoleRelateType) error
|
||||
|
||||
// 获取账号角色id列表
|
||||
GetAccountRoleIds(accountId uint64) []uint64
|
||||
// 获取账号关联角色
|
||||
GetAccountRoles(accountId uint64) ([]*entity.AccountRole, error)
|
||||
|
||||
// 保存账号角色关联信息
|
||||
SaveAccountRole(ctx context.Context, accountId uint64, roleIds []uint64)
|
||||
|
||||
DeleteAccountRole(ctx context.Context, accountId, roleId uint64)
|
||||
|
||||
GetAccountRoles(accountId uint64, toEntity any)
|
||||
// 获取角色关联的用户信息
|
||||
GetRoleAccountPage(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
}
|
||||
|
||||
func newRoleApp(roleRepo repository.Role) Role {
|
||||
func newRoleApp(roleRepo repository.Role, accountRoleRepo repository.AccountRole) Role {
|
||||
return &roleAppImpl{
|
||||
roleRepo: roleRepo,
|
||||
roleRepo: roleRepo,
|
||||
accountRoleRepo: accountRoleRepo,
|
||||
}
|
||||
}
|
||||
|
||||
type roleAppImpl struct {
|
||||
roleRepo repository.Role
|
||||
roleRepo repository.Role
|
||||
accountRoleRepo repository.AccountRole
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
func (m *roleAppImpl) GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
return m.roleRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) {
|
||||
return m.roleRepo.ListByQuery(condition)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) SaveRole(ctx context.Context, role *entity.Role) error {
|
||||
role.Code = strings.ToUpper(role.Code)
|
||||
if role.Id != 0 {
|
||||
// code不可更改,防止误传
|
||||
role.Code = ""
|
||||
@@ -65,11 +69,11 @@ func (m *roleAppImpl) SaveRole(ctx context.Context, role *entity.Role) error {
|
||||
}
|
||||
|
||||
role.Status = 1
|
||||
return gormx.Insert(role)
|
||||
return m.roleRepo.Insert(ctx, role)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) DeleteRole(ctx context.Context, id uint64) error {
|
||||
// 删除角色与资源的关联关系
|
||||
// 删除角色与资源账号的关联关系
|
||||
return gormx.Tx(
|
||||
func(db *gorm.DB) error {
|
||||
return m.roleRepo.DeleteByIdWithDb(ctx, db, id)
|
||||
@@ -77,6 +81,9 @@ func (m *roleAppImpl) DeleteRole(ctx context.Context, id uint64) error {
|
||||
func(db *gorm.DB) error {
|
||||
return gormx.DeleteByWithDb(db, &entity.RoleResource{RoleId: id})
|
||||
},
|
||||
func(db *gorm.DB) error {
|
||||
return gormx.DeleteByWithDb(db, &entity.AccountRole{RoleId: id})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -110,44 +117,35 @@ func (m *roleAppImpl) SaveRoleResource(ctx context.Context, roleId uint64, resou
|
||||
m.roleRepo.SaveRoleResource(addVals)
|
||||
|
||||
for _, v := range delIds {
|
||||
m.DeleteRoleResource(ctx, roleId, v)
|
||||
m.roleRepo.DeleteRoleResource(roleId, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) DeleteRoleResource(ctx context.Context, roleId uint64, resourceId uint64) {
|
||||
m.roleRepo.DeleteRoleResource(roleId, resourceId)
|
||||
}
|
||||
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 {
|
||||
return m.accountRoleRepo.DeleteByCond(ctx, accountRole)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) GetAccountRoleIds(accountId uint64) []uint64 {
|
||||
return m.roleRepo.GetAccountRoleIds(accountId)
|
||||
}
|
||||
|
||||
// 保存账号角色关联信息
|
||||
func (m *roleAppImpl) SaveAccountRole(ctx context.Context, accountId uint64, roleIds []uint64) {
|
||||
oIds := m.GetAccountRoleIds(accountId)
|
||||
|
||||
addIds, delIds, _ := collx.ArrayCompare(roleIds, oIds, func(i1, i2 uint64) bool {
|
||||
return i1 == i2
|
||||
})
|
||||
err := m.accountRoleRepo.GetBy(accountRole)
|
||||
if err == nil {
|
||||
return errorx.NewBiz("该用户已拥有该权限")
|
||||
}
|
||||
|
||||
la := contextx.GetLoginAccount(ctx)
|
||||
|
||||
createTime := time.Now()
|
||||
creator := la.Username
|
||||
creatorId := la.Id
|
||||
for _, v := range addIds {
|
||||
rr := &entity.AccountRole{AccountId: accountId, RoleId: v, CreateTime: &createTime, CreatorId: creatorId, Creator: creator}
|
||||
m.roleRepo.SaveAccountRole(rr)
|
||||
}
|
||||
for _, v := range delIds {
|
||||
m.DeleteAccountRole(ctx, accountId, v)
|
||||
}
|
||||
accountRole.Creator = la.Username
|
||||
accountRole.CreatorId = la.Id
|
||||
accountRole.CreateTime = &createTime
|
||||
return m.accountRoleRepo.Insert(ctx, accountRole)
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) DeleteAccountRole(ctx context.Context, accountId, roleId uint64) {
|
||||
m.roleRepo.DeleteAccountRole(accountId, roleId)
|
||||
func (m *roleAppImpl) GetAccountRoles(accountId uint64) ([]*entity.AccountRole, error) {
|
||||
var res []*entity.AccountRole
|
||||
err := m.accountRoleRepo.ListByCond(&entity.AccountRole{AccountId: accountId}, &res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) GetAccountRoles(accountId uint64, toEntity any) {
|
||||
m.roleRepo.GetAccountRoles(accountId, toEntity)
|
||||
func (m *roleAppImpl) GetRoleAccountPage(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
return m.accountRoleRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
|
||||
}
|
||||
|
||||
8
server/internal/sys/consts/consts.go
Normal file
8
server/internal/sys/consts/consts.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package consts
|
||||
|
||||
type AccountRoleRelateType int
|
||||
|
||||
const (
|
||||
AccountRoleBind AccountRoleRelateType = 1
|
||||
AccountRoleUnbind AccountRoleRelateType = -1
|
||||
)
|
||||
@@ -5,3 +5,16 @@ type SysLogQuery struct {
|
||||
Type int8 `json:"type" form:"type"`
|
||||
Description string `json:"description" form:"description"`
|
||||
}
|
||||
|
||||
type RoleQuery struct {
|
||||
Ids []uint64 `json:"ids"`
|
||||
Name string `json:"name" form:"name"`
|
||||
Code string `json:"code" form:"code"`
|
||||
NotIds []uint64 `json:"notIds"`
|
||||
}
|
||||
|
||||
type RoleAccountQuery struct {
|
||||
RoleId uint64 `json:"roleId" `
|
||||
Name string `json:"name" form:"name"`
|
||||
Username string `json:"username" form:"username"`
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@ import (
|
||||
type Role interface {
|
||||
base.Repo[*entity.Role]
|
||||
|
||||
GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
|
||||
ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error)
|
||||
|
||||
// 获取角色拥有的资源id数组,从role_resource表获取
|
||||
GetRoleResourceIds(roleId uint64) []uint64
|
||||
@@ -19,14 +21,10 @@ type Role interface {
|
||||
SaveRoleResource(rr []*entity.RoleResource)
|
||||
|
||||
DeleteRoleResource(roleId uint64, resourceId uint64)
|
||||
|
||||
// 获取账号拥有的角色id数组,从account_role表获取
|
||||
GetAccountRoleIds(accountId uint64) []uint64
|
||||
|
||||
SaveAccountRole(ar *entity.AccountRole)
|
||||
|
||||
DeleteAccountRole(accountId, roleId uint64)
|
||||
|
||||
// 获取账号角色信息列表
|
||||
GetAccountRoles(accountId uint64, toEntity any)
|
||||
}
|
||||
|
||||
type AccountRole interface {
|
||||
base.Repo[*entity.AccountRole]
|
||||
|
||||
GetPageList(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type accountRoleRepoImpl struct {
|
||||
base.RepoImpl[*entity.AccountRole]
|
||||
}
|
||||
|
||||
func newAccountRoleRepo() repository.AccountRole {
|
||||
return &accountRoleRepoImpl{base.RepoImpl[*entity.AccountRole]{M: new(entity.AccountRole)}}
|
||||
}
|
||||
|
||||
func (m *accountRoleRepoImpl) GetPageList(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := gormx.NewQueryWithTableName("t_sys_account_role t").
|
||||
Select("t.creator, t.create_time, a.username, a.name accountName, a.status accountStatus, a.id accountId").
|
||||
Joins("JOIN t_sys_account a ON t.account_id = a.id AND a.status = 1").
|
||||
Eq0("a.is_deleted", model.ModelUndeleted).
|
||||
Eq0("t.is_deleted", model.ModelUndeleted).
|
||||
RLike("a.username", condition.Username).
|
||||
RLike("a.name", condition.Name).
|
||||
Eq("t.role_id", condition.RoleId).
|
||||
OrderByDesc("t.id")
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
@@ -3,11 +3,12 @@ package persistence
|
||||
import "mayfly-go/internal/sys/domain/repository"
|
||||
|
||||
var (
|
||||
accountRepo = newAccountRepo()
|
||||
configRepo = newConfigRepo()
|
||||
resourceRepo = newResourceRepo()
|
||||
roleRepo = newRoleRepo()
|
||||
syslogRepo = newSyslogRepo()
|
||||
accountRepo = newAccountRepo()
|
||||
configRepo = newConfigRepo()
|
||||
resourceRepo = newResourceRepo()
|
||||
roleRepo = newRoleRepo()
|
||||
accountRoleRepo = newAccountRoleRepo()
|
||||
syslogRepo = newSyslogRepo()
|
||||
)
|
||||
|
||||
func GetAccountRepo() repository.Account {
|
||||
@@ -26,6 +27,10 @@ func GetRoleRepo() repository.Role {
|
||||
return roleRepo
|
||||
}
|
||||
|
||||
func GetAccountRoleRepo() repository.AccountRole {
|
||||
return accountRoleRepo
|
||||
}
|
||||
|
||||
func GetSyslogRepo() repository.Syslog {
|
||||
return syslogRepo
|
||||
}
|
||||
|
||||
@@ -16,11 +16,28 @@ func newRoleRepo() repository.Role {
|
||||
return &roleRepoImpl{base.RepoImpl[*entity.Role]{M: new(entity.Role)}}
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := gormx.NewQuery(condition).WithCondModel(condition).WithOrderBy(orderBy...)
|
||||
func (m *roleRepoImpl) GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := gormx.NewQuery(new(entity.Role)).
|
||||
Like("name", condition.Name).
|
||||
Like("code", condition.Code).
|
||||
In("id", condition.Ids).
|
||||
NotIn("id", condition.NotIds).
|
||||
WithOrderBy(orderBy...)
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) {
|
||||
var res []*entity.Role
|
||||
qd := gormx.NewQuery(new(entity.Role)).
|
||||
Like("name", condition.Name).
|
||||
Like("code", condition.Code).
|
||||
In("id", condition.Ids).
|
||||
NotIn("id", condition.NotIds).
|
||||
OrderByDesc("id")
|
||||
err := gormx.ListByQueryCond(qd, &res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// 获取角色拥有的资源id数组,从role_resource表获取
|
||||
func (m *roleRepoImpl) GetRoleResourceIds(roleId uint64) []uint64 {
|
||||
var rrs []entity.RoleResource
|
||||
@@ -51,32 +68,3 @@ func (m *roleRepoImpl) SaveRoleResource(rr []*entity.RoleResource) {
|
||||
func (m *roleRepoImpl) DeleteRoleResource(roleId uint64, resourceId uint64) {
|
||||
gormx.DeleteBy(&entity.RoleResource{RoleId: roleId, ResourceId: resourceId})
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) GetAccountRoleIds(accountId uint64) []uint64 {
|
||||
var rrs []entity.AccountRole
|
||||
|
||||
condtion := &entity.AccountRole{AccountId: accountId}
|
||||
gormx.ListBy(condtion, &rrs, "RoleId")
|
||||
|
||||
var rids []uint64
|
||||
for _, v := range rrs {
|
||||
rids = append(rids, v.RoleId)
|
||||
}
|
||||
return rids
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) SaveAccountRole(ar *entity.AccountRole) {
|
||||
gormx.Insert(ar)
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) DeleteAccountRole(accountId, roleId uint64) {
|
||||
gormx.DeleteBy(&entity.AccountRole{RoleId: roleId, AccountId: accountId})
|
||||
}
|
||||
|
||||
// 获取账号角色信息列表
|
||||
func (m *roleRepoImpl) GetAccountRoles(accountId uint64, toEntity any) {
|
||||
sql := "SELECT r.status, r.name, ar.create_time AS CreateTime, ar.creator AS creator " +
|
||||
"FROM t_sys_role r JOIN t_sys_account_role ar ON r.id = ar.role_id AND ar.account_id = ? AND r.is_deleted = 0 AND ar.is_deleted = 0 " +
|
||||
"ORDER BY ar.create_time DESC"
|
||||
gormx.GetListBySql2Model(sql, toEntity, accountId)
|
||||
}
|
||||
|
||||
@@ -47,11 +47,8 @@ func InitAccountRouter(router *gin.RouterGroup) {
|
||||
|
||||
req.NewDelete(":id", a.DeleteAccount).Log(req.NewLogSave("删除账号")).RequiredPermissionCode("account:del"),
|
||||
|
||||
// 获取所有用户角色id列表
|
||||
req.NewGet(":id/roleIds", a.AccountRoleIds),
|
||||
|
||||
// 保存用户角色
|
||||
req.NewPost("/roles", a.SaveRoles).Log(req.NewLogSave("保存用户角色")).RequiredPermissionCode("account:saveRoles"),
|
||||
// 关联用户角色
|
||||
req.NewPost("/roles", a.RelateRole).Log(req.NewLogSave("关联用户角色")).RequiredPermissionCode("account:saveRoles"),
|
||||
|
||||
// 获取用户角色
|
||||
req.NewGet(":id/roles", a.AccountRoles),
|
||||
|
||||
@@ -27,6 +27,8 @@ func InitRoleRouter(router *gin.RouterGroup) {
|
||||
req.NewGet(":id/resources", r.RoleResource),
|
||||
|
||||
req.NewPost(":id/resources", r.SaveResource).Log(req.NewLogSave("保存角色资源")).RequiredPermissionCode("role:saveResources"),
|
||||
|
||||
req.NewGet(":id/accounts", r.RoleAccount),
|
||||
}
|
||||
|
||||
req.BatchSetGroup(rg, reqs[:])
|
||||
|
||||
Reference in New Issue
Block a user