处理组织添加角色接口

This commit is contained in:
guosl 2024-08-01 17:52:56 +08:00
parent 0418bc4ab0
commit 38ee5d1010
7 changed files with 53 additions and 14 deletions

View File

@ -91,7 +91,8 @@ CREATE TABLE IF NOT EXISTS `organization_role` (
`modified_on` DATETIME DEFAULT CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP COMMENT '记录修改时间', `modified_on` DATETIME DEFAULT CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP COMMENT '记录修改时间',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `idx_org_id`(`org_id`), KEY `idx_org_id`(`org_id`),
KEY `idx_role_id`(`role_id`) KEY `idx_role_id`(`role_id`),
UNIQUE KEY `udx_role_org`(`role_id`,`org_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3 COMMENT='组织角色表'; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3 COMMENT='组织角色表';

View File

@ -13,7 +13,7 @@ func (q *Query) Default() {
} }
if q.PageSize <= 0 { if q.PageSize <= 0 {
q.PageSize = 20 q.PageSize = 10000
} }
if len(q.Sort) == 0 { if len(q.Sort) == 0 {
@ -73,12 +73,12 @@ type RemoveOrgsReq struct {
} }
type RoleUsersReq struct { type RoleUsersReq struct {
Id uint `json:"id" binding:"required"` Id uint `form:"id" json:"id" binding:"required"`
Query Query
} }
type RoleOrgsReq struct { type RoleOrgsReq struct {
Id uint `json:"id" binding:"required"` Id uint `form:"id" json:"id" binding:"required"`
Query Query
} }

View File

@ -55,6 +55,7 @@ type RoleRepo interface{
CreateOrgRole(ctx context.Context,orgRoles []OrganizationRole)error CreateOrgRole(ctx context.Context,orgRoles []OrganizationRole)error
DeleteOrgRole(ctx context.Context,roleId uint,orgIds []uint)error DeleteOrgRole(ctx context.Context,roleId uint,orgIds []uint)error
GetOrgRolesByRoleOrgId(ctx context.Context,roleId uint,orgIds []uint)([]OrganizationRole,error) GetOrgRolesByRoleOrgId(ctx context.Context,roleId uint,orgIds []uint)([]OrganizationRole,error)
GetAllRoleOrgsByRoleId(ctx context.Context,roleId uint)([]OrganizationRole,error)
} }
type roleRepo struct{ type roleRepo struct{
@ -147,6 +148,10 @@ func (r *roleRepo) GetOrgAuthors(ctx context.Context,roleid uint,orgIds []uint)(
} }
func (r *roleRepo) CreateOrgAuthor(ctx context.Context,authors []OrganizationRoleAuthor)error{ func (r *roleRepo) CreateOrgAuthor(ctx context.Context,authors []OrganizationRoleAuthor)error{
if len(authors) == 0{
return nil
}
err := r.db.Create(&authors).Error err := r.db.Create(&authors).Error
return err return err
} }
@ -157,6 +162,10 @@ func (r *roleRepo) DeleteOrgAuthor(ctx context.Context,roleId uint,orgIds []uint
} }
func (r *roleRepo) CreateOrgRole(ctx context.Context,orgRoles []OrganizationRole)error{ func (r *roleRepo) CreateOrgRole(ctx context.Context,orgRoles []OrganizationRole)error{
if len(orgRoles) == 0{
return nil
}
err := r.db.Create(&orgRoles).Error err := r.db.Create(&orgRoles).Error
return err return err
} }
@ -180,7 +189,7 @@ func (r *roleRepo)GetRoleAllOrgAuthorsByRoleId(ctx context.Context,roleid uint)(
func (r *roleRepo)SearchRoleUsers(ctx context.Context,roleCode string,query Query)([]User,error){ func (r *roleRepo)SearchRoleUsers(ctx context.Context,roleCode string,query Query)([]User,error){
users := make([]User,0) users := make([]User,0)
db := r.db.Model(User{}).Joins("join user_role on user_role.user_account = user.account user_role.role_code = ?",roleCode) db := r.db.Model(User{}).Joins("join user_role on user_role.user_account = user.account and user_role.role_code = ?",roleCode)
if query.Keyword != "" { if query.Keyword != "" {
keyword := fmt.Sprintf("%%%s%%", query.Keyword) keyword := fmt.Sprintf("%%%s%%", query.Keyword)
@ -202,4 +211,10 @@ func (r *roleRepo)SearchOrgAuthor(ctx context.Context,roleId uint,query Query)([
err := db.Order("organization_role_author.created_on desc,organization.sort").Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&orgs).Error err := db.Order("organization_role_author.created_on desc,organization.sort").Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&orgs).Error
return orgs,err return orgs,err
}
func (r *roleRepo)GetAllRoleOrgsByRoleId(ctx context.Context,roleId uint)([]OrganizationRole,error){
oRoles := make([]OrganizationRole,0)
err := r.db.Where("role_id = ?",roleId).Find(&oRoles).Error
return oRoles,err
} }

View File

@ -48,6 +48,10 @@ func NewUserOrganizationRepo(i *do.Injector)(UserOrganizationRepo,error){
} }
func (u *userOrganizationRepo)Create(ctx context.Context,userOrgs []UserOrganization)error{ func (u *userOrganizationRepo)Create(ctx context.Context,userOrgs []UserOrganization)error{
if len(userOrgs) == 0{
return nil
}
return u.db.Create(userOrgs).Error return u.db.Create(userOrgs).Error
} }

View File

@ -22,5 +22,9 @@ func (m *UserRole) TableName() string {
} }
func (r *roleRepo)CreateUserRole(ctx context.Context,items []UserRole)error{ func (r *roleRepo)CreateUserRole(ctx context.Context,items []UserRole)error{
if len(items) == 0{
return nil
}
return r.db.Create(&items).Error return r.db.Create(&items).Error
} }

View File

@ -238,7 +238,7 @@ func (o *roleService) AddOrgs(ctx context.Context, info *roleModel.AddOrgsReq) e
return err return err
} }
dbOrgRoles, err := o.roleRepo.GetOrgRolesByRoleOrgId(ctx, info.RoleId, orgIds) dbOrgRoles, err := o.roleRepo.GetAllRoleOrgsByRoleId(ctx, info.RoleId)
if err != nil { if err != nil {
return err return err
} }

View File

@ -57,6 +57,7 @@ func filterOrgAuthorRole(authors, dbAuthors []repo.OrganizationRoleAuthor, orgRo
for _, item := range authors { for _, item := range authors {
key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10)
if _, ok := dbAuthorMap[key]; !ok { if _, ok := dbAuthorMap[key]; !ok {
dbAuthorMap[key] = item
fAuthors = append(fAuthors, item) fAuthors = append(fAuthors, item)
} }
} }
@ -64,6 +65,7 @@ func filterOrgAuthorRole(authors, dbAuthors []repo.OrganizationRoleAuthor, orgRo
for _, item := range orgRole { for _, item := range orgRole {
key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10)
if _, ok := dbOrgRoleMap[key]; !ok { if _, ok := dbOrgRoleMap[key]; !ok {
dbOrgRoleMap[key] = item
fOrgRole = append(fOrgRole, item) fOrgRole = append(fOrgRole, item)
} }
} }
@ -79,6 +81,11 @@ func genRemoveOrgAuthorAndRole(roleId uint, orgIds []uint, allOrg []*repo.Organi
authorMaps[key] = item authorMaps[key] = item
} }
removeOrgMap := make(map[uint]bool)
for _, orgId := range orgIds {
removeOrgMap[orgId] = true
}
for _, orgId := range orgIds { for _, orgId := range orgIds {
// 判断是否移除当前组织的角色权限:1 父级组织没有包含子类的授权 2 组织自己没有记录 满足着两条可以删除组织角色权限 // 判断是否移除当前组织的角色权限:1 父级组织没有包含子类的授权 2 组织自己没有记录 满足着两条可以删除组织角色权限
key := strconv.FormatUint(uint64(roleId), 10) + strconv.FormatUint(uint64(orgId), 10) key := strconv.FormatUint(uint64(roleId), 10) + strconv.FormatUint(uint64(orgId), 10)
@ -88,7 +95,7 @@ func genRemoveOrgAuthorAndRole(roleId uint, orgIds []uint, allOrg []*repo.Organi
RoleID: roleId, RoleID: roleId,
}) })
if whetherRemoveOrgRole(false, author, authorMaps, orgMaps) { if whetherRemoveOrgRole(false, author, authorMaps, orgMaps, removeOrgMap) {
orgRole = append(orgRole, repo.OrganizationRole{ orgRole = append(orgRole, repo.OrganizationRole{
OrgID: orgId, OrgID: orgId,
RoleID: roleId, RoleID: roleId,
@ -99,7 +106,12 @@ func genRemoveOrgAuthorAndRole(roleId uint, orgIds []uint, allOrg []*repo.Organi
childOrgs := util.GetChildren(orgId, orgchildrenMaps) childOrgs := util.GetChildren(orgId, orgchildrenMaps)
for _, child := range childOrgs { for _, child := range childOrgs {
if whetherRemoveOrgRole(true, author, authorMaps, orgMaps) { cAuthor := repo.OrganizationRoleAuthor{
OrgID: child.ID,
RoleID: roleId,
}
if whetherRemoveOrgRole(true, cAuthor, authorMaps, orgMaps, removeOrgMap) {
orgRole = append(orgRole, genOrgRole(roleId, child.ID)) orgRole = append(orgRole, genOrgRole(roleId, child.ID))
} }
} }
@ -110,8 +122,8 @@ func genRemoveOrgAuthorAndRole(roleId uint, orgIds []uint, allOrg []*repo.Organi
return return
} }
func whetherRemoveOrgRole(child bool, author repo.OrganizationRoleAuthor, authorMaps map[string]repo.OrganizationRoleAuthor, orgMaps map[uint]*repo.Organization) bool { func whetherRemoveOrgRole(child bool, author repo.OrganizationRoleAuthor, authorMaps map[string]repo.OrganizationRoleAuthor, orgMaps map[uint]*repo.Organization, delOrgMaps map[uint]bool) bool {
if !child { if child {
// 如果是要删除组织自己就不用判断自己是否授权过,如果是要删除组织的子组织,则需要判断子组织是否添加 // 如果是要删除组织自己就不用判断自己是否授权过,如果是要删除组织的子组织,则需要判断子组织是否添加
key := strconv.FormatUint(uint64(author.RoleID), 10) + strconv.FormatUint(uint64(author.OrgID), 10) key := strconv.FormatUint(uint64(author.RoleID), 10) + strconv.FormatUint(uint64(author.OrgID), 10)
if _, ok := authorMaps[key]; ok { if _, ok := authorMaps[key]; ok {
@ -122,11 +134,14 @@ func whetherRemoveOrgRole(child bool, author repo.OrganizationRoleAuthor, author
// 判断父级是否添加 // 判断父级是否添加
parentOrg := util.GetParents(author.OrgID, orgMaps) parentOrg := util.GetParents(author.OrgID, orgMaps)
for _, org := range parentOrg { for _, org := range parentOrg {
key := strconv.FormatUint(uint64(author.RoleID), 10) + strconv.FormatUint(uint64(org.ID), 10) // 父级不在当前要删除的组织内
if _, ok := delOrgMaps[org.ID]; !ok {
key := strconv.FormatUint(uint64(author.RoleID), 10) + strconv.FormatUint(uint64(org.ID), 10)
if pAuthor, ok := authorMaps[key]; ok { if pAuthor, ok := authorMaps[key]; ok {
if pAuthor.Option == int(roleModel.IncludeChildrenOrgAuthor) { if pAuthor.Option == int(roleModel.IncludeChildrenOrgAuthor) {
return false return false
}
} }
} }
} }