@@ -15,7 +15,7 @@ public struct BackendUserForm {
15
15
let password : String
16
16
var passwordErrors : [ String ]
17
17
18
- let repeatPassword : String ?
18
+ let repeatPassword : String
19
19
var repeatPasswordErrors : [ String ]
20
20
21
21
let sendMail : Bool
@@ -42,7 +42,7 @@ public struct BackendUserForm {
42
42
self . email = email ?? " "
43
43
self . role = role ?? " "
44
44
self . password = password ?? " "
45
- self . repeatPassword = repeatPassword
45
+ self . repeatPassword = repeatPassword ?? " "
46
46
self . sendMail = sendMail ?? false
47
47
self . randomPassword = randomPassword ?? false
48
48
self . shouldResetPassword = shouldResetPassword ?? false
@@ -61,7 +61,6 @@ public struct BackendUserForm {
61
61
let sendEmail = json [ " sendEmail " ] ? . bool
62
62
let password = json [ " password " ] ? . string
63
63
let repeatPassword = json [ " repeatPassword " ] ? . string
64
- let randomPassword = json [ " randomPassword " ] ? . bool
65
64
66
65
return validate (
67
66
name: name,
@@ -70,8 +69,7 @@ public struct BackendUserForm {
70
69
shouldResetPassword: shouldResetPassword,
71
70
sendEmail: sendEmail,
72
71
password: password,
73
- repeatPassword: repeatPassword,
74
- randomPassword: randomPassword
72
+ repeatPassword: repeatPassword
75
73
)
76
74
}
77
75
@@ -83,7 +81,6 @@ public struct BackendUserForm {
83
81
let sendEmail = content [ " sendEmail " ] ? . bool
84
82
let password = content [ " password " ] ? . string
85
83
let repeatPassword = content [ " repeatPassword " ] ? . string
86
- let randomPassword = content [ " randomPassword " ] ? . bool
87
84
88
85
return validate (
89
86
name: name,
@@ -92,8 +89,7 @@ public struct BackendUserForm {
92
89
shouldResetPassword: shouldResetPassword,
93
90
sendEmail: sendEmail,
94
91
password: password,
95
- repeatPassword: repeatPassword,
96
- randomPassword: randomPassword
92
+ repeatPassword: repeatPassword
97
93
)
98
94
}
99
95
@@ -104,9 +100,10 @@ public struct BackendUserForm {
104
100
shouldResetPassword: Bool ? ,
105
101
sendEmail: Bool ? ,
106
102
password: String ? ,
107
- repeatPassword: String ? ,
108
- randomPassword: Bool ?
103
+ repeatPassword: String ?
109
104
) -> ( BackendUserForm , hasErrors: Bool ) {
105
+ var shouldResetPassword = shouldResetPassword
106
+ var password = password
110
107
var hasErrors = false
111
108
112
109
var nameErrors : [ String ] = [ ]
@@ -131,48 +128,54 @@ public struct BackendUserForm {
131
128
hasErrors = true
132
129
}
133
130
134
- let nameCharactercount = name! . utf8. count
131
+ let nameCharactercount = name? . utf8. count ?? 0
135
132
if nameCharactercount < 1 || nameCharactercount > 191 {
136
133
nameErrors. append ( " Must be between 1 and 191 characters long " )
137
134
hasErrors = true
138
135
}
139
136
140
- let emailCharactercount = email! . utf8. count
137
+ let emailCharactercount = email? . utf8. count ?? 0
141
138
if emailCharactercount < 1 || emailCharactercount > 191 {
142
139
emailErrors. append ( " Must be between 1 and 191 characters long " )
143
140
hasErrors = true
144
141
}
145
142
146
- if role! . utf8. count > 191 {
143
+ if ( role? . utf8. count ?? 0 ) > 191 {
147
144
nameErrors. append ( " Must be less than 191 characters long " )
148
145
hasErrors = true
149
146
}
150
147
151
- let randomPassword = randomPassword ?? ( password == nil )
152
- let password = password ?? String . randomAlphaNumericString ( 10 )
153
-
154
- let passwordCharactercount = password. utf8. count
155
- if passwordCharactercount < 1 || passwordCharactercount > 191 {
156
- passwordErrors. append ( " Must be between 1 and 191 characters long " )
157
- hasErrors = true
158
- }
159
-
148
+ let randomPassword = ( password == nil && repeatPassword == nil )
160
149
if !randomPassword {
161
150
if password != repeatPassword {
162
151
repeatPasswordErrors. append ( " Passwords do not match " )
163
152
hasErrors = true
164
153
}
165
-
166
- if repeatPassword == nil {
167
- repeatPasswordErrors. append ( requiredFieldError)
168
- hasErrors = true
154
+
155
+ if let password = password {
156
+ let passwordCharactercount = password. utf8. count
157
+ if passwordCharactercount < 1 || passwordCharactercount > 191 {
158
+ passwordErrors. append ( " Must be between 1 and 191 characters long " )
159
+ hasErrors = true
160
+ }
169
161
} else {
170
- let passwordRepeatCharacterCount = repeatPassword!. utf8. count
162
+ passwordErrors. append ( requiredFieldError)
163
+ hasErrors = true
164
+ }
165
+
166
+ if let repeatPassword = repeatPassword {
167
+ let passwordRepeatCharacterCount = repeatPassword. utf8. count
171
168
if passwordRepeatCharacterCount < 1 || passwordRepeatCharacterCount > 191 {
172
169
repeatPasswordErrors. append ( " Must be between 1 and 191 characters long " )
173
170
hasErrors = true
174
171
}
172
+ } else {
173
+ repeatPasswordErrors. append ( requiredFieldError)
174
+ hasErrors = true
175
175
}
176
+ } else {
177
+ password = String . randomAlphaNumericString ( 10 )
178
+ shouldResetPassword = true
176
179
}
177
180
178
181
let user = BackendUserForm (
0 commit comments