Commit a0a784e7 by Bach Dániel

Merge branch 'feature-create-user' into 'master'

Feature Create User

tests 🆗 ~~👷 👷 ~~
parents e6f7e48a ea6124b3
......@@ -32,6 +32,7 @@ from crispy_forms.layout import (
from crispy_forms.utils import render_field
from django import forms
from django.contrib.auth.forms import UserCreationForm as OrgUserCreationForm
from django.forms.widgets import TextInput
from django.template import Context
from django.template.loader import render_to_string
......@@ -1086,3 +1087,25 @@ class CirclePasswordChangeForm(PasswordChangeForm):
css_class="btn btn-primary",
css_id="submit-password-button"))
return helper
class UserCreationForm(OrgUserCreationForm):
class Meta:
model = User
fields = ("username", 'email', 'first_name', 'last_name')
@property
def helper(self):
helper = FormHelper()
helper.layout = Layout('username', 'password1', 'password2', 'email',
'first_name', 'last_name')
helper.add_input(Submit("submit", _("Save")))
return helper
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
......@@ -42,6 +42,11 @@
<form action="" method="post">{% csrf_token %}
<table class="table table-striped table-with-form-fields table-bordered" id="group-detail-user-table">
<tbody>
{% if perms.auth.add_user %}
<p class="pull-right">
<a href="{% url "dashboard.views.create-user" group.pk %}" class="btn btn-success">{% trans "Create user" %}</a>
</p>
{% endif %}
<thead><tr><th></th><th>{% trans "Who" %}</th><th>{% trans "Remove" %}</th></tr></thead>
{% for i in users %}
<tr>
......
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
{% endblock %}
......@@ -1475,6 +1475,47 @@ class GroupDetailTest(LoginMixin, TestCase):
self.assertEqual(acl_groups - 1, len(gp.get_groups_with_level()))
self.assertEqual(response.status_code, 302)
def test_unpermitted_user_add_wo_group_perm(self):
user_count = self.g1.user_set.count()
c = Client()
self.login(c, 'user1')
self.u1.user_permissions.add(Permission.objects.get(
name='Can add user'))
response = c.post('/dashboard/group/%d/create/' % self.g1.pk,
{'username': 'userx1',
'password1': 'test123',
'password2': 'test123'})
self.assertEqual(response.status_code, 403)
self.assertEqual(user_count, self.g1.user_set.count())
def test_permitted_user_add_wo_can_add_user_perm(self):
user_count = self.g1.user_set.count()
c = Client()
self.login(c, 'user0')
response = c.post('/dashboard/group/%d/create/' % self.g1.pk,
{'username': 'userx2',
'password1': 'test123',
'password2': 'test123'})
self.assertRedirects(
response,
'/accounts/login/?next=/dashboard/group/%d/create/' % self.g1.pk)
self.assertEqual(response.status_code, 302)
self.assertEqual(user_count, self.g1.user_set.count())
def test_permitted_user_add(self):
user_count = self.g1.user_set.count()
self.u0.user_permissions.add(Permission.objects.get(
name='Can add user'))
c = Client()
self.login(c, 'user0')
response = c.post('/dashboard/group/%d/create/' % self.g1.pk,
{'username': 'userx2',
'password1': 'test123',
'password2': 'test123'})
self.assertRedirects(response, '/dashboard/group/%d/' % self.g1.pk)
self.assertEqual(user_count + 1, self.g1.user_set.count())
self.assertEqual(response.status_code, 302)
class GroupListTest(LoginMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
......
......@@ -33,6 +33,7 @@ from .views import (
GroupRemoveAclUserView, GroupRemoveAclGroupView, GroupRemoveUserView,
GroupCreate,
TemplateChoose,
UserCreationView,
)
urlpatterns = patterns(
......@@ -144,4 +145,7 @@ urlpatterns = patterns(
name="dashboard.views.remove-user"),
url(r'^group/create/$', GroupCreate.as_view(),
name='dashboard.views.group-create'),
url(r'^group/(?P<group_pk>\d+)/create/$',
UserCreationView.as_view(),
name="dashboard.views.create-user"),
)
......@@ -48,12 +48,14 @@ from django.template import RequestContext
from django.forms.models import inlineformset_factory
from django_tables2 import SingleTableView
from braces.views import LoginRequiredMixin, SuperuserRequiredMixin
from braces.views import (LoginRequiredMixin, SuperuserRequiredMixin,
PermissionRequiredMixin)
from braces.views._access import AccessMixin
from .forms import (
CircleAuthenticationForm, DiskAddForm, HostForm, LeaseForm, MyProfileForm,
NodeForm, TemplateForm, TraitForm, VmCustomizeForm, GroupCreateForm,
UserCreationForm,
CirclePasswordChangeForm
)
......@@ -2576,6 +2578,34 @@ class InstanceActivityDetail(SuperuserRequiredMixin, DetailView):
return ctx
class UserCreationView(LoginRequiredMixin, PermissionRequiredMixin,
CreateView):
form_class = UserCreationForm
model = User
template_name = 'dashboard/user-create.html'
permission_required = "auth.add_user"
def get_group(self, group_pk):
self.group = get_object_or_404(Group, pk=group_pk)
if not self.group.profile.has_level(self.request.user, 'owner'):
raise PermissionDenied()
def get(self, *args, **kwargs):
self.get_group(kwargs.pop('group_pk'))
return super(UserCreationView, self).get(*args, **kwargs)
def post(self, *args, **kwargs):
group_pk = kwargs.pop('group_pk')
self.get_group(group_pk)
ret = super(UserCreationView, self).post(*args, **kwargs)
if self.object:
self.object.groups.add(self.group)
return redirect(
reverse('dashboard.views.group-detail', args=[group_pk]))
else:
return ret
class InterfaceDeleteView(DeleteView):
model = Interface
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment