作者 yangfu

基础角色

... ... @@ -54,3 +54,11 @@ func CreatePgUpdateUserService(options map[string]interface{}) (service.PgUpdate
}
return domainService.NewPgUpdateUserService(transactionContext)
}
func CreatePgCreateRoleService(options map[string]interface{}) (service.PgCreateRoleService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgCreateRoleService(transactionContext)
}
... ...
... ... @@ -9,10 +9,14 @@ import (
)
type CreateRoleCommand struct {
// 用户ID
UserId int64 `cname:"用户ID" json:"userId" valid:"Required`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required`
// 角色名称
RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
// 描述
Desc string `cname:"描述" json:"desc,string,omitempty"`
Desc string `cname:"描述" json:"desc,omitempty"`
}
func (createRoleCommand *CreateRoleCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -10,7 +10,7 @@ import (
type RemoveRoleCommand struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
RoleId int64 `cname:"角色ID" json:"roleId" valid:"Required"`
}
func (removeRoleCommand *RemoveRoleCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -14,7 +14,7 @@ type UpdateRoleCommand struct {
// 角色名称
RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
// 描述
Desc string `cname:"描述" json:"desc,string,omitempty"`
Desc string `cname:"描述" json:"desc,omitempty"`
}
func (updateRoleCommand *UpdateRoleCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -10,7 +10,7 @@ import (
type GetRoleQuery struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
RoleId int64 `cname:"角色ID" json:"roleId" valid:"Required"`
}
func (getRoleQuery *GetRoleQuery) Valid(validation *validation.Validation) {
... ...
... ... @@ -10,7 +10,7 @@ import (
type ListRoleQuery struct {
// 查询偏离量
Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
Offset int `cname:"查询偏离量" json:"offset"`
// 查询限制
Limit int `cname:"查询限制" json:"limit" valid:"Required"`
// 角色类型 1.普通角色 1024:超级管理员
... ... @@ -20,7 +20,7 @@ type ListRoleQuery struct {
// 组织名称
OrgName string `cname:"组织名称" json:"orgName,omitempty"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string,omitempty"`
OrgId int64 `cname:"组织ID" json:"orgId,omitempty"`
// 匹配多个组织
InOrgIds []int64 `cname:"匹配多个组织" json:"inOrgIds,omitempty"`
}
... ...
... ... @@ -8,6 +8,7 @@ import (
"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"
"time"
)
// 角色
... ... @@ -50,26 +51,35 @@ func (roleService *RoleService) CreateRole(createRoleCommand *command.CreateRole
defer func() {
transactionContext.RollbackTransaction()
}()
newRole := &domain.Role{
RoleName: createRoleCommand.RoleName,
Desc: createRoleCommand.Desc,
_, org, err := factory.FastPgOrg(transactionContext, createRoleCommand.OrgId)
if err != nil {
return nil, err
}
var roleRepository domain.RoleRepository
if value, err := factory.CreateRoleRepository(map[string]interface{}{
createRoleService, err := factory.CreatePgCreateRoleService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
roleRepository = value
}
if role, err := roleRepository.Save(newRole); err != nil {
newRole := &domain.Role{
RoleName: createRoleCommand.RoleName,
Desc: createRoleCommand.Desc,
OrgId: org.OrgId,
CompanyId: org.CompanyId,
AccessMenus: []int64{},
RoleType: domain.RoleTypeNormal,
Ext: &domain.Ext{
OrgName: org.OrgName,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
role, err := createRoleService.CreateRole(nil, newRole)
if 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
}
}
// 返回角色
... ... @@ -174,7 +184,9 @@ func (roleService *RoleService) ListRole(listRoleQuery *query.ListRoleQuery) (in
} else {
roleRepository = value
}
if count, roles, err := roleRepository.Find(tool_funs.SimpleStructToMap(listRoleQuery)); err != nil {
queryOptions := tool_funs.SimpleStructToMap(listRoleQuery)
queryOptions["includeDeleted"] = false
if count, roles, err := roleRepository.Find(queryOptions); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
... ...
package domain
import "time"
import (
"time"
)
// 角色类型 1.普通角色 1024:超级管理员
const (
... ... @@ -34,6 +36,8 @@ type Role struct {
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
// 删除时间
DeletedAt time.Time `json:"deletedAt,omitempty"`
}
type RoleRepository interface {
... ... @@ -104,3 +108,11 @@ func (role *Role) CloneSample() *Role {
Ext: role.Ext,
}
}
// IsDeleted 判断角色是否已删除
func (role *Role) IsDeleted() bool {
if role.DeletedAt.IsZero() {
return false
}
return true
}
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgCreateRoleService 传教角色服务
type PgCreateRoleService interface {
CreateRole(optUser *domain.CheckOptions, roleInfo *domain.Role) (*domain.Role, error)
}
... ...
... ... @@ -16,7 +16,7 @@ type PgCreateRoleService struct {
//
// optUser 操作用户
// roleInfo 角色信息
func (ptr *PgCreateRoleService) CreateRole(optUser *domain.User, roleInfo *domain.Role) (*domain.Role, error) {
func (ptr *PgCreateRoleService) CreateRole(optUser *domain.CheckOptions, roleInfo *domain.Role) (*domain.Role, error) {
if len(roleInfo.RoleName) == 0 {
return nil, fmt.Errorf("角色名称不能为空")
}
... ...
... ... @@ -27,4 +27,6 @@ type Role struct {
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
UpdatedAt time.Time `comment:"更新时间"`
// 删除时间
DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"`
}
... ...
... ... @@ -17,5 +17,6 @@ func TransformToRoleDomainModelFromPgModels(roleModel *models.Role) (*domain.Rol
Ext: roleModel.Ext,
CreatedAt: roleModel.CreatedAt,
UpdatedAt: roleModel.UpdatedAt,
DeletedAt: roleModel.DeletedAt,
}, nil
}
... ...
... ... @@ -144,6 +144,13 @@ func (repository *RoleRepository) Find(queryOptions map[string]interface{}) (int
roles := make([]*domain.Role, 0)
query := sqlbuilder.BuildQuery(tx.Model(&roleModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("role_name = ?", "roleName")
// 包含删除的
if v, ok := queryOptions["includeDeleted"]; ok && !(v.(bool)) {
query.Where("deleted_at is null")
}
query.SetOrderDirect("role_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, roles, err
... ...
... ... @@ -14,7 +14,7 @@ type RoleController struct {
func (controller *RoleController) CreateRole() {
roleService := service.NewRoleService(nil)
createRoleCommand := &command.CreateRoleCommand{}
controller.Unmarshal(createRoleCommand)
Must(controller.Unmarshal(createRoleCommand))
data, err := roleService.CreateRole(createRoleCommand)
controller.Response(data, err)
}
... ... @@ -55,6 +55,14 @@ func (controller *RoleController) ListRole() {
controller.Response(data, err)
}
func (controller *RoleController) SearchRole() {
roleService := service.NewRoleService(nil)
listRoleQuery := &query.ListRoleQuery{}
Must(controller.Unmarshal(listRoleQuery))
data, err := roleService.ListRole(listRoleQuery)
controller.Response(data, err)
}
func (controller *RoleController) GetRoleRelatedUsers() {
roleService := service.NewRoleService(nil)
getRoleRelatedUsersQuery := &query.GetRoleRelatedUsersQuery{}
... ...
... ... @@ -10,7 +10,7 @@ func init() {
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/search", &controllers.RoleController{}, "Post:SearchRole")
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")
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("分配角色给多个用户", func() {
return
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
package role
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
... ... @@ -10,6 +11,17 @@ import (
)
var _ = Describe("创建角色", func() {
BeforeEach(func() {
var userId int64
var err error
Expect(err).NotTo(HaveOccurred())
_, err = pG.DB.QueryOne(
pg.Scan(&userId),
"INSERT INTO users.org (org_id,company_id,created_at,updated_at,deleted_at,org_code,org_name,ext,org_status,is_org,parent_id,parent_path) VALUES (999,999,'2021-07-26 08:06:29.3101584+00:00:00','2021-07-26 08:06:29.3101584+00:00:00','0001-01-01 00:00:00+00:00:00','ENTERPRISE01','string1','{}',1,1,0,'');",
)
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据创建角色", func() {
Context("提交正确的新角色 (base)数据", func() {
It("返回角色 (base)数据", func() {
... ... @@ -17,6 +29,8 @@ var _ = Describe("创建角色", func() {
body := map[string]interface{}{
"roleName": "string",
"desc": "int64",
"userId": 999,
"orgId": 999,
}
httpExpect.POST("/role/").
WithJSON(body).
... ... @@ -32,7 +46,9 @@ var _ = Describe("创建角色", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.role WHERE true")
Expect(err).NotTo(HaveOccurred())
_, err = pG.DB.Exec("DELETE FROM users.org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("获取角色菜单", func() {
return
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("获取角色相关联的用户", func() {
return
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -15,15 +15,14 @@ var _ = Describe("返回角色", func() {
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")
"INSERT INTO users.role (role_id,company_id,org_id,role_type,role_name,access_menus,\"desc\",ext,created_at,updated_at) VALUES (999,999,999,1,'string','{}','int64','{\"orgName\":\"string1\"}','2021-07-27 02:42:08.8025692+00:00:00','2021-07-27 02:42:08.8025692+00:00:00') RETURNING role_id;\n")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据roleId参数返回角色 (base)", func() {
Context("传入有效的roleId", func() {
It("返回角色 (base)数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/role/{roleId}").
httpExpect.GET("/role/999").
Expect().
Status(http.StatusOK).
JSON().
... ... @@ -35,7 +34,7 @@ var _ = Describe("返回角色", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.role WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -15,8 +15,7 @@ var _ = Describe("返回角色列表", func() {
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")
"INSERT INTO users.role (role_id,company_id,org_id,role_type,role_name,access_menus,\"desc\",ext,created_at,updated_at) VALUES (999,999,999,1,'string','{}','int64','{\"orgName\":\"string1\"}','2021-07-27 02:42:08.8025692+00:00:00','2021-07-27 02:42:08.8025692+00:00:00') RETURNING role_id;\n")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回角色 (base)列表", func() {
... ... @@ -24,13 +23,13 @@ var _ = Describe("返回角色列表", func() {
It("返回角色 (base)数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"offset": "int",
"limit": "int",
"roleType": "int",
"offset": 0,
"limit": 20,
"roleType": 1,
"roleName": "string",
"orgName": "string",
"orgId": "int64",
"inOrgIds": "array",
"orgId": 999,
"inOrgIds": []int64{999},
}
httpExpect.POST("/role/search").
WithJSON(body).
... ... @@ -47,7 +46,7 @@ var _ = Describe("返回角色列表", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.role WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -15,15 +15,14 @@ var _ = Describe("移除角色", func() {
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")
"INSERT INTO users.role (role_id,company_id,org_id,role_type,role_name,access_menus,\"desc\",ext,created_at,updated_at) VALUES (999,999,999,1,'string','{}','int64','{\"orgName\":\"string1\"}','2021-07-27 02:42:08.8025692+00:00:00','2021-07-27 02:42:08.8025692+00:00:00') RETURNING role_id;\n")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除角色", func() {
Context("传入有效的roleId", func() {
It("返回被移除角色 (base)的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/role/{roleId}").
httpExpect.DELETE("/role/999").
Expect().
Status(http.StatusOK).
JSON().
... ... @@ -35,7 +34,7 @@ var _ = Describe("移除角色", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
Expect(err).NotTo(HaveOccurred())
//_, err := pG.DB.Exec("DELETE FROM users.role WHERE true")
//Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("取消用户分配的角色", func() {
return
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("设置角色菜单", func() {
return
var roleId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -15,8 +15,13 @@ var _ = Describe("更新角色", func() {
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")
"INSERT INTO users.role (role_id,company_id,org_id,role_type,role_name,access_menus,\"desc\",ext,created_at,updated_at) VALUES (999,999,999,1,'string','{}','int64','{\"orgName\":\"string1\"}','2021-07-27 02:42:08.8025692+00:00:00','2021-07-27 02:42:08.8025692+00:00:00') RETURNING role_id;\n")
Expect(err).NotTo(HaveOccurred())
_, err = pG.DB.QueryOne(
pg.Scan(&roleId),
"INSERT INTO users.org (org_id,company_id,created_at,updated_at,deleted_at,org_code,org_name,ext,org_status,is_org,parent_id,parent_path) VALUES (999,999,'2021-07-26 08:06:29.3101584+00:00:00','2021-07-26 08:06:29.3101584+00:00:00','0001-01-01 00:00:00+00:00:00','ENTERPRISE01','string1','{}',1,1,0,'');",
)
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新角色", func() {
... ... @@ -25,9 +30,9 @@ var _ = Describe("更新角色", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"roleName": "string",
"desc": "int64",
"desc": "int64111",
}
httpExpect.PUT("/role/{roleId}").
httpExpect.PUT("/role/999").
WithJSON(body).
Expect().
Status(http.StatusOK).
... ... @@ -41,7 +46,9 @@ var _ = Describe("更新角色", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM roles WHERE true")
_, err := pG.DB.Exec("DELETE FROM users.role WHERE true")
Expect(err).NotTo(HaveOccurred())
_, err = pG.DB.Exec("DELETE FROM users.org WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...