diff --git a/apps/assets/migrations/0001_initial.py b/apps/assets/migrations/0001_initial.py index 8a1733e68..d07b2ce0f 100644 --- a/apps/assets/migrations/0001_initial.py +++ b/apps/assets/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.10 on 2016-09-10 16:40 +# Generated by Django 1.10 on 2016-09-11 09:22 from __future__ import unicode_literals from django.db import migrations, models diff --git a/apps/perms/forms.py b/apps/perms/forms.py index 36cf3ff8f..1826b4c29 100644 --- a/apps/perms/forms.py +++ b/apps/perms/forms.py @@ -4,23 +4,33 @@ from __future__ import absolute_import, unicode_literals from django import forms from django.utils.translation import ugettext_lazy as _ -from .hands import User, UserGroup, Asset, AssetGroup, SystemUser -from .models import PermUserAsset +# from .hands import User, UserGroup, Asset, AssetGroup, SystemUser +from .models import AssetPermission -class PermUserAssetForm(forms.ModelForm): +class AssetPermissionForm(forms.ModelForm): + class Meta: - model = PermUserAsset + model = AssetPermission fields = [ - 'user', 'action', 'assets', 'asset_groups', 'system_users', 'date_expired', 'comment' + 'name', 'users', 'user_groups', 'assets', 'asset_groups', + 'system_users', 'action', 'is_active', 'date_expired', 'comment', ] widgets = { - 'user': forms.HiddenInput(attrs={'style': 'display: none'}), + 'users': forms.SelectMultiple(attrs={'class': 'select2', + 'data-placeholder': _('Select users')}), + 'user_groups': forms.SelectMultiple(attrs={'class': 'select2', + 'data-placeholder': _('Select user groups')}), 'assets': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Select assets')}), 'asset_groups': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Select asset groups')}), 'system_users': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Select system users')}), - + } + help_texts = { + 'name': '* required', + 'user_groups': '* User or user group at least one required', + 'asset_groups': '* Asset or Asset group at least one required', + 'system_users': '* required', } diff --git a/apps/perms/models.py b/apps/perms/models.py index e1108ce2f..dbd1996ee 100644 --- a/apps/perms/models.py +++ b/apps/perms/models.py @@ -9,70 +9,34 @@ from assets.models import Asset, AssetGroup, SystemUser from common.utils import date_expired_default -class PermUserAsset(models.Model): +class AssetPermission(models.Model): ACTION_CHOICE = ( ('1', 'Allow'), ('0', 'Deny'), ) - user = models.ForeignKey(User, on_delete=models.CASCADE) + name = models.CharField(max_length=128, verbose_name=_('Name')) + users = models.ManyToManyField(User, related_name='asset_permissions') + user_groups = models.ManyToManyField(UserGroup, related_name='asset_permissions') + assets = models.ManyToManyField(Asset, related_name='granted_by_permissions', blank=True) + asset_groups = models.ManyToManyField(AssetGroup, related_name='granted_by_permissions', blank=True) + system_users = models.ManyToManyField(SystemUser, related_name='granted_by_permissions') action = models.CharField(choices=ACTION_CHOICE, max_length=8, default='1') - assets = models.ManyToManyField(Asset, blank=True) - asset_groups = models.ManyToManyField(AssetGroup, blank=True) - system_users = models.ManyToManyField(SystemUser, blank=True) + is_active = models.BooleanField(default=True) date_expired = models.DateTimeField(default=date_expired_default, verbose_name=_('Date expired')) created_by = models.CharField(max_length=128, blank=True) date_created = models.DateTimeField(auto_now=True) comment = models.TextField(verbose_name=_('Comment'), blank=True) def __unicode__(self): - return '%(id)s: %(user)s %(action)s' % { - 'id': self.id, - 'user': self.user.username, - 'action': self.action, - } + return '%(name)s: %(action)s' % {'name': self.name, 'action': self.action} @property - def is_expired(self): - if self.date_expired > timezone.now(): - return False - else: + def is_valid(self): + if self.date_expired < timezone.now() and is_active: return True + return True class Meta: - db_table = 'perm_user_asset' - - -class PermUserGroupAsset(models.Model): - ACTION_CHOICES = ( - ('0', 'Deny'), - ('1', 'Allow'), - ) - - user_group = models.ForeignKey(User, on_delete=models.CASCADE) - action = models.CharField(choices=ACTION_CHOICES, max_length=8, default='1') - assets = models.ManyToManyField(Asset, blank=True) - asset_groups = models.ManyToManyField(AssetGroup, blank=True) - system_users = models.ManyToManyField(SystemUser, blank=True) - date_expired = models.DateTimeField(default=date_expired_default, verbose_name=_('Date expired')) - created_by = models.CharField(max_length=128) - date_created = models.DateTimeField(auto_now=True) - comment = models.TextField(verbose_name=_('Comment')) - - def __unicode__(self): - return '%(id)s: %(user)s %(action)s' % { - 'id': self.id, - 'user': self.user_group.name, - 'action': self.action, - } - - @property - def is_expired(self): - if self.date_expired > timezone.now(): - return False - else: - return True - - class Meta: - db_table = 'perm_user_group_asset' + db_table = 'asset_permission' diff --git a/apps/perms/templates/perms/perm_user_asset_create.html b/apps/perms/templates/perms/asset_permission_create.html similarity index 76% rename from apps/perms/templates/perms/perm_user_asset_create.html rename to apps/perms/templates/perms/asset_permission_create.html index 298f95cb4..68c023d59 100644 --- a/apps/perms/templates/perms/perm_user_asset_create.html +++ b/apps/perms/templates/perms/asset_permission_create.html @@ -14,7 +14,7 @@