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 удалений

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

@@ -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
}