Skip to content

Commit

Permalink
feat: use lowercase username when isUsernameLowered is enabled (#2952)
Browse files Browse the repository at this point in the history
* feat: auto trim username during login and lowercase when isUsernameLowered enabled in conf

* fix: fix linter error

* fix: fix linter error

* fix: fix linter error
  • Loading branch information
dacongda committed May 17, 2024
1 parent 21c151b commit 2daf26a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"github.com/beego/beego/utils/pagination"
"github.com/casdoor/casdoor/conf"
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
)
Expand Down Expand Up @@ -293,6 +294,11 @@ func (c *ApiController) UpdateUser() {
return
}

isUsernameLowered := conf.GetConfigBool("isUsernameLowered")
if isUsernameLowered {
user.Name = strings.ToLower(user.Name)
}

isAdmin := c.IsAdmin()
if pass, err := object.CheckPermissionForUpdateUser(oldUser, &user, isAdmin, c.GetAcceptLanguage()); !pass {
c.ResponseError(err)
Expand Down
1 change: 1 addition & 0 deletions object/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ func AddUsers(users []*User) (bool, error) {
}
}

user.Name = strings.TrimSpace(user.Name)
if isUsernameLowered {
user.Name = strings.ToLower(user.Name)
}
Expand Down
12 changes: 9 additions & 3 deletions object/user_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import (
"regexp"
"strings"

"github.com/casdoor/casdoor/conf"
"github.com/casdoor/casdoor/i18n"

jsoniter "github.com/json-iterator/go"

"github.com/casdoor/casdoor/idp"
"github.com/casdoor/casdoor/util"
jsoniter "github.com/json-iterator/go"
"github.com/xorm-io/core"
)

Expand Down Expand Up @@ -57,6 +56,13 @@ func HasUserByField(organizationName string, field string, value string) bool {
}

func GetUserByFields(organization string, field string) (*User, error) {
isUsernameLowered := conf.GetConfigBool("isUsernameLowered")
if isUsernameLowered {
field = strings.ToLower(field)
}

field = strings.TrimSpace(field)

// check username
user, err := GetUserByField(organization, "name", field)
if err != nil || user != nil {
Expand Down

0 comments on commit 2daf26a

Please sign in to comment.