diff --git a/accounts/backends.py b/accounts/backends.py index 77aa509..3fb208b 100644 --- a/accounts/backends.py +++ b/accounts/backends.py @@ -1,7 +1,30 @@ from django.contrib.auth.backends import RemoteUserBackend +from accounts.models import UserInstance, UserAttributes +from instances.models import Instance class MyRemoteUserBackend(RemoteUserBackend): + + #create_unknown_user = True + default_instances = [ 'debian8-template' ] + + def create_missing_userattributes(self, user): + try: + userattributes = user.userattributes + except UserAttributes.DoesNotExist: + userattributes = UserAttributes(user=user) + userattributes.save() + + def add_default_instances(self, user): + existing_instances = UserInstance.objects.filter(user=user) + if not existing_instances: + for instance_name in self.default_instances: + instance = Instance.objects.get(name=instance_name) + user_instance = UserInstance(user=user, instance=instance) + user_instance.save() + def configure_user(self, user): - user.is_superuser = True + #user.is_superuser = True + self.create_missing_userattributes(user) + self.add_default_instances(user) return user diff --git a/accounts/views.py b/accounts/views.py index 155ecc8..568eb6a 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -70,21 +70,11 @@ def accounts(request): :param request: :return: """ - - def create_missing_userattributes(users): - for user in users: - try: - userattributes = user.userattributes - except UserAttributes.DoesNotExist: - userattributes = UserAttributes(user=user) - userattributes.save() - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) error_messages = [] users = User.objects.all().order_by('username') - create_missing_userattributes(users) allow_empty_password = settings.ALLOW_EMPTY_PASSWORD if request.method == 'POST': diff --git a/webvirtcloud/settings.py b/webvirtcloud/settings.py index 0c31433..9e0ff3a 100644 --- a/webvirtcloud/settings.py +++ b/webvirtcloud/settings.py @@ -45,8 +45,8 @@ MIDDLEWARE_CLASSES = ( ) AUTHENTICATION_BACKENDS = ( - 'django.contrib.auth.backends.RemoteUserBackend', - #'accounts.backends.MyRemoteUserBackend', + #'django.contrib.auth.backends.RemoteUserBackend', + 'accounts.backends.MyRemoteUserBackend', ) LOGIN_URL = '/accounts/login'