diff --git a/pkg/api/oauth2_authentications.go b/pkg/api/oauth2_authentications.go index b3af7d37..d5eef59c 100644 --- a/pkg/api/oauth2_authentications.go +++ b/pkg/api/oauth2_authentications.go @@ -242,7 +242,6 @@ func (a *OAuth2AuthenticationApi) CallbackHandler(c *core.WebContext) (string, * Username: userName, Email: email, Nickname: nickName, - Password: "", Language: languageCode, DefaultCurrency: currencyCode, FirstDayOfWeek: oauth2UserInfo.FirstDayOfWeek, @@ -251,7 +250,7 @@ func (a *OAuth2AuthenticationApi) CallbackHandler(c *core.WebContext) (string, * FeatureRestriction: a.CurrentConfig().DefaultFeatureRestrictions, } - err = a.users.CreateUser(c, user) + err = a.users.CreateUser(c, user, true) if err != nil { log.Errorf(c, "[oauth2_authentications.CallbackHandler] failed to create user \"%s\", because %s", user.Username, err.Error()) @@ -260,7 +259,7 @@ func (a *OAuth2AuthenticationApi) CallbackHandler(c *core.WebContext) (string, * log.Infof(c, "[oauth2_authentications.CallbackHandler] user \"%s\" has registered successfully, uid is %d", user.Username, user.Uid) - userExternalAuth := &models.UserExternalAuth{ + userExternalAuth = &models.UserExternalAuth{ Uid: user.Uid, ExternalAuthType: userExternalAuthType, ExternalUsername: oauth2UserInfo.UserName, diff --git a/pkg/api/users.go b/pkg/api/users.go index b882953e..f8fa92d2 100644 --- a/pkg/api/users.go +++ b/pkg/api/users.go @@ -83,7 +83,7 @@ func (a *UsersApi) UserRegisterHandler(c *core.WebContext) (any, *errs.Error) { FeatureRestriction: a.CurrentConfig().DefaultFeatureRestrictions, } - err = a.users.CreateUser(c, user) + err = a.users.CreateUser(c, user, false) if err != nil { log.Errorf(c, "[users.UserRegisterHandler] failed to create user \"%s\", because %s", user.Username, err.Error()) diff --git a/pkg/cli/user_data.go b/pkg/cli/user_data.go index e5df1498..c59e2d4a 100644 --- a/pkg/cli/user_data.go +++ b/pkg/cli/user_data.go @@ -91,7 +91,7 @@ func (l *UserDataCli) AddNewUser(c *core.CliContext, username string, email stri FeatureRestriction: l.CurrentConfig().DefaultFeatureRestrictions, } - err := l.users.CreateUser(c, user) + err := l.users.CreateUser(c, user, false) if err != nil { log.CliErrorf(c, "[user_data.AddNewUser] failed to create user \"%s\", because %s", user.Username, err.Error()) diff --git a/pkg/services/users.go b/pkg/services/users.go index 8904f43d..b662e672 100644 --- a/pkg/services/users.go +++ b/pkg/services/users.go @@ -184,7 +184,7 @@ func (s *UserService) GetUserAvatar(c core.Context, uid int64, fileExtension str } // CreateUser saves a new user model to database -func (s *UserService) CreateUser(c core.Context, user *models.User) error { +func (s *UserService) CreateUser(c core.Context, user *models.User, noPassword bool) error { exists, err := s.ExistsUsername(c, user.Username) if err != nil { @@ -201,7 +201,7 @@ func (s *UserService) CreateUser(c core.Context, user *models.User) error { return errs.ErrUserEmailAlreadyExists } - if user.Password == "" { + if !noPassword && user.Password == "" { return errs.ErrPasswordIsEmpty } @@ -215,7 +215,11 @@ func (s *UserService) CreateUser(c core.Context, user *models.User) error { return errs.ErrSystemIsBusy } - user.Password = utils.EncodePassword(user.Password, user.Salt) + if !noPassword { + user.Password = utils.EncodePassword(user.Password, user.Salt) + } else { + user.Password = "" + } user.Deleted = false