作者 yangfu

add role

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type AssginRoleToUsersCommand struct {
  12 + // 角色ID
  13 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  14 + // 用户列表
  15 + UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"`
  16 +}
  17 +
  18 +func (assginRoleToUsersCommand *AssginRoleToUsersCommand) Valid(validation *validation.Validation) {
  19 + //validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (assginRoleToUsersCommand *AssginRoleToUsersCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(assginRoleToUsersCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(assginRoleToUsersCommand).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type CreateRoleCommand struct {
  12 + // 角色名称
  13 + RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
  14 + // 描述
  15 + Desc int64 `cname:"描述" json:"desc,string,omitempty"`
  16 +}
  17 +
  18 +func (createRoleCommand *CreateRoleCommand) Valid(validation *validation.Validation) {
  19 + //validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (createRoleCommand *CreateRoleCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(createRoleCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(createRoleCommand).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type RemoveRoleCommand struct {
  12 + // 角色ID
  13 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  14 +}
  15 +
  16 +func (removeRoleCommand *RemoveRoleCommand) Valid(validation *validation.Validation) {
  17 + //validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (removeRoleCommand *RemoveRoleCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(removeRoleCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(removeRoleCommand).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type UnAssginRoleToUsersCommand struct {
  12 + // 角色ID
  13 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  14 + // 用户列表
  15 + UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"`
  16 +}
  17 +
  18 +func (unAssginRoleToUsersCommand *UnAssginRoleToUsersCommand) Valid(validation *validation.Validation) {
  19 + //validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (unAssginRoleToUsersCommand *UnAssginRoleToUsersCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(unAssginRoleToUsersCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(unAssginRoleToUsersCommand).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type UpdateRoleCommand struct {
  12 + // 角色ID
  13 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  14 + // 角色名称
  15 + RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
  16 + // 描述
  17 + Desc int64 `cname:"描述" json:"desc,string,omitempty"`
  18 +}
  19 +
  20 +func (updateRoleCommand *UpdateRoleCommand) Valid(validation *validation.Validation) {
  21 + //validation.SetError("CustomValid", "未实现的自定义认证")
  22 +}
  23 +
  24 +func (updateRoleCommand *UpdateRoleCommand) ValidateCommand() error {
  25 + valid := validation.Validation{}
  26 + b, err := valid.Valid(updateRoleCommand)
  27 + if err != nil {
  28 + return err
  29 + }
  30 + if !b {
  31 + elem := reflect.TypeOf(updateRoleCommand).Elem()
  32 + for _, validErr := range valid.Errors {
  33 + field, isExist := elem.FieldByName(validErr.Field)
  34 + if isExist {
  35 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  36 + } else {
  37 + return fmt.Errorf(validErr.Message)
  38 + }
  39 + }
  40 + }
  41 + return nil
  42 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type UpdateRoleAccessMenusCommand struct {
  12 + // 角色ID
  13 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  14 + // 菜单编号列表
  15 + AccessMenus []int64 `cname:"菜单编号列表" json:"accessMenus,omitempty"`
  16 +}
  17 +
  18 +func (updateRoleAccessMenusCommand *UpdateRoleAccessMenusCommand) Valid(validation *validation.Validation) {
  19 + //validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (updateRoleAccessMenusCommand *UpdateRoleAccessMenusCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(updateRoleAccessMenusCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(updateRoleAccessMenusCommand).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type GetRoleQuery struct {
  12 + // 角色ID
  13 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  14 +}
  15 +
  16 +func (getRoleQuery *GetRoleQuery) Valid(validation *validation.Validation) {
  17 + //validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (getRoleQuery *GetRoleQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(getRoleQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(getRoleQuery).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type GetRoleAccessMenusQuery struct {
  12 + // 角色ID
  13 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  14 +}
  15 +
  16 +func (getRoleAccessMenusQuery *GetRoleAccessMenusQuery) Valid(validation *validation.Validation) {
  17 + //validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (getRoleAccessMenusQuery *GetRoleAccessMenusQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(getRoleAccessMenusQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(getRoleAccessMenusQuery).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type GetRoleRelatedUsersQuery struct {
  12 + // 角色ID
  13 + RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
  14 + // 组织ID
  15 + OrgId int64 `cname:"组织ID" json:"orgId,string,omitempty"`
  16 + // 部门编号
  17 + DepartmentId int64 `cname:"部门编号" json:"departmentId,string,omitempty"`
  18 +}
  19 +
  20 +func (getRoleRelatedUsersQuery *GetRoleRelatedUsersQuery) Valid(validation *validation.Validation) {
  21 + //validation.SetError("CustomValid", "未实现的自定义认证")
  22 +}
  23 +
  24 +func (getRoleRelatedUsersQuery *GetRoleRelatedUsersQuery) ValidateQuery() error {
  25 + valid := validation.Validation{}
  26 + b, err := valid.Valid(getRoleRelatedUsersQuery)
  27 + if err != nil {
  28 + return err
  29 + }
  30 + if !b {
  31 + elem := reflect.TypeOf(getRoleRelatedUsersQuery).Elem()
  32 + for _, validErr := range valid.Errors {
  33 + field, isExist := elem.FieldByName(validErr.Field)
  34 + if isExist {
  35 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  36 + } else {
  37 + return fmt.Errorf(validErr.Message)
  38 + }
  39 + }
  40 + }
  41 + return nil
  42 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type ListRoleQuery struct {
  12 + // 查询偏离量
  13 + Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
  14 + // 查询限制
  15 + Limit int `cname:"查询限制" json:"limit" valid:"Required"`
  16 + // 角色类型 1.普通角色 1024:超级管理员
  17 + RoleType int `cname:"角色类型 1.普通角色 1024:超级管理员" json:"roleType,omitempty"`
  18 + // 角色名称
  19 + RoleName string `cname:"角色名称" json:"roleName,omitempty"`
  20 + // 组织名称
  21 + OrgName string `cname:"组织名称" json:"orgName,omitempty"`
  22 + // 组织ID
  23 + OrgId int64 `cname:"组织ID" json:"orgId,string,omitempty"`
  24 + // 匹配多个组织
  25 + InOrgIds []int64 `cname:"匹配多个组织" json:"inOrgIds,omitempty"`
  26 +}
  27 +
  28 +func (listRoleQuery *ListRoleQuery) Valid(validation *validation.Validation) {
  29 + //validation.SetError("CustomValid", "未实现的自定义认证")
  30 +}
  31 +
  32 +func (listRoleQuery *ListRoleQuery) ValidateQuery() error {
  33 + valid := validation.Validation{}
  34 + b, err := valid.Valid(listRoleQuery)
  35 + if err != nil {
  36 + return err
  37 + }
  38 + if !b {
  39 + elem := reflect.TypeOf(listRoleQuery).Elem()
  40 + for _, validErr := range valid.Errors {
  41 + field, isExist := elem.FieldByName(validErr.Field)
  42 + if isExist {
  43 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  44 + } else {
  45 + return fmt.Errorf(validErr.Message)
  46 + }
  47 + }
  48 + }
  49 + return nil
  50 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + "github.com/linmadan/egglib-go/utils/tool_funs"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/query"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  11 +)
  12 +
  13 +// 角色
  14 +type RoleService struct {
  15 +}
  16 +
  17 +// 分配角色给多个用户
  18 +func (roleService *RoleService) AssginRoleToUsers(assginRoleToUsersCommand *command.AssginRoleToUsersCommand) (interface{}, error) {
  19 + if err := assginRoleToUsersCommand.ValidateCommand(); err != nil {
  20 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  21 + }
  22 + transactionContext, err := factory.CreateTransactionContext(nil)
  23 + if err != nil {
  24 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  25 + }
  26 + if err := transactionContext.StartTransaction(); err != nil {
  27 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  28 + }
  29 + defer func() {
  30 + transactionContext.RollbackTransaction()
  31 + }()
  32 + if err := transactionContext.CommitTransaction(); err != nil {
  33 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  34 + }
  35 + return nil, nil
  36 +}
  37 +
  38 +// 创建角色
  39 +func (roleService *RoleService) CreateRole(createRoleCommand *command.CreateRoleCommand) (interface{}, error) {
  40 + if err := createRoleCommand.ValidateCommand(); err != nil {
  41 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  42 + }
  43 + transactionContext, err := factory.CreateTransactionContext(nil)
  44 + if err != nil {
  45 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  46 + }
  47 + if err := transactionContext.StartTransaction(); err != nil {
  48 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  49 + }
  50 + defer func() {
  51 + transactionContext.RollbackTransaction()
  52 + }()
  53 + newRole := &domain.Role{
  54 + RoleName: createRoleCommand.RoleName,
  55 + Desc: createRoleCommand.Desc,
  56 + }
  57 + var roleRepository domain.RoleRepository
  58 + if value, err := factory.CreateRoleRepository(map[string]interface{}{
  59 + "transactionContext": transactionContext,
  60 + }); err != nil {
  61 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  62 + } else {
  63 + roleRepository = value
  64 + }
  65 + if role, err := roleRepository.Save(newRole); err != nil {
  66 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  67 + } else {
  68 + if err := transactionContext.CommitTransaction(); err != nil {
  69 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  70 + }
  71 + return role, nil
  72 + }
  73 +}
  74 +
  75 +// 返回角色
  76 +func (roleService *RoleService) GetRole(getRoleQuery *query.GetRoleQuery) (interface{}, error) {
  77 + if err := getRoleQuery.ValidateQuery(); err != nil {
  78 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  79 + }
  80 + transactionContext, err := factory.CreateTransactionContext(nil)
  81 + if err != nil {
  82 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  83 + }
  84 + if err := transactionContext.StartTransaction(); err != nil {
  85 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  86 + }
  87 + defer func() {
  88 + transactionContext.RollbackTransaction()
  89 + }()
  90 + var roleRepository domain.RoleRepository
  91 + if value, err := factory.CreateRoleRepository(map[string]interface{}{
  92 + "transactionContext": transactionContext,
  93 + }); err != nil {
  94 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  95 + } else {
  96 + roleRepository = value
  97 + }
  98 + role, err := roleRepository.FindOne(map[string]interface{}{"roleId": getRoleQuery.RoleId})
  99 + if err != nil {
  100 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  101 + }
  102 + if role == nil {
  103 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getRoleQuery.RoleId)))
  104 + } else {
  105 + if err := transactionContext.CommitTransaction(); err != nil {
  106 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  107 + }
  108 + return role, nil
  109 + }
  110 +}
  111 +
  112 +// 获取角色菜单
  113 +func (roleService *RoleService) GetRoleAccessMenus(getRoleAccessMenusQuery *query.GetRoleAccessMenusQuery) (interface{}, error) {
  114 + if err := getRoleAccessMenusQuery.ValidateQuery(); err != nil {
  115 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  116 + }
  117 + transactionContext, err := factory.CreateTransactionContext(nil)
  118 + if err != nil {
  119 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  120 + }
  121 + if err := transactionContext.StartTransaction(); err != nil {
  122 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  123 + }
  124 + defer func() {
  125 + transactionContext.RollbackTransaction()
  126 + }()
  127 + if err := transactionContext.CommitTransaction(); err != nil {
  128 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  129 + }
  130 + return nil, nil
  131 +}
  132 +
  133 +// 获取角色相关联的用户
  134 +func (roleService *RoleService) GetRoleRelatedUsers(getRoleRelatedUsersQuery *query.GetRoleRelatedUsersQuery) (interface{}, error) {
  135 + if err := getRoleRelatedUsersQuery.ValidateQuery(); err != nil {
  136 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  137 + }
  138 + transactionContext, err := factory.CreateTransactionContext(nil)
  139 + if err != nil {
  140 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  141 + }
  142 + if err := transactionContext.StartTransaction(); err != nil {
  143 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  144 + }
  145 + defer func() {
  146 + transactionContext.RollbackTransaction()
  147 + }()
  148 + if err := transactionContext.CommitTransaction(); err != nil {
  149 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  150 + }
  151 + return nil, nil
  152 +}
  153 +
  154 +// 返回角色列表
  155 +func (roleService *RoleService) ListRole(listRoleQuery *query.ListRoleQuery) (interface{}, error) {
  156 + if err := listRoleQuery.ValidateQuery(); err != nil {
  157 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  158 + }
  159 + transactionContext, err := factory.CreateTransactionContext(nil)
  160 + if err != nil {
  161 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  162 + }
  163 + if err := transactionContext.StartTransaction(); err != nil {
  164 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  165 + }
  166 + defer func() {
  167 + transactionContext.RollbackTransaction()
  168 + }()
  169 + var roleRepository domain.RoleRepository
  170 + if value, err := factory.CreateRoleRepository(map[string]interface{}{
  171 + "transactionContext": transactionContext,
  172 + }); err != nil {
  173 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  174 + } else {
  175 + roleRepository = value
  176 + }
  177 + if count, roles, err := roleRepository.Find(tool_funs.SimpleStructToMap(listRoleQuery)); err != nil {
  178 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  179 + } else {
  180 + if err := transactionContext.CommitTransaction(); err != nil {
  181 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  182 + }
  183 + return map[string]interface{}{
  184 + "count": count,
  185 + "roles": roles,
  186 + }, nil
  187 + }
  188 +}
  189 +
  190 +// 移除角色
  191 +func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRoleCommand) (interface{}, error) {
  192 + if err := removeRoleCommand.ValidateCommand(); err != nil {
  193 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  194 + }
  195 + transactionContext, err := factory.CreateTransactionContext(nil)
  196 + if err != nil {
  197 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  198 + }
  199 + if err := transactionContext.StartTransaction(); err != nil {
  200 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  201 + }
  202 + defer func() {
  203 + transactionContext.RollbackTransaction()
  204 + }()
  205 + var roleRepository domain.RoleRepository
  206 + if value, err := factory.CreateRoleRepository(map[string]interface{}{
  207 + "transactionContext": transactionContext,
  208 + }); err != nil {
  209 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  210 + } else {
  211 + roleRepository = value
  212 + }
  213 + role, err := roleRepository.FindOne(map[string]interface{}{"roleId": removeRoleCommand.RoleId})
  214 + if err != nil {
  215 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  216 + }
  217 + if role == nil {
  218 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeRoleCommand.RoleId)))
  219 + }
  220 + if role, err := roleRepository.Remove(role); err != nil {
  221 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  222 + } else {
  223 + if err := transactionContext.CommitTransaction(); err != nil {
  224 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  225 + }
  226 + return role, nil
  227 + }
  228 +}
  229 +
  230 +// 取消用户分配的角色
  231 +func (roleService *RoleService) UnAssginRoleToUsers(unAssginRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) {
  232 + if err := unAssginRoleToUsersCommand.ValidateCommand(); err != nil {
  233 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  234 + }
  235 + transactionContext, err := factory.CreateTransactionContext(nil)
  236 + if err != nil {
  237 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  238 + }
  239 + if err := transactionContext.StartTransaction(); err != nil {
  240 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  241 + }
  242 + defer func() {
  243 + transactionContext.RollbackTransaction()
  244 + }()
  245 + if err := transactionContext.CommitTransaction(); err != nil {
  246 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  247 + }
  248 + return nil, nil
  249 +}
  250 +
  251 +// 更新角色
  252 +func (roleService *RoleService) UpdateRole(updateRoleCommand *command.UpdateRoleCommand) (interface{}, error) {
  253 + if err := updateRoleCommand.ValidateCommand(); err != nil {
  254 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  255 + }
  256 + transactionContext, err := factory.CreateTransactionContext(nil)
  257 + if err != nil {
  258 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  259 + }
  260 + if err := transactionContext.StartTransaction(); err != nil {
  261 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  262 + }
  263 + defer func() {
  264 + transactionContext.RollbackTransaction()
  265 + }()
  266 + var roleRepository domain.RoleRepository
  267 + if value, err := factory.CreateRoleRepository(map[string]interface{}{
  268 + "transactionContext": transactionContext,
  269 + }); err != nil {
  270 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  271 + } else {
  272 + roleRepository = value
  273 + }
  274 + role, err := roleRepository.FindOne(map[string]interface{}{"roleId": updateRoleCommand.RoleId})
  275 + if err != nil {
  276 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  277 + }
  278 + if role == nil {
  279 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateRoleCommand.RoleId)))
  280 + }
  281 + if err := role.Update(tool_funs.SimpleStructToMap(updateRoleCommand)); err != nil {
  282 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  283 + }
  284 + if role, err := roleRepository.Save(role); err != nil {
  285 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  286 + } else {
  287 + if err := transactionContext.CommitTransaction(); err != nil {
  288 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  289 + }
  290 + return role, nil
  291 + }
  292 +}
  293 +
  294 +// 设置角色菜单
  295 +func (roleService *RoleService) UpdateRoleAccessMenus(updateRoleAccessMenusCommand *command.UpdateRoleAccessMenusCommand) (interface{}, error) {
  296 + if err := updateRoleAccessMenusCommand.ValidateCommand(); err != nil {
  297 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  298 + }
  299 + transactionContext, err := factory.CreateTransactionContext(nil)
  300 + if err != nil {
  301 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  302 + }
  303 + if err := transactionContext.StartTransaction(); err != nil {
  304 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  305 + }
  306 + defer func() {
  307 + transactionContext.RollbackTransaction()
  308 + }()
  309 + if err := transactionContext.CommitTransaction(); err != nil {
  310 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  311 + }
  312 + return nil, nil
  313 +}
  314 +
  315 +func NewRoleService(options map[string]interface{}) *RoleService {
  316 + newRoleService := &RoleService{}
  317 + return newRoleService
  318 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/command"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/query"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/service"
  8 +)
  9 +
  10 +type RoleController struct {
  11 + beego.BaseController
  12 +}
  13 +
  14 +func (controller *RoleController) CreateRole() {
  15 + roleService := service.NewRoleService(nil)
  16 + createRoleCommand := &command.CreateRoleCommand{}
  17 + controller.Unmarshal(createRoleCommand)
  18 + data, err := roleService.CreateRole(createRoleCommand)
  19 + controller.Response(data, err)
  20 +}
  21 +
  22 +func (controller *RoleController) UpdateRole() {
  23 + roleService := service.NewRoleService(nil)
  24 + updateRoleCommand := &command.UpdateRoleCommand{}
  25 + controller.Unmarshal(updateRoleCommand)
  26 + roleId, _ := controller.GetInt64(":roleId")
  27 + updateRoleCommand.RoleId = roleId
  28 + data, err := roleService.UpdateRole(updateRoleCommand)
  29 + controller.Response(data, err)
  30 +}
  31 +
  32 +func (controller *RoleController) GetRole() {
  33 + roleService := service.NewRoleService(nil)
  34 + getRoleQuery := &query.GetRoleQuery{}
  35 + roleId, _ := controller.GetInt64(":roleId")
  36 + getRoleQuery.RoleId = roleId
  37 + data, err := roleService.GetRole(getRoleQuery)
  38 + controller.Response(data, err)
  39 +}
  40 +
  41 +func (controller *RoleController) RemoveRole() {
  42 + roleService := service.NewRoleService(nil)
  43 + removeRoleCommand := &command.RemoveRoleCommand{}
  44 + controller.Unmarshal(removeRoleCommand)
  45 + roleId, _ := controller.GetInt64(":roleId")
  46 + removeRoleCommand.RoleId = roleId
  47 + data, err := roleService.RemoveRole(removeRoleCommand)
  48 + controller.Response(data, err)
  49 +}
  50 +
  51 +func (controller *RoleController) ListRole() {
  52 + roleService := service.NewRoleService(nil)
  53 + listRoleQuery := &query.ListRoleQuery{}
  54 + data, err := roleService.ListRole(listRoleQuery)
  55 + controller.Response(data, err)
  56 +}
  57 +
  58 +func (controller *RoleController) GetRoleRelatedUsers() {
  59 + roleService := service.NewRoleService(nil)
  60 + getRoleRelatedUsersQuery := &query.GetRoleRelatedUsersQuery{}
  61 + roleId, _ := controller.GetInt64(":roleId")
  62 + getRoleRelatedUsersQuery.RoleId = roleId
  63 + orgId, _ := controller.GetInt64("orgId")
  64 + getRoleRelatedUsersQuery.OrgId = orgId
  65 + departmentId, _ := controller.GetInt64("departmentId")
  66 + getRoleRelatedUsersQuery.DepartmentId = departmentId
  67 + data, err := roleService.GetRoleRelatedUsers(getRoleRelatedUsersQuery)
  68 + controller.Response(data, err)
  69 +}
  70 +
  71 +func (controller *RoleController) GetRoleAccessMenus() {
  72 + roleService := service.NewRoleService(nil)
  73 + getRoleAccessMenusQuery := &query.GetRoleAccessMenusQuery{}
  74 + roleId, _ := controller.GetInt64(":roleId")
  75 + getRoleAccessMenusQuery.RoleId = roleId
  76 + data, err := roleService.GetRoleAccessMenus(getRoleAccessMenusQuery)
  77 + controller.Response(data, err)
  78 +}
  79 +
  80 +func (controller *RoleController) UpdateRoleAccessMenus() {
  81 + roleService := service.NewRoleService(nil)
  82 + updateRoleAccessMenusCommand := &command.UpdateRoleAccessMenusCommand{}
  83 + controller.Unmarshal(updateRoleAccessMenusCommand)
  84 + roleId, _ := controller.GetInt64(":roleId")
  85 + updateRoleAccessMenusCommand.RoleId = roleId
  86 + data, err := roleService.UpdateRoleAccessMenus(updateRoleAccessMenusCommand)
  87 + controller.Response(data, err)
  88 +}
  89 +
  90 +func (controller *RoleController) AssginRoleToUsers() {
  91 + roleService := service.NewRoleService(nil)
  92 + assginRoleToUsersCommand := &command.AssginRoleToUsersCommand{}
  93 + controller.Unmarshal(assginRoleToUsersCommand)
  94 + data, err := roleService.AssginRoleToUsers(assginRoleToUsersCommand)
  95 + controller.Response(data, err)
  96 +}
  97 +
  98 +func (controller *RoleController) UnAssginRoleToUsers() {
  99 + roleService := service.NewRoleService(nil)
  100 + unAssginRoleToUsersCommand := &command.UnAssginRoleToUsersCommand{}
  101 + controller.Unmarshal(unAssginRoleToUsersCommand)
  102 + data, err := roleService.UnAssginRoleToUsers(unAssginRoleToUsersCommand)
  103 + controller.Response(data, err)
  104 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/role/", &controllers.RoleController{}, "Post:CreateRole")
  10 + web.Router("/role/:roleId", &controllers.RoleController{}, "Put:UpdateRole")
  11 + web.Router("/role/:roleId", &controllers.RoleController{}, "Get:GetRole")
  12 + web.Router("/role/:roleId", &controllers.RoleController{}, "Delete:RemoveRole")
  13 + web.Router("/role/search", &controllers.RoleController{}, "Post:ListRole")
  14 + web.Router("/role/:roleId/related-user", &controllers.RoleController{}, "Get:GetRoleRelatedUsers")
  15 + web.Router("/role/:roleId/access-menus", &controllers.RoleController{}, "Get:GetRoleAccessMenus")
  16 + web.Router("/role/:roleId/access-menus", &controllers.RoleController{}, "Put:UpdateRoleAccessMenus")
  17 + web.Router("/role/assign", &controllers.RoleController{}, "Post:AssginRoleToUsers")
  18 + web.Router("/role/unassign", &controllers.RoleController{}, "Post:UnAssginRoleToUsers")
  19 +}
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("分配角色给多个用户", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("分配角色给多个用户", func() {
  23 + Context("", func() {
  24 + It("", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + body := map[string]interface{}{
  27 + "roleId": "int64",
  28 + "userIds": "array",
  29 + }
  30 + httpExpect.POST("/role/assign").
  31 + WithJSON(body).
  32 + Expect().
  33 + Status(http.StatusOK).
  34 + JSON().
  35 + Object().
  36 + ContainsKey("code").ValueEqual("code", 0).
  37 + ContainsKey("msg").ValueEqual("msg", "ok").
  38 + ContainsKey("data").Value("data").Object()
  39 + })
  40 + })
  41 + })
  42 + AfterEach(func() {
  43 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  44 + Expect(err).NotTo(HaveOccurred())
  45 + })
  46 +})
  1 +package role
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("创建角色", func() {
  13 + Describe("提交数据创建角色", func() {
  14 + Context("提交正确的新角色 (base)数据", func() {
  15 + It("返回角色 (base)数据", func() {
  16 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  17 + body := map[string]interface{}{
  18 + "roleName": "string",
  19 + "desc": "int64",
  20 + }
  21 + httpExpect.POST("/role/").
  22 + WithJSON(body).
  23 + Expect().
  24 + Status(http.StatusOK).
  25 + JSON().
  26 + Object().
  27 + ContainsKey("code").ValueEqual("code", 0).
  28 + ContainsKey("msg").ValueEqual("msg", "ok").
  29 + ContainsKey("data").Value("data").Object().
  30 + ContainsKey("roleId").ValueNotEqual("roleId", BeZero())
  31 + })
  32 + })
  33 + })
  34 + AfterEach(func() {
  35 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  36 + Expect(err).NotTo(HaveOccurred())
  37 + })
  38 +})
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("获取角色菜单", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("获取角色菜单", func() {
  23 + Context("", func() {
  24 + It("", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + httpExpect.GET("/role/{roleId}/access-menus").
  27 + Expect().
  28 + Status(http.StatusOK).
  29 + JSON().
  30 + Object().
  31 + ContainsKey("code").ValueEqual("code", 0).
  32 + ContainsKey("msg").ValueEqual("msg", "ok").
  33 + ContainsKey("data").Value("data").Object()
  34 + })
  35 + })
  36 + })
  37 + AfterEach(func() {
  38 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  39 + Expect(err).NotTo(HaveOccurred())
  40 + })
  41 +})
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("获取角色相关联的用户", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("获取角色相关联的用户", func() {
  23 + Context("", func() {
  24 + It("", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + httpExpect.GET("/role/{roleId}/related-user").
  27 + WithQuery("orgId", "int64").
  28 + WithQuery("departmentId", "int64").
  29 + Expect().
  30 + Status(http.StatusOK).
  31 + JSON().
  32 + Object().
  33 + ContainsKey("code").ValueEqual("code", 0).
  34 + ContainsKey("msg").ValueEqual("msg", "ok").
  35 + ContainsKey("data").Value("data").Object()
  36 + })
  37 + })
  38 + })
  39 + AfterEach(func() {
  40 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  41 + Expect(err).NotTo(HaveOccurred())
  42 + })
  43 +})
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("返回角色", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("根据roleId参数返回角色 (base)", func() {
  23 + Context("传入有效的roleId", func() {
  24 + It("返回角色 (base)数据", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + httpExpect.GET("/role/{roleId}").
  27 + Expect().
  28 + Status(http.StatusOK).
  29 + JSON().
  30 + Object().
  31 + ContainsKey("code").ValueEqual("code", 0).
  32 + ContainsKey("msg").ValueEqual("msg", "ok").
  33 + ContainsKey("data").Value("data").Object()
  34 + })
  35 + })
  36 + })
  37 + AfterEach(func() {
  38 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  39 + Expect(err).NotTo(HaveOccurred())
  40 + })
  41 +})
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("返回角色列表", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("根据参数返回角色 (base)列表", func() {
  23 + Context("传入有效的参数", func() {
  24 + It("返回角色 (base)数据列表", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + body := map[string]interface{}{
  27 + "offset": "int",
  28 + "limit": "int",
  29 + "roleType": "int",
  30 + "roleName": "string",
  31 + "orgName": "string",
  32 + "orgId": "int64",
  33 + "inOrgIds": "array",
  34 + }
  35 + httpExpect.POST("/role/search").
  36 + WithJSON(body).
  37 + Expect().
  38 + Status(http.StatusOK).
  39 + JSON().
  40 + Object().
  41 + ContainsKey("code").ValueEqual("code", 0).
  42 + ContainsKey("msg").ValueEqual("msg", "ok").
  43 + ContainsKey("data").Value("data").Object().
  44 + ContainsKey("count").ValueEqual("count", 1).
  45 + ContainsKey("roles").Value("roles").Array()
  46 + })
  47 + })
  48 + })
  49 + AfterEach(func() {
  50 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  51 + Expect(err).NotTo(HaveOccurred())
  52 + })
  53 +})
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("移除角色", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("根据参数移除角色", func() {
  23 + Context("传入有效的roleId", func() {
  24 + It("返回被移除角色 (base)的数据", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + httpExpect.DELETE("/role/{roleId}").
  27 + Expect().
  28 + Status(http.StatusOK).
  29 + JSON().
  30 + Object().
  31 + ContainsKey("code").ValueEqual("code", 0).
  32 + ContainsKey("msg").ValueEqual("msg", "ok").
  33 + ContainsKey("data").Value("data").Object()
  34 + })
  35 + })
  36 + })
  37 + AfterEach(func() {
  38 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  39 + Expect(err).NotTo(HaveOccurred())
  40 + })
  41 +})
  1 +package role
  2 +
  3 +import (
  4 + "net/http"
  5 + "net/http/httptest"
  6 + "testing"
  7 +
  8 + "github.com/beego/beego/v2/server/web"
  9 + . "github.com/onsi/ginkgo"
  10 + . "github.com/onsi/gomega"
  11 + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  12 + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego"
  13 +)
  14 +
  15 +func TestRole(t *testing.T) {
  16 + RegisterFailHandler(Fail)
  17 + RunSpecs(t, "Beego Port Role Correlations Test Case Suite")
  18 +}
  19 +
  20 +var handler http.Handler
  21 +var server *httptest.Server
  22 +
  23 +var _ = BeforeSuite(func() {
  24 + handler = web.BeeApp.Handlers
  25 + server = httptest.NewServer(handler)
  26 +})
  27 +
  28 +var _ = AfterSuite(func() {
  29 + server.Close()
  30 +})
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("取消用户分配的角色", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("取消用户分配的角色", func() {
  23 + Context("", func() {
  24 + It("", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + body := map[string]interface{}{
  27 + "roleId": "int64",
  28 + "userIds": "array",
  29 + }
  30 + httpExpect.POST("/role/unassign").
  31 + WithJSON(body).
  32 + Expect().
  33 + Status(http.StatusOK).
  34 + JSON().
  35 + Object().
  36 + ContainsKey("code").ValueEqual("code", 0).
  37 + ContainsKey("msg").ValueEqual("msg", "ok").
  38 + ContainsKey("data").Value("data").Object()
  39 + })
  40 + })
  41 + })
  42 + AfterEach(func() {
  43 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  44 + Expect(err).NotTo(HaveOccurred())
  45 + })
  46 +})
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("设置角色菜单", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("设置角色菜单", func() {
  23 + Context("", func() {
  24 + It("", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + body := map[string]interface{}{
  27 + "accessMenus": "array",
  28 + }
  29 + httpExpect.PUT("/role/{roleId}/access-menus").
  30 + WithJSON(body).
  31 + Expect().
  32 + Status(http.StatusOK).
  33 + JSON().
  34 + Object().
  35 + ContainsKey("code").ValueEqual("code", 0).
  36 + ContainsKey("msg").ValueEqual("msg", "ok").
  37 + ContainsKey("data").Value("data").Object()
  38 + })
  39 + })
  40 + })
  41 + AfterEach(func() {
  42 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  43 + Expect(err).NotTo(HaveOccurred())
  44 + })
  45 +})
  1 +package role
  2 +
  3 +import (
  4 + "github.com/go-pg/pg/v10"
  5 + "net/http"
  6 +
  7 + "github.com/gavv/httpexpect"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("更新角色", func() {
  14 + var roleId int64
  15 + BeforeEach(func() {
  16 + _, err := pG.DB.QueryOne(
  17 + pg.Scan(&roleId),
  18 + "INSERT INTO roles (role_id, company_id, org_id, role_type, role_name, access_menus, desc, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING role_id",
  19 + "testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
  20 + Expect(err).NotTo(HaveOccurred())
  21 + })
  22 + Describe("提交数据更新角色", func() {
  23 + Context("提交正确的角色 (base)数据", func() {
  24 + It("返回更新后的角色 (base)数据", func() {
  25 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  26 + body := map[string]interface{}{
  27 + "roleName": "string",
  28 + "desc": "int64",
  29 + }
  30 + httpExpect.PUT("/role/{roleId}").
  31 + WithJSON(body).
  32 + Expect().
  33 + Status(http.StatusOK).
  34 + JSON().
  35 + Object().
  36 + ContainsKey("code").ValueEqual("code", 0).
  37 + ContainsKey("msg").ValueEqual("msg", "ok").
  38 + ContainsKey("data").Value("data").Object().
  39 + ContainsKey("roleId").ValueEqual("roleId", roleId)
  40 + })
  41 + })
  42 + })
  43 + AfterEach(func() {
  44 + _, err := pG.DB.Exec("DELETE FROM roles WHERE true")
  45 + Expect(err).NotTo(HaveOccurred())
  46 + })
  47 +})