-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1385 Add check if user has permission to add a pr…
…oject to a profile.
- Loading branch information
Showing
9 changed files
with
410 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
onadata/apps/api/management/commands/apply_can_add_project_perms.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Command apply_can_add_project_perms - applys can_add_project permission to all | ||
users who have can_add_xform permission to a user/organization profile. | ||
This was necessary because we previously (April 2018) did not have | ||
can_add_project permission on the UserProfile and OrganizationProfile classes. | ||
An attempt on doing this in migrations seems not to be a recommended approach. | ||
""" | ||
from django.core.management.base import BaseCommand | ||
from django.utils.translation import gettext as _ | ||
from guardian.shortcuts import assign_perm | ||
|
||
from onadata.apps.api.models import OrganizationProfile | ||
from onadata.apps.main.models import UserProfile | ||
|
||
|
||
def org_can_add_project_permission(): | ||
""" | ||
Set 'can_add_project' permission to all users who have 'can_add_xform' | ||
permission in the organization profile. | ||
""" | ||
organizations = OrganizationProfile.objects.all() | ||
|
||
for organization in organizations.iterator(): | ||
permissions = organization.orgprofileuserobjectpermission_set.filter( | ||
permission__codename='can_add_xform') | ||
for permission in permissions: | ||
assign_perm('can_add_project', permission.user, organization) | ||
|
||
|
||
def user_can_add_project_permission(): | ||
""" | ||
Set 'can_add_project' permission to all users who have 'can_add_xform' | ||
permission in the user profile. | ||
""" | ||
users = UserProfile.objects.all() | ||
|
||
for user in users.iterator(): | ||
permissions = user.userprofileuserobjectpermission_set.filter( | ||
permission__codename='can_add_xform') | ||
for permission in permissions: | ||
assign_perm('can_add_project', permission.user, user) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Command apply_can_add_preject_perms - applys can_add_project permission to | ||
all users who have can_add_xform permission to a user/organization profile. | ||
""" | ||
help = _(u"Apply can_add_project permissions") | ||
|
||
def handle(self, *args, **options): | ||
user_can_add_project_permission() | ||
org_can_add_project_permission() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.11 on 2018-04-25 11:54 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api', '0002_auto_20151014_0909'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name='organizationprofile', | ||
options={ | ||
'permissions': | ||
(('can_add_project', 'Can add a project to an organization'), | ||
('can_add_xform', | ||
'Can add/upload an xform to an organization'), | ||
('view_organizationprofile', 'Can view organization profile')) | ||
}, ), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.11 on 2018-04-25 11:54 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('main', '0007_auto_20160418_0525'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name='userprofile', | ||
options={ | ||
'permissions': | ||
(('can_add_project', 'Can add a project to an organization'), | ||
('can_add_xform', 'Can add/upload an xform to user profile'), | ||
('view_profile', 'Can view user profile')) | ||
}, ), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.