0003-misc-let-User-model-validate-identifiers-and-uniquen.patch
src/authentic2/custom_user/models.py | ||
---|---|---|
245 | 245 |
return '<User: %r>' % six.text_type(self) |
246 | 246 | |
247 | 247 |
def clean(self): |
248 |
pass |
|
248 |
if (not self.username |
|
249 |
and not self.email |
|
250 |
and not (self.first_name |
|
251 |
or self.last_name)): |
|
252 |
raise ValidationError(_('An account needs at least one identifier.')) |
|
249 | 253 | |
250 | 254 |
def validate_unique(self, exclude=None): |
251 | 255 |
errors = {} |
... | ... | |
259 | 263 |
qs = model.objects |
260 | 264 |
if self.pk: |
261 | 265 |
qs = qs.exclude(pk=self.pk) |
262 |
if self.ou_id: |
|
263 |
qs = qs.filter(ou_id=self.ou_id) |
|
264 |
else: |
|
265 |
qs = qs.filter(ou__isnull=True) |
|
266 | 266 | |
267 |
if 'username' not in exclude and self.username and app_settings.A2_USERNAME_IS_UNIQUE: |
|
267 |
if 'username' not in exclude and self.username and (app_settings.A2_USERNAME_IS_UNIQUE |
|
268 |
or (self.ou and self.ou.username_is_unique)): |
|
269 |
username_qs = qs |
|
270 |
if not app_settings.A2_USERNAME_IS_UNIQUE: |
|
271 |
username_qs = qs.filter(ou=self.ou) |
|
268 | 272 |
try: |
269 | 273 |
try: |
270 |
qs.get(username=self.username) |
|
274 |
username_qs.get(username=self.username)
|
|
271 | 275 |
except MultipleObjectsReturned: |
272 | 276 |
pass |
273 | 277 |
except model.DoesNotExist: |
... | ... | |
275 | 279 |
else: |
276 | 280 |
errors.setdefault('username', []).append( |
277 | 281 |
_('This username is already in use. Please supply a different username.')) |
278 |
if 'email' not in exclude and self.email and app_settings.A2_EMAIL_IS_UNIQUE: |
|
282 | ||
283 |
if 'email' not in exclude and self.email and (app_settings.A2_EMAIL_IS_UNIQUE |
|
284 |
or (self.ou and self.ou.email_is_unique)): |
|
285 |
email_qs = qs |
|
286 |
if not app_settings.A2_EMAIL_IS_UNIQUE: |
|
287 |
email_qs = email_qs.filter(ou=self.ou) |
|
279 | 288 |
try: |
280 | 289 |
try: |
281 |
qs.get(email__iexact=self.email) |
|
290 |
email_qs.get(email__iexact=self.email)
|
|
282 | 291 |
except MultipleObjectsReturned: |
283 | 292 |
pass |
284 | 293 |
except model.DoesNotExist: |
src/authentic2/manager/forms.py | ||
---|---|---|
201 | 201 |
self.data[self.add_prefix('ou')] = qs[0].pk |
202 | 202 |
self.data._mutable = False |
203 | 203 | |
204 |
def clean(self): |
|
205 |
if (self.instance.has_usable_password() |
|
206 |
and ('username' in self.fields |
|
207 |
or 'email' in self.fields)): |
|
208 |
if not self.cleaned_data.get('username') and \ |
|
209 |
not self.cleaned_data.get('email'): |
|
210 |
raise forms.ValidationError( |
|
211 |
_('You must set a username or an email.')) |
|
212 | ||
213 |
if self.cleaned_data.get('email'): |
|
214 |
qs = User.objects.all() |
|
215 |
ou = getattr(self, 'ou', None) |
|
216 | ||
217 |
if self.instance and self.instance.pk: |
|
218 |
qs = qs.exclude(pk=self.instance.pk) |
|
219 |
ou = self.instance.ou |
|
220 | ||
221 |
email = self.cleaned_data['email'] |
|
222 |
already_used = False |
|
223 | ||
224 |
if a2_app_settings.A2_EMAIL_IS_UNIQUE and qs.filter(email=email).exists(): |
|
225 |
already_used = True |
|
226 | ||
227 |
if ou and ou.email_is_unique and qs.filter(ou=ou, email=email).exists(): |
|
228 |
already_used = True |
|
229 | ||
230 |
if already_used: |
|
231 |
raise forms.ValidationError({ |
|
232 |
'email': _('Email already used.') |
|
233 |
}) |
|
234 |
return super(UserEditForm, self).clean() |
|
235 | ||
236 | 204 |
class Meta: |
237 | 205 |
model = User |
238 | 206 |
exclude = ('is_staff', 'groups', 'user_permissions', 'last_login', |
239 |
- |