-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add removeroles management command (wip) (#1391)
- Loading branch information
Showing
5 changed files
with
156 additions
and
24 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
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,117 @@ | ||
""" | ||
Removeroles management command for removing all roles from a user. | ||
""" | ||
|
||
import sys | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
|
||
from projectroles.management.logging import ManagementCommandLogger | ||
from projectroles.models import RoleAssignment, SODAR_CONSTANTS | ||
from projectroles.views import RoleAssignmentDeleteMixin | ||
|
||
logger = ManagementCommandLogger(__name__) | ||
User = get_user_model() | ||
|
||
|
||
# SODAR constants | ||
PROJECT_ROLE_OWNER = SODAR_CONSTANTS['PROJECT_ROLE_OWNER'] | ||
|
||
# Local constants | ||
USER_NOT_FOUND_MSG = 'User not found with username: {}' | ||
|
||
|
||
class Command(RoleAssignmentDeleteMixin, BaseCommand): | ||
help = ( | ||
'Remove all roles from a user. Replace owner roles with given user or ' | ||
'parent owner.' | ||
) | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'-u', | ||
'--user', | ||
dest='user', | ||
required=True, | ||
help='User name of user whose roles will be removed', | ||
) | ||
parser.add_argument( | ||
'-o', | ||
'--owner', | ||
dest='owner', | ||
required=False, | ||
help='Set owner role for user by given user name if set, otherwise ' | ||
'set to parent owner', | ||
) | ||
|
||
def handle(self, *args, **options): | ||
if options['user'] == options.get('owner'): | ||
logger.error( | ||
'Same username given for both user and new owner: {}'.format( | ||
options['user'] | ||
) | ||
) | ||
sys.exit(1) | ||
try: | ||
user = User.objects.get(username=options['user']) | ||
except User.DoesNotExist: | ||
logger.error(USER_NOT_FOUND_MSG.format(options['user'])) | ||
sys.exit(1) | ||
owner_name = options.get('owner') | ||
owner = None | ||
if owner_name: | ||
try: | ||
owner = User.objects.get(username=owner_name) | ||
except User.DoesNotExist: | ||
logger.error(USER_NOT_FOUND_MSG.format(owner_name)) | ||
sys.exit(1) | ||
|
||
logger.info('Removing roles from user "{}"..'.format(user.username)) | ||
if owner: | ||
logger.info( | ||
'New owner for replacing owner roles: {}'.format(owner.username) | ||
) | ||
role_count = 0 | ||
fail_count = 0 | ||
roles = RoleAssignment.objects.filter(user=user).order_by( | ||
'project__full_title' | ||
) | ||
if roles.count() == 0: | ||
logger.info('No roles found') | ||
return | ||
|
||
for role_as in roles: | ||
r_name = role_as.role.name | ||
project = role_as.project | ||
p_title = project.get_log_title() | ||
if project.is_remote(): # Skip remote projects | ||
logger.debug('Skipping remote project: {}'.format(p_title)) | ||
continue | ||
# Owner role reassignment | ||
if role_as.role.name == PROJECT_ROLE_OWNER: | ||
# TODO: If no set owner, get parent owner | ||
# TODO: If parent owner is not found, fail and continue | ||
# TODO: If parent owner has existing local role, promote that | ||
# TODO: Else add owner role | ||
pass | ||
# Non-owner role removal | ||
else: | ||
try: | ||
with transaction.atomic(): | ||
self.delete_assignment(role_as, None, False) | ||
except Exception as ex: | ||
logger.error( | ||
'Failed to delete assignment "{}" from {}: ' | ||
'{}'.format(r_name, p_title, ex) | ||
) | ||
fail_count += 1 | ||
continue | ||
logger.info('Deleted role "{}" from {}'.format(r_name, p_title)) | ||
role_count += 1 | ||
logger.info( | ||
'Removed roles from user "{}" ({} OK, {} failed)'.format( | ||
user.username, role_count, fail_count | ||
) | ||
) |
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