diff options
author | Theo Chatzimichos <tampakrap@gentoo.org> | 2011-07-05 04:59:22 +0300 |
---|---|---|
committer | Theo Chatzimichos <tampakrap@gentoo.org> | 2011-07-05 04:59:22 +0300 |
commit | c24f08840c4d5eaaf7832eb3410d7c557698b81c (patch) | |
tree | e89674cd149a39afdbe401ca2339c8e9d16cbde3 | |
parent | Change LDAP backend once again: (diff) | |
download | identity.gentoo.org-c24f08840c4d5eaaf7832eb3410d7c557698b81c.tar.gz identity.gentoo.org-c24f08840c4d5eaaf7832eb3410d7c557698b81c.tar.bz2 identity.gentoo.org-c24f08840c4d5eaaf7832eb3410d7c557698b81c.zip |
New view, for password edit
-rw-r--r-- | accounts/forms.py | 6 | ||||
-rw-r--r-- | accounts/urls.py | 2 | ||||
-rw-r--r-- | accounts/views.py | 49 | ||||
-rw-r--r-- | templates/account/edit.html | 2 | ||||
-rw-r--r-- | templates/account/password.html | 9 |
5 files changed, 66 insertions, 2 deletions
diff --git a/accounts/forms.py b/accounts/forms.py new file mode 100644 index 0000000..c1864a6 --- /dev/null +++ b/accounts/forms.py @@ -0,0 +1,6 @@ +from django import forms + +class PasswordForm(forms.Form): + old_password = forms.CharField(max_length = 30, widget = forms.PasswordInput(), label = 'Old password:') + password1 = forms.CharField(max_length = 30, widget = forms.PasswordInput(), label = 'New Password:') + password2 = forms.CharField(max_length = 30, widget = forms.PasswordInput(), label = 'Verify Password:') diff --git a/accounts/urls.py b/accounts/urls.py index db8fd6c..1ecdd1c 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -5,4 +5,6 @@ urlpatterns = patterns('okupy.accounts.views', # (r'^$', redirect_to, {'url': '/account/%(username)s/'}), <--- doesn't work (r'^(?P<username>\w+)/$', 'account'), (r'^(?P<username>\w+)/edit/$', 'account_edit'), + (r'^(?P<username>\w+)/edit/password/$', 'account_edit_password'), + (r'^(?P<username>\w+)/edit/email/$', 'account_edit_email'), ) diff --git a/accounts/views.py b/accounts/views.py index 901e63b..e7be5e0 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -3,9 +3,11 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import render_to_response from django.template import RequestContext from okupy.accounts.backends import LDAPBackend +from okupy.accounts.forms import * from okupy.accounts.models import * +from okupy.libraries.encryption import sha1Password from okupy.libraries.exception import OkupyException, log_extra_data -from okupy.libraries.ldap_wrappers import ldap_user_search +from okupy.libraries.ldap_wrappers import * import logging logger = logging.getLogger('okupy') @@ -44,6 +46,7 @@ def account(request, username): msg = '' user = '' current_user_full = '' + shown_attrs = '' try: if not checkUsername(request, username): raise OkupyException('Invalid URL') @@ -119,3 +122,47 @@ def account_edit(request, username): 'account/edit.html', {'form': form, 'msg': msg}, context_instance = RequestContext(request)) + +@login_required +def account_edit_password(request, username): + msg = '' + form = '' + try: + if not request.user.username == username: + raise OkupyException('Invalid URL') + if request.method == 'POST': + form = PasswordForm(request.POST) + if form.is_valid(): + if form.cleaned_data['password1'] != form.cleaned_data['password2']: + raise OkupyException('Passwords don\'t match') + l = '' + for base_dn in settings.LDAP_BASE_DN: + try: + l = ldap_bind(username = username, password = form.cleaned_data['old_password'], base_dn = base_dn) + except: + pass + if l: + break + if l: + user = ldap_user_search(filter = username, l = l) + else: + raise OkupyException('Old password is wrong Or there is a problem with the LDAP server') + mod_attrs = [(ldap.MOD_DELETE, 'userPassword', None)] + mod_attrs2 = [(ldap.MOD_ADD, 'userPassword', sha1Password(form.cleaned_data['password1']))] + try: + l.modify_s(user[0][0], mod_attrs) + l.modify_s(user[0][0], mod_attrs2) + except Exception as error: + logger.error(error, extra = log_extra_data(request)) + raise OkupyException('Could not modify LDAP data') + l.unbind_s() + msg = 'Password changed successfully' + else: + form = PasswordForm() + except OkupyException as error: + msg = error.value + logger.error(msg, extra = log_extra_data(request)) + return render_to_response( + 'account/password.html', + {'form': form, 'msg': msg}, + context_instance = RequestContext(request)) diff --git a/templates/account/edit.html b/templates/account/edit.html index 44d2d94..4400896 100644 --- a/templates/account/edit.html +++ b/templates/account/edit.html @@ -6,4 +6,4 @@ {{ field.label_tag}}{{ field }}<br /> {% endfor %} <input class="button" type="submit" value="Edit" /> -{% endif %}
\ No newline at end of file +{% endif %} diff --git a/templates/account/password.html b/templates/account/password.html new file mode 100644 index 0000000..a2f304d --- /dev/null +++ b/templates/account/password.html @@ -0,0 +1,9 @@ +<form action="." method="POST">{% csrf_token %} + {% for field in form %}<br /> + {{ field.label_tag}}{{ field }}<br /> + {% endfor %} + <input class="button" type="submit" value="Submit" /> + {% if msg %} + <br />{{ msg }} + {% endif %} +</form> |