作者 唐旭辉

修复bug:切换公司后未能正确缓存用户的权限数据

... ... @@ -48,41 +48,41 @@ func (c *AuthController) RefreshToken() {
// Login 登录
// @router /login [post]
func (c *AuthController) Login() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
// func (c *AuthController) Login() {
// var msg *protocol.ResponseMessage
// defer func() {
// c.ResposeJson(msg)
// }()
var param protocol.RequestLogin
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
if len(param.Account) == 0 || len(param.Password) == 0 {
msg = protocol.BadRequestParam("10201")
return
}
logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password)
if err != nil {
msg = protocol.NewReturnResponse(nil, err)
return
}
err = serveauth.ResetLoginToken(logintoken)
if err != nil {
log.Error("token 信息记录数据库失败")
}
err = serveauth.ResetLoginTokenRedis(logintoken)
if err != nil {
log.Error("token 信息记录redis失败")
}
data := protocol.ResponseLogin{
Access: logintoken,
}
msg = protocol.NewReturnResponse(data, nil)
return
}
// var param protocol.RequestLogin
// if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
// log.Error("json 解析失败", err)
// msg = protocol.BadRequestParam("1")
// return
// }
// if len(param.Account) == 0 || len(param.Password) == 0 {
// msg = protocol.BadRequestParam("10201")
// return
// }
// logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password)
// if err != nil {
// msg = protocol.NewReturnResponse(nil, err)
// return
// }
// err = serveauth.ResetLoginToken(logintoken)
// if err != nil {
// log.Error("token 信息记录数据库失败")
// }
// err = serveauth.ResetLoginTokenRedis(logintoken)
// if err != nil {
// log.Error("token 信息记录redis失败")
// }
// data := protocol.ResponseLogin{
// Access: logintoken,
// }
// msg = protocol.NewReturnResponse(data, nil)
// return
// }
// LoginSms 短信验证码登录
// @router /login_sms [post]
... ... @@ -276,6 +276,7 @@ func (c *AuthController) Me() {
}()
userid := c.GetUserId()
companyid := c.GetCompanyId()
userCompanyId := c.GetUserCompanyId()
userinfo, err := serveauth.UserBaseInfo(userid, companyid)
if err != nil {
log.Error("获取用户数据失败")
... ... @@ -288,7 +289,7 @@ func (c *AuthController) Me() {
if err != nil {
log.Error("获取用户的公司失败")
}
permissionMap := serveauth.GetUserMenuPermission(userid, companyid)
permissionMap := serveauth.GetUserMenuPermission(userid, companyid, userCompanyId)
data := map[string]interface{}{
"user": userinfo,
"menus": menus,
... ...
... ... @@ -34,7 +34,8 @@ func (this *BaseController) Prepare() {
p := fmt.Sprint(this.Ctx.Input.GetData("RouterPattern"))
userid := this.GetUserId()
companyid := this.GetCompanyId()
ok := serveauth.ValidUserPermission(p, userid, companyid)
userCompanyId := this.GetUserCompanyId()
ok := serveauth.ValidUserPermission(p, userid, companyid, userCompanyId)
if !ok {
msg := protocol.NewMessage("10210")
this.ResposeJson(msg)
... ... @@ -87,6 +88,12 @@ func (this *BaseController) GetUserId() int64 {
return userid
}
func (this *BaseController) GetUserCompanyId() int64 {
v := this.Ctx.Input.GetData(protocol.HeaderUCompanyid)
userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
return userid
}
//Valid valid struct
func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
/*校验*/
... ...
... ... @@ -151,7 +151,7 @@ func init() {
nsAuth := beego.NewNamespace("/auth",
beego.NSBefore(middleware.AllowOption),
beego.NSBefore(middleware.LogRequestData),
beego.NSRouter("/login_account", &controllers.AuthController{}, "post:Login"),
// beego.NSRouter("/login_account", &controllers.AuthController{}, "post:Login"),
beego.NSRouter("/login", &controllers.AuthController{}, "post:LoginSecretKey"),
beego.NSRouter("/startCaptcha", &controllers.AuthController{}, "post:RegisterGeetest"),
beego.NSRouter("/verifyCaptcha", &controllers.AuthController{}, "post:ValidateGeetest"),
... ...
... ... @@ -19,7 +19,7 @@ func getAuditUserHasPermission(userid int64, usercompanyid int64) (
serverabc.PermissionOptionObject, error) {
var permissionObject serverabc.PermissionOptionObject
var err error
if ok := redisdata.ExistUserPermission(userid); !ok {
if ok := redisdata.ExistUserPermission(usercompanyid); !ok {
//尝试重数据库获取
log.Debug("从数据库读取权限")
permissionMap, err := serverabc.GetUserPermission(usercompanyid, serverabc.M_SYSTEM_OPPORTUNITY)
... ...
... ... @@ -141,85 +141,86 @@ func ChangeLoginToken(userid, companyid int64) (protocol.LoginAuthToken, error)
log.Error("GenerateAuthToken err:%s", err)
return logintoken, protocol.NewErrWithMessage("1")
}
InitPermission(usercompany.Id, usercompany.UserId)
return logintoken, nil
}
func LoginAuthByUCenter(account, password string) (
protocol.LoginAuthToken, error) {
var (
err error
logintoken protocol.LoginAuthToken
usercompanyid int64
companys []companybase
companyid int64
userdata *models.User
)
var uclientReturn *ucenter.ResponseLogin
uclientReturn, err = ucenter.RequestUCenterLogin(account, password)
if err != nil {
return logintoken, protocol.NewErrWithMessage("10201")
}
userdata, err = models.GetUserByUCenter(uclientReturn.Data.Id)
if err != nil {
log.Debug("GetUserByUCenter(%d) err:%s", uclientReturn.Data.Id, err)
return logintoken, protocol.NewErrWithMessage("10201")
}
if ok := userdata.IsDelete(); ok {
log.Debug("userdata.IsDelete()==true")
return logintoken, protocol.NewErrWithMessage("10201")
}
if ok := userdata.IsEnable(); !ok {
log.Debug("userdata.IsEnable()==false")
return logintoken, protocol.NewErrWithMessage("10202")
}
if companys, err = getUserCompanyReal(userdata.Id); err != nil {
log.Error("getUserCompanyReal err:%s", err)
return logintoken, protocol.NewErrWithMessage("10201")
}
if len(companys) == 0 {
log.Debug("no company")
return logintoken, protocol.NewErrWithMessage("10202")
}
//获取上一次登录的公司
uAuth, err := models.GetUserAuthByUser(userdata.Id)
if err == nil {
has := false
for _, v := range companys {
if v.Id == uAuth.CurrentCompanyId {
companyid = uAuth.CurrentCompanyId
has = true
break
}
}
if !has {
companyid = companys[0].Id
}
} else {
companyid = companys[0].Id
}
ucompany, err := models.GetUserCompanyBy(userdata.Id, companyid)
if err != nil {
log.Error("获取user_company失败;%s", err)
return logintoken, protocol.NewErrWithMessage("10202")
}
usercompanyid = ucompany.Id
logintoken, _ = GenerateAuthToken(userdata.Id, companyid, usercompanyid)
//更新用户数据
userdata.Accid = uclientReturn.Data.Accid
userdata.Icon = uclientReturn.Data.Avatar
userdata.ImToken = uclientReturn.Data.Imtoken
userdata.NickName = uclientReturn.Data.NickName
userdata.CsAccount = uclientReturn.Data.CustomerAccount
userdata.LastLoginTime = time.Now()
err = models.UpdateUserById(userdata, []string{"Accid", "Icon", "ImToken", "NickName", "CsAccount", "LastLoginTime"})
if err != nil {
log.Error("更新用户数据失败:%s", err)
}
InitPermission(usercompanyid, userdata.Id)
return logintoken, err
}
// func LoginAuthByUCenter(account, password string) (
// protocol.LoginAuthToken, error) {
// var (
// err error
// logintoken protocol.LoginAuthToken
// usercompanyid int64
// companys []companybase
// companyid int64
// userdata *models.User
// )
// var uclientReturn *ucenter.ResponseLogin
// uclientReturn, err = ucenter.RequestUCenterLogin(account, password)
// if err != nil {
// return logintoken, protocol.NewErrWithMessage("10201")
// }
// userdata, err = models.GetUserByUCenter(uclientReturn.Data.Id)
// if err != nil {
// log.Debug("GetUserByUCenter(%d) err:%s", uclientReturn.Data.Id, err)
// return logintoken, protocol.NewErrWithMessage("10201")
// }
// if ok := userdata.IsDelete(); ok {
// log.Debug("userdata.IsDelete()==true")
// return logintoken, protocol.NewErrWithMessage("10201")
// }
// if ok := userdata.IsEnable(); !ok {
// log.Debug("userdata.IsEnable()==false")
// return logintoken, protocol.NewErrWithMessage("10202")
// }
// if companys, err = getUserCompanyReal(userdata.Id); err != nil {
// log.Error("getUserCompanyReal err:%s", err)
// return logintoken, protocol.NewErrWithMessage("10201")
// }
// if len(companys) == 0 {
// log.Debug("no company")
// return logintoken, protocol.NewErrWithMessage("10202")
// }
// //获取上一次登录的公司
// uAuth, err := models.GetUserAuthByUser(userdata.Id)
// if err == nil {
// has := false
// for _, v := range companys {
// if v.Id == uAuth.CurrentCompanyId {
// companyid = uAuth.CurrentCompanyId
// has = true
// break
// }
// }
// if !has {
// companyid = companys[0].Id
// }
// } else {
// companyid = companys[0].Id
// }
// ucompany, err := models.GetUserCompanyBy(userdata.Id, companyid)
// if err != nil {
// log.Error("获取user_company失败;%s", err)
// return logintoken, protocol.NewErrWithMessage("10202")
// }
// usercompanyid = ucompany.Id
// logintoken, _ = GenerateAuthToken(userdata.Id, companyid, usercompanyid)
// //更新用户数据
// userdata.Accid = uclientReturn.Data.Accid
// userdata.Icon = uclientReturn.Data.Avatar
// userdata.ImToken = uclientReturn.Data.Imtoken
// userdata.NickName = uclientReturn.Data.NickName
// userdata.CsAccount = uclientReturn.Data.CustomerAccount
// userdata.LastLoginTime = time.Now()
// err = models.UpdateUserById(userdata, []string{"Accid", "Icon", "ImToken", "NickName", "CsAccount", "LastLoginTime"})
// if err != nil {
// log.Error("更新用户数据失败:%s", err)
// }
// InitPermission(usercompanyid, userdata.Id)
// return logintoken, err
// }
type companybase struct {
Id int64 `orm:"column(id)"`
... ... @@ -374,7 +375,7 @@ func GetUserHasMenu(userid, companyid int64) ([]protocol.ResponseUserHasMenu, er
return list, nil
}
func GetUserMenuPermission(userid int64, companyid int64) map[string]serverbac.PermissionOptionObject {
func GetUserMenuPermission(userid int64, companyid int64, userCompanyId int64) map[string]serverbac.PermissionOptionObject {
var (
permissionMap = make(map[string]serverbac.PermissionOptionObject)
... ... @@ -390,7 +391,7 @@ func GetUserMenuPermission(userid int64, companyid int64) map[string]serverbac.P
permissionMap = AdminUserMenuPersmission()
return permissionMap
}
permissionMap, err = redisdata.GetUserPermissionAll(userid)
permissionMap, err = redisdata.GetUserPermissionAll(userCompanyId)
if err != nil {
log.Error("获取权限数据失败 :%s", err)
}
... ... @@ -538,14 +539,14 @@ func InitPermission(usercompanyid int64, userid int64) (map[string]serverbac.Per
return permissionMap, err
}
err = redisdata.SetUserPermission(permissionMap, userid)
err = redisdata.SetUserPermission(permissionMap, usercompanyid)
if err != nil {
log.Error("缓存用户权限失败:%s", err)
}
return permissionMap, nil
}
func ValidUserPermission(urlPath string, userid int64, companyid int64) bool {
func ValidUserPermission(urlPath string, userid int64, companyid int64, userCompanyId int64) bool {
var (
err error
permissionbase serverbac.PermissionBase
... ... @@ -565,7 +566,7 @@ func ValidUserPermission(urlPath string, userid int64, companyid int64) bool {
return true
}
permissionObj, err = redisdata.GetUserPermission(userid, permissionbase.CodeName)
permissionObj, err = redisdata.GetUserPermission(userCompanyId, permissionbase.CodeName)
if err != nil {
//尝试从 数据库取权限 TODO
log.Error("未取到权限数据 err:%s", err)
... ...
... ... @@ -33,7 +33,7 @@ func GetKeyCaptchAuth(phone string) string {
return key
}
func GetKeyUserPermission(userid int64) string {
key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_PERMISSION, userid)
func GetKeyUserPermission(userCompanyId int64) string {
key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_PERMISSION, userCompanyId)
return key
}
... ...
... ... @@ -102,8 +102,8 @@ func GetCaptchAuth(phone string) (string, error) {
return r, err
}
func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, userid int64) error {
key := GetKeyUserPermission(userid)
func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, userCompanyId int64) error {
key := GetKeyUserPermission(userCompanyId)
client := redis.GetRedis()
client.Del(key)
for k := range objMap {
... ... @@ -121,8 +121,8 @@ func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, userid int
return nil
}
func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject, error) {
key := GetKeyUserPermission(userid)
func GetUserPermission(userCompanyId int64, field string) (rbac.PermissionOptionObject, error) {
key := GetKeyUserPermission(userCompanyId)
client := redis.GetRedis()
str, err := client.HGet(key, field).Result()
if err != nil {
... ... @@ -139,8 +139,8 @@ func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject,
return permissionObj, err
}
func ExistUserPermission(userid int64) bool {
key := GetKeyUserPermission(userid)
func ExistUserPermission(userCompanyId int64) bool {
key := GetKeyUserPermission(userCompanyId)
client := redis.GetRedis()
value := client.Exists(key).Val()
if value > 0 {
... ... @@ -149,8 +149,8 @@ func ExistUserPermission(userid int64) bool {
return false
}
func GetUserPermissionAll(userid int64) (map[string]rbac.PermissionOptionObject, error) {
key := GetKeyUserPermission(userid)
func GetUserPermissionAll(userCompanyId int64) (map[string]rbac.PermissionOptionObject, error) {
key := GetKeyUserPermission(userCompanyId)
client := redis.GetRedis()
strMap, err := client.HGetAll(key).Result()
if err != nil {
... ...