mirror of
https://github.com/casdoor/casdoor.git
synced 2025-09-07 11:00:28 +08:00
feat: add useGroupPathInToken boolean field in app.conf (#4026)
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/casdoor/casdoor/conf"
|
||||||
"github.com/casdoor/casdoor/util"
|
"github.com/casdoor/casdoor/util"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
@@ -381,6 +382,14 @@ func generateJwtToken(application *Application, user *User, provider string, non
|
|||||||
refreshExpireTime = expireTime
|
refreshExpireTime = expireTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if conf.GetConfigBool("useGroupPathInToken") {
|
||||||
|
groupPath, err := user.GetUserFullGroupPath()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
user.Groups = groupPath
|
||||||
|
}
|
||||||
user = refineUser(user)
|
user = refineUser(user)
|
||||||
|
|
||||||
_, originBackend := getOriginFromHost(host)
|
_, originBackend := getOriginFromHost(host)
|
||||||
|
@@ -1331,6 +1331,56 @@ func (user *User) CheckUserFace(faceIdImage []string, provider *Provider) (bool,
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (user *User) GetUserFullGroupPath() ([]string, error) {
|
||||||
|
if len(user.Groups) == 0 {
|
||||||
|
return []string{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var orgGroups []*Group
|
||||||
|
orgGroups, err := GetGroups(user.Owner)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
groupMap := make(map[string]Group)
|
||||||
|
for _, group := range orgGroups {
|
||||||
|
groupMap[group.Name] = *group
|
||||||
|
}
|
||||||
|
|
||||||
|
var groupFullPath []string
|
||||||
|
|
||||||
|
for _, groupId := range user.Groups {
|
||||||
|
_, groupName := util.GetOwnerAndNameFromIdNoCheck(groupId)
|
||||||
|
group, ok := groupMap[groupName]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
groupPath := groupName
|
||||||
|
|
||||||
|
curGroup, ok := groupMap[group.ParentId]
|
||||||
|
if !ok {
|
||||||
|
return []string{}, fmt.Errorf("group:Group %s not exist", group.ParentId)
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
groupPath = util.GetId(curGroup.Name, groupPath)
|
||||||
|
if curGroup.IsTopGroup {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
curGroup, ok = groupMap[curGroup.ParentId]
|
||||||
|
if !ok {
|
||||||
|
return []string{}, fmt.Errorf("group:Group %s not exist", curGroup.ParentId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
groupPath = util.GetId(curGroup.Owner, groupPath)
|
||||||
|
groupFullPath = append(groupFullPath, groupPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return groupFullPath, nil
|
||||||
|
}
|
||||||
|
|
||||||
func GenerateIdForNewUser(application *Application) (string, error) {
|
func GenerateIdForNewUser(application *Application) (string, error) {
|
||||||
if application == nil || application.GetSignupItemRule("ID") != "Incremental" {
|
if application == nil || application.GetSignupItemRule("ID") != "Incremental" {
|
||||||
return util.GenerateId(), nil
|
return util.GenerateId(), nil
|
||||||
|
Reference in New Issue
Block a user