作者 yangfu

用户新增 、数据权限全局接口

... ... @@ -2,6 +2,7 @@ package factory
import (
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/domainService"
)
... ... @@ -29,3 +30,19 @@ func CreatePgAuthAccountDestroyService(options map[string]interface{}) (service.
}
return domainService.NewPgAuthAccountDestroyService(transactionContext)
}
func CreatePgCreateUserService(options map[string]interface{}) (service.PgCreateUserService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgCreateUserService(transactionContext)
}
func CreatePgDataAuthService(options map[string]interface{}) (domain.DataAuthor, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgDataAuthService(transactionContext)
}
... ...
... ... @@ -10,24 +10,28 @@ import (
)
type CreateCooperatorCommand struct {
// 企业ID
CompanyId int64 `cname:"企业ID" json:"companyId" valid:"Required"`
// 共创公司
CooperationCompany string `cname:"共创公司" json:"cooperationCompany" valid:"Required"`
CooperationCompany string `cname:"共创公司" json:"cooperationCompany"`
// 共创到期时间
CooperationDeadline time.Time `cname:"共创到期时间" json:"cooperationDeadline" valid:"Required"`
CooperationDeadline time.Time `cname:"共创到期时间" json:"cooperationDeadline"`
// 邮箱
Email string `cname:"邮箱" json:"email" valid:"Required"`
Email string `cname:"邮箱" json:"email"`
// 启用状态(启用:1 禁用:2 注销:3)
EnableStatus int `cname:"启用状态(启用:1 禁用:2 注销:3)" json:"enableStatus" valid:"Required"`
// 用户编号 企业内标识
UserCode string `cname:"用户编号 企业内标识" json:"userCode" valid:"Required"`
// 用户Id 用户唯一标识
UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string" valid:"Required"`
// 用户姓名
UserName string `cname:"用户姓名" json:"userName" valid:"Required"`
// 头像
Avatar string `cname:"头像" json:"avatar" valid:"Required"`
Avatar string `cname:"头像" json:"avatar"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string" valid:"Required"`
OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required"`
// 手机号码
Phone string `cname:"手机号码" json:"phone" valid:"Required"`
// 密码
Password string `cname:"密码" json:"password" valid:"Required"`
}
func (createCooperatorCommand *CreateCooperatorCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -2,25 +2,24 @@ package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"reflect"
"strings"
"time"
"github.com/beego/beego/v2/core/validation"
)
type CreateUserCommand struct {
// 企业id
CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"`
CompanyId int64 `cname:"企业id" json:"companyId" valid:"Required"`
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UserType int `cname:"用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)" json:"userType" valid:"Required"`
// 用户编号 企业内标识
UserCode string `cname:"用户编号 企业内标识" json:"userCode" valid:"Required"`
// 组织机构
OrganizationId int64 `cname:"组织机构" json:"organizationId,string,omitempty"`
OrganizationId int64 `cname:"组织机构" json:"organizationId,omitempty" valid:"Required"`
// 所属部门
DepartmentId int64 `cname:"所属部门" json:"departmentId,string,omitempty"`
DepartmentId int64 `cname:"所属部门" json:"departmentId,omitempty" valid:"Required"`
// 用户关联的组织
UserOrg []*domain.Org `cname:"用户关联的组织" json:"userOrg,omitempty"`
// 用户关联的角色
... ... @@ -30,7 +29,7 @@ type CreateUserCommand struct {
// 共创到期时间 (yyyy-MM-dd)
CooperationDeadline time.Time `cname:"共创到期时间 (yyyy-MM-dd)" json:"cooperationDeadline,omitempty"`
// 启用状态(启用:1 禁用:2)
EnableStatus int `cname:"启用状态(启用:1 禁用:2)" json:"enableStatus,omitempty"`
EnableStatus int `cname:"启用状态(启用:1 禁用:2)" json:"enableStatus,omitempty" valid:"Required"`
// 密码
Password string `cname:"密码" json:"password" valid:"Required"`
// 用户姓名
... ... @@ -38,9 +37,9 @@ type CreateUserCommand struct {
// 手机号码
Phone string `cname:"手机号码" json:"phone" valid:"Required"`
// 头像
Avatar string `cname:"头像" json:"avatar" valid:"Required"`
Avatar string `cname:"头像" json:"avatar"`
// 邮箱
Email string `cname:"邮箱" json:"email" valid:"Required"`
Email string `cname:"邮箱" json:"email"`
}
func (createUserCommand *CreateUserCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -14,7 +14,7 @@ type RemoveUserCommand struct {
}
func (removeUserCommand *RemoveUserCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeUserCommand *RemoveUserCommand) ValidateCommand() error {
... ...
... ... @@ -8,6 +8,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"time"
)
// 用户
... ... @@ -92,10 +93,43 @@ func (userService *UserService) CreateCooperator(createCooperatorCommand *comman
defer func() {
transactionContext.RollbackTransaction()
}()
nweUserInfo := &domain.UserInfo{
UserName: createCooperatorCommand.UserName,
Phone: createCooperatorCommand.Phone,
Avatar: createCooperatorCommand.Avatar,
Email: createCooperatorCommand.Email,
}
var sampleUserOrg = make([]*domain.Org, 0)
var sampleUserRole = make([]*domain.Role, 0)
newUser := &domain.User{
CompanyId: createCooperatorCommand.CompanyId,
UserType: domain.UserTypeCooperation,
UserCode: createCooperatorCommand.UserCode,
OrganizationId: createCooperatorCommand.OrgId,
UserOrg: sampleUserOrg,
UserRole: sampleUserRole,
FavoriteMenus: []string{},
CooperationInfo: &domain.CooperationInfo{},
UserInfo: nweUserInfo,
//EnableStatus: createUserCommand.EnableStatus,
Ext: &domain.Ext{
Phone: createCooperatorCommand.Phone,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
var user *domain.User
createUserService, _ := factory.CreatePgCreateUserService(map[string]interface{}{
"transactionContext": transactionContext,
})
if user, err = createUserService.CreateUser(nil, newUser, createCooperatorCommand.Password); err != nil {
return nil, err
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return user, nil
}
// 创建
... ... @@ -113,38 +147,49 @@ func (userService *UserService) CreateUser(createUserCommand *command.CreateUser
defer func() {
transactionContext.RollbackTransaction()
}()
newUser := &domain.User{
CompanyId: createUserCommand.CompanyId,
UserType: createUserCommand.UserType,
UserCode: createUserCommand.UserCode,
OrganizationId: createUserCommand.OrganizationId,
DepartmentId: createUserCommand.DepartmentId,
UserOrg: createUserCommand.UserOrg,
UserRole: createUserCommand.UserRole,
//CooperationInfo: createUserCommand.CooperationInfo,
EnableStatus: createUserCommand.EnableStatus,
//Password: createUserCommand.Password,
//UserName: createUserCommand.UserName,
//Phone: createUserCommand.Phone,
//Avatar: createUserCommand.Avatar,
//Email: createUserCommand.Email,
nweUserInfo := &domain.UserInfo{
UserName: createUserCommand.UserName,
Phone: createUserCommand.Phone,
Avatar: createUserCommand.Avatar,
Email: createUserCommand.Email,
}
var userRepository domain.UserRepository
if value, err := factory.CreateUserRepository(map[string]interface{}{
var sampleUserOrg = make([]*domain.Org, 0)
var sampleUserRole = make([]*domain.Role, 0)
for i := range createUserCommand.UserOrg {
sampleUserOrg = append(sampleUserOrg, createUserCommand.UserOrg[i].CloneSample())
}
for i := range createUserCommand.UserRole {
sampleUserRole = append(sampleUserRole, createUserCommand.UserRole[i].CloneSample())
}
newUser := &domain.User{
CompanyId: createUserCommand.CompanyId,
UserType: createUserCommand.UserType,
UserCode: createUserCommand.UserCode,
OrganizationId: createUserCommand.OrganizationId,
DepartmentId: createUserCommand.DepartmentId,
UserOrg: sampleUserOrg,
UserRole: sampleUserRole,
FavoriteMenus: []string{},
CooperationInfo: &domain.CooperationInfo{},
UserInfo: nweUserInfo,
EnableStatus: createUserCommand.EnableStatus,
Ext: &domain.Ext{
Phone: createUserCommand.Phone,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
var user *domain.User
createUserService, _ := factory.CreatePgCreateUserService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userRepository = value
})
if user, err = createUserService.CreateUser(nil, newUser, createUserCommand.Password); err != nil {
return nil, err
}
if user, err := userRepository.Save(newUser); 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 user, nil
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return user, nil
}
// 移除我收藏的菜单
... ... @@ -304,7 +349,7 @@ func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (in
}
}
// 移除
// 移除 (暂不需要)
func (userService *UserService) RemoveUser(removeUserCommand *command.RemoveUserCommand) (interface{}, error) {
if err := removeUserCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ...
... ... @@ -5,3 +5,32 @@ import "fmt"
var (
ErrorNotFound = fmt.Errorf("没有此资源")
)
/***** 1.数据权限 *****/
// DataAuthor 数据验证器
type DataAuthor interface {
DataAuth(options CheckOptions, data AuthedData) error
}
// AuthedData 需要认证的数据
type AuthedData interface {
// 数据所属组织
BelongOrg() int64
}
// 验证参数
type CheckOptions struct {
// 当前操作人
OptUser int64
// 当前登录的组织
Org int64
// 菜单模块
MenuCode string
}
func NewCheckOptions(optUser, org int64) CheckOptions {
return CheckOptions{
OptUser: optUser,
Org: org,
}
}
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgCreateUserService 创建用户服务
type PgCreateUserService interface {
CreateUser(optUser *domain.User, newUser *domain.User, password string) (*domain.User, error)
}
... ...
... ... @@ -15,13 +15,18 @@ type PgCreateUserAccountService struct {
// CreateUserAccount 创建账号服务
//
// p1 p1_desc
// registerAccount 注册账号
// password 密码
// userInfo 用户信息
func (ptr *PgCreateUserAccountService) CreateUserAccount(registerAccount string, password string, userInfo *domain.UserInfo) (*domain.UserBase, error) {
userBaseRepository, err := repository.NewUserBaseRepository(ptr.transactionContext)
if err != nil {
return nil, err
}
if len(password) == 0 {
return nil, fmt.Errorf("密码不能为空")
}
userBase := &domain.UserBase{
UserInfo: userInfo,
Account: registerAccount,
... ...
... ... @@ -32,6 +32,27 @@ func (ptr *PgCreateUserService) CreateUser(optUser *domain.User, newUser *domain
if err != nil {
return nil, err
}
orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
if err != nil {
return nil, err
}
// 0.冗余组织部门值
if newUser.Ext != nil {
if len(newUser.Ext.OrgName) == 0 && newUser.OrganizationId > 0 {
if org, err := orgRepository.FindOne(map[string]interface{}{"orgId": newUser.OrganizationId}); err != nil {
return nil, err
} else {
newUser.Ext.OrgName = org.OrgName
}
}
if len(newUser.Ext.DepName) == 0 && newUser.DepartmentId > 0 {
if org, err := orgRepository.FindOne(map[string]interface{}{"orgId": newUser.DepartmentId}); err != nil {
return nil, err
} else {
newUser.Ext.DepName = org.OrgName
}
}
}
// 普通账号需要验证
// 1.用户编号唯一验证 用户编号在该企业内已存在,请重新输入
... ...
package domainService
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
)
// PgDataAuthService 数据权限验证服务
type PgDataAuthService struct {
transactionContext *pgTransaction.TransactionContext
}
// DataAuth 数据权限验证
//
// options 数据参数
// data 需要验证权限的数据
func (ptr *PgDataAuthService) DataAuth(options domain.CheckOptions, data domain.AuthedData) error {
return nil
}
func NewPgDataAuthService(transactionContext *pgTransaction.TransactionContext) (*PgDataAuthService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgDataAuthService{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -4,6 +4,7 @@ import (
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/web/beego"
"github.com/linmadan/egglib-go/web/beego/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
)
func ResponseGrid(c beego.BaseController, data interface{}, err error) {
... ... @@ -25,3 +26,9 @@ func ResponseGridData(ctx *context.Context, data interface{}) utils.JsonResponse
ctx.Input.SetData("outputData", jsonResponse)
return jsonResponse
}
func Must(err error) {
if err != nil {
log.Logger.Error(err.Error())
}
}
... ...
... ... @@ -14,7 +14,7 @@ type UserController struct {
func (controller *UserController) CreateUser() {
userService := service.NewUserService(nil)
createUserCommand := &command.CreateUserCommand{}
controller.Unmarshal(createUserCommand)
Must(controller.Unmarshal(createUserCommand))
data, err := userService.CreateUser(createUserCommand)
controller.Response(data, err)
}
... ... @@ -22,7 +22,7 @@ func (controller *UserController) CreateUser() {
func (controller *UserController) UpdateUser() {
userService := service.NewUserService(nil)
updateUserCommand := &command.UpdateUserCommand{}
controller.Unmarshal(updateUserCommand)
Must(controller.Unmarshal(updateUserCommand))
userId, _ := controller.GetInt64(":userId")
updateUserCommand.UserId = userId
data, err := userService.UpdateUser(updateUserCommand)
... ... @@ -78,7 +78,7 @@ func (controller *UserController) GetUserProfile() {
func (controller *UserController) BatchAdd() {
userService := service.NewUserService(nil)
batchAddCommand := &command.BatchAddCommand{}
controller.Unmarshal(batchAddCommand)
Must(controller.Unmarshal(batchAddCommand))
data, err := userService.BatchAdd(batchAddCommand)
controller.Response(data, err)
}
... ... @@ -112,7 +112,7 @@ func (controller *UserController) UpdateUsersBase() {
func (controller *UserController) CreateCooperator() {
userService := service.NewUserService(nil)
createCooperatorCommand := &command.CreateCooperatorCommand{}
controller.Unmarshal(createCooperatorCommand)
Must(controller.Unmarshal(createCooperatorCommand))
data, err := userService.CreateCooperator(createCooperatorCommand)
controller.Response(data, err)
}
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("批量添加", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("批量修改启用状态", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("批量重置密码", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,12 +11,12 @@ import (
)
var _ = Describe("创建共创用户", func() {
var userId int64
var id int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&userId),
"INSERT INTO users (user_id, company_id, user_base_id, user_type, user_code, organization_id, department_id, user_info, user_org, user_role, favorite_menus, cooperation_info, enable_status, ext, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING user_id",
"testUserId", "testCompanyId", "testUserBaseId", "testUserType", "testUserCode", "testOrganizationId", "testDepartmentId", "testUserInfo", "testUserOrg", "testUserRole", "testFavoriteMenus", "testCooperationInfo", "testEnableStatus", "testExt", "testCreatedAt", "testUpdatedAt")
pg.Scan(&id),
"INSERT INTO users.org (org_id, company_id,org_code, org_name,is_org,org_status) VALUES (?, ?, ?, ?, ?, ?) RETURNING org_id",
999, 999, "testOrgCode", "testOrgName", 1, 1)
Expect(err).NotTo(HaveOccurred())
})
Describe("创建共创用户", func() {
... ... @@ -25,14 +25,15 @@ var _ = Describe("创建共创用户", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"cooperationCompany": "string",
"cooperationDeadline": "datetime",
"cooperationDeadline": "2021-07-25T15:11:56+08:00",
"email": "string",
"enableStatus": "int",
"enableStatus": 1,
"userCode": "string",
"userId": "int64",
"userName": "string",
"avatar": "string",
"orgId": "int64",
"orgId": 999,
"phone": "string01",
"password": "string",
}
httpExpect.POST("/user/cooperator").
WithJSON(body).
... ... @@ -47,7 +48,10 @@ var _ = Describe("创建共创用户", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM users WHERE true")
var err error
_, err = pG.DB.Exec(`DELETE FROM users."user" WHERE true`)
Expect(err).NotTo(HaveOccurred())
_, err = pG.DB.Exec(`DELETE FROM users."org" WHERE true`)
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package user
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
... ... @@ -10,26 +11,34 @@ import (
)
var _ = Describe("创建", func() {
var id int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&id),
"INSERT INTO users.org (org_id, company_id,org_code, org_name,is_org,org_status) VALUES (?, ?, ?, ?, ?, ?) RETURNING org_id",
999, 999, "testOrgCode", "testOrgName", 1, 1)
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据创建", func() {
Context("提交正确的新用户数据", func() {
It("返回用户数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyId": "int64",
"userType": "int",
"userCode": "string",
"organizationId": "int64",
"departmentId": "int64",
"userOrg": "array",
"userRole": "array",
"cooperationCompany": "string",
"cooperationDeadline": "datetime",
"enableStatus": "int",
"password": "string",
"userName": "string",
"phone": "string",
"avatar": "string",
"email": "string",
"companyId": 999,
"userType": 1,
"userCode": "string",
"organizationId": 999,
"departmentId": 999,
//"userOrg": "array",
//"userRole": "array",
//"cooperationCompany": "string",
//"cooperationDeadline": "datetime",
"enableStatus": 1,
"password": "string",
"userName": "string",
"phone": "string",
"avatar": "string",
"email": "string",
}
httpExpect.POST("/user/").
WithJSON(body).
... ... @@ -45,7 +54,10 @@ var _ = Describe("创建", func() {
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM users WHERE true")
var err error
_, err = pG.DB.Exec(`DELETE FROM users."user" 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 userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("获取用户概要数据", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("返回", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("返回列表", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("移除", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("更新共创用户", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("更新", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...
... ... @@ -11,6 +11,7 @@ import (
)
var _ = Describe("更新用户基础信息数据", func() {
return
var userId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
... ...