532 |
532 |
User = get_user_model()
|
533 |
533 |
known_uuids = User.objects.filter(uuid__in=uuids).values_list('uuid', flat=True)
|
534 |
534 |
return set(uuids) - set(known_uuids)
|
535 |
|
|
536 |
535 |
@list_route(methods=['post'], permission_classes=(DjangoPermission('custom_user.search_user'),))
|
537 |
536 |
def synchronization(self, request):
|
538 |
537 |
serializer = self.SynchronizationSerializer(data=request.data)
|
... | ... | |
576 |
575 |
return Response({'result': 1})
|
577 |
576 |
|
578 |
577 |
|
|
578 |
class ApiMissingFieldsError(Exception):
|
|
579 |
pass
|
|
580 |
|
|
581 |
|
|
582 |
class RoleAlreadyExistsError(Exception):
|
|
583 |
pass
|
|
584 |
|
|
585 |
|
579 |
586 |
class RolesAPI(ExceptionHandlerMixin, APIView):
|
|
587 |
queryset = get_role_model()
|
|
588 |
permission_classes = (permissions.IsAuthenticated,)
|
|
589 |
|
|
590 |
_api_fetched_fields = ['uuid', 'name', 'slug', 'admin_scope_ct',
|
|
591 |
'admin_scope_id', 'service']
|
|
592 |
|
|
593 |
def post(self, request, *args, **kwargs):
|
|
594 |
logger = logging.getLogger(__name__)
|
|
595 |
|
|
596 |
try:
|
|
597 |
Role = get_role_model()
|
|
598 |
ou_id_or_ou_slug = kwargs['ou_id_or_ou_slug']
|
|
599 |
role_data = request.body['role_data']
|
|
600 |
missing_fields = app_settings.A2_ROLES_REQUIRED_FIELDS - role_data.keys()
|
|
601 |
if missing_fields:
|
|
602 |
raise ApiMissingFieldsError('No such fields in API request: %r',
|
|
603 |
missing_fields)
|
|
604 |
if count(Role.objects.filter(slug=role_data['slug'])):
|
|
605 |
raise RoleAlreadyExistsError('Error: role already existing in the A2 database')
|
|
606 |
if 'ou_id_or_ou_slug' not in role_data:
|
|
607 |
raise Exception('Error: no OU identifier provided, role can\'t be created')
|
|
608 |
|
|
609 |
role = Role.objects.create()
|
|
610 |
for api_field in self._api_fetched_fields:
|
|
611 |
if api_field in role_data:
|
|
612 |
setattr(self, api_field, role_data[api_field])
|
|
613 |
OU = get_ou_model()
|
|
614 |
|
|
615 |
# try first on the OU uuid then on its slug
|
|
616 |
try:
|
|
617 |
ou = OU.objects.get(uuid=role_data.get('ou_id_or_ou_slug'))
|
|
618 |
except MultipleObjectsReturned:
|
|
619 |
ou = OU.objects.get(slug=role_data.get('ou_id_or_ou_slug'))
|
|
620 |
role.ou = ou
|
|
621 |
role.save()
|
|
622 |
|
|
623 |
except Exception as e:
|
|
624 |
logger.error('Couldn\'t create role')
|
|
625 |
return Response({'result': 0, 'detail': e},
|
|
626 |
status=status.HTTP_400_BAD_REQUEST)
|
|
627 |
|
|
628 |
return Response({'result': 1, 'detail': _('Role created')},
|
|
629 |
status=status.HTTP_201_CREATED)
|
|
630 |
|
|
631 |
roles = RolesAPI.as_view()
|
|
632 |
|
|
633 |
|
|
634 |
class RoleMembershipsAPI(ExceptionHandlerMixin, APIView):
|
580 |
635 |
permission_classes = (permissions.IsAuthenticated,)
|
581 |
636 |
|
582 |
637 |
def initial(self, request, *args, **kwargs):
|
583 |
|
super(RolesAPI, self).initial(request, *args, **kwargs)
|
|
638 |
super(RoleMembershipsAPI, self).initial(request, *args, **kwargs)
|
584 |
639 |
Role = get_role_model()
|
585 |
640 |
User = get_user_model()
|
586 |
641 |
self.role = get_object_or_404(Role, uuid=kwargs['role_uuid'])
|
... | ... | |
601 |
656 |
return Response({'result': 1, 'detail': _('User successfully removed from role')},
|
602 |
657 |
status=status.HTTP_200_OK)
|
603 |
658 |
|
604 |
|
roles = RolesAPI.as_view()
|
|
659 |
role_memberships = RoleMembershipsAPI.as_view()
|
605 |
660 |
|
606 |
661 |
|
607 |
662 |
class BaseOrganizationalUnitSerializer(serializers.ModelSerializer):
|