Migrating User Store VerifyEmail, GetByAuth and GetByEmail functions to sync by default (#10941)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
f49a0881bf
Коммит
76bab4f0c2
@@ -949,55 +949,45 @@ func (us SqlUserStore) GetSystemAdminProfiles() store.StoreChannel {
|
||||
})
|
||||
}
|
||||
|
||||
func (us SqlUserStore) GetByEmail(email string) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
email = strings.ToLower(email)
|
||||
func (us SqlUserStore) GetByEmail(email string) (*model.User, *model.AppError) {
|
||||
email = strings.ToLower(email)
|
||||
|
||||
query := us.usersQuery.Where("Email = ?", email)
|
||||
query := us.usersQuery.Where("Email = ?", email)
|
||||
|
||||
queryString, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
result.Err = model.NewAppError("SqlUserStore.GetByEmail", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
queryString, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlUserStore.GetByEmail", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
user := model.User{}
|
||||
if err := us.GetReplica().SelectOne(&user, queryString, args...); err != nil {
|
||||
result.Err = model.NewAppError("SqlUserStore.GetByEmail", store.MISSING_ACCOUNT_ERROR, nil, "email="+email+", "+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
user := model.User{}
|
||||
if err := us.GetReplica().SelectOne(&user, queryString, args...); err != nil {
|
||||
return nil, model.NewAppError("SqlUserStore.GetByEmail", store.MISSING_ACCOUNT_ERROR, nil, "email="+email+", "+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
result.Data = &user
|
||||
})
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (us SqlUserStore) GetByAuth(authData *string, authService string) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if authData == nil || *authData == "" {
|
||||
result.Err = model.NewAppError("SqlUserStore.GetByAuth", store.MISSING_AUTH_ACCOUNT_ERROR, nil, "authData='', authService="+authService, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
func (us SqlUserStore) GetByAuth(authData *string, authService string) (*model.User, *model.AppError) {
|
||||
if authData == nil || *authData == "" {
|
||||
return nil, model.NewAppError("SqlUserStore.GetByAuth", store.MISSING_AUTH_ACCOUNT_ERROR, nil, "authData='', authService="+authService, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
query := us.usersQuery.
|
||||
Where("u.AuthData = ?", authData).
|
||||
Where("u.AuthService = ?", authService)
|
||||
query := us.usersQuery.
|
||||
Where("u.AuthData = ?", authData).
|
||||
Where("u.AuthService = ?", authService)
|
||||
|
||||
queryString, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
result.Err = model.NewAppError("SqlUserStore.GetByAuth", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
queryString, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlUserStore.GetByAuth", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
user := model.User{}
|
||||
if err := us.GetReplica().SelectOne(&user, queryString, args...); err == sql.ErrNoRows {
|
||||
result.Err = model.NewAppError("SqlUserStore.GetByAuth", store.MISSING_AUTH_ACCOUNT_ERROR, nil, "authData="+*authData+", authService="+authService+", "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
} else if err != nil {
|
||||
result.Err = model.NewAppError("SqlUserStore.GetByAuth", "store.sql_user.get_by_auth.other.app_error", nil, "authData="+*authData+", authService="+authService+", "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
result.Data = &user
|
||||
})
|
||||
user := model.User{}
|
||||
if err := us.GetReplica().SelectOne(&user, queryString, args...); err == sql.ErrNoRows {
|
||||
return nil, model.NewAppError("SqlUserStore.GetByAuth", store.MISSING_AUTH_ACCOUNT_ERROR, nil, "authData="+*authData+", authService="+authService+", "+err.Error(), http.StatusInternalServerError)
|
||||
} else if err != nil {
|
||||
return nil, model.NewAppError("SqlUserStore.GetByAuth", "store.sql_user.get_by_auth.other.app_error", nil, "authData="+*authData+", authService="+authService+", "+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (us SqlUserStore) GetAllUsingAuthService(authService string) store.StoreChannel {
|
||||
@@ -1083,15 +1073,13 @@ func (us SqlUserStore) GetForLogin(loginId string, allowSignInWithUsername, allo
|
||||
})
|
||||
}
|
||||
|
||||
func (us SqlUserStore) VerifyEmail(userId, email string) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
curTime := model.GetMillis()
|
||||
if _, err := us.GetMaster().Exec("UPDATE Users SET Email = :email, EmailVerified = true, UpdateAt = :Time WHERE Id = :UserId", map[string]interface{}{"email": email, "Time": curTime, "UserId": userId}); err != nil {
|
||||
result.Err = model.NewAppError("SqlUserStore.VerifyEmail", "store.sql_user.verify_email.app_error", nil, "userId="+userId+", "+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
func (us SqlUserStore) VerifyEmail(userId, email string) (string, *model.AppError) {
|
||||
curTime := model.GetMillis()
|
||||
if _, err := us.GetMaster().Exec("UPDATE Users SET Email = :email, EmailVerified = true, UpdateAt = :Time WHERE Id = :UserId", map[string]interface{}{"email": email, "Time": curTime, "UserId": userId}); err != nil {
|
||||
return "", model.NewAppError("SqlUserStore.VerifyEmail", "store.sql_user.verify_email.app_error", nil, "userId="+userId+", "+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
result.Data = userId
|
||||
})
|
||||
return userId, nil
|
||||
}
|
||||
|
||||
func (us SqlUserStore) PermanentDelete(userId string) *model.AppError {
|
||||
|
||||
@@ -268,12 +268,12 @@ type UserStore interface {
|
||||
GetProfiles(options *model.UserGetOptions) StoreChannel
|
||||
GetProfileByIds(userId []string, allowFromCache bool, viewRestrictions *model.ViewUsersRestrictions) StoreChannel
|
||||
InvalidatProfileCacheForUser(userId string)
|
||||
GetByEmail(email string) StoreChannel
|
||||
GetByAuth(authData *string, authService string) StoreChannel
|
||||
GetByEmail(email string) (*model.User, *model.AppError)
|
||||
GetByAuth(authData *string, authService string) (*model.User, *model.AppError)
|
||||
GetAllUsingAuthService(authService string) StoreChannel
|
||||
GetByUsername(username string) StoreChannel
|
||||
GetForLogin(loginId string, allowSignInWithUsername, allowSignInWithEmail bool) StoreChannel
|
||||
VerifyEmail(userId, email string) StoreChannel
|
||||
VerifyEmail(userId, email string) (string, *model.AppError)
|
||||
GetEtagForAllProfiles() StoreChannel
|
||||
GetEtagForProfiles(teamId string) StoreChannel
|
||||
UpdateFailedPasswordAttempts(userId string, attempts int) StoreChannel
|
||||
|
||||
@@ -220,35 +220,53 @@ func (_m *UserStore) GetAnyUnreadPostCountForChannel(userId string, channelId st
|
||||
}
|
||||
|
||||
// GetByAuth provides a mock function with given fields: authData, authService
|
||||
func (_m *UserStore) GetByAuth(authData *string, authService string) store.StoreChannel {
|
||||
func (_m *UserStore) GetByAuth(authData *string, authService string) (*model.User, *model.AppError) {
|
||||
ret := _m.Called(authData, authService)
|
||||
|
||||
var r0 store.StoreChannel
|
||||
if rf, ok := ret.Get(0).(func(*string, string) store.StoreChannel); ok {
|
||||
var r0 *model.User
|
||||
if rf, ok := ret.Get(0).(func(*string, string) *model.User); ok {
|
||||
r0 = rf(authData, authService)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.StoreChannel)
|
||||
r0 = ret.Get(0).(*model.User)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
var r1 *model.AppError
|
||||
if rf, ok := ret.Get(1).(func(*string, string) *model.AppError); ok {
|
||||
r1 = rf(authData, authService)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*model.AppError)
|
||||
}
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetByEmail provides a mock function with given fields: email
|
||||
func (_m *UserStore) GetByEmail(email string) store.StoreChannel {
|
||||
func (_m *UserStore) GetByEmail(email string) (*model.User, *model.AppError) {
|
||||
ret := _m.Called(email)
|
||||
|
||||
var r0 store.StoreChannel
|
||||
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok {
|
||||
var r0 *model.User
|
||||
if rf, ok := ret.Get(0).(func(string) *model.User); ok {
|
||||
r0 = rf(email)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.StoreChannel)
|
||||
r0 = ret.Get(0).(*model.User)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
var r1 *model.AppError
|
||||
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
|
||||
r1 = rf(email)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*model.AppError)
|
||||
}
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetByUsername provides a mock function with given fields: username
|
||||
@@ -884,17 +902,24 @@ func (_m *UserStore) UpdateUpdateAt(userId string) store.StoreChannel {
|
||||
}
|
||||
|
||||
// VerifyEmail provides a mock function with given fields: userId, email
|
||||
func (_m *UserStore) VerifyEmail(userId string, email string) store.StoreChannel {
|
||||
func (_m *UserStore) VerifyEmail(userId string, email string) (string, *model.AppError) {
|
||||
ret := _m.Called(userId, email)
|
||||
|
||||
var r0 store.StoreChannel
|
||||
if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok {
|
||||
var r0 string
|
||||
if rf, ok := ret.Get(0).(func(string, string) string); ok {
|
||||
r0 = rf(userId, email)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.StoreChannel)
|
||||
r0 = ret.Get(0).(string)
|
||||
}
|
||||
|
||||
var r1 *model.AppError
|
||||
if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok {
|
||||
r1 = rf(userId, email)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*model.AppError)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
@@ -1345,33 +1345,33 @@ func testUserStoreGetByEmail(t *testing.T, ss store.Store) {
|
||||
defer func() { store.Must(ss.Bot().PermanentDelete(u3.Id)) }()
|
||||
|
||||
t.Run("get u1 by email", func(t *testing.T) {
|
||||
result := <-ss.User().GetByEmail(u1.Email)
|
||||
require.Nil(t, result.Err)
|
||||
assert.Equal(t, u1, result.Data.(*model.User))
|
||||
u, err := ss.User().GetByEmail(u1.Email)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, u1, u)
|
||||
})
|
||||
|
||||
t.Run("get u2 by email", func(t *testing.T) {
|
||||
result := <-ss.User().GetByEmail(u2.Email)
|
||||
require.Nil(t, result.Err)
|
||||
assert.Equal(t, u2, result.Data.(*model.User))
|
||||
u, err := ss.User().GetByEmail(u2.Email)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, u2, u)
|
||||
})
|
||||
|
||||
t.Run("get u3 by email", func(t *testing.T) {
|
||||
result := <-ss.User().GetByEmail(u3.Email)
|
||||
require.Nil(t, result.Err)
|
||||
assert.Equal(t, u3, result.Data.(*model.User))
|
||||
u, err := ss.User().GetByEmail(u3.Email)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, u3, u)
|
||||
})
|
||||
|
||||
t.Run("get by empty email", func(t *testing.T) {
|
||||
result := <-ss.User().GetByEmail("")
|
||||
require.NotNil(t, result.Err)
|
||||
require.Equal(t, result.Err.Id, store.MISSING_ACCOUNT_ERROR)
|
||||
_, err := ss.User().GetByEmail("")
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, err.Id, store.MISSING_ACCOUNT_ERROR)
|
||||
})
|
||||
|
||||
t.Run("get by unknown", func(t *testing.T) {
|
||||
result := <-ss.User().GetByEmail("unknown")
|
||||
require.NotNil(t, result.Err)
|
||||
require.Equal(t, result.Err.Id, store.MISSING_ACCOUNT_ERROR)
|
||||
_, err := ss.User().GetByEmail("unknown")
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, err.Id, store.MISSING_ACCOUNT_ERROR)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1413,35 +1413,35 @@ func testUserStoreGetByAuthData(t *testing.T, ss store.Store) {
|
||||
defer func() { store.Must(ss.Bot().PermanentDelete(u3.Id)) }()
|
||||
|
||||
t.Run("get by u1 auth", func(t *testing.T) {
|
||||
result := <-ss.User().GetByAuth(u1.AuthData, u1.AuthService)
|
||||
require.Nil(t, result.Err)
|
||||
assert.Equal(t, u1, result.Data.(*model.User))
|
||||
u, err := ss.User().GetByAuth(u1.AuthData, u1.AuthService)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, u1, u)
|
||||
})
|
||||
|
||||
t.Run("get by u3 auth", func(t *testing.T) {
|
||||
result := <-ss.User().GetByAuth(u3.AuthData, u3.AuthService)
|
||||
require.Nil(t, result.Err)
|
||||
assert.Equal(t, u3, result.Data.(*model.User))
|
||||
u, err := ss.User().GetByAuth(u3.AuthData, u3.AuthService)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, u3, u)
|
||||
})
|
||||
|
||||
t.Run("get by u1 auth, unknown service", func(t *testing.T) {
|
||||
result := <-ss.User().GetByAuth(u1.AuthData, "unknown")
|
||||
require.NotNil(t, result.Err)
|
||||
require.Equal(t, result.Err.Id, store.MISSING_AUTH_ACCOUNT_ERROR)
|
||||
_, err := ss.User().GetByAuth(u1.AuthData, "unknown")
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, err.Id, store.MISSING_AUTH_ACCOUNT_ERROR)
|
||||
})
|
||||
|
||||
t.Run("get by unknown auth, u1 service", func(t *testing.T) {
|
||||
unknownAuth := ""
|
||||
result := <-ss.User().GetByAuth(&unknownAuth, u1.AuthService)
|
||||
require.NotNil(t, result.Err)
|
||||
require.Equal(t, result.Err.Id, store.MISSING_AUTH_ACCOUNT_ERROR)
|
||||
_, err := ss.User().GetByAuth(&unknownAuth, u1.AuthService)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, err.Id, store.MISSING_AUTH_ACCOUNT_ERROR)
|
||||
})
|
||||
|
||||
t.Run("get by unknown auth, unknown service", func(t *testing.T) {
|
||||
unknownAuth := ""
|
||||
result := <-ss.User().GetByAuth(&unknownAuth, "unknown")
|
||||
require.NotNil(t, result.Err)
|
||||
require.Equal(t, result.Err.Id, store.MISSING_AUTH_ACCOUNT_ERROR)
|
||||
_, err := ss.User().GetByAuth(&unknownAuth, "unknown")
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, err.Id, store.MISSING_AUTH_ACCOUNT_ERROR)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1605,10 +1605,9 @@ func testUserStoreUpdatePassword(t *testing.T, ss store.Store) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if r1 := <-ss.User().GetByEmail(u1.Email); r1.Err != nil {
|
||||
t.Fatal(r1.Err)
|
||||
if user, err := ss.User().GetByEmail(u1.Email); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
user := r1.Data.(*model.User)
|
||||
if user.Password != hashedPassword {
|
||||
t.Fatal("Password was not updated correctly")
|
||||
}
|
||||
@@ -1643,10 +1642,9 @@ func testUserStoreUpdateAuthData(t *testing.T, ss store.Store) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if r1 := <-ss.User().GetByEmail(u1.Email); r1.Err != nil {
|
||||
t.Fatal(r1.Err)
|
||||
if user, err := ss.User().GetByEmail(u1.Email); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
user := r1.Data.(*model.User)
|
||||
if user.AuthService != service {
|
||||
t.Fatal("AuthService was not updated correctly")
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user