default password and reset function

This commit is contained in:
Oskar Hahn 2011-09-03 17:17:29 +02:00
parent 28785c0ed4
commit e849716ee4
7 changed files with 60 additions and 21 deletions

View File

@ -9,10 +9,20 @@
:copyright: 2011 by the OpenSlides team, see AUTHORS. :copyright: 2011 by the OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details. :license: GNU GPL, see LICENSE for more details.
""" """
from random import choice
import string
from django.contrib.auth.models import User from django.contrib.auth.models import User
def gen_password():
chars = string.letters + string.digits
newpassword = ''
for i in range(8):
newpassword += choice(chars)
return newpassword
def gen_username(first_name, last_name): def gen_username(first_name, last_name):
testname = "%s%s" % (first_name, last_name) testname = "%s%s" % (first_name, last_name)
try: try:

View File

@ -12,24 +12,25 @@
from django.forms import Form, ModelForm, CharField, EmailField, FileField, FileInput, MultipleChoiceField from django.forms import Form, ModelForm, CharField, EmailField, FileField, FileInput, MultipleChoiceField
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.contrib.auth.forms import AdminPasswordChangeForm
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from participant.models import Profile from participant.models import Profile
class UserForm(ModelForm): class UserForm(ModelForm):
error_css_class = 'error' error_css_class = 'error'
required_css_class = 'required' required_css_class = 'required'
first_name = CharField(label=_("First name")) first_name = CharField(label=_("First name"))
last_name = CharField(label=_("Last name")) last_name = CharField(label=_("Last name"))
class Meta: class Meta:
model = User model = User
exclude = ('username', 'password', 'is_staff', 'last_login', 'date_joined', 'user_permissions') exclude = ('password', 'is_staff', 'last_login', 'date_joined', 'user_permissions')
class UsernameForm(ModelForm): class UsernameForm(ModelForm):
error_css_class = 'error' error_css_class = 'error'
required_css_class = 'required' required_css_class = 'required'
class Meta: class Meta:
model = User model = User
exclude = ('first_name', 'last_name', 'email', 'is_active','is_superuser', 'groups', 'password', 'is_staff', 'last_login', 'date_joined', 'user_permissions') exclude = ('first_name', 'last_name', 'email', 'is_active','is_superuser', 'groups', 'password', 'is_staff', 'last_login', 'date_joined', 'user_permissions')
@ -60,4 +61,4 @@ class UserImportForm(Form):
error_css_class = 'error' error_css_class = 'error'
required_css_class = 'required' required_css_class = 'required'
csvfile = FileField(widget=FileInput(attrs={'size':'50'}), label=_("CSV File")) csvfile = FileField(widget=FileInput(attrs={'size':'50'}), label=_("CSV File"))

View File

@ -14,6 +14,8 @@ from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from participant.api import gen_password
class Profile(models.Model): class Profile(models.Model):
GENDER_CHOICES = ( GENDER_CHOICES = (
('none', _('Not specified')), ('none', _('Not specified')),
@ -32,6 +34,11 @@ class Profile(models.Model):
group = models.CharField(max_length=100, null=True, blank=True, verbose_name = _("Group")) group = models.CharField(max_length=100, null=True, blank=True, verbose_name = _("Group"))
type = models.CharField(max_length=100, choices=TYPE_CHOICE, default='delegate', verbose_name = _("Typ")) type = models.CharField(max_length=100, choices=TYPE_CHOICE, default='delegate', verbose_name = _("Typ"))
committee = models.CharField(max_length=100, null=True, blank=True, verbose_name = _("Committee")) committee = models.CharField(max_length=100, null=True, blank=True, verbose_name = _("Committee"))
firstpassword = models.CharField(max_length=100, null=True, blank=True, verbose_name = _("First Password"))
def reset_password(self):
self.user.set_password(self.firstpassword)
def __unicode__(self): def __unicode__(self):
if self.group: if self.group:
@ -44,3 +51,10 @@ class Profile(models.Model):
('can_view_participants', "Can see the list of participants"), ('can_view_participants', "Can see the list of participants"),
('can_manage_participants', "Can manage the participant list"), ('can_manage_participants', "Can manage the participant list"),
) )
def set_first_user_passwords():
for user in Profile.objects.filter(firstpassword=''):
user.firstpassword = gen_password()
user.user.set_password(user.firstpassword)
user.user.save()
user.save()

View File

@ -14,6 +14,7 @@
<li><a href="{% url user_group_new %}">{%trans "New user group" %}</a></li> <li><a href="{% url user_group_new %}">{%trans "New user group" %}</a></li>
<li><a href="{% url user_print %}"><img src="/static/images/icons/application-pdf.png"> {%trans 'Print participant list' %}</a></li> <li><a href="{% url user_print %}"><img src="/static/images/icons/application-pdf.png"> {%trans 'Print participant list' %}</a></li>
<li><a href="{% url user_import %}"> {%trans 'Import' %}</a></li> <li><a href="{% url user_import %}"> {%trans 'Import' %}</a></li>
<li><a href="{% url user_gen_passwords %}">{% trans 'Set Default Passwords' %}</a></li>
{% endif %} {% endif %}
</ul> </ul>
{% endblock %} {% endblock %}

View File

