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 ( ...@@ -33,7 +33,7 @@ from crispy_forms.layout import (
from crispy_forms.utils import render_field from crispy_forms.utils import render_field
from django import forms from django import forms
from django.contrib.auth.forms import UserCreationForm as OrgUserCreationForm 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 import Context
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
...@@ -316,50 +316,40 @@ class VmCustomizeForm(forms.Form): ...@@ -316,50 +316,40 @@ class VmCustomizeForm(forms.Form):
class GroupCreateForm(forms.ModelForm): class GroupCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
new_groups = kwargs.pop('new_groups', None)
super(GroupCreateForm, self).__init__(*args, **kwargs) super(GroupCreateForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self) choices = [('', '--')]
self.helper.form_show_labels = False if new_groups:
self.helper.layout = Layout( choices += [(g, g) for g in new_groups if len(g) <= 64]
Div( self.fields['org_id'] = forms.ChoiceField(
Div( # TRANSLATORS: directory like in LDAP
AnyTag( choices=choices, required=False, label=_('Directory identifier'))
'h4', if not new_groups:
HTML(_("Name")), self.fields['org_id'].widget = HiddenInput()
),
css_class="col-sm-10",
),
css_class="row",
),
Div(
Div(
Field('name', id="group-create-name"),
css_class="col-sm-10",
),
css_class="row",
),
Div( # buttons def save(self, commit=True):
Div( if not commit:
AnyTag( # tip: don't try to use Button class raise AttributeError('Committing is mandatory.')
"button", group = super(GroupCreateForm, self).save()
AnyTag(
"i",
css_class="icon-play"
),
HTML(" Create"),
css_id="vm-create-submit",
css_class="btn btn-success",
), orgid = self.cleaned_data['org_id']
css_class="col-sm-5", if orgid:
), profile = group.profile
css_class="row", 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: class Meta:
model = Group model = Group
fields = ['name', ] fields = ('name', )
class HostForm(forms.ModelForm): class HostForm(forms.ModelForm):
......
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
</div> </div>
<div id="group-details-h1-name"> <div id="group-details-h1-name">
{{ group.name }} {{ group.name }}
{% if group.groupprofile.org_id %}
<small>{{group.groupprofile.org_id}}</small>
{% endif %}
</div> </div>
</h1> </h1>
<div class="group-details-help js-hidden"> <div class="group-details-help js-hidden">
......
...@@ -1541,6 +1541,40 @@ class GroupCreate(LoginRequiredMixin, TemplateView): ...@@ -1541,6 +1541,40 @@ class GroupCreate(LoginRequiredMixin, TemplateView):
form_class = GroupCreateForm form_class = GroupCreateForm
form = None 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): def get_template_names(self):
if self.request.is_ajax(): if self.request.is_ajax():
return ['dashboard/modal-wrapper.html'] return ['dashboard/modal-wrapper.html']
...@@ -1551,7 +1585,8 @@ class GroupCreate(LoginRequiredMixin, TemplateView): ...@@ -1551,7 +1585,8 @@ class GroupCreate(LoginRequiredMixin, TemplateView):
if not request.user.has_module_perms('auth'): if not request.user.has_module_perms('auth'):
raise PermissionDenied() raise PermissionDenied()
if form is None: 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 = self.get_context_data(**kwargs)
context.update({ context.update({
'template': 'dashboard/group-create.html', 'template': 'dashboard/group-create.html',
...@@ -1564,7 +1599,8 @@ class GroupCreate(LoginRequiredMixin, TemplateView): ...@@ -1564,7 +1599,8 @@ class GroupCreate(LoginRequiredMixin, TemplateView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
if not request.user.has_module_perms('auth'): if not request.user.has_module_perms('auth'):
raise PermissionDenied() 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(): if not form.is_valid():
return self.get(request, form, *args, **kwargs) return self.get(request, form, *args, **kwargs)
form.cleaned_data 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