diff --git a/account/cbase_members.py b/account/cbase_members.py index 47dcbfa..b3fe08b 100644 --- a/account/cbase_members.py +++ b/account/cbase_members.py @@ -100,11 +100,14 @@ class MemberValues(object): result[key] = self.get(key) return result - def _get_bind_dn(self): + def _get_bind_dn(self, username=None): """ Adds the uid=userid, to the base dn and returns that. """ - bind_dn = 'uid=%s,' % self._username + if not username: + bind_dn = 'uid=%s,' % self._username + else: + bind_dn = 'uid=%s,' % username bind_dn += settings.CBASE_BASE_DN return bind_dn @@ -128,5 +131,39 @@ class MemberValues(object): # TODO: latin1 print "result is: ", result # TODO: if len(result)==0 - return result[0][1] session.unbind_s() + return result[0][1] + + def admin_change_password(self, username, new_password): + """ + Change the password of the member. + You do not need to call save() after calling change_password(). + """ + l = ldap.initialize(settings.CBASE_LDAP_URL) + user_dn = self._get_bind_dn() + l.simple_bind_s(user_dn, self._password) + l.passwd_s(self._get_bind_dn(username), None, new_password) + l.unbind_s() + + def list_users(self): + l = ldap.initialize(settings.CBASE_LDAP_URL) + user_dn = self._get_bind_dn() + l.simple_bind_s(user_dn, self._password) + try: + ldap_result_id = l.search(settings.CBASE_BASE_DN, ldap.SCOPE_SUBTREE, "memberOf=cn=crew,ou=groups,dc=c-base,dc=org", None) + result_set = [] + while 1: + result_type, result_data = l.result(ldap_result_id, 0) + if (result_data == []): + break + else: + ## here you don't have to append to a list + ## you could do whatever you want with the individual entry + ## The appending to list is just for illustration. + if result_type == ldap.RES_SEARCH_ENTRY: + result_set.append(result_data) + + userlist = [x[0][1]['uid'][0] for x in result_set] + return sorted(userlist) + except: + return [] \ No newline at end of file diff --git a/account/forms.py b/account/forms.py index 01b5f24..84f4fc1 100644 --- a/account/forms.py +++ b/account/forms.py @@ -121,4 +121,36 @@ class NRF24Form(forms.Form): class CLabPinForm(forms.Form): c_lab_pin1 = GastroPinField(label=_('New c-lab PIN')) c_lab_pin2 = GastroPinField(label=_('Repeat c-lab PIN'), - help_text=_('Numerical only, 4 to 6 digits')) \ No newline at end of file + help_text=_('Numerical only, 4 to 6 digits')) + + +class AdminForm(forms.Form): + password1 = forms.CharField(max_length=255, widget=forms.PasswordInput, + label=_('New password')) + password2 = forms.CharField(max_length=255, widget=forms.PasswordInput, + label=_('Repeat password')) + + + def __init__(self, *args, **kwargs): + self._request = kwargs.pop('request', None) + self._users = kwargs.pop('users', []) + choices = [(x, x) for x in self._users] + choices.insert(0, ('', 'Select username ...')) + super(AdminForm, self).__init__(*args, **kwargs) + self.fields.insert(0, 'username', forms.ChoiceField(choices=choices, + help_text=_('Select the username for whom you want to reset the password.'))) + + def clean(self): + cleaned_data = super(AdminForm, self).clean() + + password1 = cleaned_data.get('password1') + password2 = cleaned_data.get('password2') + if password1 != password2: + raise forms.ValidationError( + _('The new passwords were not identical.'), + code='not_identical') + + return cleaned_data + + def get_member_choices(self): + return [(x, x) for x in self._users] \ No newline at end of file diff --git a/account/templates/access_denied.html b/account/templates/access_denied.html new file mode 100644 index 0000000..dfe4e30 --- /dev/null +++ b/account/templates/access_denied.html @@ -0,0 +1,9 @@ +{% extends "member_base.html" %} +{% load i18n %} +{% load crispy_forms_tags %} + +{% block form_title %}{% trans "Password"%}{% endblock %} + +{% block container %} +
{% blocktrans %}You can change other users passwords here.{% endblocktrans %}
+{% endblock %} + +{% block form_fields %} + +{% endblock form_fields %} \ No newline at end of file diff --git a/account/templates/member_base.html b/account/templates/member_base.html index 67bfd7d..6b850e9 100644 --- a/account/templates/member_base.html +++ b/account/templates/member_base.html @@ -36,6 +36,15 @@