Add Password field to Bulk Importer. (#6268)

This commit is contained in:
George Goldberg
2017-04-28 23:01:56 +01:00
committed by Corey Hulen
parent 96906482ce
commit 6f6b7e4e97
4 changed files with 50 additions and 2 deletions

View File

@@ -52,6 +52,7 @@ type UserImportData struct {
Email *string `json:"email"` Email *string `json:"email"`
AuthService *string `json:"auth_service"` AuthService *string `json:"auth_service"`
AuthData *string `json:"auth_data"` AuthData *string `json:"auth_data"`
Password *string `json:"password"`
Nickname *string `json:"nickname"` Nickname *string `json:"nickname"`
FirstName *string `json:"first_name"` FirstName *string `json:"first_name"`
LastName *string `json:"last_name"` LastName *string `json:"last_name"`
@@ -429,8 +430,11 @@ func ImportUser(data *UserImportData, dryRun bool) *model.AppError {
if data.AuthData != nil { if data.AuthData != nil {
authData = data.AuthData authData = data.AuthData
password = "" password = ""
} else if data.Password != nil {
password = *data.Password
authData = nil
} else { } else {
// If no Auth Data is specified, we must generate a password. // If no AuthData or Password is specified, we must generate a password.
password = model.NewId() password = model.NewId()
authData = nil authData = nil
} }
@@ -695,10 +699,22 @@ func validateUserImportData(data *UserImportData) *model.AppError {
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_service_length.error", nil, "", http.StatusBadRequest) return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_service_length.error", nil, "", http.StatusBadRequest)
} }
if data.AuthData != nil && data.Password != nil {
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_and_password.error", nil, "", http.StatusBadRequest)
}
if data.AuthData != nil && len(*data.AuthData) > model.USER_AUTH_DATA_MAX_LENGTH { if data.AuthData != nil && len(*data.AuthData) > model.USER_AUTH_DATA_MAX_LENGTH {
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_length.error", nil, "", http.StatusBadRequest) return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_length.error", nil, "", http.StatusBadRequest)
} }
if data.Password != nil && len(*data.Password) == 0 {
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.pasword_length.error", nil, "", http.StatusBadRequest)
}
if data.Password != nil && len(*data.Password) > model.USER_PASSWORD_MAX_LENGTH {
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.password_length.error", nil, "", http.StatusBadRequest)
}
if data.Nickname != nil && utf8.RuneCountInString(*data.Nickname) > model.USER_NICKNAME_MAX_RUNES { if data.Nickname != nil && utf8.RuneCountInString(*data.Nickname) > model.USER_NICKNAME_MAX_RUNES {
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.nickname_length.error", nil, "", http.StatusBadRequest) return model.NewAppError("BulkImport", "app.import.validate_user_import_data.nickname_length.error", nil, "", http.StatusBadRequest)
} }

View File

@@ -1013,6 +1013,29 @@ func TestImportImportUser(t *testing.T) {
} }
} }
// Check Password and AuthData together.
data.Password = ptrStr("PasswordTest")
if err := ImportUser(&data, false); err == nil {
t.Fatalf("Should have failed to import invalid user.")
}
data.AuthData = nil
if err := ImportUser(&data, false); err != nil {
t.Fatalf("Should have succeeded to update valid user %v", err)
}
data.Password = ptrStr("")
if err := ImportUser(&data, false); err == nil {
t.Fatalf("Should have failed to import invalid user.")
}
data.Password = ptrStr(strings.Repeat("0123456789", 10))
if err := ImportUser(&data, false); err == nil {
t.Fatalf("Should have failed to import invalid user.")
}
data.Password = ptrStr("TestPassword")
// Test team and channel memberships // Test team and channel memberships
teamName := model.NewId() teamName := model.NewId()
ImportTeam(&TeamImportData{ ImportTeam(&TeamImportData{

View File

@@ -3095,6 +3095,14 @@
"id": "app.import.validate_user_import_data.auth_data_length.error", "id": "app.import.validate_user_import_data.auth_data_length.error",
"translation": "User AuthData is too long." "translation": "User AuthData is too long."
}, },
{
"id": "app.import.validate_user_import_data.auth_data_and_password.error",
"translation": "User AuthData and Password are mutually exclusive."
},
{
"id": "app.import.validate_user_import_data.pasword_length.error",
"translation": "User Password has invalid length."
},
{ {
"id": "app.import.validate_user_import_data.auth_service_length.error", "id": "app.import.validate_user_import_data.auth_service_length.error",
"translation": "User AuthService should not be empty if it is provided." "translation": "User AuthService should not be empty if it is provided."

View File

@@ -36,6 +36,7 @@ const (
USER_AUTH_DATA_MAX_LENGTH = 128 USER_AUTH_DATA_MAX_LENGTH = 128
USER_NAME_MAX_LENGTH = 64 USER_NAME_MAX_LENGTH = 64
USER_NAME_MIN_LENGTH = 1 USER_NAME_MIN_LENGTH = 1
USER_PASSWORD_MAX_LENGTH = 72
) )
type User struct { type User struct {
@@ -130,7 +131,7 @@ func (u *User) IsValid() *AppError {
return InvalidUserError("auth_data_pwd", u.Id) return InvalidUserError("auth_data_pwd", u.Id)
} }
if len(u.Password) > 72 { if len(u.Password) > USER_PASSWORD_MAX_LENGTH {
return InvalidUserError("password_limit", u.Id) return InvalidUserError("password_limit", u.Id)
} }