mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 02:46:04 +01:00 
			
		
		
		
	Check if email is used when updating user (#21289)
Fix #21075 When updating user data should check if email is used by other users
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							b7309b8ccb
						
					
				
				
					commit
					1d3095b718
				
			| @@ -893,7 +893,11 @@ func UpdateUser(ctx context.Context, u *User, changePrimaryEmail bool, cols ...s | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		if !has { | ||||
| 		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, | ||||
| @@ -901,6 +905,7 @@ func UpdateUser(ctx context.Context, u *User, changePrimaryEmail bool, cols ...s | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		if !has { | ||||
| 			emailAddress.Email = u.Email | ||||
| 			emailAddress.UID = u.ID | ||||
| 			emailAddress.IsActivated = true | ||||
|   | ||||
| @@ -302,10 +302,26 @@ func TestUpdateUser(t *testing.T) { | ||||
| 	user = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 	assert.True(t, user.KeepActivityPrivate) | ||||
|  | ||||
| 	newEmail := "new_" + user.Email | ||||
| 	user.Email = newEmail | ||||
| 	assert.NoError(t, user_model.UpdateUser(db.DefaultContext, user, true)) | ||||
| 	user = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 	assert.Equal(t, newEmail, user.Email) | ||||
|  | ||||
| 	user.Email = "no mail@mail.org" | ||||
| 	assert.Error(t, user_model.UpdateUser(db.DefaultContext, user, true)) | ||||
| } | ||||
|  | ||||
| func TestUpdateUserEmailAlreadyUsed(t *testing.T) { | ||||
| 	assert.NoError(t, unittest.PrepareTestDatabase()) | ||||
| 	user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||||
| 	user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) | ||||
|  | ||||
| 	user2.Email = user3.Email | ||||
| 	err := user_model.UpdateUser(db.DefaultContext, user2, true) | ||||
| 	assert.True(t, user_model.IsErrEmailAlreadyUsed(err)) | ||||
| } | ||||
|  | ||||
| func TestNewUserRedirect(t *testing.T) { | ||||
| 	// redirect to a completely new name | ||||
| 	assert.NoError(t, unittest.PrepareTestDatabase()) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user