user: replace all uses of Attribute.set_value() (#31937)
This commit is contained in:
parent
1079ac2e00
commit
ff92cb1fb9
|
@ -108,16 +108,15 @@ class BaseUserForm(forms.ModelForm):
|
|||
super(BaseUserForm, self).clean()
|
||||
|
||||
def save_attributes(self):
|
||||
from authentic2 import models
|
||||
|
||||
verified_attributes = {}
|
||||
for av in models.AttributeValue.objects.with_owner(
|
||||
self.instance).filter(verified=True):
|
||||
verified_attributes[av.attribute.name] = True
|
||||
|
||||
# only save non verified attributes here
|
||||
verified_attributes = set(
|
||||
self.instance.attribute_values.filter(verified=True).values_list('attribute__name', flat=True)
|
||||
)
|
||||
for attribute in self.attributes:
|
||||
if attribute.name in self.fields and attribute.name not in verified_attributes:
|
||||
attribute.set_value(self.instance, self.cleaned_data[attribute.name])
|
||||
name = attribute.name
|
||||
if name in self.fields and name not in verified_attributes:
|
||||
value = self.cleaned_data[name]
|
||||
setattr(self.instance.attributes, name, value)
|
||||
|
||||
def save(self, commit=True):
|
||||
result = super(BaseUserForm, self).save(commit=commit)
|
||||
|
|
|
@ -58,7 +58,7 @@ class DjangoUserLDIFParser(ldif.LDIFParser):
|
|||
continue
|
||||
attribute = self.options['extra_attribute'][attribute]
|
||||
a.append((attribute, v))
|
||||
d[attribute.name] = v
|
||||
d[attribute] = v
|
||||
if self.callback:
|
||||
m.extend(self.callback(u, dn, entry, self.options, d))
|
||||
if 'username' not in d:
|
||||
|
@ -91,7 +91,7 @@ class ExtraAttributeAction(argparse.Action):
|
|||
raise argparse.ArgumentTypeError(
|
||||
'django attribute %s does not exist' % django_attribute)
|
||||
res = getattr(namespace, self.dest, {})
|
||||
res[ldap_attribute] = attribute
|
||||
res[ldap_attribute] = django_attribute
|
||||
setattr(namespace, self.dest, res)
|
||||
|
||||
|
||||
|
@ -145,6 +145,6 @@ class Command(BaseCommand):
|
|||
for u, a, m in parser.users:
|
||||
u.save()
|
||||
for attribute, value in a:
|
||||
attribute.set_value(u, value)
|
||||
setattr(u.attributes, attribute, value)
|
||||
for model, kwargs in m:
|
||||
model.objects.get_or_create(**kwargs)
|
||||
|
|
|
@ -166,8 +166,6 @@ class OIDCBackend(ModelBackend):
|
|||
# map claims to attributes or user fields
|
||||
# mapping is done before eventual creation of user as EMAIL_IS_UNIQUE needs to know if the
|
||||
# mapping will provide some mail to us
|
||||
attributes = utils.get_attributes()
|
||||
attributes_map = {attribute.name: attribute for attribute in attributes}
|
||||
ou_map = {ou.slug: ou for ou in get_ou_model().cached()}
|
||||
user_ou = provider.ou
|
||||
save_user = False
|
||||
|
@ -240,6 +238,8 @@ class OIDCBackend(ModelBackend):
|
|||
logger.info(u'auth_oidc: set user %s attribute %s to value %s',
|
||||
user, attribute, value)
|
||||
setattr(user, attribute, value)
|
||||
if attribute == 'email' and verified:
|
||||
user.email_verified = True
|
||||
save_user = True
|
||||
|
||||
if user.ou != user_ou:
|
||||
|
@ -258,13 +258,14 @@ class OIDCBackend(ModelBackend):
|
|||
|
||||
# new style attributes
|
||||
for attribute, value, verified in mappings:
|
||||
if attribute not in attributes_map:
|
||||
if attribute in ('username', 'email'):
|
||||
continue
|
||||
if attributes_map[attribute].get_value(user, verified=verified) != value:
|
||||
logger.info(u'auth_oidc: set user %s attribute %s to %s',
|
||||
user, attribute, value)
|
||||
attributes_map[attribute].set_value(user, value, verified=verified)
|
||||
|
||||
if attribute in ('first_name', 'last_name') and not verified:
|
||||
continue
|
||||
if verified:
|
||||
setattr(user.verified_attributes, attribute, value)
|
||||
else:
|
||||
setattr(user.attributes, attribute, value)
|
||||
return user
|
||||
|
||||
def get_saml2_authn_context(self):
|
||||
|
|
|
@ -63,7 +63,7 @@ def test_load_ldif(db, monkeypatch, tmpdir):
|
|||
assert len(args) == 1
|
||||
assert isinstance(args[0], file)
|
||||
assert kwargs['options']['extra_attribute'] == {
|
||||
'ldap_attr': Attribute.objects.get(name='first_name')}
|
||||
'ldap_attr': 'first_name'}
|
||||
assert kwargs['options']['result'] == 'result'
|
||||
|
||||
def parse(self):
|
||||
|
|
Loading…
Reference in New Issue