Commit 9deb2f3b by Őry Máté

dashboard: allow creating group with org_id

fixes #72
parent a1481faf
......@@ -33,7 +33,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.forms.widgets import TextInput, HiddenInput
from django.template import Context
from django.template.loader import render_to_string
from django.utils.translation import ugettext as _
......@@ -316,50 +316,40 @@ class VmCustomizeForm(forms.Form):
class GroupCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
new_groups = kwargs.pop('new_groups', None)
super(GroupCreateForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_show_labels = False
self.helper.layout = Layout(
Div(
Div(
AnyTag(
'h4',
HTML(_("Name")),
),
css_class="col-sm-10",
),
css_class="row",
),
Div(
Div(
Field('name', id="group-create-name"),
css_class="col-sm-10",
),
css_class="row",
),
choices = [('', '--')]
if new_groups:
choices += [(g, g) for g in new_groups if len(g) <= 64]
self.fields['org_id'] = forms.ChoiceField(
# TRANSLATORS: directory like in LDAP
choices=choices, required=False, label=_('Directory identifier'))
if not new_groups:
self.fields['org_id'].widget = HiddenInput()
Div( # buttons
Div(
AnyTag( # tip: don't try to use Button class
"button",
AnyTag(
"i",
css_class="icon-play"
),
HTML(" Create"),
css_id="vm-create-submit",
css_class="btn btn-success",
def save(self, commit=True):
if not commit:
raise AttributeError('Committing is mandatory.')
group = super(GroupCreateForm, self).save()
),
css_class="col-sm-5",
),
css_class="row",
),
)
orgid = self.cleaned_data['org_id']
if orgid:
profile = group.profile
profile.org_id = orgid
profile.save()
return group
@property
def helper(self):
helper = FormHelper(self)
helper.add_input(Submit("submit", _("Create")))
helper.form_tag = False
return helper
class Meta:
model = Group
fields = ['name', ]
fields = ('name', )
class HostForm(forms.ModelForm):
......
......@@ -19,6 +19,9 @@
</div>
<div id="group-details-h1-name">
{{ group.name }}
{% if group.groupprofile.org_id %}
<small>{{group.groupprofile.org_id}}</small>
{% endif %}
</div>
</h1>
<div class="group-details-help js-hidden">
......
......@@ -1541,6 +1541,40 @@ class GroupCreate(LoginRequiredMixin, TemplateView):
form_class = GroupCreateForm
form = None
def get_available_group_codes(self, request):
newgroups = []
if saml_available:
from djangosaml2.cache import StateCache, IdentityCache
from djangosaml2.conf import get_config
from djangosaml2.views import _get_subject_id
from saml2.client import Saml2Client
state = StateCache(request.session)
conf = get_config(None, request)
client = Saml2Client(conf, state_cache=state,
identity_cache=IdentityCache(request.session),
logger=logger)
subject_id = _get_subject_id(request.session)
identity = client.users.get_identity(subject_id,
check_not_on_or_after=False)
if identity:
attributes = identity[0]
owneratrs = getattr(
settings, 'SAML_GROUP_OWNER_ATTRIBUTES', [])
groups = []
for i in owneratrs:
try:
groups += attributes[i]
except KeyError:
pass
for group in groups:
try:
GroupProfile.search(group)
except Group.DoesNotExist:
newgroups.append(group)
return newgroups
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/modal-wrapper.html']
......@@ -1551,7 +1585,8 @@ class GroupCreate(LoginRequiredMixin, TemplateView):
if not request.user.has_module_perms('auth'):
raise PermissionDenied()
if form is None:
form = self.form_class()
form = self.form_class(
new_groups=self.get_available_group_codes(request))
context = self.get_context_data(**kwargs)
context.update({
'template': 'dashboard/group-create.html',
......@@ -1564,7 +1599,8 @@ class GroupCreate(LoginRequiredMixin, TemplateView):
def post(self, request, *args, **kwargs):
if not request.user.has_module_perms('auth'):
raise PermissionDenied()
form = self.form_class(request.POST)
form = self.form_class(
request.POST, new_groups=self.get_available_group_codes(request))
if not form.is_valid():
return self.get(request, form, *args, **kwargs)
form.cleaned_data
......
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