diff --git a/ipynbsrv/admin/admin.py b/ipynbsrv/admin/admin.py index 891d68a..b53a461 100644 --- a/ipynbsrv/admin/admin.py +++ b/ipynbsrv/admin/admin.py @@ -1,15 +1,13 @@ from django_admin_conf_vars.models import ConfigurationVariable -from django.conf.urls import patterns, url +from django.conf.urls import patterns from django.contrib import admin, messages from django.contrib.auth.admin import GroupAdmin from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from ipynbsrv.admin.forms import CollaborationGroupAdminForm, ShareAdminForm -from ipynbsrv.core.auth.authentication_backends import BackendProxyAuthentication -from ipynbsrv.core.helpers import get_user_backend_connected from ipynbsrv.core.models import * -from ipynbsrv.contract.backends import GroupBackend, UserBackend +from ipynbsrv.core.management.commands import import_users class CoreAdminSite(admin.AdminSite): @@ -571,25 +569,13 @@ def get_urls(self): def import_users(self, request): # custom view which should return an HttpResponse try: - backend = get_user_backend_connected() - users = backend.get_users() - helper = BackendProxyAuthentication() - new_users = [] - for user in users: - username = str(user.get(UserBackend.FIELD_PK)) - password = '' - obj = User.objects.filter(username=username) - if not obj: - # if user is not existing yet, create him - uid = BackendUser.generate_internal_uid() - group = helper.create_user_groups(username, uid) - user = helper.create_users(username, password, uid, group.backend_group) - group.add_user(user.backend_user) - new_users.append(username) + # Todo: imports + new_users = import_users.import_users() + if len(new_users) == 0: self.message_user(request, "All users already imported.", messages.INFO) else: - self.message_user(request, "Successfully imported {} users. ({})".format(len(new_users), ', '.join(new_users))) + self.message_user(request, "Successfully imported {} users: {}".format(len(new_users), ', '.join(new_users))) except Exception: self.message_user(request, "Operation failed.", messages.ERROR) return HttpResponseRedirect(reverse('admin:auth_user_changelist')) diff --git a/ipynbsrv/core/management/__init__.py b/ipynbsrv/core/management/__init__.py new file mode 100644 index 0000000..de40ea7 --- /dev/null +++ b/ipynbsrv/core/management/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) diff --git a/ipynbsrv/core/management/commands/__init__.py b/ipynbsrv/core/management/commands/__init__.py new file mode 100644 index 0000000..de40ea7 --- /dev/null +++ b/ipynbsrv/core/management/commands/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) diff --git a/ipynbsrv/core/management/commands/import_users.py b/ipynbsrv/core/management/commands/import_users.py new file mode 100644 index 0000000..c9486e0 --- /dev/null +++ b/ipynbsrv/core/management/commands/import_users.py @@ -0,0 +1,49 @@ +from django.core.management.base import BaseCommand, CommandError +from django.contrib.auth.models import User +from ipynbsrv.core.auth.authentication_backends import BackendProxyAuthentication +from ipynbsrv.core.helpers import get_user_backend_connected +from ipynbsrv.core.models import * +from ipynbsrv.contract.backends import UserBackend + + +def import_users(): + """ + Imports all the users found on the user backend into django. + """ + backend = get_user_backend_connected() + users = backend.get_users() + helper = BackendProxyAuthentication() + new_users = [] + for user in users: + username = str(user.get(UserBackend.FIELD_PK)) + password = '' + obj = User.objects.filter(username=username) + if not obj: + # if user is not existing yet, create him + uid = BackendUser.generate_internal_uid() + group = helper.create_user_groups(username, uid) + user = helper.create_users(username, password, uid, group.backend_group) + group.add_user(user.backend_user) + new_users.append(username) + return new_users + + +class Command(BaseCommand): + + """ + Custom manage.py command to import users from the user backend. + + https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/ + """ + + help = 'Import all users from the UserBackend defined in the config vars.' + + # def add_arguments(self, parser): + # Todo: make command more powerful, to import single users by username + # parser.add_argument('username', nargs='+', type=str) + + def handle(self, *args, **options): + new_users = import_users() + self.stdout.write("Successfully imported {} users:".format(len(new_users))) + for user in new_users: + self.stdout.write(user)