作者 唐旭辉

登录相关 完成

... ... @@ -39,7 +39,7 @@ func (adminPermissionSrv AdminPermissionService) ListAdminPermission(queryOption
} else {
permissionRepository = value
}
permissions, err = permissionRepository.Find(map[string]interface{}{})
permissions, err = permissionRepository.Find(domain.PermissionFindOption{})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
... ...
... ... @@ -13,7 +13,7 @@ type SaveAdminUserCommand struct {
//密码
Password string `json:"password"`
// 员工角色
PermissionId []int `json:"PermissionId"`
PermissionId []int64 `json:"PermissionId"`
IsUsable bool `json:"isUsable"`
}
... ...
... ... @@ -87,8 +87,8 @@ func (adminUserSrv AdminUserService) SaveAdminUser(saveUserCmd *command.SaveAdmi
} else {
permissionRepository = value
}
permissions, err = permissionRepository.Find(map[string]interface{}{
"IdsIn": saveUserCmd.PermissionId,
permissions, err = permissionRepository.Find(domain.PermissionFindOption{
Ids: saveUserCmd.PermissionId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
... ...
package query
type ValidatePermissionQuery struct {
PermissionCode string
UserId int64
CompanyId int64
}
... ...
... ... @@ -5,6 +5,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/serviceGateway"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
... ... @@ -56,8 +57,8 @@ func (service UsersService) UserLoginBySecretKey(cmd command.LoginBySecretKeyCom
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
//检索本系统的公司数据判断公司权限
companyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": loginResp.Data.Muid,
companyData, err = companyRespository.FindOne(domain.CompanyFindOneOptions{
Id: loginResp.Data.Muid,
})
if err != nil {
e := fmt.Sprintf("获取公司(id=%d)数据失败:%s", loginResp.Data.Muid, err.Error())
... ... @@ -67,9 +68,9 @@ func (service UsersService) UserLoginBySecretKey(cmd command.LoginBySecretKeyCom
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "该公司没有操作权限")
}
//检索本系统的用户数据
usersData, err = userRespository.FindOne(map[string]interface{}{
"OpenId": loginResp.Data.Id,
"CompanyId": companyData.Id,
usersData, err = userRespository.FindOne(domain.UsersFindOneQuery{
OpenId: loginResp.Data.Id,
CompanyId: companyData.Id,
})
if err != nil {
e := fmt.Sprintf("获取用户(OpenId=%d;CompanyId=%d)数据失败:%s",
... ... @@ -81,17 +82,136 @@ func (service UsersService) UserLoginBySecretKey(cmd command.LoginBySecretKeyCom
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "用户被禁用")
}
err = transactionContext.CommitTransaction()
newJwt := lib.NewMyToken(usersData.Id, usersData.Id)
newToken, err := newJwt.CreateJWTToken()
//生成token
return nil, nil
returnData := map[string]interface{}{
"access": map[string]interface{}{
"accessToken": newToken,
"expiresIn": lib.JWtExpiresSecond,
},
}
return returnData, nil
}
//GetAdminpPofile 登录后获取用户的权限配置数据
func (service UsersService) GetAdminpPofile() (interface{}, error) {
return nil, nil
func (service UsersService) GetUserPofile(userId int64) (interface{}, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
userRespository domain.UsersRepository
usersData domain.Users
permissionRepository domain.AdminPermissionRepository
permissionList []domain.AdminPermission
)
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if permissionRepository, err = factory.CreateAdminPermissionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
usersData, err = userRespository.FindOne(domain.UsersFindOneQuery{
Id: userId,
})
if err != nil {
e := fmt.Sprintf("获取用户(Id=%d)数据失败:%s",
userId, err.Error())
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
var permissionQuery domain.PermissionFindOption
permissionList, err = permissionRepository.Find(permissionQuery)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
returnData := service.buildUserPofile(usersData, permissionList)
return returnData, nil
}
//buildUserPofile 组装前端需要的数据 ,用户登录后获取的配置
func (service UsersService) buildUserPofile(userData domain.Users, permissionList []domain.AdminPermission) map[string]interface{} {
menus := make([]map[string]interface{}, len(permissionList))
for _, v := range permissionList {
m := map[string]interface{}{
"code": v.Code,
"icon": v.Icon,
"parentId": v.ParentId,
"sort": v.Sort,
"name": v.Name,
"id": v.Id,
"status": 0, //状态 1-启用 0-禁用,前端需要
}
for _, p := range userData.Permission {
if p.Id == v.Id {
m["status"] = 1
break
}
if p.Id == v.ParentId {
m["status"] = 1
break
}
}
menus = append(menus, m)
}
user := map[string]string{
"id": fmt.Sprint(userData.Id),
"name": userData.Name,
"adminType": "2", // 前端需要的管理员类型 1-超级管理员 2-子管理员
}
if userData.IsSuperAdmin() {
user["adminType"] = "1"
}
respData := map[string]interface{}{
"user": user,
"menus": menus,
}
return respData
}
//ValidateAdminpPermission 校验用户的操作权限
func (service UsersService) ValidateAdminpPermission() (interface{}, error) {
return nil, nil
func (service UsersService) ValidateAdminpPermission(queryOption query.ValidatePermissionQuery) (bool, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return false, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
userRespository domain.UsersRepository
usersData domain.Users
)
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return false, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
usersData, err = userRespository.FindOne(domain.UsersFindOneQuery{
Id: queryOption.UserId,
})
if err != nil {
return false, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if !usersData.IsUsable() {
return false, nil
}
err = transactionContext.CommitTransaction()
ok := usersData.HasPermissionByCode(queryOption.PermissionCode)
return ok, nil
}
... ...
... ... @@ -12,20 +12,30 @@ const (
)
type AdminPermissionBase struct {
Id int `json:"id"`
Id int64 `json:"id"`
Code string `json:"code"`
}
// 权限结构
type AdminPermission struct {
Id int `json:"id"`
ParentId int `json:"parentId"`
Id int64 `json:"id"`
ParentId int64 `json:"parentId"`
Code string `json:"code"`
Name string `json:"name"`
Sort int `json:"sort"`
Icon string `json:"icon"`
}
type PermissionFindOption struct {
Ids []int64
}
type PermissionFindOneOption struct {
Id int64
Code string
}
type AdminPermissionRepository interface {
Find(map[string]interface{}) ([]AdminPermission, error)
Find(PermissionFindOption) ([]AdminPermission, error)
FindOne(PermissionFindOneOption) (AdminPermission, error)
}
... ...
... ... @@ -50,15 +50,27 @@ func (u Users) IsUsable() bool {
return u.Status == userStatusUsable
}
//IsCharge 用户是否是主管
func (u Users) IsCharge() bool {
return u.ChargeStatus == UserIsCompanyCharge
//IsSuperAdmin 用户是否是负责人
func (u Users) IsSuperAdmin() bool {
return u.AdminType == UserIsAdmin
}
func (u Users) InCompany(companyid int64) bool {
return u.CompanyId == companyid
}
func (u Users) HasPermissionByCode(code string) bool {
if u.IsSuperAdmin() {
return true
}
for _, v := range u.Permission {
if v.Code == code {
return true
}
}
return false
}
func (u *Users) Update(m map[string]interface{}) error {
if v, ok := m["CompanyId"]; ok {
u.CompanyId = v.(int64)
... ...
... ... @@ -3,13 +3,13 @@ package models
type AdminPermission struct {
tableName struct{} `pg:"admin_permission,alias:admin_permission"`
//id
Id int `pg:",pk"`
Id int64 `pg:",pk"`
//权限名称、菜单名称
Name string
//权限编码
Code string
//父级id
ParentId int
ParentId int64
//排序编号
Sort int
//图标
... ...
... ... @@ -33,13 +33,12 @@ func (reponsitory AdminPermissionRepository) transformPgModelToDomainModel(permi
return result, nil
}
func (reponsitory AdminPermissionRepository) Find(queryOptions map[string]interface{}) ([]domain.AdminPermission, error) {
func (reponsitory AdminPermissionRepository) Find(queryOptions domain.PermissionFindOption) ([]domain.AdminPermission, error) {
db := reponsitory.transactionContext.PgDd
permissionModels := make([]models.AdminPermission, 0)
query := db.Model(&permissionModels)
if v, ok := queryOptions["IdsIn"]; ok {
in := v.([]int)
query = query.WhereIn("id in (?) ", in)
if len(queryOptions.Ids) > 0 {
query = query.WhereIn("id in (?) ", queryOptions.Ids)
}
if err := query.Select(); err != nil {
return nil, err
... ... @@ -51,3 +50,23 @@ func (reponsitory AdminPermissionRepository) Find(queryOptions map[string]interf
}
return result, nil
}
func (reponsitory AdminPermissionRepository) FindOne(queryOptions domain.PermissionFindOneOption) (domain.AdminPermission, error) {
db := reponsitory.transactionContext.PgDd
var permissionModels models.AdminPermission
query := db.Model(&permissionModels)
if len(queryOptions.Code) > 0 {
query = query.Where("code=?", queryOptions.Code)
}
if queryOptions.Id > 0 {
query = query.Where("id=?", queryOptions.Id)
}
var result domain.AdminPermission
err := query.First()
if err != nil {
return result, err
}
result, _ = reponsitory.transformPgModelToDomainModel(&permissionModels)
return result, nil
}
... ...
... ... @@ -8,12 +8,8 @@ import (
"github.com/GeeTeam/gt3-golang-sdk/geetest"
"github.com/astaxie/beego/logs"
adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
adminPermissionService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/service"
adminuserCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
userCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/command"
userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service"
)
type AdminLoginController struct {
... ... @@ -86,36 +82,20 @@ func (c *AdminLoginController) Prepare() {
// }
func (c *AdminLoginController) Login() {
type Paramter struct {
Username string `json:"username"`
Password string `json:"password"`
}
var (
param Paramter
err error
param userCmd.LoginBySecretKeyCommand
)
if err = c.BindJsonData(&param); err != nil {
if err := c.BindJsonData(&param); err != nil {
c.ResponseError(fmt.Errorf("json解析失败:%s", err))
return
}
newAdminuserquery := adminuserquery.GetAdminUserQuery{AdminAccount: param.Username}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
_ = newAdminuserquery
_ = newAdminUserService
newJwt := lib.NewMyToken(0, 0)
newToken, err := newJwt.CreateJWTToken()
newUserService := userService.NewUsersService(nil)
resp, err := newUserService.UserLoginBySecretKey(param)
if err != nil {
logs.Error("生成jwt数据失败:%s", err)
c.ResponseError(errors.New("服务异常"))
c.ResponseError(err)
return
}
rspdata := map[string]interface{}{
"access": map[string]interface{}{
"accessToken": newToken,
"expiresIn": lib.JWtExpiresSecond,
},
}
c.ResponseData(rspdata)
c.ResponseData(resp)
return
}
... ... @@ -145,86 +125,42 @@ func (c *AdminLoginController) CaptchaInit() {
//AdminpPofile 获取登录用户的权限配置
func (c *AdminLoginController) AdminpPofile() {
adminId := c.GetUserId()
newAdminuserquery := adminuserquery.GetAdminUserQuery{Id: adminId}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
newUserService := userService.NewUsersService(nil)
respData, err := newUserService.GetUserPofile(adminId)
if err != nil {
logs.Error("获取用户数据失败:%s", err)
c.ResponseError(errors.New("用户不存在"))
return
}
newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{})
if err != nil {
logs.Error("获取权限数据失败:%s", err)
c.ResponseError(errors.New("服务异常"))
return
}
//适配前端的数据格式
permissionSlice := make([]map[string]interface{}, 0)
for _, v := range allPermission {
m := map[string]interface{}{
"code": v.Code,
"icon": v.Icon,
"parentId": v.ParentId,
"sort": v.Sort,
"name": v.Name,
"id": v.Id,
"status": 0, //状态 1-启用 0-禁用,前端需要
}
for _, p := range adminuser.Permission {
if p.Id == v.Id {
m["status"] = 1
break
}
if p.Id == v.ParentId {
m["status"] = 1
break
}
}
permissionSlice = append(permissionSlice, m)
}
userData := map[string]string{
"id": fmt.Sprint(adminuser.Id),
"name": adminuser.AdminName,
"adminType": "2", // 管理员类型 1-超级管理员 2-子管理员
}
if adminuser.IsDefault {
userData["adminType"] = "1"
}
respData := map[string]interface{}{
"user": userData,
"menus": permissionSlice,
}
c.ResponseData(respData)
}
func (c *AdminLoginController) PwdUpdate() {
//用与适配前端定义的数据结构
type Paramater struct {
Pwd string `json:"pwd"`
}
var (
param Paramater
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
uid := c.GetUserId()
newAdminUserService := adminuserservice.NewAdminUserService(nil)
cmd := adminuserCmd.UpdateAdminUserPwdCommand{
Id: uid,
Password: param.Pwd,
}
err = newAdminUserService.UpdateAdminPassword(cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
// func (c *AdminLoginController) PwdUpdate() {
// //用与适配前端定义的数据结构
// type Paramater struct {
// Pwd string `json:"pwd"`
// }
// var (
// param Paramater
// err error
// )
// if err = c.BindJsonData(&param); err != nil {
// logs.Error(err)
// c.ResponseError(errors.New("json数据解析失败"))
// return
// }
// uid := c.GetUserId()
// newAdminUserService := adminuserservice.NewAdminUserService(nil)
// cmd := adminuserCmd.UpdateAdminUserPwdCommand{
// Id: uid,
// Password: param.Pwd,
// }
// err = newAdminUserService.UpdateAdminPassword(cmd)
// if err != nil {
// c.ResponseError(err)
// return
// }
// c.ResponseData(nil)
// return
// }
... ...
... ... @@ -28,11 +28,11 @@ func (c *AdminUserController) Prepare() {
}
type adminDetailParam struct {
Id int64 `json:"id"`
PermissionType []int `json:"permissionType"`
Status int `json:"status"`
Account string `json:"account"`
IsAdmin int `json:"isAdmin"`
Id int64 `json:"id"`
PermissionType []int64 `json:"permissionType"`
Status int `json:"status"`
Account string `json:"account"`
IsAdmin int `json:"isAdmin"`
}
func (c *AdminUserController) SaveAdminUser() {
... ... @@ -149,7 +149,7 @@ func (c *AdminUserController) ListAdminUser() {
c.ResponseError(errors.New("服务异常"))
return
}
permissionMap := map[int]domain.AdminPermission{}
permissionMap := map[int64]domain.AdminPermission{}
for i := range allPermission {
permissionMap[allPermission[i].Id] = allPermission[i]
}
... ...
... ... @@ -11,8 +11,8 @@ import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
userQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/query"
userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/protocol"
)
... ... @@ -162,27 +162,22 @@ func (controller *BaseController) ValidAdminPermission(permissionCode string, ex
controller.ResponseError(errors.New("无操作权限"))
return false
}
newAdminuserquery := adminuserquery.GetAdminUserQuery{Id: userId}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
comanyId := controller.GetUserCompany()
newQuery := userQuery.ValidatePermissionQuery{
UserId: userId,
PermissionCode: permissionCode,
CompanyId: comanyId,
}
newUserService := userService.NewUsersService(nil)
ok, err := newUserService.ValidateAdminpPermission(newQuery)
if err != nil {
logs.Error("获取用户数据失败:%s", err)
logs.Error("用户鉴权失败:%s", err)
controller.ResponseError(errors.New("无操作权限"))
return false
}
if adminuser.IsDefault {
logs.Debug("用户是超级管理员")
if ok {
return true
}
if !adminuser.IsUsable {
controller.ResponseError(errors.New("用户被禁用"))
return false
}
for _, v := range adminuser.Permission {
if v.Code == permissionCode {
return true
}
}
controller.ResponseError(errors.New("无操作权限"))
return false
}
... ...