def test_to_super(self): UserFactory() UserFactory(is_staff=True) user2 = UserFactory(is_superuser=True) ACLRuleFactory(slug='page', permissions=['change_page'], apply_to=ACLRule.TO_SUPERUSERS) users = ACLRule.get_users_for(['change_page'], 'page') self.assertEqual(set(users), {user2})
def test_simple_user_for_multiples_perms(self): user1 = UserFactory() user2 = UserFactory() ACLRuleFactory( slug='page', permissions=['view_page'], users=[user1, user2]) ACLRuleFactory(slug='page', permissions=['change_page'], users=[user1]) users = ACLRule.get_users_for(['view_page', 'change_page'], 'page') self.assertEqual(set(users), {user1})
def test_to_staff(self): UserFactory() user2 = UserFactory(is_staff=True) ACLRuleFactory(slug='page', permissions=['view_page'], apply_to=ACLRule.TO_STAFF) users = ACLRule.get_users_for(['view_page'], 'page') self.assertEqual(set(users), {user2})
def test_any_logged_user(self): user1 = UserFactory() ACLRuleFactory(slug='page', permissions=['view_page'], apply_to=ACLRule.TO_LOGGED) users = ACLRule.get_users_for(['view_page'], 'page') self.assertNotIn(AnonymousUser(), users) self.assertIn(user1, users)
def test_is_distinct(self): user = UserFactory() group1_users = [user] group1 = GroupFactory(users=group1_users) ACLRuleFactory(slug='page', permissions=['view_page'], groups=[group1], users=[user]) users = ACLRule.get_users_for('view_page', 'page') self.assertEqual(users.count(), 1) self.assertEqual(set(users), set(group1_users))
def test_mixing_group_and_users(self): user = UserFactory() group1_users = [UserFactory(), UserFactory()] group2_users = [UserFactory(), UserFactory()] group1 = GroupFactory(users=group1_users) group2 = GroupFactory(users=group2_users) ACLRuleFactory(slug='page', permissions=['view_page'], groups=[group1, group2], users=[user]) users = ACLRule.get_users_for('view_page', 'page') self.assertEqual(set(users), set(group1_users + group2_users + [user]))
def test_simple_user_for_multiples_perms(self): user1 = UserFactory() user2 = UserFactory() ACLRuleFactory(slug='page', permissions=['view_page'], users=[user1, user2]) ACLRuleFactory(slug='page', permissions=['view_page', 'change_page'], users=[user1]) users = ACLRule.get_users_for(['view_page', 'change_page'], 'page') self.assertEqual(set(users), {user1})
def test_simple_group(self): group_users = [UserFactory(), UserFactory()] group = GroupFactory(users=group_users) ACLRuleFactory(slug='page', permissions=['view_page'], groups=[group]) users = ACLRule.get_users_for('view_page', 'page') self.assertEqual(set(users), set(group_users))
def test_simple_user(self): user = UserFactory() ACLRuleFactory(slug='page', permissions=['view_page'], users=[user]) users = ACLRule.get_users_for('view_page', 'page') self.assertEqual(set(users), {user})
def get_name(request, slug): """ Get name to create view aclrule with or without edit/change/write permission Only staff users can currently add reviewers. TODO: [x] make sure author doesn't insert himself. [] gently fail """ # get related project p = get_object_or_404(Project, slug=slug) # if request.user != p.creator: if request.user.is_staff: # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = ReviewerForm(request.POST) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required try: u = User.objects.get(name=form.cleaned_data['user_name']) except ObjectDoesNotExist: messages.add_message( request, messages.WARNING, 'User name "%s" does not exist.' % form.cleaned_data['user_name']) return HttpResponseRedirect("/p/" + slug + "/_allow/") if u == request.user: messages.add_message( request, messages.WARNING, '... I cannot do that right now. Not yet. Maybe one day.' ) return HttpResponseRedirect("/p/" + slug + "/_allow/") if form.cleaned_data['user_name'] == p.creator.name: messages.add_message( request, messages.WARNING, '... I cannot do that right now. Not yet. Maybe one day.' ) return HttpResponseRedirect("/p/" + slug + "/_allow/") p.reviewers.add(u) name = '%s on "%s" with read' % (u.name, p.title) if form.cleaned_data['can_edit'] == True: name += " and write" try: rule = ACLRule.get(name=name, slug=p.slug, apply_to='explicit') except: messages.add_message(request, messages.SUCCESS, 'Rule seems new :)') else: messages.add_message(request, messages.WARNING, 'Rule already exists.') return HttpResponseRedirect("/p/" + slug + "/_allow/") try: newrule = ACLRule(name=name, slug=p.slug, apply_to='explicit') newrule.save() messages.add_message(request, messages.SUCCESS, 'Rule saved.') except: messages.add_message(request, messages.WARNING, 'Error when saving aclrule.') return HttpResponseRedirect("/p/" + slug + "/_allow/") try: newrule.users.add(u) newrule.permissions.add( Permission.objects.get(codename='view_page')) if form.cleaned_data['can_edit'] == True: newrule.permissions.add( Permission.objects.get(codename='change_page')) except: messages.add_message( request, messages.WARNING, 'Error when adding reviewer or permissions to rule.') return HttpResponseRedirect("/p/" + slug + "/_allow/") else: p.save() newrule.save() messages.add_message(request, messages.SUCCESS, 'User and permission saved to rule.') # redirect to a new URL: return HttpResponseRedirect('/') # if a GET (or any other method) we'll create a blank form else: form = ReviewerForm() else: raise Http404('Nope.') return render(request, 'projects/add_reviewer.html', { 'form': form, 'slug': slug })