作者 yangfu

chore: 批量导入用户所属组织使用操作的组织,管理员角色返回所有权限

... ... @@ -66,7 +66,7 @@ func (userService *UserService) BatchAdd2(batchAddCommand *command.BatchAdd2Comm
"transactionContext": transactionContext,
})
var failRows []*domain.BatchAddUserItem
if failRows, err = batchAddUserService.BatchAddUser2(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil {
if failRows, err = batchAddUserService.BatchAddUser(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil {
return batchAddCommand.Users, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if len(failRows) != 0 {
... ...
... ... @@ -3,6 +3,8 @@ package domain
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"strconv"
"strings"
"time"
)
... ... @@ -197,6 +199,17 @@ func (org *Org) ID() string {
return org.GetFullPath()
}
func (org *Org) IsChild(pid int64) bool {
paths := strings.Split(org.ParentPath, PathSegment)
pidStr := strconv.FormatInt(pid, 10)
for _, v := range paths {
if strings.EqualFold(pidStr, v) {
return true
}
}
return false
}
/***** 2.缓存模块 *****/
func (m *Org) CacheKeyFunc() string {
... ...
... ... @@ -4,6 +4,5 @@ import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgBatchAddUserService 批量添加用户服务
type PgBatchAddUserService interface {
BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error
BatchAddUser2(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error)
BatchAddUser(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error)
}
... ...
... ... @@ -18,71 +18,71 @@ type PgBatchAddUserService struct {
// optUser 操作用户
// users 待添加用户列表数据
// password 密码
func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error {
var (
err error
)
orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
if err != nil {
return err
}
_, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId})
if err != nil {
return err
}
var mapOrg = make(map[int64]*domain.Org)
for i := range orgs {
mapOrg[orgs[i].OrgId] = orgs[i]
}
createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
for i := range users {
user := users[i]
if err = ptr.preCheck(user); err != nil {
return err
}
newUser := &domain.User{
CompanyId: user.CompanyId,
UserType: user.UserType,
UserCode: user.UserCode,
OrganizationId: user.OrganizationId,
DepartmentId: user.DepartmentId,
UserOrg: []*domain.Org{},
UserRole: []*domain.Role{},
FavoriteMenus: []string{},
CooperationInfo: user.CooperationInfo,
UserInfo: user.UserInfo,
EnableStatus: int(domain.UserStatusEnable),
Ext: &domain.Ext{
Phone: user.UserInfo.Phone,
UserName: user.UserInfo.UserName,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if user.OrganizationId > 0 {
if v, ok := mapOrg[user.OrganizationId]; ok && v.CompanyId == user.CompanyId {
newUser.Ext.OrgName = v.OrgName
} else {
return fmt.Errorf("导入的组织机构不存在")
}
}
if user.DepartmentId > 0 {
if v, ok := mapOrg[user.DepartmentId]; ok && v.CompanyId == user.CompanyId {
newUser.Ext.DepName = v.OrgName
} else {
return fmt.Errorf("导入的所属部门不存在")
}
}
if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil {
return err
}
}
return nil
}
//func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error {
// var (
// err error
// )
// orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
// if err != nil {
// return err
// }
// _, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId})
// if err != nil {
// return err
// }
// var mapOrg = make(map[int64]*domain.Org)
// for i := range orgs {
// mapOrg[orgs[i].OrgId] = orgs[i]
// }
//
// createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
// for i := range users {
// user := users[i]
// if err = ptr.preCheck(user); err != nil {
// return err
// }
// newUser := &domain.User{
// CompanyId: user.CompanyId,
// UserType: user.UserType,
// UserCode: user.UserCode,
// OrganizationId: user.OrganizationId,
// DepartmentId: user.DepartmentId,
// UserOrg: []*domain.Org{},
// UserRole: []*domain.Role{},
// FavoriteMenus: []string{},
// CooperationInfo: user.CooperationInfo,
// UserInfo: user.UserInfo,
// EnableStatus: int(domain.UserStatusEnable),
// Ext: &domain.Ext{
// Phone: user.UserInfo.Phone,
// UserName: user.UserInfo.UserName,
// },
// CreatedAt: time.Now(),
// UpdatedAt: time.Now(),
// }
// if user.OrganizationId > 0 {
// if v, ok := mapOrg[user.OrganizationId]; ok && v.CompanyId == user.CompanyId {
// newUser.Ext.OrgName = v.OrgName
// } else {
// return fmt.Errorf("导入的组织机构不存在")
// }
// }
// if user.DepartmentId > 0 {
// if v, ok := mapOrg[user.DepartmentId]; ok && v.CompanyId == user.CompanyId {
// newUser.Ext.DepName = v.OrgName
// } else {
// return fmt.Errorf("导入的所属部门不存在")
// }
// }
//
// if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil {
// return err
// }
// }
// return nil
//}
func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error) {
func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error) {
var (
err error
)
... ... @@ -99,6 +99,10 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
for i := range orgs {
mapOrg[orgs[i].OrgCode] = orgs[i]
}
optUserOrg, err := orgRepository.FindOne(map[string]interface{}{"orgId": optUser.OrgId})
if err != nil {
return failRows, err
}
createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
for i := range users {
... ... @@ -110,13 +114,19 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
}
var org, dep *domain.Org
var ok bool
if org, ok = mapOrg[user.Org]; !ok {
user.FailReason = "导入的组织机构不存在:" + user.Org
// 使用导入用户的组织作为默认组织
//if org, ok = mapOrg[user.Org]; !ok {
// user.FailReason = "导入的组织机构不存在:" + user.Org
// failRows = append(failRows, user)
// continue
//}
if dep, ok = mapOrg[user.Department]; !ok && user.UserType != domain.UserTypeCooperation {
user.FailReason = "导入的所属部门不存在:" + user.Department
failRows = append(failRows, user)
continue
}
if dep, ok = mapOrg[user.Department]; !ok && user.UserType != domain.UserTypeCooperation {
user.FailReason = "导入的所属部门不存在:" + user.Department
if dep != nil && !dep.IsChild(optUser.OrgId) {
user.FailReason = fmt.Sprintf("导入的所属部门不是当前登录组织(%v)的子部门:%v", optUserOrg.OrgName, user.Department)
failRows = append(failRows, user)
continue
}
... ... @@ -124,7 +134,7 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
CompanyId: user.CompanyId,
UserType: user.UserType,
UserCode: user.UserCode,
OrganizationId: org.OrgId,
OrganizationId: optUser.OrgId,
UserOrg: []*domain.Org{},
UserRole: []*domain.Role{},
FavoriteMenus: []string{},
... ... @@ -172,24 +182,24 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
return failRows, nil
}
func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error {
if len(user.UserCode) == 0 {
return fmt.Errorf("导入的用户编码为空值")
}
if len(user.UserInfo.UserName) == 0 {
return fmt.Errorf("导入的用户姓名为空值")
}
if len(user.UserInfo.Phone) == 0 || len(user.UserInfo.Phone) != 11 {
return fmt.Errorf("导入的手机号不是有效手机号")
}
if user.OrganizationId == 0 {
return fmt.Errorf("导入的组织机构不存在")
}
if user.DepartmentId == 0 && user.UserType == domain.UserTypeEmployee {
return fmt.Errorf("导入的所属部门不存在")
}
return nil
}
//func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error {
// if len(user.UserCode) == 0 {
// return fmt.Errorf("导入的用户编码为空值")
// }
// if len(user.UserInfo.UserName) == 0 {
// return fmt.Errorf("导入的用户姓名为空值")
// }
// if len(user.UserInfo.Phone) == 0 || len(user.UserInfo.Phone) != 11 {
// return fmt.Errorf("导入的手机号不是有效手机号")
// }
// if user.OrganizationId == 0 {
// return fmt.Errorf("导入的组织机构不存在")
// }
// if user.DepartmentId == 0 && user.UserType == domain.UserTypeEmployee {
// return fmt.Errorf("导入的所属部门不存在")
// }
// return nil
//}
func (ptr *PgBatchAddUserService) preCheck2(user *domain.BatchAddUserItem) error {
if len(user.UserCode) == 0 {
... ...
... ... @@ -22,6 +22,7 @@ type PgRoleAccessMenusService struct {
func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, roleIds []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) {
var err error
var menus []*domain.Menu
var hasAdminRole bool = false
menuIdSet := hashset.New()
if len(roleIds) == 0 {
return menus, nil
... ... @@ -41,6 +42,9 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro
for i := 0; i < len(role.AccessMenus); i++ {
menuIdSet.Add(role.AccessMenus[i])
}
if role.RoleType&domain.RoleTypeAdmin > 0 {
hasAdminRole = true
}
}
// 2.获取所有公开的菜单
... ... @@ -66,7 +70,7 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro
if err != nil {
return nil, err
}
_, alias, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": option.CompanyId})
_, menuAlias, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": option.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
... ... @@ -74,18 +78,25 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro
// 4. 设置别名、返回有效的菜单列表
var mapMenus = make(map[int64]*domain.Menu)
for i := range menus {
menus[i].EnableStatus = domain.MenuStatusDisable
if menus[i].EnableStatus == domain.MenuStatusEnable {
menus[i].EnableStatus = domain.MenuStatusDisable
if hasAdminRole {
menus[i].EnableStatus = domain.MenuStatusEnable //管理员角色,返回所有权限
}
}
mapMenus[menus[i].MenuId] = menus[i]
}
for i := range alias {
if v, ok := mapMenus[alias[i].MenuId]; ok {
v.MenuName = alias[i].MenuAlias // 设置别名
v.Sort = alias[i].Sort
// 4.1.设置别名
for i := range menuAlias {
if v, ok := mapMenus[menuAlias[i].MenuId]; ok {
v.MenuName = menuAlias[i].MenuAlias
v.Sort = menuAlias[i].Sort
}
}
// 4.2.设置菜单权限状态
values := menuIdSet.Values()
for i := range values {
menuId := (values[i]).(int64) // 设置菜单权限状态
menuId := (values[i]).(int64)
if v, ok := mapMenus[menuId]; ok {
v.EnableStatus = domain.MenuStatusEnable
}
... ...