SyncStore: Migrate User.GetForLogin method to Sync (#11501)

Этот коммит содержится в:
Phillip Ahereza
2019-07-03 18:12:33 +03:00
коммит произвёл Michael Kochell
родитель 0b89aa2472
Коммит df869e3f86
5 изменённых файлов: 64 добавлений и 62 удалений

Просмотреть файл

@@ -93,8 +93,8 @@ func (a *App) GetUserForLogin(id, loginId string) (*model.User, *model.AppError)
} }
// Try to get the user by username/email // Try to get the user by username/email
if result := <-a.Srv.Store.User().GetForLogin(loginId, enableUsername, enableEmail); result.Err == nil { if user, err := a.Srv.Store.User().GetForLogin(loginId, enableUsername, enableEmail); err == nil {
return result.Data.(*model.User), nil return user, nil
} }
// Try to get the user with LDAP if enabled // Try to get the user with LDAP if enabled

Просмотреть файл

@@ -1074,45 +1074,38 @@ func (us SqlUserStore) GetByUsername(username string) store.StoreChannel {
}) })
} }
func (us SqlUserStore) GetForLogin(loginId string, allowSignInWithUsername, allowSignInWithEmail bool) store.StoreChannel { func (us SqlUserStore) GetForLogin(loginId string, allowSignInWithUsername, allowSignInWithEmail bool) (*model.User, *model.AppError) {
return store.Do(func(result *store.StoreResult) { query := us.usersQuery
query := us.usersQuery if allowSignInWithUsername && allowSignInWithEmail {
query = query.Where("Username = ? OR Email = ?", loginId, loginId)
} else if allowSignInWithUsername {
query = query.Where("Username = ?", loginId)
} else if allowSignInWithEmail {
query = query.Where("Email = ?", loginId)
} else {
return nil, model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.app_error", nil, "", http.StatusInternalServerError)
}
if allowSignInWithUsername && allowSignInWithEmail { queryString, args, err := query.ToSql()
query = query.Where("Username = ? OR Email = ?", loginId, loginId) if err != nil {
} else if allowSignInWithUsername { return nil, model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
query = query.Where("Username = ?", loginId) }
} else if allowSignInWithEmail {
query = query.Where("Email = ?", loginId)
} else {
result.Err = model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.app_error", nil, "", http.StatusInternalServerError)
return
}
queryString, args, err := query.ToSql() users := []*model.User{}
if err != nil { if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil {
result.Err = model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.app_error", nil, err.Error(), http.StatusInternalServerError)
return }
}
users := []*model.User{} if len(users) == 0 {
if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil { return nil, model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.app_error", nil, "", http.StatusInternalServerError)
result.Err = model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.app_error", nil, err.Error(), http.StatusInternalServerError) }
return
}
if len(users) == 0 { if len(users) > 1 {
result.Err = model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.app_error", nil, "", http.StatusInternalServerError) return nil, model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.multiple_users", nil, "", http.StatusInternalServerError)
return }
}
if len(users) > 1 { return users[0], nil
result.Err = model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.multiple_users", nil, "", http.StatusInternalServerError)
return
}
result.Data = users[0]
})
} }
func (us SqlUserStore) VerifyEmail(userId, email string) (string, *model.AppError) { func (us SqlUserStore) VerifyEmail(userId, email string) (string, *model.AppError) {

Просмотреть файл

@@ -275,7 +275,7 @@ type UserStore interface {
GetByAuth(authData *string, authService string) (*model.User, *model.AppError) GetByAuth(authData *string, authService string) (*model.User, *model.AppError)
GetAllUsingAuthService(authService string) ([]*model.User, *model.AppError) GetAllUsingAuthService(authService string) ([]*model.User, *model.AppError)
GetByUsername(username string) StoreChannel GetByUsername(username string) StoreChannel
GetForLogin(loginId string, allowSignInWithUsername, allowSignInWithEmail bool) StoreChannel GetForLogin(loginId string, allowSignInWithUsername, allowSignInWithEmail bool) (*model.User, *model.AppError)
VerifyEmail(userId, email string) (string, *model.AppError) VerifyEmail(userId, email string) (string, *model.AppError)
GetEtagForAllProfiles() StoreChannel GetEtagForAllProfiles() StoreChannel
GetEtagForProfiles(teamId string) StoreChannel GetEtagForProfiles(teamId string) StoreChannel

Просмотреть файл

@@ -412,19 +412,28 @@ func (_m *UserStore) GetEtagForProfilesNotInTeam(teamId string) store.StoreChann
} }
// GetForLogin provides a mock function with given fields: loginId, allowSignInWithUsername, allowSignInWithEmail // GetForLogin provides a mock function with given fields: loginId, allowSignInWithUsername, allowSignInWithEmail
func (_m *UserStore) GetForLogin(loginId string, allowSignInWithUsername bool, allowSignInWithEmail bool) store.StoreChannel { func (_m *UserStore) GetForLogin(loginId string, allowSignInWithUsername bool, allowSignInWithEmail bool) (*model.User, *model.AppError) {
ret := _m.Called(loginId, allowSignInWithUsername, allowSignInWithEmail) ret := _m.Called(loginId, allowSignInWithUsername, allowSignInWithEmail)
var r0 store.StoreChannel var r0 *model.User
if rf, ok := ret.Get(0).(func(string, bool, bool) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, bool, bool) *model.User); ok {
r0 = rf(loginId, allowSignInWithUsername, allowSignInWithEmail) r0 = rf(loginId, allowSignInWithUsername, allowSignInWithEmail)
} else { } else {
if ret.Get(0) != nil { 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, bool, bool) *model.AppError); ok {
r1 = rf(loginId, allowSignInWithUsername, allowSignInWithEmail)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetNewUsersForTeam provides a mock function with given fields: teamId, offset, limit, viewRestrictions // GetNewUsersForTeam provides a mock function with given fields: teamId, offset, limit, viewRestrictions

Просмотреть файл

@@ -1698,45 +1698,45 @@ func testUserStoreGetForLogin(t *testing.T, ss store.Store) {
defer func() { require.Nil(t, ss.Bot().PermanentDelete(u3.Id)) }() defer func() { require.Nil(t, ss.Bot().PermanentDelete(u3.Id)) }()
t.Run("get u1 by username, allow both", func(t *testing.T) { t.Run("get u1 by username, allow both", func(t *testing.T) {
result := <-ss.User().GetForLogin(u1.Username, true, true) user, err := ss.User().GetForLogin(u1.Username, true, true)
require.Nil(t, result.Err) require.Nil(t, err)
assert.Equal(t, u1, result.Data.(*model.User)) assert.Equal(t, u1, user)
}) })
t.Run("get u1 by username, allow only email", func(t *testing.T) { t.Run("get u1 by username, allow only email", func(t *testing.T) {
result := <-ss.User().GetForLogin(u1.Username, false, true) _, err := ss.User().GetForLogin(u1.Username, false, true)
require.NotNil(t, result.Err) require.NotNil(t, err)
require.Equal(t, result.Err.Id, "store.sql_user.get_for_login.app_error") require.Equal(t, err.Id, "store.sql_user.get_for_login.app_error")
}) })
t.Run("get u1 by email, allow both", func(t *testing.T) { t.Run("get u1 by email, allow both", func(t *testing.T) {
result := <-ss.User().GetForLogin(u1.Email, true, true) user, err := ss.User().GetForLogin(u1.Email, true, true)
require.Nil(t, result.Err) require.Nil(t, err)
assert.Equal(t, u1, result.Data.(*model.User)) assert.Equal(t, u1, user)
}) })
t.Run("get u1 by email, allow only username", func(t *testing.T) { t.Run("get u1 by email, allow only username", func(t *testing.T) {
result := <-ss.User().GetForLogin(u1.Email, true, false) _, err := ss.User().GetForLogin(u1.Email, true, false)
require.NotNil(t, result.Err) require.NotNil(t, err)
require.Equal(t, result.Err.Id, "store.sql_user.get_for_login.app_error") require.Equal(t, err.Id, "store.sql_user.get_for_login.app_error")
}) })
t.Run("get u2 by username, allow both", func(t *testing.T) { t.Run("get u2 by username, allow both", func(t *testing.T) {
result := <-ss.User().GetForLogin(u2.Username, true, true) user, err := ss.User().GetForLogin(u2.Username, true, true)
require.Nil(t, result.Err) require.Nil(t, err)
assert.Equal(t, u2, result.Data.(*model.User)) assert.Equal(t, u2, user)
}) })
t.Run("get u2 by email, allow both", func(t *testing.T) { t.Run("get u2 by email, allow both", func(t *testing.T) {
result := <-ss.User().GetForLogin(u2.Email, true, true) user, err := ss.User().GetForLogin(u2.Email, true, true)
require.Nil(t, result.Err) require.Nil(t, err)
assert.Equal(t, u2, result.Data.(*model.User)) assert.Equal(t, u2, user)
}) })
t.Run("get u2 by username, allow neither", func(t *testing.T) { t.Run("get u2 by username, allow neither", func(t *testing.T) {
result := <-ss.User().GetForLogin(u2.Username, false, false) _, err := ss.User().GetForLogin(u2.Username, false, false)
require.NotNil(t, result.Err) require.NotNil(t, err)
require.Equal(t, result.Err.Id, "store.sql_user.get_for_login.app_error") require.Equal(t, err.Id, "store.sql_user.get_for_login.app_error")
}) })
} }