if self.request.user.is_authenticated(): q = q | Q(flagger=self.request.user, content_type=ct, object_id=self.object.pk) return bool(ContentFlag.objects.filter(q).count()) class ObjectModerationStatusIsPending(nuka.BasePermission): unauthorized_message = _("Sisältö on jo moderoitu.") def __init__(self, **kwargs): self.object = kwargs['obj'] super(ObjectModerationStatusIsPending, self).__init__(**kwargs) def get_unauthorized_response(self): super(ObjectModerationStatusIsPending, self).get_unauthorized_response() return JsonResponse({'reload': True}) def is_authorized(self): return self.object.moderation_status == MODERATION_STATUS_PENDING CanFlagObject = perms.And(ContentTypeIsModeratable, perms.Not(ObjectAlreadyFlaggedByUser)) CanModerateContent = perms.And(nuka.IsAuthenticated, nuka.IsModerator) CanModerateObject = perms.And(CanModerateContent, ObjectModerationStatusIsPending)
class IsOrganizationAdmin(nuka.BasePermission): def __init__(self, **kwargs): self.organization = kwargs.pop('obj') super(IsOrganizationAdmin, self).__init__(**kwargs) def is_authorized(self): return bool(self.organization.admins.filter(pk=self.user.pk)) class OrganizationIsPublic(nuka.BasePermission): def __init__(self, **kwargs): self.organization = kwargs['obj'] super(OrganizationIsPublic, self).__init__(**kwargs) def is_authorized(self): return self.organization.is_active is True and self.organization.type is not \ self.organization.TYPE_UNKNOWN CanViewOrganization = perms.Or( OrganizationIsPublic, perms.And(nuka.IsAuthenticated, perms.Or(IsOrganizationAdmin, nuka.IsModerator))) CanEditOrganization = perms.And( nuka.IsAuthenticated, perms.Or(IsOrganizationAdmin, nuka.IsModerator)) CanExportInitiatives = CanEditOrganization
def is_authorized(self): return self.idea_survey.interaction == IdeaSurvey.INTERACTION_EVERYONE class SurveyInitiativeIsPublic(BaseSurveyPermissionExtended): def is_authorized(self): return self.idea_survey.idea.is_public() class SurveyHasAnswers(BaseSurveyPermissionExtended): def is_authorized(self): return self.survey.submissions.exists() CanEditSurvey = perms.And( default_perms.CanEditSurvey, perms.Or(OwnsSurvey, nuka.IsModerator), ) CanEditSurveyName = perms.Or(OwnsSurvey, nuka.IsModerator) CanAnswerSurvey = perms.And( default_perms.CanAnswerSurvey, SurveyIsOpen, perms.Not(OwnsSurvey), perms.Or(nuka.IsAuthenticated, SurveyInteractionEveryone)) ShowSurveyResults = perms.Or( OwnsSurvey, default_perms.ShowSurveyResults, perms.And(SurveyShowResultsOwners, OwnsSurvey), ) CanViewSurvey = perms.Or(perms.Not(SurveyIsDraft),
from nkvote.models import Vote from nkvote.utils import get_voter from nuka import perms as nuka class OwnsCommentCheck(nuka.BasePermission): def __init__(self, **kwargs): self.comment = kwargs['obj'] super(OwnsCommentCheck, self).__init__(**kwargs) def is_authorized(self): return self.comment.user_id is not None and self.user.pk == self.comment.user_id OwnsComment = perms.And(nuka.IsAuthenticated, OwnsCommentCheck) class CommentAlreadyVotedByUser(nuka.BasePermission): unauthorized_message = _("Olet jo äänestänyt kommenttia.") def __init__(self, **kwargs): self.comment = kwargs['obj'] super(CommentAlreadyVotedByUser, self).__init__(**kwargs) def is_authorized(self): voter = get_voter(self.request, create=False) if voter: return Vote.objects.filter(voter=voter, comments=self.comment).exists() else:
def is_authorized(self): return bool(self.user.settings.email) class IsClosed(AccountBasePermission): def get_login_url(self): return reverse('frontpage') def get_unauthorized_message(self): return ugettext("Käyttäjäprofiilia ei ole palvelussa.") def is_authorized(self): return self.account.status == User.STATUS_ARCHIVED class CanDisconnectSocial(AccountBasePermission): def is_authorized(self): return len(self.account.password) > 0 CanEditUser = perms.And( nuka.IsAuthenticated, perms.Or(OwnAccount, nuka.IsAdmin, perms.And(nuka.IsModerator, nuka.ObjectIsParticipant))) CanViewUser = perms.Or( perms.Not(IsClosed), nuka.IsModerator, )
return self.user.is_authenticated() def get_login_url(self): return reverse('account:login') class IsModeratorCheck(BasePermission): unauthorized_message = _("Toiminto vaatii moderaattorin oikeudet.") def is_authorized(self): # Admins are always moderators too, for now at least. mod_groups = [GROUP_NAME_MODERATORS, GROUP_NAME_ADMINS] return len(set(self.user.group_names) & set(mod_groups)) > 0 IsModerator = perms.And(IsAuthenticated, IsModeratorCheck) class IsAdmin(BasePermission): unauthorized_message = _("Toiminto vaatii ylläpitäjän oikeudet.") def is_authorized(self): admin_groups = [GROUP_NAME_ADMINS] return len(set(self.user.group_names) & set(admin_groups)) > 0 class ObjectIsParticipant(BasePermission): def __init__(self, **kwargs): self.object = kwargs['obj'] super(ObjectIsParticipant, self).__init__(**kwargs)
def is_authorized(self): return self.gallup.answer_set.all().count() > 0 class GallupInitiativeIsPublic(nuka.BasePermission): def __init__(self, **kwargs): self.gallup = kwargs["obj"] super(GallupInitiativeIsPublic, self).__init__(**kwargs) def is_authorized(self): return self.gallup.idea.is_public() # Requires initiative id (obj_by_pk(Initiative, 'initiative_id')). CanCreateGallup = perms.And( InitiativeIsNotArchived, perms.Or(OwnsInitiative, nuka.IsModerator) ) CanAnswerGallup = perms.And( GallupIsOpen, perms.Not(GallupAnswered), perms.Or( perms.Not(AnsweringGallupRequiresLogin), perms.And( AnsweringGallupRequiresLogin, nuka.IsAuthenticated ) ) ) CanViewGallup = perms.Or(
class ReceiverIsModerator(nuka.BasePermission): def __init__(self, **kwargs): self.target_user = kwargs["obj"] super(ReceiverIsModerator, self).__init__(**kwargs) def is_authorized(self): return self.target_user.is_moderator class ReceiverIsOrganizationAdmin(nuka.BasePermission): def __init__(self, **kwargs): self.target_user = kwargs["obj"] super(ReceiverIsOrganizationAdmin, self).__init__(**kwargs) def is_authorized(self): result = self.target_user.organizations.all().exists() return result CanSendMessageTo = perms.And( nuka.IsAuthenticated, perms.Not(ReceiverIsSelf), perms.Or( nuka.IsModerator, perms.Or( ReceiverIsModerator, ReceiverIsOrganizationAdmin ) ) )
class SurveyShowResultsEveryone(BaseSurveyPermission): def is_authorized(self): assert hasattr(config, "SHOW_RESULTS_EVERYONE"), \ "Current survey configuration does not have SHOW_RESULTS_EVERYONE. " \ "Permmission can't be used." return self.survey.show_results == config.SHOW_RESULTS_EVERYONE class SurveyShowResultsAnsweredUsers(BaseSurveyPermission): def is_authorized(self): assert hasattr(config, "SHOW_RESULTS_ANSWERED"), \ "Current survey configuration does not have SHOW_RESULTS_ANSWERED. " \ "Permmission can't be used." return self.survey.show_results == config.SHOW_RESULTS_ANSWERED class AllowAll(BaseSurveyPermission): def is_authorized(self): return True CanEditSurvey = perms.And(perms.Not(SurveyHasAnswers), ) CanAnswerSurvey = perms.And(perms.Not(SurveyAnsweredByUser), ) ShowSurveyResults = perms.Or( SurveyShowResultsEveryone, perms.And(SurveyShowResultsAnsweredUsers, SurveyAnsweredByUser), )