作者 唐旭辉

权限设置调整

... ... @@ -67,7 +67,10 @@ func (c *AuthController) Login() {
if err != nil {
log.Error("token 信息记录redis失败")
}
msg = protocol.NewReturnResponse(logintoken, nil)
data := map[string]interface{}{
"access": logintoken,
}
msg = protocol.NewReturnResponse(data, nil)
return
}
... ... @@ -113,6 +116,22 @@ func (c *AuthController) Me() {
userid := c.GetUserId()
companyid := c.GetCompanyId()
userinfo, err := serveauth.UserBaseInfo(userid, companyid)
msg = protocol.NewReturnResponse(userinfo, err)
if err != nil {
log.Error("%s", err)
}
menus, err := serveauth.GetUserHasMenu(userid, companyid)
if err != nil {
log.Error("%s", err)
}
companys, err := serveauth.UserHasCompanys(userid)
if err != nil {
log.Error("%s", err)
}
data := map[string]interface{}{
"user": userinfo,
"menus": menus,
"companys": companys,
}
msg = protocol.NewReturnResponse(data, nil)
return
}
... ...
... ... @@ -55,7 +55,7 @@ type ResponseMeInfo struct {
Companyid int64 `json:"company_id"`
Companyname string `json:"company_name"`
Logo string `json:"logo"`
Companys []MeCompany `json:"companys"`
Companys []MeCompany `json:"-"`
// Menu
}
... ...
... ... @@ -3,13 +3,11 @@ package auth
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/services/ucenter"
"oppmg/storage/redisdata"
"oppmg/utils"
"strings"
... ... @@ -201,9 +199,15 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
log.Debug("no company")
return logintoken, protocol.NewErrWithMessage("10022")
}
//获取上一次登录的公司
uAuth, err := models.GetUserAuthByUser(userdata.Id)
if err == nil {
companyid = uAuth.CurrentCompanyId
} else {
companyid = companys[0].Id
}
// var uclientReturn *ucenter.ResponseLogin
// uclientReturn, err = requestUCenterLogin(account, password)
// uclientReturn, err = ucenter.RequestUCenterLogin(account, password)
// if err != nil {
// return logintoken, protocol.NewErrWithMessage("10021")
// }
... ... @@ -223,31 +227,6 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
return logintoken, err
}
func requestUCenterLogin(account, password string) (*ucenter.ResponseLogin, error) {
var uclientReturn *ucenter.ResponseLogin
param := ucenter.RequesLogin{
Type: 1,
Phone: account,
Password: password,
}
uclient := ucenter.NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
err = json.Unmarshal(btBody, &uclientReturn)
if err != nil {
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(uclientReturn.Code == ucenter.ResponseCode0 &&
uclientReturn.Msg == ucenter.ResponseMsgOk) {
return nil, protocol.NewErrWithMessage("10021")
}
return uclientReturn, nil
}
type companybase struct {
Id int64 `orm:"column(id)"`
Name string `orm:"coumn(name)"`
... ... @@ -288,7 +267,7 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) {
var (
err error
userinfo *models.User
companylist []companybase
meInfo protocol.ResponseMeInfo
currentCompany *models.Company
)
... ... @@ -302,11 +281,7 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) {
log.Error("GetCompanyById(%d) err:%s", companyid, err)
return meInfo, protocol.NewErrWithMessage("1", err)
}
companylist, err = getUserCompanyReal(userid)
if err != nil {
log.Error("getUserCompanyReal(%d) err:%s", userid, err)
return meInfo, protocol.NewErrWithMessage("1", err)
}
meInfo = protocol.ResponseMeInfo{
NickName: userinfo.NickName,
Icon: userinfo.Icon,
... ... @@ -314,12 +289,42 @@ func UserBaseInfo(userid, companyid int64) (protocol.ResponseMeInfo, error) {
Companyname: currentCompany.Name,
Logo: currentCompany.Logo,
}
return meInfo, nil
}
func UserHasCompanys(userid int64) ([]protocol.MeCompany, error) {
var (
companylist []companybase
err error
mecompanys []protocol.MeCompany
)
companylist, err = getUserCompanyReal(userid)
if err != nil {
log.Error("getUserCompanyReal(%d) err:%s", userid, err)
return nil, protocol.NewErrWithMessage("1", err)
}
for _, v := range companylist {
t := protocol.MeCompany{
Id: v.Id,
Name: v.Name,
}
meInfo.Companys = append(meInfo.Companys, t)
mecompanys = append(mecompanys, t)
}
return meInfo, nil
return mecompanys, nil
}
func GetUserHasMenu(userid, companyid int64) ([]protocol.PermissionItem, error) {
const datasql string = `SELECT id,name,icon,parent_id,senior_status,sort,code
FROM menu WHERE enabled=1 ORDER BY sort `
var (
list []protocol.PermissionItem
err error
)
err = utils.ExecuteQueryAll(&list, datasql)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
return list, nil
}
... ...
... ... @@ -27,19 +27,19 @@ func UserAdd(param protocol.RequestUserAdd) error {
err = validCompanyRole(param.CompanyId, param.Roles)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10031")
return err
}
//校验部门
err = validCompanyDepart(param.CompanyId, param.Departments)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10032")
return err
}
//校验职位
err = validCompanyPosition(param.CompanyId, param.Positions)
if err != nil {
log.Error(err.Error())
return protocol.NewErrWithMessage("10033")
return err
}
userm = &models.User{
NickName: param.Name,
... ... @@ -344,12 +344,6 @@ func UserEdit(param protocol.RequestUserEdit) error {
}
o := orm.NewOrm()
o.Begin()
// err = registUser(userm, param.CompanyId, o)
// if err != nil {
// o.Rollback()
// log.Error("registUser err:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// TODO 用户数据更新
//添加角色
err = editUserRole(userm.Id, param.CompanyId, param.Roles, o)
... ...
... ... @@ -154,5 +154,4 @@ func RoleMenuEdit(companyid int64, roleId int64, menuids []int64) error {
}
o.Commit()
return nil
}
... ...
package ucenter
import (
"encoding/json"
"oppmg/common/log"
"oppmg/protocol"
)
func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
var uclientReturn *ResponseLogin
param := RequesLogin{
Type: 1,
Phone: account,
Password: password,
}
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
err = json.Unmarshal(btBody, &uclientReturn)
if err != nil {
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(uclientReturn.Code == ResponseCode0 &&
uclientReturn.Msg == ResponseMsgOk) {
return nil, protocol.NewErrWithMessage("10021")
}
return uclientReturn, nil
}
... ...