Migrate to idiomatic error handling in app/user.go (#9711)
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
1ee872578c
Коммит
cd4abefecd
389
app/user.go
389
app/user.go
@@ -28,7 +28,6 @@ import (
|
||||
"github.com/mattermost/mattermost-server/einterfaces"
|
||||
"github.com/mattermost/mattermost-server/mlog"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
"github.com/mattermost/mattermost-server/utils"
|
||||
)
|
||||
|
||||
@@ -63,19 +62,17 @@ func (a *App) CreateUserWithToken(user *model.User, tokenId string) (*model.User
|
||||
|
||||
tokenData := model.MapFromJson(strings.NewReader(token.Extra))
|
||||
|
||||
var team *model.Team
|
||||
if result := <-a.Srv.Store.Team().Get(tokenData["teamId"]); result.Err != nil {
|
||||
result = <-a.Srv.Store.Team().Get(tokenData["teamId"])
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
team = result.Data.(*model.Team)
|
||||
}
|
||||
team := result.Data.(*model.Team)
|
||||
|
||||
user.Email = tokenData["email"]
|
||||
user.EmailVerified = true
|
||||
|
||||
var ruser *model.User
|
||||
var err *model.AppError
|
||||
if ruser, err = a.CreateUser(user); err != nil {
|
||||
ruser, err := a.CreateUser(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -97,18 +94,16 @@ func (a *App) CreateUserWithInviteId(user *model.User, inviteId string) (*model.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var team *model.Team
|
||||
if result := <-a.Srv.Store.Team().GetByInviteId(inviteId); result.Err != nil {
|
||||
result := <-a.Srv.Store.Team().GetByInviteId(inviteId)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
team = result.Data.(*model.Team)
|
||||
}
|
||||
team := result.Data.(*model.Team)
|
||||
|
||||
user.EmailVerified = false
|
||||
|
||||
var ruser *model.User
|
||||
var err *model.AppError
|
||||
if ruser, err = a.CreateUser(user); err != nil {
|
||||
ruser, err := a.CreateUser(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -172,14 +167,13 @@ func (a *App) IsUserSignUpAllowed() *model.AppError {
|
||||
|
||||
func (a *App) IsFirstUserAccount() bool {
|
||||
if a.SessionCacheLength() == 0 {
|
||||
if cr := <-a.Srv.Store.User().GetTotalUsersCount(); cr.Err != nil {
|
||||
cr := <-a.Srv.Store.User().GetTotalUsersCount()
|
||||
if cr.Err != nil {
|
||||
mlog.Error(fmt.Sprint(cr.Err))
|
||||
return false
|
||||
} else {
|
||||
count := cr.Data.(int64)
|
||||
if count <= 0 {
|
||||
return true
|
||||
}
|
||||
if cr.Data.(int64) <= 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,29 +189,28 @@ func (a *App) CreateUser(user *model.User) (*model.User, *model.AppError) {
|
||||
|
||||
// Below is a special case where the first user in the entire
|
||||
// system is granted the system_admin role
|
||||
if result := <-a.Srv.Store.User().GetTotalUsersCount(); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetTotalUsersCount()
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
count := result.Data.(int64)
|
||||
if count <= 0 {
|
||||
user.Roles = model.SYSTEM_ADMIN_ROLE_ID + " " + model.SYSTEM_USER_ROLE_ID
|
||||
}
|
||||
if result.Data.(int64) <= 0 {
|
||||
user.Roles = model.SYSTEM_ADMIN_ROLE_ID + " " + model.SYSTEM_USER_ROLE_ID
|
||||
}
|
||||
|
||||
if _, ok := utils.GetSupportedLocales()[user.Locale]; !ok {
|
||||
user.Locale = *a.Config().LocalizationSettings.DefaultClientLocale
|
||||
}
|
||||
|
||||
if ruser, err := a.createUser(user); err != nil {
|
||||
ruser, err := a.createUser(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
}
|
||||
// This message goes to everyone, so the teamId, channelId and userId are irrelevant
|
||||
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_NEW_USER, "", "", "", nil)
|
||||
message.Add("user_id", ruser.Id)
|
||||
a.Publish(message)
|
||||
|
||||
return ruser, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) createUser(user *model.User) (*model.User, *model.AppError) {
|
||||
@@ -227,10 +220,11 @@ func (a *App) createUser(user *model.User) (*model.User, *model.AppError) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result := <-a.Srv.Store.User().Save(user); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().Save(user)
|
||||
if result.Err != nil {
|
||||
mlog.Error(fmt.Sprintf("Couldn't save the user err=%v", result.Err))
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
ruser := result.Data.(*model.User)
|
||||
|
||||
if user.EmailVerified {
|
||||
@@ -246,7 +240,6 @@ func (a *App) createUser(user *model.User) (*model.User, *model.AppError) {
|
||||
|
||||
ruser.Sanitize(map[string]bool{})
|
||||
return ruser, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) CreateOAuthUser(service string, userData io.Reader, teamId string) (*model.User, *model.AppError) {
|
||||
@@ -254,13 +247,11 @@ func (a *App) CreateOAuthUser(service string, userData io.Reader, teamId string)
|
||||
return nil, model.NewAppError("CreateOAuthUser", "api.user.create_user.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
var user *model.User
|
||||
provider := einterfaces.GetOauthProvider(service)
|
||||
if provider == nil {
|
||||
return nil, model.NewAppError("CreateOAuthUser", "api.user.create_oauth_user.not_available.app_error", map[string]interface{}{"Service": strings.Title(service)}, "", http.StatusNotImplemented)
|
||||
} else {
|
||||
user = provider.GetUserFromJson(userData)
|
||||
}
|
||||
user := provider.GetUserFromJson(userData)
|
||||
|
||||
if user == nil {
|
||||
return nil, model.NewAppError("CreateOAuthUser", "api.user.create_oauth_user.create.app_error", map[string]interface{}{"Service": service}, "", http.StatusInternalServerError)
|
||||
@@ -274,7 +265,7 @@ func (a *App) CreateOAuthUser(service string, userData io.Reader, teamId string)
|
||||
for found {
|
||||
if found = a.IsUsernameTaken(user.Username); found {
|
||||
user.Username = user.Username + strconv.Itoa(count)
|
||||
count += 1
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,9 +277,8 @@ func (a *App) CreateOAuthUser(service string, userData io.Reader, teamId string)
|
||||
authService := result.Data.(*model.User).AuthService
|
||||
if authService == "" {
|
||||
return nil, model.NewAppError("CreateOAuthUser", "api.user.create_oauth_user.already_attached.app_error", map[string]interface{}{"Service": service, "Auth": model.USER_AUTH_SERVICE_EMAIL}, "email="+user.Email, http.StatusBadRequest)
|
||||
} else {
|
||||
return nil, model.NewAppError("CreateOAuthUser", "api.user.create_oauth_user.already_attached.app_error", map[string]interface{}{"Service": service, "Auth": authService}, "email="+user.Email, http.StatusBadRequest)
|
||||
}
|
||||
return nil, model.NewAppError("CreateOAuthUser", "api.user.create_oauth_user.already_attached.app_error", map[string]interface{}{"Service": service, "Auth": authService}, "email="+user.Email, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
user.EmailVerified = true
|
||||
@@ -313,7 +303,7 @@ func (a *App) CreateOAuthUser(service string, userData io.Reader, teamId string)
|
||||
return ruser, nil
|
||||
}
|
||||
|
||||
// Check that a user's email domain matches a list of space-delimited domains as a string.
|
||||
// CheckUserDomain checks that a user's email domain matches a list of space-delimited domains as a string.
|
||||
func CheckUserDomain(user *model.User, domains string) bool {
|
||||
if len(domains) == 0 {
|
||||
return true
|
||||
@@ -330,9 +320,8 @@ func CheckUserDomain(user *model.User, domains string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the username is already used by another user. Return false if the username is invalid.
|
||||
// IsUsernameTaken checks if the username is already used by another user. Return false if the username is invalid.
|
||||
func (a *App) IsUsernameTaken(name string) bool {
|
||||
|
||||
if !model.IsValidUsername(name) {
|
||||
return false
|
||||
}
|
||||
@@ -345,49 +334,49 @@ func (a *App) IsUsernameTaken(name string) bool {
|
||||
}
|
||||
|
||||
func (a *App) GetUser(userId string) (*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().Get(userId); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().Get(userId)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUserByUsername(username string) (*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetByUsername(username); result.Err != nil && result.Err.Id == "store.sql_user.get_by_username.app_error" {
|
||||
result := <-a.Srv.Store.User().GetByUsername(username)
|
||||
if result.Err != nil && result.Err.Id == "store.sql_user.get_by_username.app_error" {
|
||||
result.Err.StatusCode = http.StatusNotFound
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUserByEmail(email string) (*model.User, *model.AppError) {
|
||||
|
||||
if result := <-a.Srv.Store.User().GetByEmail(email); result.Err != nil && result.Err.Id == "store.sql_user.missing_account.const" {
|
||||
result := <-a.Srv.Store.User().GetByEmail(email)
|
||||
if result.Err != nil {
|
||||
if result.Err.Id == "store.sql_user.missing_account.const" {
|
||||
result.Err.StatusCode = http.StatusNotFound
|
||||
return nil, result.Err
|
||||
} else if result.Err != nil {
|
||||
}
|
||||
result.Err.StatusCode = http.StatusBadRequest
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUserByAuth(authData *string, authService string) (*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetByAuth(authData, authService); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetByAuth(authData, authService)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsers(offset int, limit int) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetAllProfiles(offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetAllProfiles(offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersMap(offset int, limit int, asAdmin bool) (map[string]*model.User, *model.AppError) {
|
||||
@@ -420,19 +409,19 @@ func (a *App) GetUsersEtag() string {
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInTeam(teamId string, offset int, limit int) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetProfiles(teamId, offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfiles(teamId, offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersNotInTeam(teamId string, offset int, limit int) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetProfilesNotInTeam(teamId, offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfilesNotInTeam(teamId, offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInTeamMap(teamId string, offset int, limit int, asAdmin bool) (map[string]*model.User, *model.AppError) {
|
||||
@@ -478,19 +467,19 @@ func (a *App) GetUsersNotInTeamEtag(teamId string) string {
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInChannel(channelId string, offset int, limit int) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetProfilesInChannel(channelId, offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfilesInChannel(channelId, offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInChannelByStatus(channelId string, offset int, limit int) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetProfilesInChannelByStatus(channelId, offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfilesInChannelByStatus(channelId, offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInChannelMap(channelId string, offset int, limit int, asAdmin bool) (map[string]*model.User, *model.AppError) {
|
||||
@@ -514,7 +503,6 @@ func (a *App) GetUsersInChannelPage(channelId string, page int, perPage int, asA
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return a.sanitizeProfiles(users, asAdmin), nil
|
||||
}
|
||||
|
||||
@@ -523,16 +511,15 @@ func (a *App) GetUsersInChannelPageByStatus(channelId string, page int, perPage
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return a.sanitizeProfiles(users, asAdmin), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersNotInChannel(teamId string, channelId string, offset int, limit int) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetProfilesNotInChannel(teamId, channelId, offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfilesNotInChannel(teamId, channelId, offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersNotInChannelMap(teamId string, channelId string, offset int, limit int, asAdmin bool) (map[string]*model.User, *model.AppError) {
|
||||
@@ -570,29 +557,27 @@ func (a *App) GetUsersWithoutTeamPage(page int, perPage int, asAdmin bool) ([]*m
|
||||
}
|
||||
|
||||
func (a *App) GetUsersWithoutTeam(offset int, limit int) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetProfilesWithoutTeam(offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfilesWithoutTeam(offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
return result.Data.([]*model.User), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersByIds(userIds []string, asAdmin bool) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetProfileByIds(userIds, true); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfileByIds(userIds, true)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
users := result.Data.([]*model.User)
|
||||
return a.sanitizeProfiles(users, asAdmin), nil
|
||||
}
|
||||
return a.sanitizeProfiles(result.Data.([]*model.User), asAdmin), nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersByUsernames(usernames []string, asAdmin bool) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().GetProfilesByUsernames(usernames, ""); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfilesByUsernames(usernames, "")
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
users := result.Data.([]*model.User)
|
||||
return a.sanitizeProfiles(users, asAdmin), nil
|
||||
}
|
||||
return a.sanitizeProfiles(result.Data.([]*model.User), asAdmin), nil
|
||||
}
|
||||
|
||||
func (a *App) sanitizeProfiles(users []*model.User, asAdmin bool) []*model.User {
|
||||
@@ -608,9 +593,8 @@ func (a *App) GenerateMfaSecret(userId string) (*model.MfaSecret, *model.AppErro
|
||||
return nil, model.NewAppError("generateMfaSecret", "api.user.generate_mfa_qr.not_available.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
var user *model.User
|
||||
var err *model.AppError
|
||||
if user, err = a.GetUser(userId); err != nil {
|
||||
user, err := a.GetUser(userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -629,12 +613,11 @@ func (a *App) ActivateMfa(userId, token string) *model.AppError {
|
||||
return err
|
||||
}
|
||||
|
||||
var user *model.User
|
||||
if result := <-a.Srv.Store.User().Get(userId); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().Get(userId)
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
} else {
|
||||
user = result.Data.(*model.User)
|
||||
}
|
||||
user := result.Data.(*model.User)
|
||||
|
||||
if len(user.AuthService) > 0 && user.AuthService != model.USER_AUTH_SERVICE_LDAP {
|
||||
return model.NewAppError("ActivateMfa", "api.user.activate_mfa.email_and_ldap_only.app_error", nil, "", http.StatusBadRequest)
|
||||
@@ -724,9 +707,8 @@ func CreateProfileImage(username string, userId string, initialFont string) ([]b
|
||||
|
||||
if imgErr := png.Encode(buf, dstImg); imgErr != nil {
|
||||
return nil, model.NewAppError("CreateProfileImage", "api.user.create_profile_image.encode.app_error", nil, imgErr.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func getFont(initialFont string) (*truetype.Font, error) {
|
||||
@@ -827,7 +809,8 @@ func (a *App) SetProfileImageFromFile(userId string, file multipart.File) *model
|
||||
config, _, err := image.DecodeConfig(file)
|
||||
if err != nil {
|
||||
return model.NewAppError("SetProfileImage", "api.user.upload_profile_user.decode_config.app_error", nil, err.Error(), http.StatusBadRequest)
|
||||
} else if config.Width*config.Height > model.MaxImageSize {
|
||||
}
|
||||
if config.Width*config.Height > model.MaxImageSize {
|
||||
return model.NewAppError("SetProfileImage", "api.user.upload_profile_user.too_large.app_error", nil, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
@@ -864,25 +847,25 @@ func (a *App) SetProfileImageFromFile(userId string, file multipart.File) *model
|
||||
|
||||
a.InvalidateCacheForUser(userId)
|
||||
|
||||
if user, err := a.GetUser(userId); err != nil {
|
||||
user, err := a.GetUser(userId)
|
||||
if err != nil {
|
||||
mlog.Error(fmt.Sprintf("Error in getting users profile for id=%v forcing logout", userId), mlog.String("user_id", userId))
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
options := a.Config().GetSanitizeOptions()
|
||||
user.SanitizeProfile(options)
|
||||
|
||||
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_UPDATED, "", "", "", nil)
|
||||
message.Add("user", user)
|
||||
a.Publish(message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) UpdatePasswordAsUser(userId, currentPassword, newPassword string) *model.AppError {
|
||||
var user *model.User
|
||||
var err *model.AppError
|
||||
|
||||
if user, err = a.GetUser(userId); err != nil {
|
||||
user, err := a.GetUser(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -915,9 +898,11 @@ func (a *App) UpdateActive(user *model.User, active bool) (*model.User, *model.A
|
||||
user.DeleteAt = model.GetMillis()
|
||||
}
|
||||
|
||||
if result := <-a.Srv.Store.User().Update(user, true); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().Update(user, true)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
|
||||
if user.DeleteAt > 0 {
|
||||
if err := a.RevokeAllSessions(user.Id); err != nil {
|
||||
return nil, err
|
||||
@@ -949,7 +934,6 @@ func (a *App) UpdateActive(user *model.User, active bool) (*model.User, *model.A
|
||||
a.sendUpdatedUserEvent(*ruser)
|
||||
|
||||
return ruser, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) SanitizeProfile(user *model.User, asAdmin bool) {
|
||||
@@ -1042,9 +1026,10 @@ func (a *App) UpdateUser(user *model.User, sendNotifications bool) (*model.User,
|
||||
}
|
||||
}
|
||||
|
||||
if result := <-a.Srv.Store.User().Update(user, false); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().Update(user, false)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
rusers := result.Data.([2]*model.User)
|
||||
|
||||
if sendNotifications {
|
||||
@@ -1076,20 +1061,18 @@ func (a *App) UpdateUser(user *model.User, sendNotifications bool) (*model.User,
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
|
||||
return rusers[0], nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) UpdateUserNotifyProps(userId string, props map[string]string) (*model.User, *model.AppError) {
|
||||
var user *model.User
|
||||
var err *model.AppError
|
||||
if user, err = a.GetUser(userId); err != nil {
|
||||
user, err := a.GetUser(userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user.NotifyProps = props
|
||||
|
||||
var ruser *model.User
|
||||
if ruser, err = a.UpdateUser(user, true); err != nil {
|
||||
ruser, err := a.UpdateUser(user, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1108,10 +1091,8 @@ func (a *App) UpdateMfa(activate bool, userId, token string) *model.AppError {
|
||||
}
|
||||
|
||||
a.Go(func() {
|
||||
var user *model.User
|
||||
var err *model.AppError
|
||||
|
||||
if user, err = a.GetUser(userId); err != nil {
|
||||
user, err := a.GetUser(userId)
|
||||
if err != nil {
|
||||
mlog.Error(err.Error())
|
||||
return
|
||||
}
|
||||
@@ -1125,9 +1106,8 @@ func (a *App) UpdateMfa(activate bool, userId, token string) *model.AppError {
|
||||
}
|
||||
|
||||
func (a *App) UpdatePasswordByUserIdSendEmail(userId, newPassword, method string) *model.AppError {
|
||||
var user *model.User
|
||||
var err *model.AppError
|
||||
if user, err = a.GetUser(userId); err != nil {
|
||||
user, err := a.GetUser(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1163,18 +1143,16 @@ func (a *App) UpdatePasswordSendEmail(user *model.User, newPassword, method stri
|
||||
}
|
||||
|
||||
func (a *App) ResetPasswordFromToken(userSuppliedTokenString, newPassword string) *model.AppError {
|
||||
var token *model.Token
|
||||
var err *model.AppError
|
||||
if token, err = a.GetPasswordRecoveryToken(userSuppliedTokenString); err != nil {
|
||||
token, err := a.GetPasswordRecoveryToken(userSuppliedTokenString)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
}
|
||||
if model.GetMillis()-token.CreateAt >= PASSWORD_RECOVER_EXPIRY_TIME {
|
||||
return model.NewAppError("resetPassword", "api.user.reset_password.link_expired.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
var user *model.User
|
||||
if user, err = a.GetUser(token.Extra); err != nil {
|
||||
user, err := a.GetUser(token.Extra)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1196,9 +1174,8 @@ func (a *App) ResetPasswordFromToken(userSuppliedTokenString, newPassword string
|
||||
}
|
||||
|
||||
func (a *App) SendPasswordReset(email string, siteURL string) (bool, *model.AppError) {
|
||||
var user *model.User
|
||||
var err *model.AppError
|
||||
if user, err = a.GetUserByEmail(email); err != nil {
|
||||
user, err := a.GetUserByEmail(email)
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -1206,8 +1183,8 @@ func (a *App) SendPasswordReset(email string, siteURL string) (bool, *model.AppE
|
||||
return false, model.NewAppError("SendPasswordReset", "api.user.send_password_reset.sso.app_error", nil, "userId="+user.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var token *model.Token
|
||||
if token, err = a.CreatePasswordRecoveryToken(user.Id); err != nil {
|
||||
token, err := a.CreatePasswordRecoveryToken(user.Id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@@ -1225,15 +1202,15 @@ func (a *App) CreatePasswordRecoveryToken(userId string) (*model.Token, *model.A
|
||||
}
|
||||
|
||||
func (a *App) GetPasswordRecoveryToken(token string) (*model.Token, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Token().GetByToken(token); result.Err != nil {
|
||||
result := <-a.Srv.Store.Token().GetByToken(token)
|
||||
if result.Err != nil {
|
||||
return nil, model.NewAppError("GetPasswordRecoveryToken", "api.user.reset_password.invalid_link.app_error", nil, result.Err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
token := result.Data.(*model.Token)
|
||||
if token.Type != TOKEN_TYPE_PASSWORD_RECOVERY {
|
||||
}
|
||||
rtoken := result.Data.(*model.Token)
|
||||
if rtoken.Type != TOKEN_TYPE_PASSWORD_RECOVERY {
|
||||
return nil, model.NewAppError("GetPasswordRecoveryToken", "api.user.reset_password.broken_token.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
return rtoken, nil
|
||||
}
|
||||
|
||||
func (a *App) DeleteToken(token *model.Token) *model.AppError {
|
||||
@@ -1245,9 +1222,8 @@ func (a *App) DeleteToken(token *model.Token) *model.AppError {
|
||||
}
|
||||
|
||||
func (a *App) UpdateUserRoles(userId string, newRoles string, sendWebSocketEvent bool) (*model.User, *model.AppError) {
|
||||
var user *model.User
|
||||
var err *model.AppError
|
||||
if user, err = a.GetUser(userId); err != nil {
|
||||
user, err := a.GetUser(userId)
|
||||
if err != nil {
|
||||
err.StatusCode = http.StatusBadRequest
|
||||
return nil, err
|
||||
}
|
||||
@@ -1260,12 +1236,11 @@ func (a *App) UpdateUserRoles(userId string, newRoles string, sendWebSocketEvent
|
||||
uchan := a.Srv.Store.User().Update(user, true)
|
||||
schan := a.Srv.Store.Session().UpdateRoles(user.Id, newRoles)
|
||||
|
||||
var ruser *model.User
|
||||
if result := <-uchan; result.Err != nil {
|
||||
result := <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
ruser = result.Data.([2]*model.User)[0]
|
||||
}
|
||||
ruser := result.Data.([2]*model.User)[0]
|
||||
|
||||
if result := <-schan; result.Err != nil {
|
||||
// soft error since the user roles were still updated
|
||||
@@ -1330,15 +1305,14 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
|
||||
return result.Err
|
||||
}
|
||||
|
||||
fchan := a.Srv.Store.FileInfo().GetForUser(user.Id)
|
||||
var infos []*model.FileInfo
|
||||
if result := <-fchan; result.Err != nil {
|
||||
result := <-a.Srv.Store.FileInfo().GetForUser(user.Id)
|
||||
if result.Err != nil {
|
||||
mlog.Warn("Error getting file list for user from FileInfoStore")
|
||||
} else {
|
||||
infos = result.Data.([]*model.FileInfo)
|
||||
}
|
||||
|
||||
infos := result.Data.([]*model.FileInfo)
|
||||
for _, info := range infos {
|
||||
res, err := a.FileExists(info.Path)
|
||||
|
||||
if err != nil {
|
||||
mlog.Warn(
|
||||
"Error checking existence of file",
|
||||
@@ -1363,7 +1337,6 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if result := <-a.Srv.Store.FileInfo().PermanentDeleteByUser(user.Id); result.Err != nil {
|
||||
return result.Err
|
||||
@@ -1387,14 +1360,14 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
|
||||
}
|
||||
|
||||
func (a *App) PermanentDeleteAllUsers() *model.AppError {
|
||||
if result := <-a.Srv.Store.User().GetAll(); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetAll()
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
} else {
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
for _, user := range users {
|
||||
a.PermanentDeleteUser(user)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1407,17 +1380,15 @@ func (a *App) SendEmailVerification(user *model.User) *model.AppError {
|
||||
|
||||
if _, err := a.GetStatus(user.Id); err != nil {
|
||||
return a.SendVerifyEmail(user.Email, user.Locale, a.GetSiteURL(), token.Token)
|
||||
} else {
|
||||
return a.SendEmailChangeVerifyEmail(user.Email, user.Locale, a.GetSiteURL(), token.Token)
|
||||
}
|
||||
return a.SendEmailChangeVerifyEmail(user.Email, user.Locale, a.GetSiteURL(), token.Token)
|
||||
}
|
||||
|
||||
func (a *App) VerifyEmailFromToken(userSuppliedTokenString string) *model.AppError {
|
||||
var token *model.Token
|
||||
var err *model.AppError
|
||||
if token, err = a.GetVerifyEmailToken(userSuppliedTokenString); err != nil {
|
||||
token, err := a.GetVerifyEmailToken(userSuppliedTokenString)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
}
|
||||
if model.GetMillis()-token.CreateAt >= PASSWORD_RECOVER_EXPIRY_TIME {
|
||||
return model.NewAppError("resetPassword", "api.user.reset_password.link_expired.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
@@ -1427,7 +1398,6 @@ func (a *App) VerifyEmailFromToken(userSuppliedTokenString string) *model.AppErr
|
||||
if err := a.DeleteToken(token); err != nil {
|
||||
mlog.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1443,24 +1413,24 @@ func (a *App) CreateVerifyEmailToken(userId string) (*model.Token, *model.AppErr
|
||||
}
|
||||
|
||||
func (a *App) GetVerifyEmailToken(token string) (*model.Token, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Token().GetByToken(token); result.Err != nil {
|
||||
result := <-a.Srv.Store.Token().GetByToken(token)
|
||||
if result.Err != nil {
|
||||
return nil, model.NewAppError("GetVerifyEmailToken", "api.user.verify_email.bad_link.app_error", nil, result.Err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
token := result.Data.(*model.Token)
|
||||
if token.Type != TOKEN_TYPE_VERIFY_EMAIL {
|
||||
}
|
||||
rtoken := result.Data.(*model.Token)
|
||||
if rtoken.Type != TOKEN_TYPE_VERIFY_EMAIL {
|
||||
return nil, model.NewAppError("GetVerifyEmailToken", "api.user.verify_email.broken_token.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
return rtoken, nil
|
||||
}
|
||||
|
||||
func (a *App) GetTotalUsersStats() (*model.UsersStats, *model.AppError) {
|
||||
stats := &model.UsersStats{}
|
||||
|
||||
if result := <-a.Srv.Store.User().GetTotalUsersCount(); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetTotalUsersCount()
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
stats.TotalUsersCount = result.Data.(int64)
|
||||
}
|
||||
stats := &model.UsersStats{
|
||||
TotalUsersCount: result.Data.(int64),
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
@@ -1472,21 +1442,24 @@ func (a *App) VerifyUserEmail(userId string) *model.AppError {
|
||||
func (a *App) SearchUsers(props *model.UserSearch, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
if props.WithoutTeam {
|
||||
return a.SearchUsersWithoutTeam(props.Term, options)
|
||||
} else if props.InChannelId != "" {
|
||||
return a.SearchUsersInChannel(props.InChannelId, props.Term, options)
|
||||
} else if props.NotInChannelId != "" {
|
||||
return a.SearchUsersNotInChannel(props.TeamId, props.NotInChannelId, props.Term, options)
|
||||
} else if props.NotInTeamId != "" {
|
||||
return a.SearchUsersNotInTeam(props.NotInTeamId, props.Term, options)
|
||||
} else {
|
||||
return a.SearchUsersInTeam(props.TeamId, props.Term, options)
|
||||
}
|
||||
if props.InChannelId != "" {
|
||||
return a.SearchUsersInChannel(props.InChannelId, props.Term, options)
|
||||
}
|
||||
if props.NotInChannelId != "" {
|
||||
return a.SearchUsersNotInChannel(props.TeamId, props.NotInChannelId, props.Term, options)
|
||||
}
|
||||
if props.NotInTeamId != "" {
|
||||
return a.SearchUsersNotInTeam(props.NotInTeamId, props.Term, options)
|
||||
}
|
||||
return a.SearchUsersInTeam(props.TeamId, props.Term, options)
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersInChannel(channelId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().SearchInChannel(channelId, term, options); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().SearchInChannel(channelId, term, options)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1494,13 +1467,13 @@ func (a *App) SearchUsersInChannel(channelId string, term string, options *model
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersNotInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().SearchNotInChannel(teamId, channelId, term, options); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().SearchNotInChannel(teamId, channelId, term, options)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1508,13 +1481,13 @@ func (a *App) SearchUsersNotInChannel(teamId string, channelId string, term stri
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().Search(teamId, term, options); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().Search(teamId, term, options)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1522,13 +1495,13 @@ func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserS
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().SearchNotInTeam(notInTeamId, term, options); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().SearchNotInTeam(notInTeamId, term, options)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1536,13 +1509,13 @@ func (a *App) SearchUsersNotInTeam(notInTeamId string, term string, options *mod
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersWithoutTeam(term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
if result := <-a.Srv.Store.User().SearchWithoutTeam(term, options); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().SearchWithoutTeam(term, options)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1550,7 +1523,6 @@ func (a *App) SearchUsersWithoutTeam(term string, options *model.UserSearchOptio
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, *model.AppError) {
|
||||
@@ -1559,9 +1531,10 @@ func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term s
|
||||
|
||||
autocomplete := &model.UserAutocompleteInChannel{}
|
||||
|
||||
if result := <-uchan; result.Err != nil {
|
||||
result := <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1569,19 +1542,18 @@ func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term s
|
||||
}
|
||||
|
||||
autocomplete.InChannel = users
|
||||
}
|
||||
|
||||
if result := <-nuchan; result.Err != nil {
|
||||
result = <-nuchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
users := result.Data.([]*model.User)
|
||||
}
|
||||
users = result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
a.SanitizeProfile(user, options.IsAdmin)
|
||||
}
|
||||
|
||||
autocomplete.OutOfChannel = users
|
||||
}
|
||||
|
||||
return autocomplete, nil
|
||||
}
|
||||
@@ -1589,9 +1561,10 @@ func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term s
|
||||
func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInTeam, *model.AppError) {
|
||||
autocomplete := &model.UserAutocompleteInTeam{}
|
||||
|
||||
if result := <-a.Srv.Store.User().Search(teamId, term, options); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().Search(teamId, term, options)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1599,14 +1572,12 @@ func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model
|
||||
}
|
||||
|
||||
autocomplete.InTeam = users
|
||||
}
|
||||
|
||||
return autocomplete, nil
|
||||
}
|
||||
|
||||
func (a *App) UpdateOAuthUserAttrs(userData io.Reader, user *model.User, provider einterfaces.OauthProvider, service string) *model.AppError {
|
||||
oauthUser := provider.GetUserFromJson(userData)
|
||||
|
||||
if oauthUser == nil {
|
||||
return model.NewAppError("UpdateOAuthUserAttrs", "api.user.update_oauth_user_attrs.get_user.app_error", map[string]interface{}{"Service": service}, "", http.StatusBadRequest)
|
||||
}
|
||||
@@ -1640,8 +1611,8 @@ func (a *App) UpdateOAuthUserAttrs(userData io.Reader, user *model.User, provide
|
||||
}
|
||||
|
||||
if userAttrsChanged {
|
||||
var result store.StoreResult
|
||||
if result = <-a.Srv.Store.User().Update(user, true); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().Update(user, true)
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user