作者 唐旭辉

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

@@ -48,41 +48,41 @@ func (c *AuthController) RefreshToken() { @@ -48,41 +48,41 @@ func (c *AuthController) RefreshToken() {
48 48
49 // Login 登录 49 // Login 登录
50 // @router /login [post] 50 // @router /login [post]
51 -func (c *AuthController) Login() {  
52 - var msg *protocol.ResponseMessage  
53 - defer func() {  
54 - c.ResposeJson(msg)  
55 - }() 51 +// func (c *AuthController) Login() {
  52 +// var msg *protocol.ResponseMessage
  53 +// defer func() {
  54 +// c.ResposeJson(msg)
  55 +// }()
56 56
57 - var param protocol.RequestLogin  
58 - if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {  
59 - log.Error("json 解析失败", err)  
60 - msg = protocol.BadRequestParam("1")  
61 - return  
62 - }  
63 - if len(param.Account) == 0 || len(param.Password) == 0 {  
64 - msg = protocol.BadRequestParam("10201")  
65 - return  
66 - }  
67 - logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password)  
68 - if err != nil {  
69 - msg = protocol.NewReturnResponse(nil, err)  
70 - return  
71 - }  
72 - err = serveauth.ResetLoginToken(logintoken)  
73 - if err != nil {  
74 - log.Error("token 信息记录数据库失败")  
75 - }  
76 - err = serveauth.ResetLoginTokenRedis(logintoken)  
77 - if err != nil {  
78 - log.Error("token 信息记录redis失败")  
79 - }  
80 - data := protocol.ResponseLogin{  
81 - Access: logintoken,  
82 - }  
83 - msg = protocol.NewReturnResponse(data, nil)  
84 - return  
85 -} 57 +// var param protocol.RequestLogin
  58 +// if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  59 +// log.Error("json 解析失败", err)
  60 +// msg = protocol.BadRequestParam("1")
  61 +// return
  62 +// }
  63 +// if len(param.Account) == 0 || len(param.Password) == 0 {
  64 +// msg = protocol.BadRequestParam("10201")
  65 +// return
  66 +// }
  67 +// logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password)
  68 +// if err != nil {
  69 +// msg = protocol.NewReturnResponse(nil, err)
  70 +// return
  71 +// }
  72 +// err = serveauth.ResetLoginToken(logintoken)
  73 +// if err != nil {
  74 +// log.Error("token 信息记录数据库失败")
  75 +// }
  76 +// err = serveauth.ResetLoginTokenRedis(logintoken)
  77 +// if err != nil {
  78 +// log.Error("token 信息记录redis失败")
  79 +// }
  80 +// data := protocol.ResponseLogin{
  81 +// Access: logintoken,
  82 +// }
  83 +// msg = protocol.NewReturnResponse(data, nil)
  84 +// return
  85 +// }
86 86
87 // LoginSms 短信验证码登录 87 // LoginSms 短信验证码登录
88 // @router /login_sms [post] 88 // @router /login_sms [post]
@@ -276,6 +276,7 @@ func (c *AuthController) Me() { @@ -276,6 +276,7 @@ func (c *AuthController) Me() {
276 }() 276 }()
277 userid := c.GetUserId() 277 userid := c.GetUserId()
278 companyid := c.GetCompanyId() 278 companyid := c.GetCompanyId()
  279 + userCompanyId := c.GetUserCompanyId()
279 userinfo, err := serveauth.UserBaseInfo(userid, companyid) 280 userinfo, err := serveauth.UserBaseInfo(userid, companyid)
280 if err != nil { 281 if err != nil {
281 log.Error("获取用户数据失败") 282 log.Error("获取用户数据失败")
@@ -288,7 +289,7 @@ func (c *AuthController) Me() { @@ -288,7 +289,7 @@ func (c *AuthController) Me() {
288 if err != nil { 289 if err != nil {
289 log.Error("获取用户的公司失败") 290 log.Error("获取用户的公司失败")
290 } 291 }
291 - permissionMap := serveauth.GetUserMenuPermission(userid, companyid) 292 + permissionMap := serveauth.GetUserMenuPermission(userid, companyid, userCompanyId)
292 data := map[string]interface{}{ 293 data := map[string]interface{}{
293 "user": userinfo, 294 "user": userinfo,
294 "menus": menus, 295 "menus": menus,
@@ -34,7 +34,8 @@ func (this *BaseController) Prepare() { @@ -34,7 +34,8 @@ func (this *BaseController) Prepare() {
34 p := fmt.Sprint(this.Ctx.Input.GetData("RouterPattern")) 34 p := fmt.Sprint(this.Ctx.Input.GetData("RouterPattern"))
35 userid := this.GetUserId() 35 userid := this.GetUserId()
36 companyid := this.GetCompanyId() 36 companyid := this.GetCompanyId()
37 - ok := serveauth.ValidUserPermission(p, userid, companyid) 37 + userCompanyId := this.GetUserCompanyId()
  38 + ok := serveauth.ValidUserPermission(p, userid, companyid, userCompanyId)
38 if !ok { 39 if !ok {
39 msg := protocol.NewMessage("10210") 40 msg := protocol.NewMessage("10210")
40 this.ResposeJson(msg) 41 this.ResposeJson(msg)
@@ -87,6 +88,12 @@ func (this *BaseController) GetUserId() int64 { @@ -87,6 +88,12 @@ func (this *BaseController) GetUserId() int64 {
87 return userid 88 return userid
88 } 89 }
89 90
  91 +func (this *BaseController) GetUserCompanyId() int64 {
  92 + v := this.Ctx.Input.GetData(protocol.HeaderUCompanyid)
  93 + userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
  94 + return userid
  95 +}
  96 +
90 //Valid valid struct 97 //Valid valid struct
91 func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) { 98 func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
92 /*校验*/ 99 /*校验*/
@@ -151,7 +151,7 @@ func init() { @@ -151,7 +151,7 @@ func init() {
151 nsAuth := beego.NewNamespace("/auth", 151 nsAuth := beego.NewNamespace("/auth",
152 beego.NSBefore(middleware.AllowOption), 152 beego.NSBefore(middleware.AllowOption),
153 beego.NSBefore(middleware.LogRequestData), 153 beego.NSBefore(middleware.LogRequestData),
154 - beego.NSRouter("/login_account", &controllers.AuthController{}, "post:Login"), 154 + // beego.NSRouter("/login_account", &controllers.AuthController{}, "post:Login"),
155 beego.NSRouter("/login", &controllers.AuthController{}, "post:LoginSecretKey"), 155 beego.NSRouter("/login", &controllers.AuthController{}, "post:LoginSecretKey"),
156 beego.NSRouter("/startCaptcha", &controllers.AuthController{}, "post:RegisterGeetest"), 156 beego.NSRouter("/startCaptcha", &controllers.AuthController{}, "post:RegisterGeetest"),
157 beego.NSRouter("/verifyCaptcha", &controllers.AuthController{}, "post:ValidateGeetest"), 157 beego.NSRouter("/verifyCaptcha", &controllers.AuthController{}, "post:ValidateGeetest"),
@@ -19,7 +19,7 @@ func getAuditUserHasPermission(userid int64, usercompanyid int64) ( @@ -19,7 +19,7 @@ func getAuditUserHasPermission(userid int64, usercompanyid int64) (
19 serverabc.PermissionOptionObject, error) { 19 serverabc.PermissionOptionObject, error) {
20 var permissionObject serverabc.PermissionOptionObject 20 var permissionObject serverabc.PermissionOptionObject
21 var err error 21 var err error
22 - if ok := redisdata.ExistUserPermission(userid); !ok { 22 + if ok := redisdata.ExistUserPermission(usercompanyid); !ok {
23 //尝试重数据库获取 23 //尝试重数据库获取
24 log.Debug("从数据库读取权限") 24 log.Debug("从数据库读取权限")
25 permissionMap, err := serverabc.GetUserPermission(usercompanyid, serverabc.M_SYSTEM_OPPORTUNITY) 25 permissionMap, err := serverabc.GetUserPermission(usercompanyid, serverabc.M_SYSTEM_OPPORTUNITY)
@@ -141,85 +141,86 @@ func ChangeLoginToken(userid, companyid int64) (protocol.LoginAuthToken, error) @@ -141,85 +141,86 @@ func ChangeLoginToken(userid, companyid int64) (protocol.LoginAuthToken, error)
141 log.Error("GenerateAuthToken err:%s", err) 141 log.Error("GenerateAuthToken err:%s", err)
142 return logintoken, protocol.NewErrWithMessage("1") 142 return logintoken, protocol.NewErrWithMessage("1")
143 } 143 }
  144 + InitPermission(usercompany.Id, usercompany.UserId)
144 return logintoken, nil 145 return logintoken, nil
145 } 146 }
146 147
147 -func LoginAuthByUCenter(account, password string) (  
148 - protocol.LoginAuthToken, error) {  
149 - var (  
150 - err error  
151 - logintoken protocol.LoginAuthToken  
152 - usercompanyid int64  
153 - companys []companybase  
154 - companyid int64  
155 - userdata *models.User  
156 - )  
157 - var uclientReturn *ucenter.ResponseLogin  
158 - uclientReturn, err = ucenter.RequestUCenterLogin(account, password)  
159 - if err != nil {  
160 - return logintoken, protocol.NewErrWithMessage("10201")  
161 - }  
162 - userdata, err = models.GetUserByUCenter(uclientReturn.Data.Id)  
163 - if err != nil {  
164 - log.Debug("GetUserByUCenter(%d) err:%s", uclientReturn.Data.Id, err)  
165 - return logintoken, protocol.NewErrWithMessage("10201")  
166 - }  
167 - if ok := userdata.IsDelete(); ok {  
168 - log.Debug("userdata.IsDelete()==true")  
169 - return logintoken, protocol.NewErrWithMessage("10201")  
170 - }  
171 - if ok := userdata.IsEnable(); !ok {  
172 - log.Debug("userdata.IsEnable()==false")  
173 - return logintoken, protocol.NewErrWithMessage("10202")  
174 - }  
175 - if companys, err = getUserCompanyReal(userdata.Id); err != nil {  
176 - log.Error("getUserCompanyReal err:%s", err)  
177 - return logintoken, protocol.NewErrWithMessage("10201")  
178 - }  
179 - if len(companys) == 0 {  
180 - log.Debug("no company")  
181 - return logintoken, protocol.NewErrWithMessage("10202")  
182 - }  
183 - //获取上一次登录的公司  
184 - uAuth, err := models.GetUserAuthByUser(userdata.Id)  
185 - if err == nil {  
186 - has := false  
187 - for _, v := range companys {  
188 - if v.Id == uAuth.CurrentCompanyId {  
189 - companyid = uAuth.CurrentCompanyId  
190 - has = true  
191 - break  
192 - }  
193 - }  
194 - if !has {  
195 - companyid = companys[0].Id  
196 - }  
197 -  
198 - } else {  
199 - companyid = companys[0].Id  
200 - }  
201 - ucompany, err := models.GetUserCompanyBy(userdata.Id, companyid)  
202 - if err != nil {  
203 - log.Error("获取user_company失败;%s", err)  
204 - return logintoken, protocol.NewErrWithMessage("10202")  
205 - }  
206 - usercompanyid = ucompany.Id  
207 - logintoken, _ = GenerateAuthToken(userdata.Id, companyid, usercompanyid)  
208 - //更新用户数据  
209 - userdata.Accid = uclientReturn.Data.Accid  
210 - userdata.Icon = uclientReturn.Data.Avatar  
211 - userdata.ImToken = uclientReturn.Data.Imtoken  
212 - userdata.NickName = uclientReturn.Data.NickName  
213 - userdata.CsAccount = uclientReturn.Data.CustomerAccount  
214 - userdata.LastLoginTime = time.Now()  
215 - err = models.UpdateUserById(userdata, []string{"Accid", "Icon", "ImToken", "NickName", "CsAccount", "LastLoginTime"})  
216 - if err != nil {  
217 - log.Error("更新用户数据失败:%s", err)  
218 - }  
219 -  
220 - InitPermission(usercompanyid, userdata.Id)  
221 - return logintoken, err  
222 -} 148 +// func LoginAuthByUCenter(account, password string) (
  149 +// protocol.LoginAuthToken, error) {
  150 +// var (
  151 +// err error
  152 +// logintoken protocol.LoginAuthToken
  153 +// usercompanyid int64
  154 +// companys []companybase
  155 +// companyid int64
  156 +// userdata *models.User
  157 +// )
  158 +// var uclientReturn *ucenter.ResponseLogin
  159 +// uclientReturn, err = ucenter.RequestUCenterLogin(account, password)
  160 +// if err != nil {
  161 +// return logintoken, protocol.NewErrWithMessage("10201")
  162 +// }
  163 +// userdata, err = models.GetUserByUCenter(uclientReturn.Data.Id)
  164 +// if err != nil {
  165 +// log.Debug("GetUserByUCenter(%d) err:%s", uclientReturn.Data.Id, err)
  166 +// return logintoken, protocol.NewErrWithMessage("10201")
  167 +// }
  168 +// if ok := userdata.IsDelete(); ok {
  169 +// log.Debug("userdata.IsDelete()==true")
  170 +// return logintoken, protocol.NewErrWithMessage("10201")
  171 +// }
  172 +// if ok := userdata.IsEnable(); !ok {
  173 +// log.Debug("userdata.IsEnable()==false")
  174 +// return logintoken, protocol.NewErrWithMessage("10202")
  175 +// }
  176 +// if companys, err = getUserCompanyReal(userdata.Id); err != nil {
  177 +// log.Error("getUserCompanyReal err:%s", err)
  178 +// return logintoken, protocol.NewErrWithMessage("10201")
  179 +// }
  180 +// if len(companys) == 0 {
  181 +// log.Debug("no company")
  182 +// return logintoken, protocol.NewErrWithMessage("10202")
  183 +// }
  184 +// //获取上一次登录的公司
  185 +// uAuth, err := models.GetUserAuthByUser(userdata.Id)
  186 +// if err == nil {
  187 +// has := false
  188 +// for _, v := range companys {
  189 +// if v.Id == uAuth.CurrentCompanyId {
  190 +// companyid = uAuth.CurrentCompanyId
  191 +// has = true
  192 +// break
  193 +// }
  194 +// }
  195 +// if !has {
  196 +// companyid = companys[0].Id
  197 +// }
  198 +
  199 +// } else {
  200 +// companyid = companys[0].Id
  201 +// }
  202 +// ucompany, err := models.GetUserCompanyBy(userdata.Id, companyid)
  203 +// if err != nil {
  204 +// log.Error("获取user_company失败;%s", err)
  205 +// return logintoken, protocol.NewErrWithMessage("10202")
  206 +// }
  207 +// usercompanyid = ucompany.Id
  208 +// logintoken, _ = GenerateAuthToken(userdata.Id, companyid, usercompanyid)
  209 +// //更新用户数据
  210 +// userdata.Accid = uclientReturn.Data.Accid
  211 +// userdata.Icon = uclientReturn.Data.Avatar
  212 +// userdata.ImToken = uclientReturn.Data.Imtoken
  213 +// userdata.NickName = uclientReturn.Data.NickName
  214 +// userdata.CsAccount = uclientReturn.Data.CustomerAccount
  215 +// userdata.LastLoginTime = time.Now()
  216 +// err = models.UpdateUserById(userdata, []string{"Accid", "Icon", "ImToken", "NickName", "CsAccount", "LastLoginTime"})
  217 +// if err != nil {
  218 +// log.Error("更新用户数据失败:%s", err)
  219 +// }
  220 +
  221 +// InitPermission(usercompanyid, userdata.Id)
  222 +// return logintoken, err
  223 +// }
223 224
224 type companybase struct { 225 type companybase struct {
225 Id int64 `orm:"column(id)"` 226 Id int64 `orm:"column(id)"`
@@ -374,7 +375,7 @@ func GetUserHasMenu(userid, companyid int64) ([]protocol.ResponseUserHasMenu, er @@ -374,7 +375,7 @@ func GetUserHasMenu(userid, companyid int64) ([]protocol.ResponseUserHasMenu, er
374 return list, nil 375 return list, nil
375 } 376 }
376 377
377 -func GetUserMenuPermission(userid int64, companyid int64) map[string]serverbac.PermissionOptionObject { 378 +func GetUserMenuPermission(userid int64, companyid int64, userCompanyId int64) map[string]serverbac.PermissionOptionObject {
378 379
379 var ( 380 var (
380 permissionMap = make(map[string]serverbac.PermissionOptionObject) 381 permissionMap = make(map[string]serverbac.PermissionOptionObject)
@@ -390,7 +391,7 @@ func GetUserMenuPermission(userid int64, companyid int64) map[string]serverbac.P @@ -390,7 +391,7 @@ func GetUserMenuPermission(userid int64, companyid int64) map[string]serverbac.P
390 permissionMap = AdminUserMenuPersmission() 391 permissionMap = AdminUserMenuPersmission()
391 return permissionMap 392 return permissionMap
392 } 393 }
393 - permissionMap, err = redisdata.GetUserPermissionAll(userid) 394 + permissionMap, err = redisdata.GetUserPermissionAll(userCompanyId)
394 if err != nil { 395 if err != nil {
395 log.Error("获取权限数据失败 :%s", err) 396 log.Error("获取权限数据失败 :%s", err)
396 } 397 }
@@ -538,14 +539,14 @@ func InitPermission(usercompanyid int64, userid int64) (map[string]serverbac.Per @@ -538,14 +539,14 @@ func InitPermission(usercompanyid int64, userid int64) (map[string]serverbac.Per
538 return permissionMap, err 539 return permissionMap, err
539 } 540 }
540 541
541 - err = redisdata.SetUserPermission(permissionMap, userid) 542 + err = redisdata.SetUserPermission(permissionMap, usercompanyid)
542 if err != nil { 543 if err != nil {
543 log.Error("缓存用户权限失败:%s", err) 544 log.Error("缓存用户权限失败:%s", err)
544 } 545 }
545 return permissionMap, nil 546 return permissionMap, nil
546 } 547 }
547 548
548 -func ValidUserPermission(urlPath string, userid int64, companyid int64) bool { 549 +func ValidUserPermission(urlPath string, userid int64, companyid int64, userCompanyId int64) bool {
549 var ( 550 var (
550 err error 551 err error
551 permissionbase serverbac.PermissionBase 552 permissionbase serverbac.PermissionBase
@@ -565,7 +566,7 @@ func ValidUserPermission(urlPath string, userid int64, companyid int64) bool { @@ -565,7 +566,7 @@ func ValidUserPermission(urlPath string, userid int64, companyid int64) bool {
565 return true 566 return true
566 } 567 }
567 568
568 - permissionObj, err = redisdata.GetUserPermission(userid, permissionbase.CodeName) 569 + permissionObj, err = redisdata.GetUserPermission(userCompanyId, permissionbase.CodeName)
569 if err != nil { 570 if err != nil {
570 //尝试从 数据库取权限 TODO 571 //尝试从 数据库取权限 TODO
571 log.Error("未取到权限数据 err:%s", err) 572 log.Error("未取到权限数据 err:%s", err)
@@ -33,7 +33,7 @@ func GetKeyCaptchAuth(phone string) string { @@ -33,7 +33,7 @@ func GetKeyCaptchAuth(phone string) string {
33 return key 33 return key
34 } 34 }
35 35
36 -func GetKeyUserPermission(userid int64) string {  
37 - key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_PERMISSION, userid) 36 +func GetKeyUserPermission(userCompanyId int64) string {
  37 + key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_PERMISSION, userCompanyId)
38 return key 38 return key
39 } 39 }
@@ -102,8 +102,8 @@ func GetCaptchAuth(phone string) (string, error) { @@ -102,8 +102,8 @@ func GetCaptchAuth(phone string) (string, error) {
102 return r, err 102 return r, err
103 } 103 }
104 104
105 -func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, userid int64) error {  
106 - key := GetKeyUserPermission(userid) 105 +func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, userCompanyId int64) error {
  106 + key := GetKeyUserPermission(userCompanyId)
107 client := redis.GetRedis() 107 client := redis.GetRedis()
108 client.Del(key) 108 client.Del(key)
109 for k := range objMap { 109 for k := range objMap {
@@ -121,8 +121,8 @@ func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, userid int @@ -121,8 +121,8 @@ func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, userid int
121 return nil 121 return nil
122 } 122 }
123 123
124 -func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject, error) {  
125 - key := GetKeyUserPermission(userid) 124 +func GetUserPermission(userCompanyId int64, field string) (rbac.PermissionOptionObject, error) {
  125 + key := GetKeyUserPermission(userCompanyId)
126 client := redis.GetRedis() 126 client := redis.GetRedis()
127 str, err := client.HGet(key, field).Result() 127 str, err := client.HGet(key, field).Result()
128 if err != nil { 128 if err != nil {
@@ -139,8 +139,8 @@ func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject, @@ -139,8 +139,8 @@ func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject,
139 return permissionObj, err 139 return permissionObj, err
140 } 140 }
141 141
142 -func ExistUserPermission(userid int64) bool {  
143 - key := GetKeyUserPermission(userid) 142 +func ExistUserPermission(userCompanyId int64) bool {
  143 + key := GetKeyUserPermission(userCompanyId)
144 client := redis.GetRedis() 144 client := redis.GetRedis()
145 value := client.Exists(key).Val() 145 value := client.Exists(key).Val()
146 if value > 0 { 146 if value > 0 {
@@ -149,8 +149,8 @@ func ExistUserPermission(userid int64) bool { @@ -149,8 +149,8 @@ func ExistUserPermission(userid int64) bool {
149 return false 149 return false
150 } 150 }
151 151
152 -func GetUserPermissionAll(userid int64) (map[string]rbac.PermissionOptionObject, error) {  
153 - key := GetKeyUserPermission(userid) 152 +func GetUserPermissionAll(userCompanyId int64) (map[string]rbac.PermissionOptionObject, error) {
  153 + key := GetKeyUserPermission(userCompanyId)
154 client := redis.GetRedis() 154 client := redis.GetRedis()
155 strMap, err := client.HGetAll(key).Result() 155 strMap, err := client.HGetAll(key).Result()
156 if err != nil { 156 if err != nil {