forked from forgejo/forgejo
Unify user update methods (#28733)
Fixes #28660 Fixes an admin api bug related to `user.LoginSource` Fixed `/user/emails` response not identical to GitHub api This PR unifies the user update methods. The goal is to keep the logic only at one place (having audit logs in mind). For example, do the password checks only in one method not everywhere a password is updated. After that PR is merged, the user creation should be next.
This commit is contained in:
parent
b4513f48ce
commit
f8b471ace1
42 changed files with 1383 additions and 1068 deletions
|
@ -196,18 +196,6 @@ func (u *User) SetLastLogin() {
|
|||
u.LastLoginUnix = timeutil.TimeStampNow()
|
||||
}
|
||||
|
||||
// UpdateUserDiffViewStyle updates the users diff view style
|
||||
func UpdateUserDiffViewStyle(ctx context.Context, u *User, style string) error {
|
||||
u.DiffViewStyle = style
|
||||
return UpdateUserCols(ctx, u, "diff_view_style")
|
||||
}
|
||||
|
||||
// UpdateUserTheme updates a users' theme irrespective of the site wide theme
|
||||
func UpdateUserTheme(ctx context.Context, u *User, themeName string) error {
|
||||
u.Theme = themeName
|
||||
return UpdateUserCols(ctx, u, "theme")
|
||||
}
|
||||
|
||||
// GetPlaceholderEmail returns an noreply email
|
||||
func (u *User) GetPlaceholderEmail() string {
|
||||
return fmt.Sprintf("%s@%s", u.LowerName, setting.Service.NoReplyAddress)
|
||||
|
@ -378,13 +366,6 @@ func (u *User) NewGitSig() *git.Signature {
|
|||
// SetPassword hashes a password using the algorithm defined in the config value of PASSWORD_HASH_ALGO
|
||||
// change passwd, salt and passwd_hash_algo fields
|
||||
func (u *User) SetPassword(passwd string) (err error) {
|
||||
if len(passwd) == 0 {
|
||||
u.Passwd = ""
|
||||
u.Salt = ""
|
||||
u.PasswdHashAlgo = ""
|
||||
return nil
|
||||
}
|
||||
|
||||
if u.Salt, err = GetUserSalt(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -488,21 +469,6 @@ func (u *User) IsMailable() bool {
|
|||
return u.IsActive
|
||||
}
|
||||
|
||||
// EmailNotifications returns the User's email notification preference
|
||||
func (u *User) EmailNotifications() string {
|
||||
return u.EmailNotificationsPreference
|
||||
}
|
||||
|
||||
// SetEmailNotifications sets the user's email notification preference
|
||||
func SetEmailNotifications(ctx context.Context, u *User, set string) error {
|
||||
u.EmailNotificationsPreference = set
|
||||
if err := UpdateUserCols(ctx, u, "email_notifications_preference"); err != nil {
|
||||
log.Error("SetEmailNotifications: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsUserExist checks if given user name exist,
|
||||
// the user name should be noncased unique.
|
||||
// If uid is presented, then check will rule out that one,
|
||||
|
@ -705,8 +671,13 @@ func CreateUser(ctx context.Context, u *User, overwriteDefault ...*CreateUserOve
|
|||
if u.Rands, err = GetUserSalt(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = u.SetPassword(u.Passwd); err != nil {
|
||||
return err
|
||||
if u.Passwd != "" {
|
||||
if err = u.SetPassword(u.Passwd); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
u.Salt = ""
|
||||
u.PasswdHashAlgo = ""
|
||||
}
|
||||
|
||||
// save changes to database
|
||||
|
@ -817,24 +788,6 @@ func VerifyUserActiveCode(ctx context.Context, code string) (user *User) {
|
|||
return nil
|
||||
}
|
||||
|
||||
// checkDupEmail checks whether there are the same email with the user
|
||||
func checkDupEmail(ctx context.Context, u *User) error {
|
||||
u.Email = strings.ToLower(u.Email)
|
||||
has, err := db.GetEngine(ctx).
|
||||
Where("id!=?", u.ID).
|
||||
And("type=?", u.Type).
|
||||
And("email=?", u.Email).
|
||||
Get(new(User))
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
return ErrEmailAlreadyUsed{
|
||||
Email: u.Email,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateUser check if user is valid to insert / update into database
|
||||
func ValidateUser(u *User, cols ...string) error {
|
||||
if len(cols) == 0 || util.SliceContainsString(cols, "visibility", true) {
|
||||
|
@ -843,81 +796,9 @@ func ValidateUser(u *User, cols ...string) error {
|
|||
}
|
||||
}
|
||||
|
||||
if len(cols) == 0 || util.SliceContainsString(cols, "email", true) {
|
||||
u.Email = strings.ToLower(u.Email)
|
||||
if err := ValidateEmail(u.Email); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateUser updates user's information.
|
||||
func UpdateUser(ctx context.Context, u *User, changePrimaryEmail bool, cols ...string) error {
|
||||
err := ValidateUser(u, cols...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e := db.GetEngine(ctx)
|
||||
|
||||
if changePrimaryEmail {
|
||||
var emailAddress EmailAddress
|
||||
has, err := e.Where("lower_email=?", strings.ToLower(u.Email)).Get(&emailAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if has && emailAddress.UID != u.ID {
|
||||
return ErrEmailAlreadyUsed{
|
||||
Email: u.Email,
|
||||
}
|
||||
}
|
||||
// 1. Update old primary email
|
||||
if _, err = e.Where("uid=? AND is_primary=?", u.ID, true).Cols("is_primary").Update(&EmailAddress{
|
||||
IsPrimary: false,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !has {
|
||||
emailAddress.Email = u.Email
|
||||
emailAddress.UID = u.ID
|
||||
emailAddress.IsActivated = true
|
||||
emailAddress.IsPrimary = true
|
||||
if _, err := e.Insert(&emailAddress); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if _, err := e.ID(emailAddress.ID).Cols("is_primary").Update(&EmailAddress{
|
||||
IsPrimary: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if !u.IsOrganization() { // check if primary email in email_address table
|
||||
primaryEmailExist, err := e.Where("uid=? AND is_primary=?", u.ID, true).Exist(&EmailAddress{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !primaryEmailExist {
|
||||
if _, err := e.Insert(&EmailAddress{
|
||||
Email: u.Email,
|
||||
UID: u.ID,
|
||||
IsActivated: true,
|
||||
IsPrimary: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(cols) == 0 {
|
||||
_, err = e.ID(u.ID).AllCols().Update(u)
|
||||
} else {
|
||||
_, err = e.ID(u.ID).Cols(cols...).Update(u)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateUserCols update user according special columns
|
||||
func UpdateUserCols(ctx context.Context, u *User, cols ...string) error {
|
||||
if err := ValidateUser(u, cols...); err != nil {
|
||||
|
@ -928,25 +809,6 @@ func UpdateUserCols(ctx context.Context, u *User, cols ...string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// UpdateUserSetting updates user's settings.
|
||||
func UpdateUserSetting(ctx context.Context, u *User) (err error) {
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer committer.Close()
|
||||
|
||||
if !u.IsOrganization() {
|
||||
if err = checkDupEmail(ctx, u); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err = UpdateUser(ctx, u, false); err != nil {
|
||||
return err
|
||||
}
|
||||
return committer.Commit()
|
||||
}
|
||||
|
||||
// GetInactiveUsers gets all inactive users
|
||||
func GetInactiveUsers(ctx context.Context, olderThan time.Duration) ([]*User, error) {
|
||||
var cond builder.Cond = builder.Eq{"is_active": false}
|
||||
|
@ -1044,7 +906,7 @@ func GetUserEmailsByNames(ctx context.Context, names []string) []string {
|
|||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if u.IsMailable() && u.EmailNotifications() != EmailNotificationsDisabled {
|
||||
if u.IsMailable() && u.EmailNotificationsPreference != EmailNotificationsDisabled {
|
||||
mails = append(mails, u.Email)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue