From 2daf26aa884dfe3f27efb25382d111bc1efa3af5 Mon Sep 17 00:00:00 2001 From: DacongDA Date: Fri, 17 May 2024 11:43:19 +0800 Subject: [PATCH] feat: use lowercase username when isUsernameLowered is enabled (#2952) * 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 --- controllers/user.go | 6 ++++++ object/user.go | 1 + object/user_util.go | 12 +++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/controllers/user.go b/controllers/user.go index 0d4298da..fd786607 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -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" ) @@ -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) diff --git a/object/user.go b/object/user.go index fbecd650..c9fb6396 100644 --- a/object/user.go +++ b/object/user.go @@ -877,6 +877,7 @@ func AddUsers(users []*User) (bool, error) { } } + user.Name = strings.TrimSpace(user.Name) if isUsernameLowered { user.Name = strings.ToLower(user.Name) } diff --git a/object/user_util.go b/object/user_util.go index 1f55ba0a..addb691e 100644 --- a/object/user_util.go +++ b/object/user_util.go @@ -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" ) @@ -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 {