Add simple rdn selection

This commit is contained in:
Götz 2019-03-28 03:19:41 +01:00
parent c35c59ebfc
commit 2147439e6c
15 changed files with 98 additions and 4 deletions

View File

@ -0,0 +1,10 @@
from django import forms
from rdn_helper.models import LdapUserRDN
class AddLDAPUserForm(forms.Form):
rdn = forms.ModelChoiceField(queryset=LdapUserRDN.objects.all())
username = forms.CharField(label='username', max_length=400)
first_name = forms.CharField(label='first_name', max_length=400)
last_name = forms.CharField(label='last_name', max_length=400)
password = forms.CharField(widget=forms.PasswordInput)

View File

@ -1,4 +1,5 @@
# Create your models here.
from django.db import models
from ldapdb.models import fields as ldap_fields
from ldapdb.models.base import Model

View File

@ -2,6 +2,7 @@ from django.urls import path
from . import views
urlpatterns = [
path('user-list/', views.userlist, name='userlist'),
path('user/<str:dn>', views.userlist, name='user'),
path('user/list/', views.userlist, name='user-list'),
path('user/get/<str:dn>/', views.userlist, name='user'),
path('user/add/', views.adduser, name='user-add'),
]

View File

@ -1,8 +1,9 @@
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.shortcuts import render, redirect
from .models import LdapGroup, LdapUser
from django.contrib.auth.models import User
from .forms import AddLDAPUserForm
# @login_required
@ -33,3 +34,27 @@ def changelist(request, dn):
user = User.objects.get(dn=dn)
context = {'user': user, }
return render(request, 'user_detail.jinja', context)
def adduser(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = AddLDAPUserForm(request.POST)
# check whether it's valid:
if form.is_valid():
rdn = form.cleaned_data['rdn']
username = form.cleaned_data['username']
password = form.cleaned_data['password']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
LdapUser.objects.create(rdn=rdn, username=username,
password=password, first_name=first_name,
last_name=last_name, )
return redirect('user-list')
# if a GET (or any other method) we'll create a blank form
else:
form = AddLDAPUserForm()
return render(request, 'user_add.jinja', {'form': form})

View File

@ -37,7 +37,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'account_manager.apps.AccountManagerConfig',
'account_manager',
'rdn_helper',
]
MIDDLEWARE = [

0
rdn_helper/__init__.py Normal file
View File

3
rdn_helper/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
rdn_helper/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class RdnHelperConfig(AppConfig):
name = 'rdn_helper'

View File

View File

@ -0,0 +1,21 @@
from django.core.management.base import BaseCommand, CommandError
from rdn_helper.models import LdapGroupRDN, LdapUserRDN
LDAP_OUS = ['ou=fs_wiai,ou=fachschaften', 'ou=fs_sowi,ou=fachschaften']
class Command(BaseCommand):
help = 'Load Possible User Dns in LDAP'
def handle(self, *args, **options):
added_groups_rdn = 0
added_user_rdn = 0
for ou in LDAP_OUS:
_, group_created = LdapGroupRDN.objects.get_or_create(rdn=f'ou=groups,{ou}')
_, user_created = LdapUserRDN.objects.get_or_create(rdn=f'ou=people,{ou}')
if group_created:
added_groups_rdn += 1
if user_created:
added_user_rdn += 1
print(f'Added {added_user_rdn} user rdns\nAdded {added_groups_rdn} group rdns')

16
rdn_helper/models.py Normal file
View File

@ -0,0 +1,16 @@
from django.db import models
# Create your models here.
class LdapUserRDN(models.Model):
rdn = models.CharField(max_length=400, unique=True)
def __str__(self):
return self.rdn
class LdapGroupRDN(models.Model):
rdn = models.CharField(max_length=400, unique=True)
def __str__(self):
return self.rdn

3
rdn_helper/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
rdn_helper/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

5
templates/user_add.jinja Normal file
View File

@ -0,0 +1,5 @@
<form action="{{ url('user-add') }}" method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
{{ form.as_p() }}
<input type="submit" value="Submit">
</form>