作者 yangfu

add role

package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type AssginRoleToUsersCommand struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
// 用户列表
UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"`
}
func (assginRoleToUsersCommand *AssginRoleToUsersCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (assginRoleToUsersCommand *AssginRoleToUsersCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(assginRoleToUsersCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(assginRoleToUsersCommand).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
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type CreateRoleCommand struct {
// 角色名称
RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
// 描述
Desc int64 `cname:"描述" json:"desc,string,omitempty"`
}
func (createRoleCommand *CreateRoleCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (createRoleCommand *CreateRoleCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(createRoleCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(createRoleCommand).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
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type RemoveRoleCommand struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
}
func (removeRoleCommand *RemoveRoleCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeRoleCommand *RemoveRoleCommand) 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
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type UnAssginRoleToUsersCommand struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
// 用户列表
UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"`
}
func (unAssginRoleToUsersCommand *UnAssginRoleToUsersCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (unAssginRoleToUsersCommand *UnAssginRoleToUsersCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(unAssginRoleToUsersCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(unAssginRoleToUsersCommand).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
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type UpdateRoleCommand struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
// 角色名称
RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
// 描述
Desc int64 `cname:"描述" json:"desc,string,omitempty"`
}
func (updateRoleCommand *UpdateRoleCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateRoleCommand *UpdateRoleCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateRoleCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(updateRoleCommand).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
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type UpdateRoleAccessMenusCommand struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
// 菜单编号列表
AccessMenus []int64 `cname:"菜单编号列表" json:"accessMenus,omitempty"`
}
func (updateRoleAccessMenusCommand *UpdateRoleAccessMenusCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateRoleAccessMenusCommand *UpdateRoleAccessMenusCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateRoleAccessMenusCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(updateRoleAccessMenusCommand).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
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type GetRoleQuery struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
}
func (getRoleQuery *GetRoleQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getRoleQuery *GetRoleQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getRoleQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(getRoleQuery).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
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type GetRoleAccessMenusQuery struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
}
func (getRoleAccessMenusQuery *GetRoleAccessMenusQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getRoleAccessMenusQuery *GetRoleAccessMenusQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getRoleAccessMenusQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(getRoleAccessMenusQuery).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
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type GetRoleRelatedUsersQuery struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string,omitempty"`
// 部门编号
DepartmentId int64 `cname:"部门编号" json:"departmentId,string,omitempty"`
}
func (getRoleRelatedUsersQuery *GetRoleRelatedUsersQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getRoleRelatedUsersQuery *GetRoleRelatedUsersQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getRoleRelatedUsersQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(getRoleRelatedUsersQuery).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
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type ListRoleQuery struct {
// 查询偏离量
Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
// 查询限制
Limit int `cname:"查询限制" json:"limit" valid:"Required"`
// 角色类型 1.普通角色 1024:超级管理员
RoleType int `cname:"角色类型 1.普通角色 1024:超级管理员" json:"roleType,omitempty"`
// 角色名称
RoleName string `cname:"角色名称" json:"roleName,omitempty"`
// 组织名称
OrgName string `cname:"组织名称" json:"orgName,omitempty"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string,omitempty"`
// 匹配多个组织
InOrgIds []int64 `cname:"匹配多个组织" json:"inOrgIds,omitempty"`
}
func (listRoleQuery *ListRoleQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listRoleQuery *ListRoleQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listRoleQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(listRoleQuery).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
}
... ...
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
)
// 角色
type RoleService struct {
}
// 分配角色给多个用户
func (roleService *RoleService) AssginRoleToUsers(assginRoleToUsersCommand *command.AssginRoleToUsersCommand) (interface{}, error) {
if err := assginRoleToUsersCommand.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()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 创建角色
func (roleService *RoleService) CreateRole(createRoleCommand *command.CreateRoleCommand) (interface{}, error) {
if err := createRoleCommand.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()
}()
newRole := &domain.Role{
RoleName: createRoleCommand.RoleName,
Desc: createRoleCommand.Desc,
}
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
}
if role, err := roleRepository.Save(newRole); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return role, nil
}
}
// 返回角色
func (roleService *RoleService) GetRole(getRoleQuery *query.GetRoleQuery) (interface{}, error) {
if err := getRoleQuery.ValidateQuery(); 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
}
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": getRoleQuery.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("%s", string(getRoleQuery.RoleId)))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return role, nil
}
}
// 获取角色菜单
func (roleService *RoleService) GetRoleAccessMenus(getRoleAccessMenusQuery *query.GetRoleAccessMenusQuery) (interface{}, error) {
if err := getRoleAccessMenusQuery.ValidateQuery(); 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()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 获取角色相关联的用户
func (roleService *RoleService) GetRoleRelatedUsers(getRoleRelatedUsersQuery *query.GetRoleRelatedUsersQuery) (interface{}, error) {
if err := getRoleRelatedUsersQuery.ValidateQuery(); 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()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 返回角色列表
func (roleService *RoleService) ListRole(listRoleQuery *query.ListRoleQuery) (interface{}, error) {
if err := listRoleQuery.ValidateQuery(); 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
}
if count, roles, err := roleRepository.Find(tool_funs.SimpleStructToMap(listRoleQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"count": count,
"roles": roles,
}, nil
}
}
// 移除角色
func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRoleCommand) (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
}
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": removeRoleCommand.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("%s", string(removeRoleCommand.RoleId)))
}
if role, err := roleRepository.Remove(role); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return role, nil
}
}
// 取消用户分配的角色
func (roleService *RoleService) UnAssginRoleToUsers(unAssginRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) {
if err := unAssginRoleToUsersCommand.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()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 更新角色
func (roleService *RoleService) UpdateRole(updateRoleCommand *command.UpdateRoleCommand) (interface{}, error) {
if err := updateRoleCommand.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
}
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": updateRoleCommand.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("%s", string(updateRoleCommand.RoleId)))
}
if err := role.Update(tool_funs.SimpleStructToMap(updateRoleCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if role, err := roleRepository.Save(role); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return role, nil
}
}
// 设置角色菜单
func (roleService *RoleService) UpdateRoleAccessMenus(updateRoleAccessMenusCommand *command.UpdateRoleAccessMenusCommand) (interface{}, error) {
if err := updateRoleAccessMenusCommand.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()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
func NewRoleService(options map[string]interface{}) *RoleService {
newRoleService := &RoleService{}
return newRoleService
}
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/service"
)
type RoleController struct {
beego.BaseController
}
func (controller *RoleController) CreateRole() {
roleService := service.NewRoleService(nil)
createRoleCommand := &command.CreateRoleCommand{}
controller.Unmarshal(createRoleCommand)
data, err := roleService.CreateRole(createRoleCommand)
controller.Response(data, err)
}
func (controller *RoleController) UpdateRole() {
roleService := service.NewRoleService(nil)
updateRoleCommand := &command.UpdateRoleCommand{}
controller.Unmarshal(updateRoleCommand)
roleId, _ := controller.GetInt64(":roleId")
updateRoleCommand.RoleId = roleId
data, err := roleService.UpdateRole(updateRoleCommand)
controller.Response(data, err)
}
func (controller *RoleController) GetRole() {
roleService := service.NewRoleService(nil)
getRoleQuery := &query.GetRoleQuery{}
roleId, _ := controller.GetInt64(":roleId")
getRoleQuery.RoleId = roleId
data, err := roleService.GetRole(getRoleQuery)
controller.Response(data, err)
}
func (controller *RoleController) RemoveRole() {
roleService := service.NewRoleService(nil)
removeRoleCommand := &command.RemoveRoleCommand{}
controller.Unmarshal(removeRoleCommand)
roleId, _ := controller.GetInt64(":roleId")
removeRoleCommand.RoleId = roleId
data, err := roleService.RemoveRole(removeRoleCommand)
controller.Response(data, err)
}
func (controller *RoleController) ListRole() {
roleService := service.NewRoleService(nil)
listRoleQuery := &query.ListRoleQuery{}
data, err := roleService.ListRole(listRoleQuery)
controller.Response(data, err)
}
func (controller *RoleController) GetRoleRelatedUsers() {
roleService := service.NewRoleService(nil)
getRoleRelatedUsersQuery := &query.GetRoleRelatedUsersQuery{}
roleId, _ := controller.GetInt64(":roleId")
getRoleRelatedUsersQuery.RoleId = roleId
orgId, _ := controller.GetInt64("orgId")
getRoleRelatedUsersQuery.OrgId = orgId
departmentId, _ := controller.GetInt64("departmentId")
getRoleRelatedUsersQuery.DepartmentId = departmentId
data, err := roleService.GetRoleRelatedUsers(getRoleRelatedUsersQuery)
controller.Response(data, err)
}
func (controller *RoleController) GetRoleAccessMenus() {
roleService := service.NewRoleService(nil)
getRoleAccessMenusQuery := &query.GetRoleAccessMenusQuery{}
roleId, _ := controller.GetInt64(":roleId")
getRoleAccessMenusQuery.RoleId = roleId
data, err := roleService.GetRoleAccessMenus(getRoleAccessMenusQuery)
controller.Response(data, err)
}
func (controller *RoleController) UpdateRoleAccessMenus() {
roleService := service.NewRoleService(nil)
updateRoleAccessMenusCommand := &command.UpdateRoleAccessMenusCommand{}
controller.Unmarshal(updateRoleAccessMenusCommand)
roleId, _ := controller.GetInt64(":roleId")
updateRoleAccessMenusCommand.RoleId = roleId
data, err := roleService.UpdateRoleAccessMenus(updateRoleAccessMenusCommand)
controller.Response(data, err)
}
func (controller *RoleController) AssginRoleToUsers() {
roleService := service.NewRoleService(nil)
assginRoleToUsersCommand := &command.AssginRoleToUsersCommand{}
controller.Unmarshal(assginRoleToUsersCommand)
data, err := roleService.AssginRoleToUsers(assginRoleToUsersCommand)
controller.Response(data, err)
}
func (controller *RoleController) UnAssginRoleToUsers() {
roleService := service.NewRoleService(nil)
unAssginRoleToUsersCommand := &command.UnAssginRoleToUsersCommand{}
controller.Unmarshal(unAssginRoleToUsersCommand)
data, err := roleService.UnAssginRoleToUsers(unAssginRoleToUsersCommand)
controller.Response(data, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego/controllers"
)
func init() {
web.Router("/role/", &controllers.RoleController{}, "Post:CreateRole")
web.Router("/role/:roleId", &controllers.RoleController{}, "Put:UpdateRole")
web.Router("/role/:roleId", &controllers.RoleController{}, "Get:GetRole")
web.Router("/role/:roleId", &controllers.RoleController{}, "Delete:RemoveRole")
web.Router("/role/search", &controllers.RoleController{}, "Post:ListRole")
web.Router("/role/:roleId/related-user", &controllers.RoleController{}, "Get:GetRoleRelatedUsers")
web.Router("/role/:roleId/access-menus", &controllers.RoleController{}, "Get:GetRoleAccessMenus")
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")
}
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("分配角色给多个用户", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("分配角色给多个用户", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"roleId": "int64",
"userIds": "array",
}
httpExpect.POST("/role/assign").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("创建角色", func() {
Describe("提交数据创建角色", func() {
Context("提交正确的新角色 (base)数据", func() {
It("返回角色 (base)数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"roleName": "string",
"desc": "int64",
}
httpExpect.POST("/role/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("roleId").ValueNotEqual("roleId", BeZero())
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("获取角色菜单", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("获取角色菜单", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/role/{roleId}/access-menus").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("获取角色相关联的用户", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("获取角色相关联的用户", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/role/{roleId}/related-user").
WithQuery("orgId", "int64").
WithQuery("departmentId", "int64").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("返回角色", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据roleId参数返回角色 (base)", func() {
Context("传入有效的roleId", func() {
It("返回角色 (base)数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/role/{roleId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("返回角色列表", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回角色 (base)列表", func() {
Context("传入有效的参数", func() {
It("返回角色 (base)数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"offset": "int",
"limit": "int",
"roleType": "int",
"roleName": "string",
"orgName": "string",
"orgId": "int64",
"inOrgIds": "array",
}
httpExpect.POST("/role/search").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("count").ValueEqual("count", 1).
ContainsKey("roles").Value("roles").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("移除角色", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除角色", func() {
Context("传入有效的roleId", func() {
It("返回被移除角色 (base)的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/role/{roleId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/beego/beego/v2/server/web"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego"
)
func TestRole(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port Role Correlations Test Case Suite")
}
var handler http.Handler
var server *httptest.Server
var _ = BeforeSuite(func() {
handler = web.BeeApp.Handlers
server = httptest.NewServer(handler)
})
var _ = AfterSuite(func() {
server.Close()
})
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("取消用户分配的角色", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("取消用户分配的角色", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"roleId": "int64",
"userIds": "array",
}
httpExpect.POST("/role/unassign").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("设置角色菜单", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("设置角色菜单", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"accessMenus": "array",
}
httpExpect.PUT("/role/{roleId}/access-menus").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg"
)
var _ = Describe("更新角色", func() {
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&roleId),
"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",
"testRoleId", "testCompanyId", "testOrgId", "testRoleType", "testRoleName", "testAccessMenus", "testDesc", "testExt", "testCreatedAt", "testUpdatedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新角色", func() {
Context("提交正确的角色 (base)数据", func() {
It("返回更新后的角色 (base)数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"roleName": "string",
"desc": "int64",
}
httpExpect.PUT("/role/{roleId}").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("roleId").ValueEqual("roleId", roleId)
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...