diff --git a/.gitignore b/.gitignore index 289210e..f90e8ec 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ tags dhcpd.* webvirtcloud/settings.py *migrations/* +.coverage +htmlcov \ No newline at end of file diff --git a/accounts/__init__.py b/accounts/__init__.py index e69de29..8319823 100644 --- a/accounts/__init__.py +++ b/accounts/__init__.py @@ -0,0 +1 @@ +default_app_config = 'accounts.apps.AccountsConfig' diff --git a/accounts/admin.py b/accounts/admin.py deleted file mode 100644 index 8c38f3f..0000000 --- a/accounts/admin.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/accounts/apps.py b/accounts/apps.py new file mode 100644 index 0000000..94f4d27 --- /dev/null +++ b/accounts/apps.py @@ -0,0 +1,51 @@ +from django.apps import AppConfig +from django.db.models.signals import post_migrate + + +def apply_change_password(sender, **kwargs): + ''' + Apply new change_password permission for all users + Depending on settings SHOW_PROFILE_EDIT_PASSWORD + ''' + from django.conf import settings + from django.contrib.auth.models import User, Permission + if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'): + print('\033[92mSHOW_PROFILE_EDIT_PASSWORD is found inside settings.py\033[0m') + print('\033[92mApplying permission can_change_password for all users\033[0m') + users = User.objects.all() + permission = Permission.objects.get(codename='change_password') + if settings.SHOW_PROFILE_EDIT_PASSWORD: + print('\033[91mWarning!!! Setting to True for all users\033[0m') + for user in users: + user.user_permissions.add(permission) + else: + print('\033[91mWarning!!! Setting to False for all users\033[0m') + for user in users: + user.user_permissions.remove(permission) + print('\033[1mDon`t forget to remove the option from settings.py\033[0m') + + +def create_admin(sender, **kwargs): + ''' + Create initial admin user + ''' + from django.contrib.auth.models import User + from accounts.models import UserAttributes + + plan = kwargs['plan'] + for migration, rolled_back in plan: + if migration.app_label == 'accounts' and migration.name == '0001_initial' and not rolled_back: + if User.objects.count() == 0: + print('\033[92mCreating default admin user\033[0m') + admin = User.objects.create_superuser('admin', None, 'admin') + UserAttributes(user=admin, max_instances=-1, max_cpus=-1, max_memory=-1, max_disk_size=-1).save() + break + + +class AccountsConfig(AppConfig): + name = 'accounts' + verbose_name = 'Accounts' + + def ready(self): + post_migrate.connect(apply_change_password, sender=self) + post_migrate.connect(create_admin, sender=self) diff --git a/accounts/backends.py b/accounts/backends.py deleted file mode 100644 index e66b94a..0000000 --- a/accounts/backends.py +++ /dev/null @@ -1,13 +0,0 @@ -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 - - def configure_user(self, user): - #user.is_superuser = True - UserAttributes.configure_user(user) - return user - diff --git a/accounts/migrations/0002_addAdmin.py b/accounts/migrations/0002_addAdmin.py deleted file mode 100644 index 09a91fc..0000000 --- a/accounts/migrations/0002_addAdmin.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 2.2.10 on 2020-01-28 07:01 - -from django.db import migrations - - -def add_useradmin(apps, schema_editor): - from django.utils import timezone - from django.contrib.auth.models import User - from accounts.models import UserAttributes - - admin = User.objects.create_superuser('admin', None, 'admin', last_login=timezone.now()) - UserAttributes(user=admin, max_instances=-1, max_cpus=-1, max_memory=-1, max_disk_size=-1).save() - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0001_initial'), - ] - - operations = [ - migrations.RunPython(add_useradmin), - ] diff --git a/accounts/migrations/0003_permissionset.py b/accounts/migrations/0002_permissionset.py similarity index 91% rename from accounts/migrations/0003_permissionset.py rename to accounts/migrations/0002_permissionset.py index d7cf465..5b36210 100644 --- a/accounts/migrations/0003_permissionset.py +++ b/accounts/migrations/0002_permissionset.py @@ -6,7 +6,7 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('accounts', '0002_addAdmin'), + ('accounts', '0001_initial'), ] operations = [ @@ -16,7 +16,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ], options={ - 'permissions': (('change_password', 'Can change password'),), + 'permissions': (('change_password', 'Can change password'), ), 'managed': False, 'default_permissions': (), }, diff --git a/accounts/migrations/0004_apply_change_password.py b/accounts/migrations/0004_apply_change_password.py deleted file mode 100644 index f313b7e..0000000 --- a/accounts/migrations/0004_apply_change_password.py +++ /dev/null @@ -1,25 +0,0 @@ -from django.db import migrations - - -def apply_change_password(apps, schema_editor): - from django.conf import settings - from django.contrib.auth.models import User, Permission - - if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'): - if settings.SHOW_PROFILE_EDIT_PASSWORD: - permission = Permission.objects.get(codename='change_password') - users = User.objects.all() - user: User - for user in users: - user.user_permissions.add(permission) - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0003_permissionset'), - ] - - operations = [ - migrations.RunPython(apply_change_password), - ] diff --git a/accounts/migrations/0005_remove_userattributes_can_clone_instances.py b/accounts/migrations/0005_remove_userattributes_can_clone_instances.py deleted file mode 100644 index 76e0458..0000000 --- a/accounts/migrations/0005_remove_userattributes_can_clone_instances.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2.12 on 2020-05-28 04:24 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0004_apply_change_password'), - ('instances', '0003_migrate_can_clone_instances'), - ] - - operations = [ - migrations.RemoveField( - model_name='userattributes', - name='can_clone_instances', - ), - ] diff --git a/accounts/models.py b/accounts/models.py index 8484a7e..33b6251 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -29,6 +29,7 @@ class UserSSHKey(models.Model): class UserAttributes(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) + can_clone_instances = models.BooleanField(default=True) max_instances = models.IntegerField(default=1, help_text="-1 for unlimited. Any integer value", validators=[ diff --git a/accounts/tests.py b/accounts/tests.py index 7ce503c..4d6c01e 100644 --- a/accounts/tests.py +++ b/accounts/tests.py @@ -1,3 +1,28 @@ -from django.test import TestCase +from django.contrib.auth.models import User +from django.shortcuts import reverse +from django.test import Client, TestCase -# Create your tests here. + +class AccountsTestCase(TestCase): + def setUp(self): + self.client.login(username='admin', password='admin') + User.objects.create_user(username='test', password='test') + + def test_profile(self): + response = self.client.get(reverse('profile')) + self.assertEqual(response.status_code, 200) + + response = self.client.get(reverse('account', args=[2])) + self.assertEqual(response.status_code, 200) + + def test_login_logout(self): + user = User.objects.get(username='test') + self.assertEqual(user.id, 2) + + client = Client() + + response = client.post(reverse('login'), {'username': 'test', 'password': 'test'}) + self.assertRedirects(response, reverse('profile')) + + response = client.get(reverse('logout')) + self.assertRedirects(response, reverse('login')) diff --git a/admin/forms.py b/admin/forms.py index 29c87b8..5c658aa 100644 --- a/admin/forms.py +++ b/admin/forms.py @@ -91,4 +91,4 @@ class UserCreateForm(UserForm): class UserAttributesForm(forms.ModelForm): class Meta: model = UserAttributes - exclude = ['user'] + exclude = ['user', 'can_clone_instances'] diff --git a/admin/templates/admin/common/form.html b/admin/templates/admin/common/form.html index 4ff3fd3..ced001e 100644 --- a/admin/templates/admin/common/form.html +++ b/admin/templates/admin/common/form.html @@ -11,6 +11,7 @@