From 38ee5d10109daf10146b86304a9a50c6bd5850f2 Mon Sep 17 00:00:00 2001 From: guosl Date: Thu, 1 Aug 2024 17:52:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E7=BB=84=E7=BB=87=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=A7=92=E8=89=B2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/sql/init/user.sql | 3 ++- internal/models/role/request.go | 6 +++--- internal/repo/role.go | 17 +++++++++++++++- internal/repo/user_organization.go | 4 ++++ internal/repo/user_role.go | 4 ++++ internal/service/role/role.go | 2 +- internal/service/role/util.go | 31 ++++++++++++++++++++++-------- 7 files changed, 53 insertions(+), 14 deletions(-) diff --git a/deploy/sql/init/user.sql b/deploy/sql/init/user.sql index 042c90e..31753c9 100644 --- a/deploy/sql/init/user.sql +++ b/deploy/sql/init/user.sql @@ -91,7 +91,8 @@ CREATE TABLE IF NOT EXISTS `organization_role` ( `modified_on` DATETIME DEFAULT CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP COMMENT '记录修改时间', PRIMARY KEY (`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='组织角色表'; diff --git a/internal/models/role/request.go b/internal/models/role/request.go index e594a44..ecce79a 100644 --- a/internal/models/role/request.go +++ b/internal/models/role/request.go @@ -13,7 +13,7 @@ func (q *Query) Default() { } if q.PageSize <= 0 { - q.PageSize = 20 + q.PageSize = 10000 } if len(q.Sort) == 0 { @@ -73,12 +73,12 @@ type RemoveOrgsReq struct { } type RoleUsersReq struct { - Id uint `json:"id" binding:"required"` + Id uint `form:"id" json:"id" binding:"required"` Query } type RoleOrgsReq struct { - Id uint `json:"id" binding:"required"` + Id uint `form:"id" json:"id" binding:"required"` Query } diff --git a/internal/repo/role.go b/internal/repo/role.go index eeabfec..a7c91c0 100644 --- a/internal/repo/role.go +++ b/internal/repo/role.go @@ -55,6 +55,7 @@ type RoleRepo interface{ CreateOrgRole(ctx context.Context,orgRoles []OrganizationRole)error DeleteOrgRole(ctx context.Context,roleId uint,orgIds []uint)error GetOrgRolesByRoleOrgId(ctx context.Context,roleId uint,orgIds []uint)([]OrganizationRole,error) + GetAllRoleOrgsByRoleId(ctx context.Context,roleId uint)([]OrganizationRole,error) } 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{ + if len(authors) == 0{ + return nil + } + err := r.db.Create(&authors).Error 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{ + if len(orgRoles) == 0{ + return nil + } + err := r.db.Create(&orgRoles).Error 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){ 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 != "" { 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 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 } \ No newline at end of file diff --git a/internal/repo/user_organization.go b/internal/repo/user_organization.go index e4b6b0a..21b9132 100644 --- a/internal/repo/user_organization.go +++ b/internal/repo/user_organization.go @@ -48,6 +48,10 @@ func NewUserOrganizationRepo(i *do.Injector)(UserOrganizationRepo,error){ } func (u *userOrganizationRepo)Create(ctx context.Context,userOrgs []UserOrganization)error{ + if len(userOrgs) == 0{ + return nil + } + return u.db.Create(userOrgs).Error } diff --git a/internal/repo/user_role.go b/internal/repo/user_role.go index c866058..49692d4 100644 --- a/internal/repo/user_role.go +++ b/internal/repo/user_role.go @@ -22,5 +22,9 @@ func (m *UserRole) TableName() string { } func (r *roleRepo)CreateUserRole(ctx context.Context,items []UserRole)error{ + if len(items) == 0{ + return nil + } + return r.db.Create(&items).Error } \ No newline at end of file diff --git a/internal/service/role/role.go b/internal/service/role/role.go index cc1f8ab..1114ac3 100644 --- a/internal/service/role/role.go +++ b/internal/service/role/role.go @@ -238,7 +238,7 @@ func (o *roleService) AddOrgs(ctx context.Context, info *roleModel.AddOrgsReq) e return err } - dbOrgRoles, err := o.roleRepo.GetOrgRolesByRoleOrgId(ctx, info.RoleId, orgIds) + dbOrgRoles, err := o.roleRepo.GetAllRoleOrgsByRoleId(ctx, info.RoleId) if err != nil { return err } diff --git a/internal/service/role/util.go b/internal/service/role/util.go index 7fe6195..3d7c55f 100644 --- a/internal/service/role/util.go +++ b/internal/service/role/util.go @@ -57,6 +57,7 @@ func filterOrgAuthorRole(authors, dbAuthors []repo.OrganizationRoleAuthor, orgRo for _, item := range authors { key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) if _, ok := dbAuthorMap[key]; !ok { + dbAuthorMap[key] = item fAuthors = append(fAuthors, item) } } @@ -64,6 +65,7 @@ func filterOrgAuthorRole(authors, dbAuthors []repo.OrganizationRoleAuthor, orgRo for _, item := range orgRole { key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) if _, ok := dbOrgRoleMap[key]; !ok { + dbOrgRoleMap[key] = item fOrgRole = append(fOrgRole, item) } } @@ -79,6 +81,11 @@ func genRemoveOrgAuthorAndRole(roleId uint, orgIds []uint, allOrg []*repo.Organi authorMaps[key] = item } + removeOrgMap := make(map[uint]bool) + for _, orgId := range orgIds { + removeOrgMap[orgId] = true + } + for _, orgId := range orgIds { // 判断是否移除当前组织的角色权限:1 父级组织没有包含子类的授权 2 组织自己没有记录 满足着两条可以删除组织角色权限 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, }) - if whetherRemoveOrgRole(false, author, authorMaps, orgMaps) { + if whetherRemoveOrgRole(false, author, authorMaps, orgMaps, removeOrgMap) { orgRole = append(orgRole, repo.OrganizationRole{ OrgID: orgId, RoleID: roleId, @@ -99,7 +106,12 @@ func genRemoveOrgAuthorAndRole(roleId uint, orgIds []uint, allOrg []*repo.Organi childOrgs := util.GetChildren(orgId, orgchildrenMaps) 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)) } } @@ -110,8 +122,8 @@ func genRemoveOrgAuthorAndRole(roleId uint, orgIds []uint, allOrg []*repo.Organi return } -func whetherRemoveOrgRole(child bool, author repo.OrganizationRoleAuthor, authorMaps map[string]repo.OrganizationRoleAuthor, orgMaps map[uint]*repo.Organization) bool { - if !child { +func whetherRemoveOrgRole(child bool, author repo.OrganizationRoleAuthor, authorMaps map[string]repo.OrganizationRoleAuthor, orgMaps map[uint]*repo.Organization, delOrgMaps map[uint]bool) bool { + if child { // 如果是要删除组织自己就不用判断自己是否授权过,如果是要删除组织的子组织,则需要判断子组织是否添加 key := strconv.FormatUint(uint64(author.RoleID), 10) + strconv.FormatUint(uint64(author.OrgID), 10) if _, ok := authorMaps[key]; ok { @@ -122,11 +134,14 @@ func whetherRemoveOrgRole(child bool, author repo.OrganizationRoleAuthor, author // 判断父级是否添加 parentOrg := util.GetParents(author.OrgID, orgMaps) 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.Option == int(roleModel.IncludeChildrenOrgAuthor) { - return false + if pAuthor, ok := authorMaps[key]; ok { + if pAuthor.Option == int(roleModel.IncludeChildrenOrgAuthor) { + return false + } } } }