...
|
...
|
@@ -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)
|
...
|
...
|
|