Migrate User Store methods related to enterprise to sync by default (#11332)

Этот коммит содержится в:
Jesús Espino
2019-06-26 10:41:45 +02:00
коммит произвёл GitHub
родитель 7e918e38bc
Коммит 6df57d7a83
14 изменённых файлов: 316 добавлений и 340 удалений

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

@@ -1111,9 +1111,9 @@ func TestOAuthComplete(t *testing.T) {
closeBody(r)
}
if result := <-th.App.Srv.Store.User().UpdateAuthData(
th.BasicUser.Id, model.SERVICE_GITLAB, &th.BasicUser.Email, th.BasicUser.Email, true); result.Err != nil {
t.Fatal(result.Err)
if _, err := th.App.Srv.Store.User().UpdateAuthData(
th.BasicUser.Id, model.SERVICE_GITLAB, &th.BasicUser.Email, th.BasicUser.Email, true); err != nil {
t.Fatal(err)
}
redirect, resp = Client.AuthorizeOAuthApp(authRequest)

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

@@ -1192,7 +1192,7 @@ func TestGetTotalUsersStat(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
total := <-th.Server.Store.User().Count(model.UserCountOptions{
total, _ := th.Server.Store.User().Count(model.UserCountOptions{
IncludeDeleted: false,
IncludeBotAccounts: true,
})
@@ -1200,7 +1200,7 @@ func TestGetTotalUsersStat(t *testing.T) {
rstats, resp := th.Client.GetTotalUsersStats("")
CheckNoError(t, resp)
if rstats.TotalUsersCount != total.Data.(int64) {
if rstats.TotalUsersCount != total {
t.Fatal("wrong count")
}
}
@@ -1617,8 +1617,8 @@ func TestUpdateUserActive(t *testing.T) {
CheckNoError(t, resp)
authData := model.NewId()
result := <-th.App.Srv.Store.User().UpdateAuthData(user.Id, "random", &authData, "", true)
require.Nil(t, result.Err)
_, err := th.App.Srv.Store.User().UpdateAuthData(user.Id, "random", &authData, "", true)
require.Nil(t, err)
_, resp = th.SystemAdminClient.UpdateUserActive(user.Id, false)
CheckNoError(t, resp)
@@ -2301,8 +2301,8 @@ func TestResetPassword(t *testing.T) {
_, resp = th.Client.ResetPassword(recoveryToken.Token, "newpwd")
CheckBadRequestStatus(t, resp)
authData := model.NewId()
if result := <-th.App.Srv.Store.User().UpdateAuthData(user.Id, "random", &authData, "", true); result.Err != nil {
t.Fatal(result.Err)
if _, err := th.App.Srv.Store.User().UpdateAuthData(user.Id, "random", &authData, "", true); err != nil {
t.Fatal(err)
}
_, resp = th.Client.SendPasswordResetEmail(user.Email)
CheckBadRequestStatus(t, resp)
@@ -2974,8 +2974,8 @@ func TestSwitchAccount(t *testing.T) {
th.LoginBasic()
fakeAuthData := model.NewId()
if result := <-th.App.Srv.Store.User().UpdateAuthData(th.BasicUser.Id, model.USER_AUTH_SERVICE_GITLAB, &fakeAuthData, th.BasicUser.Email, true); result.Err != nil {
t.Fatal(result.Err)
if _, err := th.App.Srv.Store.User().UpdateAuthData(th.BasicUser.Id, model.USER_AUTH_SERVICE_GITLAB, &fakeAuthData, th.BasicUser.Email, true); err != nil {
t.Fatal(err)
}
sr = &model.SwitchRequest{

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

@@ -19,12 +19,11 @@ const (
func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *model.AppError) {
skipIntensiveQueries := false
var systemUserCount int64
r := <-a.Srv.Store.User().Count(model.UserCountOptions{})
if r.Err != nil {
return nil, r.Err
systemUserCount, err := a.Srv.Store.User().Count(model.UserCountOptions{})
if err != nil {
return nil, err
}
systemUserCount = r.Data.(int64)
if systemUserCount > int64(*a.Config().AnalyticsSettings.MaxUsersForStatistics) {
mlog.Debug(fmt.Sprintf("More than %v users on the system, intensive queries skipped", *a.Config().AnalyticsSettings.MaxUsersForStatistics))
skipIntensiveQueries = true
@@ -62,9 +61,12 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
if teamId == "" {
userInactiveChan = a.Srv.Store.User().AnalyticsGetInactiveUsersCount()
} else {
userChan = a.Srv.Store.User().Count(model.UserCountOptions{
TeamId: teamId,
})
userChan := make(chan store.StoreResult, 1)
go func() {
count, err := a.Srv.Store.User().Count(model.UserCountOptions{TeamId: teamId})
userChan <- store.StoreResult{Data: count, Err: err}
close(userChan)
}()
}
var postChan store.StoreChannel

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

@@ -149,17 +149,12 @@ func (a *App) trackActivity() {
activeUsersMonthlyCount = r.Data.(int64)
}
if ucr := <-a.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
}); ucr.Err == nil {
userCount = ucr.Data.(int64)
if count, err := a.Srv.Store.User().Count(model.UserCountOptions{IncludeDeleted: true}); err == nil {
userCount = count
}
if bc := <-a.Srv.Store.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
ExcludeRegularUsers: true,
}); bc.Err == nil {
botAccountsCount = bc.Data.(int64)
if count, err := a.Srv.Store.User().Count(model.UserCountOptions{IncludeBotAccounts: true, ExcludeRegularUsers: true}); err == nil {
botAccountsCount = count
}
if iucr := <-a.Srv.Store.User().AnalyticsGetInactiveUsersCount(); iucr.Err == nil {

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

@@ -498,8 +498,8 @@ func (a *App) ImportUser(data *UserImportData, dryRun bool) *model.AppError {
}
} else {
if hasUserAuthDataChanged {
if res := <-a.Srv.Store.User().UpdateAuthData(user.Id, authService, authData, user.Email, false); res.Err != nil {
return res.Err
if _, err = a.Srv.Store.User().UpdateAuthData(user.Id, authService, authData, user.Email, false); err != nil {
return err
}
}
}

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

@@ -789,76 +789,60 @@ func TestImportImportUser(t *testing.T) {
defer th.TearDown()
// Check how many users are in the database.
var userCount int64
if r := <-th.App.Srv.Store.User().Count(model.UserCountOptions{
userCount, err := th.App.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
IncludeBotAccounts: false,
}); r.Err == nil {
userCount = r.Data.(int64)
} else {
t.Fatalf("Failed to get user count.")
}
})
require.Nil(t, err, "Failed to get user count.")
// Do an invalid user in dry-run mode.
data := UserImportData{
Username: ptrStr(model.NewId()),
}
if err := th.App.ImportUser(&data, true); err == nil {
if err = th.App.ImportUser(&data, true); err == nil {
t.Fatalf("Should have failed to import invalid user.")
}
// Check that no more users are in the DB.
if r := <-th.App.Srv.Store.User().Count(model.UserCountOptions{
userCount2, err := th.App.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
IncludeBotAccounts: false,
}); r.Err == nil {
if r.Data.(int64) != userCount {
t.Fatalf("Unexpected number of users")
}
} else {
t.Fatalf("Failed to get user count.")
}
})
require.Nil(t, err, "Failed to get user count.")
assert.Equal(t, userCount, userCount2, "Unexpected number of users")
// Do a valid user in dry-run mode.
data = UserImportData{
Username: ptrStr(model.NewId()),
Email: ptrStr(model.NewId() + "@example.com"),
}
if err := th.App.ImportUser(&data, true); err != nil {
if err = th.App.ImportUser(&data, true); err != nil {
t.Fatalf("Should have succeeded to import valid user.")
}
// Check that no more users are in the DB.
if r := <-th.App.Srv.Store.User().Count(model.UserCountOptions{
userCount3, err := th.App.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
IncludeBotAccounts: false,
}); r.Err == nil {
if r.Data.(int64) != userCount {
t.Fatalf("Unexpected number of users")
}
} else {
t.Fatalf("Failed to get user count.")
}
})
require.Nil(t, err, "Failed to get user count.")
assert.Equal(t, userCount, userCount3, "Unexpected number of users")
// Do an invalid user in apply mode.
data = UserImportData{
Username: ptrStr(model.NewId()),
}
if err := th.App.ImportUser(&data, false); err == nil {
if err = th.App.ImportUser(&data, false); err == nil {
t.Fatalf("Should have failed to import invalid user.")
}
// Check that no more users are in the DB.
if r := <-th.App.Srv.Store.User().Count(model.UserCountOptions{
userCount4, err := th.App.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
IncludeBotAccounts: false,
}); r.Err == nil {
if r.Data.(int64) != userCount {
t.Fatalf("Unexpected number of users")
}
} else {
t.Fatalf("Failed to get user count.")
}
})
require.Nil(t, err, "Failed to get user count.")
assert.Equal(t, userCount, userCount4, "Unexpected number of users")
// Do a valid user in apply mode.
username := model.NewId()
@@ -872,24 +856,20 @@ func TestImportImportUser(t *testing.T) {
LastName: ptrStr(model.NewId()),
Position: ptrStr(model.NewId()),
}
if err := th.App.ImportUser(&data, false); err != nil {
if err = th.App.ImportUser(&data, false); err != nil {
t.Fatalf("Should have succeeded to import valid user.")
}
// Check that one more user is in the DB.
if r := <-th.App.Srv.Store.User().Count(model.UserCountOptions{
userCount5, err := th.App.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
IncludeBotAccounts: false,
}); r.Err == nil {
if r.Data.(int64) != userCount+1 {
t.Fatalf("Unexpected number of users")
}
} else {
t.Fatalf("Failed to get user count.")
}
})
require.Nil(t, err, "Failed to get user count.")
assert.Equal(t, userCount+1, userCount5, "Unexpected number of users")
// Get the user and check all the fields are correct.
if user, err := th.App.GetUserByUsername(username); err != nil {
if user, err2 := th.App.GetUserByUsername(username); err2 != nil {
t.Fatalf("Failed to get user from database.")
} else {
if user.Email != *data.Email || user.Nickname != *data.Nickname || user.FirstName != *data.FirstName || user.LastName != *data.LastName || user.Position != *data.Position {
@@ -932,24 +912,20 @@ func TestImportImportUser(t *testing.T) {
data.Position = ptrStr(model.NewId())
data.Roles = ptrStr("system_admin system_user")
data.Locale = ptrStr("zh_CN")
if err := th.App.ImportUser(&data, false); err != nil {
if err = th.App.ImportUser(&data, false); err != nil {
t.Fatalf("Should have succeeded to update valid user %v", err)
}
// Check user count the same.
if r := <-th.App.Srv.Store.User().Count(model.UserCountOptions{
userCount6, err := th.App.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
IncludeBotAccounts: false,
}); r.Err == nil {
if r.Data.(int64) != userCount+1 {
t.Fatalf("Unexpected number of users")
}
} else {
t.Fatalf("Failed to get user count.")
}
})
require.Nil(t, err, "Failed to get user count.")
assert.Equal(t, userCount+1, userCount6, "Unexpected number of users")
// Get the user and check all the fields are correct.
if user, err := th.App.GetUserByUsername(username); err != nil {
if user, err2 := th.App.GetUserByUsername(username); err2 != nil {
t.Fatalf("Failed to get user from database.")
} else {
if user.Email != *data.Email || user.Nickname != *data.Nickname || user.FirstName != *data.FirstName || user.LastName != *data.LastName || user.Position != *data.Position {
@@ -983,22 +959,22 @@ func TestImportImportUser(t *testing.T) {
// Check Password and AuthData together.
data.Password = ptrStr("PasswordTest")
if err := th.App.ImportUser(&data, false); err == nil {
if err = th.App.ImportUser(&data, false); err == nil {
t.Fatalf("Should have failed to import invalid user.")
}
data.AuthData = nil
if err := th.App.ImportUser(&data, false); err != nil {
if err = th.App.ImportUser(&data, false); err != nil {
t.Fatalf("Should have succeeded to update valid user %v", err)
}
data.Password = ptrStr("")
if err := th.App.ImportUser(&data, false); err == nil {
if err = th.App.ImportUser(&data, false); err == nil {
t.Fatalf("Should have failed to import invalid user.")
}
data.Password = ptrStr(strings.Repeat("0123456789", 10))
if err := th.App.ImportUser(&data, false); err == nil {
if err = th.App.ImportUser(&data, false); err == nil {
t.Fatalf("Should have failed to import invalid user.")
}

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

@@ -53,11 +53,10 @@ func (a *App) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError)
}
license := model.LicenseFromJson(strings.NewReader(licenseStr))
result := <-a.Srv.Store.User().Count(model.UserCountOptions{})
if result.Err != nil {
return nil, model.NewAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error(), http.StatusBadRequest)
uniqueUserCount, err := a.Srv.Store.User().Count(model.UserCountOptions{})
if err != nil {
return nil, model.NewAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, err.Error(), http.StatusBadRequest)
}
uniqueUserCount := result.Data.(int64)
if uniqueUserCount > int64(*license.Features.Users) {
return nil, model.NewAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "", http.StatusBadRequest)
@@ -75,7 +74,7 @@ func (a *App) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError)
record.Id = license.Id
record.Bytes = string(licenseBytes)
_, err := a.Srv.Store.License().Save(record)
_, err = a.Srv.Store.License().Save(record)
if err != nil {
a.RemoveLicense()
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)

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

@@ -577,8 +577,8 @@ func (a *App) CompleteSwitchWithOAuth(service string, userData io.Reader, email
return nil, err
}
if result := <-a.Srv.Store.User().UpdateAuthData(user.Id, service, &authData, ssoEmail, true); result.Err != nil {
return nil, result.Err
if _, err = a.Srv.Store.User().UpdateAuthData(user.Id, service, &authData, ssoEmail, true); err != nil {
return nil, err
}
a.Srv.Go(func() {

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

@@ -69,10 +69,8 @@ func (s *Server) DoSecurityUpdateCheck() {
s.Store.System().Update(systemSecurityLastTime)
}
if ucr := <-s.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
}); ucr.Err == nil {
v.Set(PROP_SECURITY_USER_COUNT, strconv.FormatInt(ucr.Data.(int64), 10))
if count, err := s.Store.User().Count(model.UserCountOptions{IncludeDeleted: true}); err == nil {
v.Set(PROP_SECURITY_USER_COUNT, strconv.FormatInt(count, 10))
}
if ucr, err := s.Store.Status().GetTotalActiveUsersCount(); err == nil {

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

@@ -173,14 +173,12 @@ func (a *App) IsUserSignUpAllowed() *model.AppError {
func (a *App) IsFirstUserAccount() bool {
if a.SessionCacheLength() == 0 {
cr := <-a.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
})
if cr.Err != nil {
mlog.Error(fmt.Sprint(cr.Err))
count, err := a.Srv.Store.User().Count(model.UserCountOptions{IncludeDeleted: true})
if err != nil {
mlog.Error(fmt.Sprint(err))
return false
}
if cr.Data.(int64) <= 0 {
if count <= 0 {
return true
}
}
@@ -246,13 +244,11 @@ func (a *App) createUserOrGuest(user *model.User, guest bool) (*model.User, *mod
// Below is a special case where the first user in the entire
// system is granted the system_admin role
result := <-a.Srv.Store.User().Count(model.UserCountOptions{
IncludeDeleted: true,
})
if result.Err != nil {
return nil, result.Err
count, err := a.Srv.Store.User().Count(model.UserCountOptions{IncludeDeleted: true})
if err != nil {
return nil, err
}
if result.Data.(int64) <= 0 {
if count <= 0 {
user.Roles = model.SYSTEM_ADMIN_ROLE_ID + " " + model.SYSTEM_USER_ROLE_ID
}
@@ -1088,8 +1084,8 @@ func (a *App) UpdateUserAuth(userId string, userAuth *model.UserAuth) (*model.Us
} else {
userAuth.Password = ""
if result := <-a.Srv.Store.User().UpdateAuthData(userId, userAuth.AuthService, userAuth.AuthData, "", false); result.Err != nil {
return nil, result.Err
if _, err := a.Srv.Store.User().UpdateAuthData(userId, userAuth.AuthService, userAuth.AuthData, "", false); err != nil {
return nil, err
}
}
@@ -1639,15 +1635,15 @@ func (a *App) GetVerifyEmailToken(token string) (*model.Token, *model.AppError)
// GetTotalUsersStats is used for the DM list total
func (a *App) GetTotalUsersStats(viewRestrictions *model.ViewUsersRestrictions) (*model.UsersStats, *model.AppError) {
result := <-a.Srv.Store.User().Count(model.UserCountOptions{
count, err := a.Srv.Store.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
ViewRestrictions: viewRestrictions,
})
if result.Err != nil {
return nil, result.Err
if err != nil {
return nil, err
}
stats := &model.UsersStats{
TotalUsersCount: result.Data.(int64),
TotalUsersCount: count,
}
return stats, nil
}

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

@@ -268,13 +268,12 @@ func (us SqlUserStore) UpdateFailedPasswordAttempts(userId string, attempts int)
})
}
func (us SqlUserStore) UpdateAuthData(userId string, service string, authData *string, email string, resetMfa bool) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
email = strings.ToLower(email)
func (us SqlUserStore) UpdateAuthData(userId string, service string, authData *string, email string, resetMfa bool) (string, *model.AppError) {
email = strings.ToLower(email)
updateAt := model.GetMillis()
updateAt := model.GetMillis()
query := `
query := `
UPDATE
Users
SET
@@ -285,26 +284,23 @@ func (us SqlUserStore) UpdateAuthData(userId string, service string, authData *s
AuthService = :AuthService,
AuthData = :AuthData`
if len(email) != 0 {
query += ", Email = :Email"
}
if len(email) != 0 {
query += ", Email = :Email"
}
if resetMfa {
query += ", MfaActive = false, MfaSecret = ''"
}
if resetMfa {
query += ", MfaActive = false, MfaSecret = ''"
}
query += " WHERE Id = :UserId"
query += " WHERE Id = :UserId"
if _, err := us.GetMaster().Exec(query, map[string]interface{}{"LastPasswordUpdate": updateAt, "UpdateAt": updateAt, "UserId": userId, "AuthService": service, "AuthData": authData, "Email": email}); err != nil {
if IsUniqueConstraintError(err, []string{"Email", "users_email_key", "idx_users_email_unique", "AuthData", "users_authdata_key"}) {
result.Err = model.NewAppError("SqlUserStore.UpdateAuthData", "store.sql_user.update_auth_data.email_exists.app_error", map[string]interface{}{"Service": service, "Email": email}, "user_id="+userId+", "+err.Error(), http.StatusBadRequest)
} else {
result.Err = model.NewAppError("SqlUserStore.UpdateAuthData", "store.sql_user.update_auth_data.app_error", nil, "id="+userId+", "+err.Error(), http.StatusInternalServerError)
}
} else {
result.Data = userId
if _, err := us.GetMaster().Exec(query, map[string]interface{}{"LastPasswordUpdate": updateAt, "UpdateAt": updateAt, "UserId": userId, "AuthService": service, "AuthData": authData, "Email": email}); err != nil {
if IsUniqueConstraintError(err, []string{"Email", "users_email_key", "idx_users_email_unique", "AuthData", "users_authdata_key"}) {
return "", model.NewAppError("SqlUserStore.UpdateAuthData", "store.sql_user.update_auth_data.email_exists.app_error", map[string]interface{}{"Service": service, "Email": email}, "user_id="+userId+", "+err.Error(), http.StatusBadRequest)
}
})
return "", model.NewAppError("SqlUserStore.UpdateAuthData", "store.sql_user.update_auth_data.app_error", nil, "id="+userId+", "+err.Error(), http.StatusInternalServerError)
}
return userId, nil
}
func (us SqlUserStore) UpdateMfaSecret(userId, secret string) store.StoreChannel {
@@ -1045,26 +1041,22 @@ func (us SqlUserStore) GetByAuth(authData *string, authService string) (*model.U
return &user, nil
}
func (us SqlUserStore) GetAllUsingAuthService(authService string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
query := us.usersQuery.
Where("u.AuthService = ?", authService).
OrderBy("u.Username ASC")
func (us SqlUserStore) GetAllUsingAuthService(authService string) ([]*model.User, *model.AppError) {
query := us.usersQuery.
Where("u.AuthService = ?", authService).
OrderBy("u.Username ASC")
queryString, args, err := query.ToSql()
if err != nil {
result.Err = model.NewAppError("SqlUserStore.GetAllUsingAuthService", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
return
}
queryString, args, err := query.ToSql()
if err != nil {
return nil, model.NewAppError("SqlUserStore.GetAllUsingAuthService", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
}
var data []*model.User
if _, err := us.GetReplica().Select(&data, queryString, args...); err != nil {
result.Err = model.NewAppError("SqlUserStore.GetAllUsingAuthService", "store.sql_user.get_by_auth.other.app_error", nil, "authService="+authService+", "+err.Error(), http.StatusInternalServerError)
return
}
var users []*model.User
if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil {
return nil, model.NewAppError("SqlUserStore.GetAllUsingAuthService", "store.sql_user.get_by_auth.other.app_error", nil, "authService="+authService+", "+err.Error(), http.StatusInternalServerError)
}
result.Data = data
})
return users, nil
}
func (us SqlUserStore) GetByUsername(username string) store.StoreChannel {
@@ -1144,48 +1136,44 @@ func (us SqlUserStore) PermanentDelete(userId string) *model.AppError {
return nil
}
func (us SqlUserStore) Count(options model.UserCountOptions) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
query := sq.Select("COUNT(DISTINCT u.Id)").From("Users AS u")
func (us SqlUserStore) Count(options model.UserCountOptions) (int64, *model.AppError) {
query := sq.Select("COUNT(DISTINCT u.Id)").From("Users AS u")
if !options.IncludeDeleted {
query = query.Where("u.DeleteAt = 0")
}
if !options.IncludeDeleted {
query = query.Where("u.DeleteAt = 0")
}
if options.IncludeBotAccounts {
if options.ExcludeRegularUsers {
query = query.Join("Bots ON u.Id = Bots.UserId")
}
} else {
query = query.LeftJoin("Bots ON u.Id = Bots.UserId").Where("Bots.UserId IS NULL")
if options.ExcludeRegularUsers {
// Currenty this doesn't make sense because it will always return 0
result.Err = model.NewAppError("SqlUserStore.Count", "store.sql_user.count.app_error", nil, "", http.StatusInternalServerError)
return
}
if options.IncludeBotAccounts {
if options.ExcludeRegularUsers {
query = query.Join("Bots ON u.Id = Bots.UserId")
}
} else {
query = query.LeftJoin("Bots ON u.Id = Bots.UserId").Where("Bots.UserId IS NULL")
if options.ExcludeRegularUsers {
// Currenty this doesn't make sense because it will always return 0
return int64(0), model.NewAppError("SqlUserStore.Count", "store.sql_user.count.app_error", nil, "", http.StatusInternalServerError)
}
}
if options.TeamId != "" {
query = query.LeftJoin("TeamMembers AS tm ON u.Id = tm.UserId").Where("tm.TeamId = ? AND tm.DeleteAt = 0", options.TeamId)
}
query = applyViewRestrictionsFilter(query, options.ViewRestrictions, false)
if options.TeamId != "" {
query = query.LeftJoin("TeamMembers AS tm ON u.Id = tm.UserId").Where("tm.TeamId = ? AND tm.DeleteAt = 0", options.TeamId)
}
query = applyViewRestrictionsFilter(query, options.ViewRestrictions, false)
if us.DriverName() == model.DATABASE_DRIVER_POSTGRES {
query = query.PlaceholderFormat(sq.Dollar)
}
if us.DriverName() == model.DATABASE_DRIVER_POSTGRES {
query = query.PlaceholderFormat(sq.Dollar)
}
queryString, args, err := query.ToSql()
if err != nil {
result.Err = model.NewAppError("SqlUserStore.Get", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
return
}
queryString, args, err := query.ToSql()
if err != nil {
return int64(0), model.NewAppError("SqlUserStore.Get", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
}
if count, err := us.GetReplica().SelectInt(queryString, args...); err != nil {
result.Err = model.NewAppError("SqlUserStore.Count", "store.sql_user.get_total_users_count.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = count
}
})
count, err := us.GetReplica().SelectInt(queryString, args...)
if err != nil {
return int64(0), model.NewAppError("SqlUserStore.Count", "store.sql_user.get_total_users_count.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return count, nil
}
func (us SqlUserStore) AnalyticsActiveCount(timePeriod int64) store.StoreChannel {
@@ -1570,30 +1558,28 @@ func (us SqlUserStore) InferSystemInstallDate() store.StoreChannel {
})
}
func (us SqlUserStore) GetUsersBatchForIndexing(startTime, endTime int64, limit int) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
var users []*model.User
usersQuery, args, _ := us.usersQuery.
Where(sq.GtOrEq{"u.CreateAt": startTime}).
Where(sq.Lt{"u.CreateAt": endTime}).
OrderBy("u.CreateAt").
Limit(uint64(limit)).
ToSql()
_, err1 := us.GetSearchReplica().Select(&users, usersQuery, args...)
func (us SqlUserStore) GetUsersBatchForIndexing(startTime, endTime int64, limit int) ([]*model.UserForIndexing, *model.AppError) {
var users []*model.User
usersQuery, args, _ := us.usersQuery.
Where(sq.GtOrEq{"u.CreateAt": startTime}).
Where(sq.Lt{"u.CreateAt": endTime}).
OrderBy("u.CreateAt").
Limit(uint64(limit)).
ToSql()
_, err1 := us.GetSearchReplica().Select(&users, usersQuery, args...)
if err1 != nil {
result.Err = model.NewAppError("SqlUserStore.GetUsersBatchForIndexing", "store.sql_user.get_users_batch_for_indexing.get_users.app_error", nil, err1.Error(), http.StatusInternalServerError)
return
}
if err1 != nil {
return nil, model.NewAppError("SqlUserStore.GetUsersBatchForIndexing", "store.sql_user.get_users_batch_for_indexing.get_users.app_error", nil, err1.Error(), http.StatusInternalServerError)
}
userIds := []string{}
for _, user := range users {
userIds = append(userIds, user.Id)
}
userIds := []string{}
for _, user := range users {
userIds = append(userIds, user.Id)
}
var channelMembers []*model.ChannelMember
channelMembersQuery, args, _ := us.getQueryBuilder().
Select(`
var channelMembers []*model.ChannelMember
channelMembersQuery, args, _ := us.getQueryBuilder().
Select(`
cm.ChannelId,
cm.UserId,
cm.Roles,
@@ -1606,66 +1592,63 @@ func (us SqlUserStore) GetUsersBatchForIndexing(startTime, endTime int64, limit
cm.SchemeAdmin,
(cm.SchemeGuest IS NOT NULL AND cm.SchemeGuest) as SchemeGuest
`).
From("ChannelMembers cm").
Join("Channels c ON cm.ChannelId = c.Id").
Where(sq.Eq{"c.Type": "O", "cm.UserId": userIds}).
ToSql()
_, err2 := us.GetSearchReplica().Select(&channelMembers, channelMembersQuery, args...)
From("ChannelMembers cm").
Join("Channels c ON cm.ChannelId = c.Id").
Where(sq.Eq{"c.Type": "O", "cm.UserId": userIds}).
ToSql()
_, err2 := us.GetSearchReplica().Select(&channelMembers, channelMembersQuery, args...)
if err2 != nil {
result.Err = model.NewAppError("SqlUserStore.GetUsersBatchForIndexing", "store.sql_user.get_users_batch_for_indexing.get_channel_members.app_error", nil, err2.Error(), http.StatusInternalServerError)
return
if err2 != nil {
return nil, model.NewAppError("SqlUserStore.GetUsersBatchForIndexing", "store.sql_user.get_users_batch_for_indexing.get_channel_members.app_error", nil, err2.Error(), http.StatusInternalServerError)
}
var teamMembers []*model.TeamMember
teamMembersQuery, args, _ := us.getQueryBuilder().
Select("TeamId, UserId, Roles, DeleteAt, (SchemeGuest IS NOT NULL AND SchemeGuest) as SchemeGuest, SchemeUser, SchemeAdmin").
From("TeamMembers").
Where(sq.Eq{"UserId": userIds, "DeleteAt": 0}).
ToSql()
_, err3 := us.GetSearchReplica().Select(&teamMembers, teamMembersQuery, args...)
if err3 != nil {
return nil, model.NewAppError("SqlUserStore.GetUsersBatchForIndexing", "store.sql_user.get_users_batch_for_indexing.get_team_members.app_error", nil, err3.Error(), http.StatusInternalServerError)
}
userMap := map[string]*model.UserForIndexing{}
for _, user := range users {
userMap[user.Id] = &model.UserForIndexing{
Id: user.Id,
Username: user.Username,
Nickname: user.Nickname,
FirstName: user.FirstName,
LastName: user.LastName,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
TeamsIds: []string{},
ChannelsIds: []string{},
}
}
var teamMembers []*model.TeamMember
teamMembersQuery, args, _ := us.getQueryBuilder().
Select("TeamId, UserId, Roles, DeleteAt, (SchemeGuest IS NOT NULL AND SchemeGuest) as SchemeGuest, SchemeUser, SchemeAdmin").
From("TeamMembers").
Where(sq.Eq{"UserId": userIds, "DeleteAt": 0}).
ToSql()
_, err3 := us.GetSearchReplica().Select(&teamMembers, teamMembersQuery, args...)
if err3 != nil {
result.Err = model.NewAppError("SqlUserStore.GetUsersBatchForIndexing", "store.sql_user.get_users_batch_for_indexing.get_team_members.app_error", nil, err3.Error(), http.StatusInternalServerError)
return
for _, c := range channelMembers {
if userMap[c.UserId] != nil {
userMap[c.UserId].ChannelsIds = append(userMap[c.UserId].ChannelsIds, c.ChannelId)
}
userMap := map[string]*model.UserForIndexing{}
for _, user := range users {
userMap[user.Id] = &model.UserForIndexing{
Id: user.Id,
Username: user.Username,
Nickname: user.Nickname,
FirstName: user.FirstName,
LastName: user.LastName,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
TeamsIds: []string{},
ChannelsIds: []string{},
}
}
for _, t := range teamMembers {
if userMap[t.UserId] != nil {
userMap[t.UserId].TeamsIds = append(userMap[t.UserId].TeamsIds, t.TeamId)
}
}
for _, c := range channelMembers {
if userMap[c.UserId] != nil {
userMap[c.UserId].ChannelsIds = append(userMap[c.UserId].ChannelsIds, c.ChannelId)
}
}
for _, t := range teamMembers {
if userMap[t.UserId] != nil {
userMap[t.UserId].TeamsIds = append(userMap[t.UserId].TeamsIds, t.TeamId)
}
}
usersForIndexing := []*model.UserForIndexing{}
for _, user := range userMap {
usersForIndexing = append(usersForIndexing, user)
}
sort.Slice(usersForIndexing, func(i, j int) bool {
return usersForIndexing[i].CreateAt < usersForIndexing[j].CreateAt
})
result.Data = usersForIndexing
usersForIndexing := []*model.UserForIndexing{}
for _, user := range userMap {
usersForIndexing = append(usersForIndexing, user)
}
sort.Slice(usersForIndexing, func(i, j int) bool {
return usersForIndexing[i].CreateAt < usersForIndexing[j].CreateAt
})
return usersForIndexing, nil
}
func (us SqlUserStore) GetTeamGroupUsers(teamID string) store.StoreChannel {

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

@@ -252,7 +252,7 @@ type UserStore interface {
ResetLastPictureUpdate(userId string) StoreChannel
UpdateUpdateAt(userId string) StoreChannel
UpdatePassword(userId, newPassword string) StoreChannel
UpdateAuthData(userId string, service string, authData *string, email string, resetMfa bool) StoreChannel
UpdateAuthData(userId string, service string, authData *string, email string, resetMfa bool) (string, *model.AppError)
UpdateMfaSecret(userId, secret string) StoreChannel
UpdateMfaActive(userId string, active bool) StoreChannel
Get(id string) (*model.User, *model.AppError)
@@ -273,7 +273,7 @@ type UserStore interface {
InvalidatProfileCacheForUser(userId string)
GetByEmail(email string) (*model.User, *model.AppError)
GetByAuth(authData *string, authService string) (*model.User, *model.AppError)
GetAllUsingAuthService(authService string) StoreChannel
GetAllUsingAuthService(authService string) ([]*model.User, *model.AppError)
GetByUsername(username string) StoreChannel
GetForLogin(loginId string, allowSignInWithUsername, allowSignInWithEmail bool) StoreChannel
VerifyEmail(userId, email string) (string, *model.AppError)
@@ -300,8 +300,8 @@ type UserStore interface {
ClearAllCustomRoleAssignments() StoreChannel
InferSystemInstallDate() StoreChannel
GetAllAfter(limit int, afterId string) StoreChannel
GetUsersBatchForIndexing(startTime, endTime int64, limit int) StoreChannel
Count(options model.UserCountOptions) StoreChannel
GetUsersBatchForIndexing(startTime, endTime int64, limit int) ([]*model.UserForIndexing, *model.AppError)
Count(options model.UserCountOptions) (int64, *model.AppError)
GetTeamGroupUsers(teamID string) StoreChannel
GetChannelGroupUsers(channelID string) StoreChannel
}

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

@@ -83,19 +83,26 @@ func (_m *UserStore) ClearCaches() {
}
// Count provides a mock function with given fields: options
func (_m *UserStore) Count(options model.UserCountOptions) store.StoreChannel {
func (_m *UserStore) Count(options model.UserCountOptions) (int64, *model.AppError) {
ret := _m.Called(options)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(model.UserCountOptions) store.StoreChannel); ok {
var r0 int64
if rf, ok := ret.Get(0).(func(model.UserCountOptions) int64); ok {
r0 = rf(options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(int64)
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(model.UserCountOptions) *model.AppError); ok {
r1 = rf(options)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0
return r0, r1
}
// Get provides a mock function with given fields: id
@@ -188,19 +195,28 @@ func (_m *UserStore) GetAllProfilesInChannel(channelId string, allowFromCache bo
}
// GetAllUsingAuthService provides a mock function with given fields: authService
func (_m *UserStore) GetAllUsingAuthService(authService string) store.StoreChannel {
func (_m *UserStore) GetAllUsingAuthService(authService string) ([]*model.User, *model.AppError) {
ret := _m.Called(authService)
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(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) *model.AppError); ok {
r1 = rf(authService)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// GetAnyUnreadPostCountForChannel provides a mock function with given fields: userId, channelId
@@ -615,19 +631,28 @@ func (_m *UserStore) GetUnreadCountForChannel(userId string, channelId string) s
}
// GetUsersBatchForIndexing provides a mock function with given fields: startTime, endTime, limit
func (_m *UserStore) GetUsersBatchForIndexing(startTime int64, endTime int64, limit int) store.StoreChannel {
func (_m *UserStore) GetUsersBatchForIndexing(startTime int64, endTime int64, limit int) ([]*model.UserForIndexing, *model.AppError) {
ret := _m.Called(startTime, endTime, limit)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(int64, int64, int) store.StoreChannel); ok {
var r0 []*model.UserForIndexing
if rf, ok := ret.Get(0).(func(int64, int64, int) []*model.UserForIndexing); ok {
r0 = rf(startTime, endTime, limit)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).([]*model.UserForIndexing)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(int64, int64, int) *model.AppError); ok {
r1 = rf(startTime, endTime, limit)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// InferSystemInstallDate provides a mock function with given fields:
@@ -815,19 +840,26 @@ func (_m *UserStore) Update(user *model.User, allowRoleUpdate bool) (*model.User
}
// UpdateAuthData provides a mock function with given fields: userId, service, authData, email, resetMfa
func (_m *UserStore) UpdateAuthData(userId string, service string, authData *string, email string, resetMfa bool) store.StoreChannel {
func (_m *UserStore) UpdateAuthData(userId string, service string, authData *string, email string, resetMfa bool) (string, *model.AppError) {
ret := _m.Called(userId, service, authData, email, resetMfa)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string, string, *string, string, bool) store.StoreChannel); ok {
var r0 string
if rf, ok := ret.Get(0).(func(string, string, *string, string, bool) string); ok {
r0 = rf(userId, service, authData, email, resetMfa)
} 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, *string, string, bool) *model.AppError); ok {
r1 = rf(userId, service, authData, email, resetMfa)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0
return r0, r1
}
// UpdateFailedPasswordAttempts provides a mock function with given fields: userId, attempts

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

@@ -330,21 +330,21 @@ func testGetAllUsingAuthService(t *testing.T, ss store.Store) {
defer func() { require.Nil(t, ss.User().PermanentDelete(u3.Id)) }()
t.Run("get by unknown auth service", func(t *testing.T) {
result := <-ss.User().GetAllUsingAuthService("unknown")
require.Nil(t, result.Err)
assert.Equal(t, []*model.User{}, result.Data.([]*model.User))
users, err := ss.User().GetAllUsingAuthService("unknown")
require.Nil(t, err)
assert.Equal(t, []*model.User{}, users)
})
t.Run("get by auth service", func(t *testing.T) {
result := <-ss.User().GetAllUsingAuthService("service")
require.Nil(t, result.Err)
assert.Equal(t, []*model.User{u1, u2}, result.Data.([]*model.User))
users, err := ss.User().GetAllUsingAuthService("service")
require.Nil(t, err)
assert.Equal(t, []*model.User{u1, u2}, users)
})
t.Run("get by other auth service", func(t *testing.T) {
result := <-ss.User().GetAllUsingAuthService("service2")
require.Nil(t, result.Err)
assert.Equal(t, []*model.User{u3}, result.Data.([]*model.User))
users, err := ss.User().GetAllUsingAuthService("service2")
require.Nil(t, err)
assert.Equal(t, []*model.User{u3}, users)
})
}
@@ -1771,9 +1771,8 @@ func testUserStoreUpdateAuthData(t *testing.T, ss store.Store) {
service := "someservice"
authData := model.NewId()
if err := (<-ss.User().UpdateAuthData(u1.Id, service, &authData, "", true)).Err; err != nil {
t.Fatal(err)
}
_, err := ss.User().UpdateAuthData(u1.Id, service, &authData, "", true)
require.Nil(t, err)
if user, err := ss.User().GetByEmail(u1.Email); err != nil {
t.Fatal(err)
@@ -3131,80 +3130,80 @@ func testCount(t *testing.T, ss store.Store) {
u3.IsBot = true
defer func() { require.Nil(t, ss.Bot().PermanentDelete(u3.Id)) }()
result := <-ss.User().Count(model.UserCountOptions{
count, err := ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: false,
IncludeDeleted: false,
TeamId: "",
})
require.Nil(t, result.Err)
require.Equal(t, int64(1), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(1), count)
result = <-ss.User().Count(model.UserCountOptions{
count, err = ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
IncludeDeleted: false,
TeamId: "",
})
require.Nil(t, result.Err)
require.Equal(t, int64(2), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(2), count)
result = <-ss.User().Count(model.UserCountOptions{
count, err = ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: false,
IncludeDeleted: true,
TeamId: "",
})
require.Nil(t, result.Err)
require.Equal(t, int64(2), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(2), count)
result = <-ss.User().Count(model.UserCountOptions{
count, err = ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
IncludeDeleted: true,
TeamId: "",
})
require.Nil(t, result.Err)
require.Equal(t, int64(3), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(3), count)
result = <-ss.User().Count(model.UserCountOptions{
count, err = ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
IncludeDeleted: true,
ExcludeRegularUsers: true,
TeamId: "",
})
require.Nil(t, result.Err)
require.Equal(t, int64(1), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(1), count)
result = <-ss.User().Count(model.UserCountOptions{
count, err = ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
IncludeDeleted: true,
TeamId: teamId,
})
require.Nil(t, result.Err)
require.Equal(t, int64(1), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(1), count)
result = <-ss.User().Count(model.UserCountOptions{
count, err = ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
IncludeDeleted: true,
TeamId: model.NewId(),
})
require.Nil(t, result.Err)
require.Equal(t, int64(0), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(0), count)
result = <-ss.User().Count(model.UserCountOptions{
count, err = ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
IncludeDeleted: true,
TeamId: teamId,
ViewRestrictions: &model.ViewUsersRestrictions{Teams: []string{teamId}},
})
require.Nil(t, result.Err)
require.Equal(t, int64(1), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(1), count)
result = <-ss.User().Count(model.UserCountOptions{
count, err = ss.User().Count(model.UserCountOptions{
IncludeBotAccounts: true,
IncludeDeleted: true,
TeamId: teamId,
ViewRestrictions: &model.ViewUsersRestrictions{Teams: []string{model.NewId()}},
})
require.Nil(t, result.Err)
require.Equal(t, int64(0), result.Data.(int64))
require.Nil(t, err)
require.Equal(t, int64(0), count)
}
func testUserStoreAnalyticsGetInactiveUsersCount(t *testing.T, ss store.Store) {
@@ -3661,9 +3660,8 @@ func testUserStoreGetUsersBatchForIndexing(t *testing.T, ss store.Store) {
endTime := u3.CreateAt
// First and last user should be outside the range
res1 := <-ss.User().GetUsersBatchForIndexing(startTime, endTime, 100)
assert.Nil(t, res1.Err)
res1List := res1.Data.([]*model.UserForIndexing)
res1List, err := ss.User().GetUsersBatchForIndexing(startTime, endTime, 100)
assert.Nil(t, err)
assert.Len(t, res1List, 1)
assert.Equal(t, res1List[0].Username, u2.Username)
@@ -3672,9 +3670,8 @@ func testUserStoreGetUsersBatchForIndexing(t *testing.T, ss store.Store) {
// Update startTime to include first user
startTime = u1.CreateAt
res2 := <-ss.User().GetUsersBatchForIndexing(startTime, endTime, 100)
assert.Nil(t, res1.Err)
res2List := res2.Data.([]*model.UserForIndexing)
res2List, err := ss.User().GetUsersBatchForIndexing(startTime, endTime, 100)
assert.Nil(t, err)
assert.Len(t, res2List, 2)
assert.Equal(t, res2List[0].Username, u1.Username)
@@ -3684,9 +3681,8 @@ func testUserStoreGetUsersBatchForIndexing(t *testing.T, ss store.Store) {
// Update endTime to include last user
endTime = model.GetMillis()
res3 := <-ss.User().GetUsersBatchForIndexing(startTime, endTime, 100)
assert.Nil(t, res3.Err)
res3List := res3.Data.([]*model.UserForIndexing)
res3List, err := ss.User().GetUsersBatchForIndexing(startTime, endTime, 100)
assert.Nil(t, err)
assert.Len(t, res3List, 3)
assert.Equal(t, res3List[0].Username, u1.Username)
@@ -3696,9 +3692,8 @@ func testUserStoreGetUsersBatchForIndexing(t *testing.T, ss store.Store) {
assert.ElementsMatch(t, res3List[2].ChannelsIds, []string{cPub2.Id})
// Testing the limit
res4 := <-ss.User().GetUsersBatchForIndexing(startTime, endTime, 2)
assert.Nil(t, res4.Err)
res4List := res4.Data.([]*model.UserForIndexing)
res4List, err := ss.User().GetUsersBatchForIndexing(startTime, endTime, 2)
assert.Nil(t, err)
assert.Len(t, res4List, 2)
assert.Equal(t, res4List[0].Username, u1.Username)