作者 yangfu

批量删除角色

package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type BatchDeleteRoleCommand struct {
// 用户ID
UserId int64 `cname:"用户ID" json:"userId"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId"`
// 角色ID
RoleIds []int64 `cname:"角色ID" json:"roleIds" valid:"Required"`
}
func (removeRoleCommand *BatchDeleteRoleCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeRoleCommand *BatchDeleteRoleCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(removeRoleCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(removeRoleCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -10,9 +10,9 @@ import (
type CreateRoleCommand struct {
// 用户ID
UserId int64 `cname:"用户ID" json:"userId" valid:"Required`
UserId int64 `cname:"用户ID" json:"userId" valid:"Required"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required`
OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required"`
// 角色名称
RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
// 描述
... ...
... ... @@ -314,6 +314,53 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole
}
}
// 批量移除角色
func (roleService *RoleService) BatchDeleteRole(removeRoleCommand *command.BatchDeleteRoleCommand) (interface{}, error) {
if err := removeRoleCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var roleRepository domain.RoleRepository
if value, err := factory.CreateRoleRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
roleRepository = value
}
for i := range removeRoleCommand.RoleIds {
roleId := removeRoleCommand.RoleIds[i]
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": roleId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if role == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%v", roleId))
}
if role.RoleType == domain.RoleTypeAdmin {
return nil, application.ThrowError(application.BUSINESS_ERROR, "主管理员角色不可删除")
}
role.DeletedAt = time.Now()
if _, err := roleRepository.Remove(role); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
// 取消用户分配的角色
func (roleService *RoleService) UnAssginRoleToUsers(unAssignRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) {
if err := unAssignRoleToUsersCommand.ValidateCommand(); err != nil {
... ... @@ -409,10 +456,20 @@ func (roleService *RoleService) UpdateRoleAccessMenus(updateRoleAccessMenusComma
defer func() {
transactionContext.RollbackTransaction()
}()
roleRepository, role, err := factory.FastPgRole(transactionContext, updateRoleAccessMenusCommand.RoleId)
if err != nil {
return nil, err
}
role.AccessMenus = updateRoleAccessMenusCommand.AccessMenus
if role, err = roleRepository.Save(role); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return role, nil
}
func NewRoleService(options map[string]interface{}) *RoleService {
... ...
... ... @@ -61,6 +61,7 @@ func (controller *CompanyController) ListCompanyCustomizeMenus() {
controller.Unmarshal(listCompanyCustomizeMenusCommand)
companyId, _ := controller.GetInt64(":companyId")
listCompanyCustomizeMenusCommand.CompanyId = companyId
listCompanyCustomizeMenusCommand.MenuCategory = controller.GetString("menuCategory")
data, err := companyService.ListCompanyCustomizeMenus(listCompanyCustomizeMenusCommand)
controller.Response(data, err)
}
... ...
... ... @@ -48,6 +48,14 @@ func (controller *RoleController) RemoveRole() {
controller.Response(data, err)
}
func (controller *RoleController) BatchDeleteRole() {
roleService := service.NewRoleService(nil)
removeRoleCommand := &command.BatchDeleteRoleCommand{}
controller.Unmarshal(removeRoleCommand)
data, err := roleService.BatchDeleteRole(removeRoleCommand)
controller.Response(data, err)
}
func (controller *RoleController) ListRole() {
roleService := service.NewRoleService(nil)
listRoleQuery := &query.ListRoleQuery{}
... ... @@ -110,3 +118,18 @@ func (controller *RoleController) UnAssginRoleToUsers() {
data, err := roleService.UnAssginRoleToUsers(unAssginRoleToUsersCommand)
controller.Response(data, err)
}
/***** 1.适配web模块 *****/
func (controller *RoleController) WebSearchRole() {
roleService := service.NewRoleService(nil)
listRoleQuery := &query.ListRoleQuery{}
Must(controller.Unmarshal(listRoleQuery))
listRoleQuery.Limit = 20
listRoleQuery.CompanyId = 23
data, err := roleService.ListRole(listRoleQuery)
webData := map[string]interface{}{
"list": data.(map[string]interface{})["roles"],
"total": data.(map[string]interface{})["count"],
}
ResponseGrid(controller.BaseController, webData, err)
}
... ...
... ... @@ -16,4 +16,16 @@ func init() {
web.Router("/role/:roleId/access-menus", &controllers.RoleController{}, "Put:UpdateRoleAccessMenus")
web.Router("/role/assign", &controllers.RoleController{}, "Post:AssginRoleToUsers")
web.Router("/role/unassign", &controllers.RoleController{}, "Post:UnAssginRoleToUsers")
web.Router("/role/batch-delete", &controllers.RoleController{}, "Post:BatchDeleteRole")
web.Router("/v1/web/roles/", &controllers.RoleController{}, "Post:CreateRole")
web.Router("/v1/web/roles/:roleId", &controllers.RoleController{}, "Put:UpdateRole")
web.Router("/v1/web/roles/:roleId", &controllers.RoleController{}, "Get:GetRole")
web.Router("/v1/web/roles/:roleId", &controllers.RoleController{}, "Delete:RemoveRole")
web.Router("/v1/web/roles/search", &controllers.RoleController{}, "Post:WebSearchRole")
web.Router("/v1/web/roles/:roleId/related-user", &controllers.RoleController{}, "Get:GetRoleRelatedUsers")
web.Router("/v1/web/roles/:roleId/access-menus", &controllers.RoleController{}, "Get:GetRoleAccessMenus")
web.Router("/v1/web/roles/:roleId/access-menus", &controllers.RoleController{}, "Put:UpdateRoleAccessMenus")
web.Router("/v1/web/roles/assign", &controllers.RoleController{}, "Post:AssginRoleToUsers")
web.Router("/v1/web/roles/unassign", &controllers.RoleController{}, "Post:UnAssginRoleToUsers")
}
... ...