from rest_framework.permissions import IsAuthenticated

from energy_base.utils.microservices import is_internal_microservice_request


class BaseRolePermissions(IsAuthenticated):
    required_role = None

    def has_permission(self, request, view):
        if not super().has_permission(request, view):
            return False

        if is_internal_microservice_request(request):
            return True

        if hasattr(request.user, 'roles'):
            return self.required_role in request.user.roles

        return False