576 |
576 |
return Response({'result': 1})
|
577 |
577 |
|
578 |
578 |
|
|
579 |
class RolesAPI(ExceptionHandlerMixin, APIView):
|
|
580 |
queryset = get_role_model()
|
|
581 |
permission_classes = (permissions.IsAuthenticated,)
|
|
582 |
_api_fetched_fields = ['uuid', 'name', 'slug', 'admin_scope_ct',
|
|
583 |
'admin_scope_id', 'service']
|
|
584 |
|
|
585 |
def post(self, request, *args, **kwargs):
|
|
586 |
logger = logging.getLogger(__name__)
|
|
587 |
|
|
588 |
try:
|
|
589 |
Role = get_role_model()
|
|
590 |
ou_id_or_slug = kwargs['ou_id_or_slug']
|
|
591 |
role_data = request.body['role_data']
|
|
592 |
missing_fields = app_settings.A2_ROLES_REQUIRED_FIELDS - role_data.keys()
|
|
593 |
if missing_fields:
|
|
594 |
return Response({'result': 0, 'detail': 'Missing fields in API request: %r' % missing_fields},
|
|
595 |
status=status.HTTP_400_BAD_REQUEST)
|
|
596 |
if count(Role.objects.filter(slug=role_data['slug'])):
|
|
597 |
return Response({'result': 0, 'detail': 'Error: role already existing in the A2 database'},
|
|
598 |
status=status.HTTP_400_BAD_REQUEST)
|
|
599 |
if 'ou_id_or_slug' not in role_data:
|
|
600 |
return Response({'result': 0, 'detail': 'Error: no OU identifier provided, role can\'t be created'},
|
|
601 |
status=status.HTTP_400_BAD_REQUEST)
|
|
602 |
role = Role.objects.create()
|
|
603 |
for api_field in self._api_fetched_fields:
|
|
604 |
if api_field in role_data:
|
|
605 |
setattr(self, api_field, role_data[api_field])
|
|
606 |
OU = get_ou_model()
|
|
607 |
|
|
608 |
# try first on the OU uuid then on its slug
|
|
609 |
try:
|
|
610 |
ou = OU.objects.get(uuid=role_data.get('ou_id_or_slug'))
|
|
611 |
except MultipleObjectsReturned:
|
|
612 |
ou = OU.objects.get(slug=role_data.get('ou_id_or_slug'))
|
|
613 |
role.ou = ou
|
|
614 |
role.save()
|
|
615 |
|
|
616 |
except Exception as e:
|
|
617 |
logger.error('Couldn\'t create role')
|
|
618 |
return Response({'result': 0, 'detail': e},
|
|
619 |
status=status.HTTP_400_BAD_REQUEST)
|
|
620 |
|
|
621 |
return Response({'result': 1, 'detail': _('Role created')},
|
|
622 |
status=status.HTTP_201_CREATED)
|
|
623 |
|
|
624 |
roles = RolesAPI.as_view()
|
|
625 |
|
579 |
626 |
class RoleMembershipsAPI(ExceptionHandlerMixin, APIView):
|
580 |
627 |
permission_classes = (permissions.IsAuthenticated,)
|
581 |
628 |
|