Compare commits

...

1 Commits

Author SHA1 Message Date
DacongDA
a28b871a46 feat: add useGroupPathInToken boolean field in app.conf (#4026) 2025-08-02 01:40:26 +08:00
2 changed files with 59 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ import (
"strings"
"time"
"github.com/casdoor/casdoor/conf"
"github.com/casdoor/casdoor/util"
"github.com/golang-jwt/jwt/v5"
)
@@ -381,6 +382,14 @@ func generateJwtToken(application *Application, user *User, provider string, non
refreshExpireTime = expireTime
}
if conf.GetConfigBool("useGroupPathInToken") {
groupPath, err := user.GetUserFullGroupPath()
if err != nil {
return "", "", "", err
}
user.Groups = groupPath
}
user = refineUser(user)
_, originBackend := getOriginFromHost(host)

View File

@@ -1331,6 +1331,56 @@ func (user *User) CheckUserFace(faceIdImage []string, provider *Provider) (bool,
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) {
if application == nil || application.GetSignupItemRule("ID") != "Incremental" {
return util.GenerateId(), nil