@ -9,11 +9,11 @@
{% endif %} {% endif %}
<form action="" method="post">{% csrf_token %} <form action="" method="post">{% csrf_token %}
{% if edituser %}
{{ usernameform.as_p }}
{% endif %}
{{ userform.as_p }} {{ userform.as_p }}
{{ profileform.as_p }} {{ profileform.as_p }}
{% if edituser %}
<a href="{% url user_reset_passwords edituser.id %}">{% trans 'Reset Password' %}</a>
{% endif %}
<button type="submit"> <button type="submit">
<span class="icon ok">{%trans 'Save' %}</span> <span class="icon ok">{%trans 'Save' %}</span>
</button> </button>

View File

@ -27,6 +27,8 @@ urlpatterns = patterns('participant.views',
url(r'^participant/group/(?P<group_id>\d+)/edit$', 'group_edit', name='user_group_edit'), url(r'^participant/group/(?P<group_id>\d+)/edit$', 'group_edit', name='user_group_edit'),
url(r'^participant/group/(?P<group_id>\d+)/del$', 'group_delete', name='user_group_delete'), url(r'^participant/group/(?P<group_id>\d+)/del$', 'group_delete', name='user_group_delete'),
url(r'^user/settings$', 'user_settings', name='user_settings'), url(r'^user/settings$', 'user_settings', name='user_settings'),
url(r'^participant/genpasswords$', 'gen_passwords', name='user_gen_passwords'),
url(r'^participant/resetpassword/(?P<user_id>\d+)$', 'reset_password', name='user_reset_passwords'),
) )
urlpatterns += patterns('django.contrib.auth.views', urlpatterns += patterns('django.contrib.auth.views',

View File

@ -20,9 +20,9 @@ from django.contrib.auth.forms import SetPasswordForm
from django.contrib import messages from django.contrib import messages
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from participant.models import Profile from participant.models import Profile, set_first_user_passwords
from participant.api import gen_username from participant.api import gen_username
from participant.forms import UserForm, UsernameForm, ProfileForm, UsersettingsForm, UserImportForm, GroupForm from participant.forms import UserForm, UsernameForm, ProfileForm, UsersettingsForm, UserImportForm, GroupForm, AdminPasswordChangeForm
from utils.utils import template, permission_required, gen_confirm_form from utils.utils import template, permission_required, gen_confirm_form
from utils.pdf import print_userlist from utils.pdf import print_userlist
@ -51,7 +51,7 @@ def get_overview(request):
pass pass
if 'reverse' in request.GET: if 'reverse' in request.GET:
query = query.reverse() query = query.reverse()
userlist = query.all() userlist = query.all()
users = [] users = []
for user in userlist: for user in userlist:
@ -81,24 +81,18 @@ def edit(request, user_id=None):
if request.method == 'POST': if request.method == 'POST':
userform = UserForm(request.POST, instance=user, prefix="user") userform = UserForm(request.POST, instance=user, prefix="user")
usernameform = UsernameForm(request.POST, instance=user, prefix="username")
try: try:
profileform = ProfileForm(request.POST, instance=user.profile, prefix="profile") profileform = ProfileForm(request.POST, instance=user.profile, prefix="profile")
except: except:
profileform = ProfileForm(request.POST, prefix="profile") profileform = ProfileForm(request.POST, prefix="profile")
formlist = [userform, profileform] formlist = [userform, profileform]
formerror = 0 formerror = 0
if user:
formlist.append(usernameform)
for f in formlist: for f in formlist:
if not f.is_valid(): if not f.is_valid():
formerror += 1 formerror += 1
if formerror == 0: if formerror == 0:
user = userform.save() user = userform.save()
if user_id is None:
user.username = gen_username(user.first_name, user.last_name)
user.set_password("%s%s" % (user.first_name, user.last_name))
user.save()
profile = profileform.save(commit=False) profile = profileform.save(commit=False)
profile.user = user profile.user = user
profile.save() profile.save()
@ -110,14 +104,12 @@ def edit(request, user_id=None):
messages.error(request, _('Please check the form for errors.')) messages.error(request, _('Please check the form for errors.'))
else: else:
userform = UserForm(instance=user, prefix="user") userform = UserForm(instance=user, prefix="user")
usernameform = UsernameForm(instance=user, prefix="username")
try: try:
profileform = ProfileForm(instance=user.profile, prefix="profile") profileform = ProfileForm(instance=user.profile, prefix="profile")
except AttributeError: except AttributeError:
profileform = ProfileForm(prefix="profile") profileform = ProfileForm(prefix="profile")
return { return {
'userform': userform, 'userform': userform,
'usernameform': usernameform,
'profileform': profileform, 'profileform': profileform,
'edituser': user, 'edituser': user,
} }
@ -273,4 +265,23 @@ def user_import(request):
form = UserImportForm() form = UserImportForm()
return { return {
'form': form, 'form': form,
} }
@permission_required('participant.can_manage_participants')
def gen_passwords(request):
set_first_user_passwords()
return redirect(reverse('user_overview'))
@permission_required('participant.can_manage_participants')
def reset_password(request, user_id):
user = User.objects.get(pk=user_id)
if request.method == 'POST':
user.profile.reset_password()
user.profile.save()
messages.success(request, _('The Password for <b>%s</b> was successfully resettet') % user)
else:
gen_confirm_form(request, _('Do you really want to reset the password for <b>%s</b>') % user,
reverse('user_overview'))
return redirect(reverse('user_edit', args=[user_id]))