Exemplo n.º 1
0
 def save(self, commit=True):
     # Call the ModelForm save method directly, since we are overriding the
     # Django UserCreationForm save() functionality here
     user = forms.ModelForm.save(self, commit=False)
     if USE_LDAP:
         # Create an entry in LDAP for this new user:
         ldap_utils.create_user(
             user.get_username(), self.cleaned_data['password1'], user.email,
             user.first_name, user.last_name)
         # Use the LDAPUser proxy model for this object
         make_ldap_user(user)
     user.set_password(self.cleaned_data['password1'])
     if commit:
         user.save()
     return user
Exemplo n.º 2
0
 def save(self, commit=True):
     # Call the ModelForm save method directly, since we are overriding the
     # Django UserCreationForm save() functionality here
     user = forms.ModelForm.save(self, commit=False)
     if USE_LDAP:
         # Create an entry in LDAP for this new user:
         ldap_utils.create_user(user.get_username(),
                                self.cleaned_data['password1'], user.email,
                                user.first_name, user.last_name)
         # Use the LDAPUser proxy model for this object
         make_ldap_user(user)
     user.set_password(self.cleaned_data['password1'])
     if commit:
         user.save()
     return user
Exemplo n.º 3
0
 def setUp(self):
     rnd = os.getpid()
     self.user = '******' % rnd
     self.new_user = '******' % rnd
     self.password = '******'
     self.first_name = 'Silly'
     self.last_name = 'Test'
     self.email = '*****@*****.**' % rnd
     self.group_of_names = 'test%d_group_of_names' % rnd
     self.posix_group = 'test%d_posix_group' % rnd
     self.super_group = 'test%d-it' % rnd
     utils.create_user(self.user, self.password, self.email,
                       self.first_name, self.last_name)
     utils.create_group(self.group_of_names, object_class='groupOfNames')
     utils.create_group(self.posix_group, object_class='posixGroup')
     utils.create_group(self.super_group, object_class='posixGroup')
Exemplo n.º 4
0
 def setUp(self):
     rnd = os.getpid()
     self.user = '******' % rnd
     self.new_user = '******' % rnd
     self.password = '******'
     self.first_name = 'Silly'
     self.last_name = 'Test'
     self.email = '*****@*****.**' % rnd
     self.group_of_names = 'test%d_group_of_names' % rnd
     self.posix_group = 'test%d_posix_group' % rnd
     self.super_group = 'test%d-it' % rnd
     utils.create_user(self.user, self.password, self.email,
                       self.first_name, self.last_name)
     utils.create_group(self.group_of_names, object_class='groupOfNames')
     utils.create_group(self.posix_group, object_class='posixGroup')
     utils.create_group(self.super_group, object_class='posixGroup')
Exemplo n.º 5
0
 def test_create_new_user(self):
     """Can properly create new user"""
     self.assertFalse(utils.username_exists(self.new_user))
     self.assertTrue(utils.create_user(self.new_user, self.password,
                                       self.email,
                                       self.first_name, self.last_name))
     self.assertTrue(utils.username_exists(self.new_user))
     utils.delete_user(self.new_user)
     self.assertFalse(utils.username_exists(self.new_user))
Exemplo n.º 6
0
 def test_create_new_user(self):
     """Can properly create new user"""
     self.assertFalse(utils.username_exists(self.new_user))
     self.assertTrue(
         utils.create_user(self.new_user, self.password, self.email,
                           self.first_name, self.last_name))
     self.assertTrue(utils.username_exists(self.new_user))
     utils.delete_user(self.new_user)
     self.assertFalse(utils.username_exists(self.new_user))
