Permalink
Browse files

Ask for new password on login screen for the first time

  • Loading branch information...
william-gr committed Nov 4, 2013
1 parent bb6e71e commit 9cb8d5bdb09c4594c92ac7fe965fb582c54ed32d
Showing with 59 additions and 12 deletions.
  1. +51 −2 gui/account/forms.py
  2. +6 −1 gui/account/views.py
  3. +2 −9 gui/templates/registration/login.html
View
@@ -28,10 +28,11 @@
import os
import re
-from django.db import transaction
+from django.contrib.auth import authenticate
+from django.contrib.auth.models import User as django_User
from django.core.urlresolvers import reverse
+from django.db import transaction
from django.utils.translation import ugettext as __, ugettext_lazy as _
-from django.contrib.auth.models import User as django_User
from django.utils.safestring import mark_safe
from django.http import QueryDict
@@ -44,6 +45,54 @@
log = logging.getLogger('account.forms')
+class NewPasswordForm(Form):
+
+ def __init__(self, request=None, *args, **kwargs):
+ super(NewPasswordForm, self).__init__(*args, **kwargs)
+
+ password = forms.CharField(
+ label=_('New Password'),
+ widget=forms.widgets.PasswordInput(),
+ )
+
+ confirm_password = forms.CharField(
+ label=_('Confirm New Password'),
+ widget=forms.widgets.PasswordInput(),
+ )
+
+ def clean_confirm_password(self):
+ p1 = self.cleaned_data.get('password')
+ p2 = self.cleaned_data.get('confirm_password')
+ if p1 != p2:
+ raise forms.ValidationError(_('Passwords do not match'))
+
+ def get_user(self):
+ return self.user_cache
+
+ def is_valid(self):
+ valid = super(NewPasswordForm, self).is_valid()
+ if valid:
+ qs = models.bsdUsers.objects.filter(bsdusr_uid=0, bsdusr_unixhash='*')
+ if qs.exists():
+ print "hm"
+ user = qs[0]
+ _notifier = notifier()
+ unixhash, smbhash = _notifier.user_changepassword(
+ username=str(user.bsdusr_username),
+ password=self.cleaned_data['password'].encode('utf-8'),
+ )
+ user.bsdusr_unixhash = unixhash
+ user.bsdusr_smbhash = smbhash
+ self.user_cache = authenticate(
+ username=user.bsdusr_username,
+ password=self.cleaned_data['password'].encode('utf-8'),
+ )
+ user.save()
+ _notifier.reload("user")
+ print "wtf"
+ return valid
+
+
class bsdUserGroupMixin:
def _populate_shell_choices(self):
with open('/etc/shells') as fd:
View
@@ -34,7 +34,7 @@
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.forms import AuthenticationForm
-from freenasUI.account import forms
+from freenasUI.account import forms, models
from freenasUI.common.freenasldap import FLAGS_DBINIT
from freenasUI.common.freenascache import (
FLAGS_CACHE_READ_USER, FLAGS_CACHE_WRITE_USER, FLAGS_CACHE_READ_GROUP,
@@ -161,6 +161,11 @@ def login_wrapper(
'sw_login_version': get_sw_login_version(),
'sw_name': get_sw_name(),
})
+ qs = models.bsdUsers.objects.filter(bsdusr_uid=0).exclude(
+ bsdusr_unixhash='*'
+ )
+ if not qs.exists():
+ authentication_form = forms.NewPasswordForm
response = login(
request,
template_name='registration/login.html',
@@ -7,6 +7,7 @@
"dojo/parser",
"dojo/fx/easing",
"dijit/_base/manager",
+ "dijit/form/TextBox",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dijit/layout/TabContainer",
@@ -44,17 +45,9 @@
<p>{% blocktrans with user.username as username %}Welcome, {{ username }}. Thanks for logging in.{% endblocktrans %}</p>
{% else %}
<div id="login" data-dojo-type="dijit.Dialog" data-dojo-props="title: '{% blocktrans with sw_login_version=sw_login_version sw_name=sw_name %}Welcome to {{ sw_name }}&trade; {{ sw_login_version }}{% endblocktrans %}'">
- <h3>{% trans "Please log in." %}</h3>
<form action="." method="post">{% csrf_token %}
<table>
-<tr>
- <td>{{ form.username.label_tag }}</td>
- <td>{{ form.username }}</td>
-</tr>
-<tr>
- <td>{{ form.password.label_tag }}</td>
- <td>{{ form.password }}</td>
-</tr>
+ {{ form }}
</table>
<button data-dojo-type="dijit.form.Button" type="submit" data-dojo-props="type:'submit'">{% trans "Log In" %}</button>
<input type="hidden" name="next" value="{{ next }}" />

0 comments on commit 9cb8d5b

Please sign in to comment.