def accessible_to(self, user): """ returns all the items that are accessible to the specified user if user is not authenticated will return public items :param user: an user instance or integer representing user id """ # if user param is an integer if isinstance(user, int): user = User.objects.get(pk=user) if user.is_superuser: try: queryset = self.get_query_set() except AttributeError: queryset = self elif user.is_authenticated(): # get user group (higher id) group = user.groups.all().order_by('-id')[0] queryset = self.filter( access_level__lte=ACCESS_LEVELS.get(group.name)) else: queryset = self.filter( access_level__lte=ACCESS_LEVELS.get('public')) return queryset
def accessible_to(self, user): """ returns all the items that are accessible to the specified user if user is not authenticated will return public items :param user: an user instance """ if user.is_superuser: try: queryset = self.get_queryset() except AttributeError: queryset = self elif user.is_authenticated(): # get user group (higher id) group = user.groups.all().order_by('-id')[0] queryset = self.filter(access_level__lte=ACCESS_LEVELS.get(group.name)) else: queryset = self.filter(access_level__lte=ACCESS_LEVELS.get('public')) return queryset
def access_level_up_to(self, access_level): """ returns all items that have an access level equal or lower than the one specified """ # if access_level is number if isinstance(access_level, int): value = access_level # else if is string get the numeric value else: value = ACCESS_LEVELS.get(access_level) # return queryset return self.filter(access_level__lte=value)
def test_menu_list_check_acl(self): url = reverse('api_menu_list') # ensure link is public response = self.client.get(url) self.assertIn('Software Credits', response.content) # set about menu item as restricted access item = MenuItem.objects.get(name='Software Credits') item.access_level = ACCESS_LEVELS.get('trusted') item.save() # ensure menu item is public anymore response = self.client.get(url) self.assertNotIn('Software Credits', response.content) # ensure admin can see it self.client.login(username='******', password='******') response = self.client.get(url) self.assertIn('Software Credits', response.content)
class Layer(BaseDate): """ Layer Model """ name = models.CharField(_('name'), max_length=50, unique=True) slug = models.SlugField(max_length=50, db_index=True, unique=True) description = models.CharField(_('description'), max_length=250, blank=True, null=True) # record management is_published = models.BooleanField(_('published'), default=True) is_external = models.BooleanField(_('is it external?')) # geographic related fields center = models.PointField(_('center coordinates'), null=True, blank=True) area = models.PolygonField(_('area'), null=True, blank=True) zoom = models.SmallIntegerField( _('default zoom level'), choices=MAP_ZOOM, default=settings.NODESHOT['DEFAULTS']['ZONE_ZOOM']) # organizational organization = models.CharField( _('organization'), help_text=_('Organization which is responsible to manage this layer'), max_length=255) website = models.URLField(_('Website'), blank=True, null=True) email = models.EmailField( _('email'), help_text= _('possibly an email address that delivers messages to all the active participants; if you don\'t have such an email you can add specific users in the "mantainers" field' ), blank=True) mantainers = models.ManyToManyField( User, verbose_name=_('mantainers'), help_text= _('you can specify the users who are mantaining this layer so they will receive emails from the system' ), blank=True) # settings minimum_distance = models.IntegerField( default=settings.NODESHOT['DEFAULTS']['ZONE_MINIMUM_DISTANCE'], help_text=_( 'minimum distance between nodes, 0 means feature disabled')) write_access_level = models.SmallIntegerField( _('write access level'), choices=choicify(ACCESS_LEVELS), default=ACCESS_LEVELS.get('public'), help_text=_('minimum access level to insert new nodes in this layer')) # default manager objects = LayerManager() class Meta: db_table = 'layers_layer' app_label = 'layers' def __unicode__(self): return '%s' % self.name if 'grappelli' in settings.INSTALLED_APPS: @staticmethod def autocomplete_search_fields(): return ('name__icontains', 'slug__icontains') def clean(self, *args, **kwargs): """ Custom validation """ pass