Exemplo n.º 7
0
    def save(self, *args, **kwargs):
        """Only save the instance if user exists in LDAP. Allows renaming
        username, but does not update other LDAP entry attributes (yet)."""
        new_username = self.get_username()
        # Password can be None or '' for unusuable password
        if (not new_username or not self.email or
                not self.first_name or not self.last_name):
            raise ValidationError('Users must have username, email, '
                                  'first name and last name')

        try:
            old_user = LDAPUser.objects.get(pk=self.pk)
            old_username = old_user.get_username()
            renaming_user = old_username != new_username
            updating_email = old_user.email != self.email
        except LDAPUser.DoesNotExist:
            renaming_user = False
            updating_email = False

        # Update username
        if renaming_user:
            success, reason = ldap_utils.rename_user(
                old_username,
                new_username)
            if not success:
                raise ValidationError(
                    'Encountered error while renaming username from {old} '
                    'to {new}. Reason: {reason}'.format(
                        old=old_username,
                        new=new_username,
                        reason=reason))
        elif not ldap_utils.username_exists(new_username):
            if ldap_utils.create_user(new_username, self.password, self.email,
                                      self.first_name, self.last_name):
                # Successfully created LDAP entry for new user
                # Set an unusable password for the Django DB instance
                super(LDAPUser, self).set_unusable_password()
            else:
                # Failed to create user some how.
                raise ValidationError(
                    'Error creating new LDAP entry for {name} with username '
                    '"{username}"'.format(
                        name=self.get_full_name(),
                        username=new_username))

        # Update email
        # New username should exist by now after rename.
        if (updating_email and
                not ldap_utils.set_email(new_username, self.email)):
            raise ValidationError(
                'Encountered error while updating user email to {new}'.format(
                    new=self.email))
        # TODO(flieee): update LDAP other entry attributes (i.e. name) too?
        super(LDAPUser, self).save(*args, **kwargs)
Exemplo n.º 8
0
    def save(self, *args, **kwargs):
        """Only save the instance if user exists in LDAP. Allows renaming
        username, but does not update other LDAP entry attributes (yet)."""
        new_username = self.get_username()
        # Password can be None or '' for unusuable password
        if (not new_username or not self.email or not self.first_name
                or not self.last_name):
            raise ValidationError('Users must have username, email, '
                                  'first name and last name')

        try:
            old_user = LDAPUser.objects.get(pk=self.pk)
            old_username = old_user.get_username()
            renaming_user = old_username != new_username
            updating_email = old_user.email != self.email
        except LDAPUser.DoesNotExist:
            renaming_user = False
            updating_email = False

        # Update username
        if renaming_user:
            success, reason = ldap_utils.rename_user(old_username,
                                                     new_username)
            if not success:
                raise ValidationError(
                    'Encountered error while renaming username from {old} '
                    'to {new}. Reason: {reason}'.format(old=old_username,
                                                        new=new_username,
                                                        reason=reason))
        elif not ldap_utils.username_exists(new_username):
            if ldap_utils.create_user(new_username, self.password, self.email,
                                      self.first_name, self.last_name):
                # Successfully created LDAP entry for new user
                # Set an unusable password for the Django DB instance
                super(LDAPUser, self).set_unusable_password()
            else:
                # Failed to create user some how.
                raise ValidationError(
                    'Error creating new LDAP entry for {name} with username '
                    '"{username}"'.format(name=self.get_full_name(),
                                          username=new_username))

        # Update email
        # New username should exist by now after rename.
        if (updating_email
                and not ldap_utils.set_email(new_username, self.email)):
            raise ValidationError(
                'Encountered error while updating user email to {new}'.format(
                    new=self.email))
        # TODO(flieee): update LDAP other entry attributes (i.e. name) too?
        super(LDAPUser, self).save(*args, **kwargs)
Exemplo n.º 9
0
 def test_create_existing_user(self):
     """Don't create new user if username already exists"""
     self.assertTrue(utils.username_exists(self.user))
     self.assertFalse(utils.create_user(self.user, self.password,
                                        self.email,
                                        self.first_name, self.last_name))
Exemplo n.º 10
0
 def test_create_existing_user(self):
     """Don't create new user if username already exists"""
     self.assertTrue(utils.username_exists(self.user))
     self.assertFalse(
         utils.create_user(self.user, self.password, self.email,
                           self.first_name, self.last_name))