2016-09-10 13:08:10 +00:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
2016-09-13 17:08:26 +00:00
|
|
|
import functools
|
2016-08-08 16:43:11 +00:00
|
|
|
|
|
|
|
from django.db import models
|
2016-09-10 13:08:10 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-09-11 01:50:42 +00:00
|
|
|
from django.utils import timezone
|
2016-08-08 16:43:11 +00:00
|
|
|
|
2016-09-10 13:08:10 +00:00
|
|
|
from users.models import User, UserGroup
|
|
|
|
from assets.models import Asset, AssetGroup, SystemUser
|
2016-09-13 17:08:26 +00:00
|
|
|
from common.utils import date_expired_default, combine_seq
|
2016-09-10 13:08:10 +00:00
|
|
|
|
|
|
|
|
2016-09-11 14:45:24 +00:00
|
|
|
class AssetPermission(models.Model):
|
2016-09-16 17:04:52 +00:00
|
|
|
PRIVATE_FOR_CHOICE = (
|
|
|
|
('N', 'None'),
|
|
|
|
('U', 'user'),
|
|
|
|
('G', 'user group'),
|
|
|
|
)
|
|
|
|
name = models.CharField(max_length=128, unique=True, verbose_name=_('Name'))
|
2016-09-13 17:08:26 +00:00
|
|
|
users = models.ManyToManyField(User, related_name='asset_permissions', blank=True)
|
|
|
|
user_groups = models.ManyToManyField(UserGroup, related_name='asset_permissions', blank=True)
|
2016-09-11 14:45:24 +00:00
|
|
|
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')
|
2016-09-17 05:04:26 +00:00
|
|
|
private_for = models.CharField(choices=PRIVATE_FOR_CHOICE, max_length=1, default='N', blank=True,
|
|
|
|
verbose_name=_('Private for'))
|
2016-09-12 16:37:24 +00:00
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
|
2016-09-11 01:50:42 +00:00
|
|
|
date_expired = models.DateTimeField(default=date_expired_default, verbose_name=_('Date expired'))
|
2016-09-12 16:37:24 +00:00
|
|
|
created_by = models.CharField(max_length=128, blank=True, verbose_name=_('Created by'))
|
|
|
|
date_created = models.DateTimeField(auto_now=True, verbose_name=_('Date created'))
|
2016-09-11 08:59:19 +00:00
|
|
|
comment = models.TextField(verbose_name=_('Comment'), blank=True)
|
2016-09-11 01:50:42 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-09-16 08:09:11 +00:00
|
|
|
return self.name
|
2016-09-11 01:50:42 +00:00
|
|
|
|
|
|
|
@property
|
2016-09-11 14:45:24 +00:00
|
|
|
def is_valid(self):
|
2016-09-12 16:37:24 +00:00
|
|
|
if self.date_expired < timezone.now() and self.is_active:
|
2016-09-11 01:50:42 +00:00
|
|
|
return True
|
2016-09-11 14:45:24 +00:00
|
|
|
return True
|
2016-09-11 01:50:42 +00:00
|
|
|
|
2016-09-13 17:08:26 +00:00
|
|
|
def get_granted_users(self):
|
2016-09-16 09:23:47 +00:00
|
|
|
return list(set(self.users.all()) | self.get_granted_user_groups_member())
|
2016-09-13 17:08:26 +00:00
|
|
|
|
|
|
|
def get_granted_user_groups_member(self):
|
2016-09-16 01:38:07 +00:00
|
|
|
users = set()
|
|
|
|
for user_group in self.user_groups.all():
|
|
|
|
for user in user_group.users.all():
|
2016-09-16 09:23:47 +00:00
|
|
|
setattr(user, 'is_inherit_from_user_groups', True)
|
|
|
|
setattr(user, 'inherit_from_user_groups',
|
|
|
|
getattr(user, b'inherit_from_user_groups', set()).add(user_group))
|
2016-09-16 01:38:07 +00:00
|
|
|
users.add(user)
|
|
|
|
return users
|
2016-09-13 17:08:26 +00:00
|
|
|
|
|
|
|
def get_granted_assets(self):
|
2016-09-16 09:23:47 +00:00
|
|
|
return list(set(self.assets.all()) | self.get_granted_asset_groups_member())
|
2016-09-13 17:08:26 +00:00
|
|
|
|
|
|
|
def get_granted_asset_groups_member(self):
|
2016-09-16 01:38:07 +00:00
|
|
|
assets = set()
|
|
|
|
for asset_group in self.asset_groups.all():
|
|
|
|
for asset in asset_group.assets.all():
|
2016-09-16 09:23:47 +00:00
|
|
|
setattr(asset, 'is_inherit_from_asset_groups', True)
|
|
|
|
setattr(asset, 'inherit_from_asset_groups',
|
|
|
|
getattr(asset, b'inherit_from_user_groups', set()).add(asset_group))
|
2016-09-16 01:38:07 +00:00
|
|
|
assets.add(asset)
|
|
|
|
return assets
|
2016-09-13 17:08:26 +00:00
|
|
|
|
2016-09-11 01:50:42 +00:00
|
|
|
class Meta:
|
2016-09-11 14:45:24 +00:00
|
|
|
db_table = 'asset_permission'
|
2016-09-10 13:08:10 +00:00
|
|
|
|