pumpwood_communication.microservice_abc.system

Module for pumpwood internals calls.

1"""Module for pumpwood internals calls."""
2from .general import ABCSystemMicroservice
3from .permission import ABCPermissionMicroservice
4
5__docformat__ = "google"
6__all__ = [
7    ABCSystemMicroservice,ABCPermissionMicroservice
8]
class ABCSystemMicroservice(abc.ABC, pumpwood_communication.microservice_abc.base.base.PumpWoodMicroServiceBase):
 12class ABCSystemMicroservice(ABC, PumpWoodMicroServiceBase):
 13    """ABC class to define system associated requests.
 14
 15    System associated request involve list of routes, services that were
 16    registed on Pumpwood, but also some test usefull features such as
 17    cache invalidation and health check calls.
 18    """
 19
 20    def list_registered_routes(self, auth_header: dict = None):
 21        """List routes that have been registed at Kong."""
 22        list_url = 'rest/pumpwood/routes/'
 23        routes = self.request_get(
 24            url=list_url, auth_header=auth_header)
 25        for key, item in routes.items():
 26            item.sort()
 27        return routes
 28
 29    def is_microservice_registered(self, microservice: str,
 30                                   auth_header: dict = None) -> bool:
 31        """Check if a microservice (kong service) is registered at Kong.
 32
 33        Args:
 34            microservice (str):
 35                Service associated with microservice registered on
 36                Pumpwood Kong.
 37            auth_header (dict):
 38                Auth header to substitute the microservice original
 39                at the request (user impersonation).
 40
 41        Returns:
 42            Return true if microservice is registered.
 43        """
 44        routes = self.list_registered_routes(auth_header=auth_header)
 45        return microservice in routes.keys()
 46
 47    def list_registered_endpoints(self, auth_header: dict = None,
 48                                  availability: str = 'front_avaiable'
 49                                  ) -> list:
 50        """List all routes and services that have been registed at Kong.
 51
 52        It is possible to restrict the return to end-points that should be
 53        avaiable at the frontend. Using this feature it is possibel to 'hide'
 54        services from GUI keeping them avaiable for programatic calls.
 55
 56        Args:
 57            auth_header:
 58                Auth header to substitute the microservice original
 59                at the request (user impersonation).
 60            availability:
 61                Set the availability that is associated with the service.
 62                So far it is implemented 'front_avaiable' and 'all'.
 63
 64        Returns:
 65            Return a list of serialized services objects containing the
 66            routes associated with at `route_set`.
 67
 68            Service and routes have `notes__verbose` and `description__verbose`
 69            that are  the repective strings associated with note and
 70            description but translated using Pumpwood's I8s,
 71
 72        Raises:
 73            PumpWoodWrongParameters:
 74                Raise PumpWoodWrongParameters if availability passed as
 75                paraemter is not implemented.
 76        """
 77        list_url = 'rest/pumpwood/endpoints/'
 78        routes = self.request_get(
 79            url=list_url, parameters={'availability': availability},
 80            auth_header=auth_header)
 81        return routes
 82
 83    def dummy_call(self, payload: dict = None,
 84                   auth_header: dict = None) -> dict:
 85        """Return a dummy call to ensure headers and payload reaching app.
 86
 87        The request just bounce on the server and return the headers and
 88        payload that reached the application. It is usefull for probing
 89        proxy servers, API gateways and other security and load balance
 90        tools.
 91
 92        Args:
 93            payload:
 94                Payload to be returned by the dummy call end-point.
 95            auth_header:
 96                Auth header to substitute the microservice original
 97                at the request (user impersonation).
 98
 99        Returns:
100            Return a dictonary with:
101            - **full_path**: Full path of the request.
102            - **method**: Method used at the call
103            - **headers**: Headers at the request.
104            - **data**: Post payload sent at the request.
105        """
106        list_url = 'rest/pumpwood/dummy-call/'
107        if payload is None:
108            return self.request_get(
109                url=list_url, auth_header=auth_header)
110        else:
111            return self.request_post(
112                url=list_url, data=payload,
113                auth_header=auth_header)
114
115    def dummy_raise(self, exception_class: str, exception_deep: int,
116                    payload: dict = {}, auth_header: dict = None) -> None:
117        """Raise an Pumpwood error with the payload.
118
119        This and point raises an Arbitrary PumpWoodException error, it can be
120        used for debuging error treatment.
121
122        Args:
123            exception_class:
124                Class of the exception to be raised.
125            exception_deep:
126                Deep of the exception in microservice calls. This arg will
127                make error recusive, calling the end-point it self for
128                `exception_deep` time before raising the error.
129            payload:
130                Payload that will be returned with error.
131            auth_header:
132                Auth header to substitute the microservice original
133                at the request (user impersonation).
134
135        Returns:
136            Should not return any results, all possible call should result
137            in raising the correspondent error.
138
139        Raises:
140            Should raise the correspondent error passed on exception_class
141            arg, with payload.
142        """
143        url = 'rest/pumpwood/dummy-raise/'
144        payload["exception_class"] = exception_class
145        payload["exception_deep"] = exception_deep
146        self.request_post(url=url, data=payload, auth_header=auth_header)
147
148    def clear_service_cache(self, service: str,
149                            auth_header: dict = None) -> bool:
150        """Clear disk cache associated with service container.
151
152        This request will ask service to clear disk cache. If there are
153        replicas at same service it is possible that the cache might be
154        invalidated on just one of the replicas leading to inconsistent
155        cache behavior.
156
157        Args:
158            service (str):
159                Pumpwood service to have its cache invalidated, it is expected
160                that the service implement the invalidation end-point
161                `/rest/[service-name]/clear-diskcache`.
162            auth_header (dict):
163                Auth header to substitute the microservice original
164                at the request (user impersonation).
165
166        Returns:
167            True if cache was invalidated.
168        """
169        # Service url begging are related to service general functions
170        url = 'service/{service}/clear-diskcache/'\
171            .format(service=service)
172        response = self.request_post(
173            url=url, data={}, auth_header=auth_header)
174        return response
175
176    def health_check_service(self, service: str,
177                             raise_error: bool = False) -> bool:
178        """Hits the health_check, it is not necessary authentication.
179
180        Helth check end-points are simple and does not involve connection
181        with database or storage. It will just check if container is up
182        and receiving requests. It is usefull for tests and for inter-service
183        calls.
184
185        If `raise_error` is set as True, function will raise
186        PumpWoodMicroserviceUnavailableError.
187
188        Args:
189            service (str):
190                Pumpwood service to have its cache invalidated, it is expected
191                that the service implement the invalidation end-point
192                `/rest/[service-name]/clear-diskcache`.
193            raise_error (bool):
194                If request does not return 200 status, it will raise a
195                PumpWoodMicroserviceUnavailableError.
196
197        Returns:
198            True if service is healthy.
199
200        Raises:
201            PumpWoodMicroserviceUnavailableError if request does not
202            return 200 code.
203        """
204        # Service url begging are related to service general functions
205        url = 'service/{service}/health-check/'.format(service=service)
206        get_url = urljoin(self.server_url, url)
207        response = requests.get(get_url, timeout=5)
208
209        # Raise a Pumpwood error if raise_if_not_healthy informing that the
210        # service health-check did not return a OK status
211        if not response.ok and raise_error:
212            msg = (
213                "Call on service [{service}] health-check end-point did not "
214                "return a OK status.")
215            raise PumpWoodMicroserviceUnavailableError(
216                msg, payload={'service': service})
217
218        return response.ok

ABC class to define system associated requests.

System associated request involve list of routes, services that were registed on Pumpwood, but also some test usefull features such as cache invalidation and health check calls.

def list_registered_routes(self, auth_header: dict = None):
20    def list_registered_routes(self, auth_header: dict = None):
21        """List routes that have been registed at Kong."""
22        list_url = 'rest/pumpwood/routes/'
23        routes = self.request_get(
24            url=list_url, auth_header=auth_header)
25        for key, item in routes.items():
26            item.sort()
27        return routes

List routes that have been registed at Kong.

def is_microservice_registered(self, microservice: str, auth_header: dict = None) -> bool:
29    def is_microservice_registered(self, microservice: str,
30                                   auth_header: dict = None) -> bool:
31        """Check if a microservice (kong service) is registered at Kong.
32
33        Args:
34            microservice (str):
35                Service associated with microservice registered on
36                Pumpwood Kong.
37            auth_header (dict):
38                Auth header to substitute the microservice original
39                at the request (user impersonation).
40
41        Returns:
42            Return true if microservice is registered.
43        """
44        routes = self.list_registered_routes(auth_header=auth_header)
45        return microservice in routes.keys()

Check if a microservice (kong service) is registered at Kong.

Arguments:
  • microservice (str): Service associated with microservice registered on Pumpwood Kong.
  • auth_header (dict): Auth header to substitute the microservice original at the request (user impersonation).
Returns:

Return true if microservice is registered.

def list_registered_endpoints( self, auth_header: dict = None, availability: str = 'front_avaiable') -> list:
47    def list_registered_endpoints(self, auth_header: dict = None,
48                                  availability: str = 'front_avaiable'
49                                  ) -> list:
50        """List all routes and services that have been registed at Kong.
51
52        It is possible to restrict the return to end-points that should be
53        avaiable at the frontend. Using this feature it is possibel to 'hide'
54        services from GUI keeping them avaiable for programatic calls.
55
56        Args:
57            auth_header:
58                Auth header to substitute the microservice original
59                at the request (user impersonation).
60            availability:
61                Set the availability that is associated with the service.
62                So far it is implemented 'front_avaiable' and 'all'.
63
64        Returns:
65            Return a list of serialized services objects containing the
66            routes associated with at `route_set`.
67
68            Service and routes have `notes__verbose` and `description__verbose`
69            that are  the repective strings associated with note and
70            description but translated using Pumpwood's I8s,
71
72        Raises:
73            PumpWoodWrongParameters:
74                Raise PumpWoodWrongParameters if availability passed as
75                paraemter is not implemented.
76        """
77        list_url = 'rest/pumpwood/endpoints/'
78        routes = self.request_get(
79            url=list_url, parameters={'availability': availability},
80            auth_header=auth_header)
81        return routes

List all routes and services that have been registed at Kong.

It is possible to restrict the return to end-points that should be avaiable at the frontend. Using this feature it is possibel to 'hide' services from GUI keeping them avaiable for programatic calls.

Arguments:
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • availability: Set the availability that is associated with the service. So far it is implemented 'front_avaiable' and 'all'.
Returns:

Return a list of serialized services objects containing the routes associated with at route_set.

Service and routes have notes__verbose and description__verbose that are the repective strings associated with note and description but translated using Pumpwood's I8s,

Raises:
  • PumpWoodWrongParameters: Raise PumpWoodWrongParameters if availability passed as paraemter is not implemented.
def dummy_call(self, payload: dict = None, auth_header: dict = None) -> dict:
 83    def dummy_call(self, payload: dict = None,
 84                   auth_header: dict = None) -> dict:
 85        """Return a dummy call to ensure headers and payload reaching app.
 86
 87        The request just bounce on the server and return the headers and
 88        payload that reached the application. It is usefull for probing
 89        proxy servers, API gateways and other security and load balance
 90        tools.
 91
 92        Args:
 93            payload:
 94                Payload to be returned by the dummy call end-point.
 95            auth_header:
 96                Auth header to substitute the microservice original
 97                at the request (user impersonation).
 98
 99        Returns:
100            Return a dictonary with:
101            - **full_path**: Full path of the request.
102            - **method**: Method used at the call
103            - **headers**: Headers at the request.
104            - **data**: Post payload sent at the request.
105        """
106        list_url = 'rest/pumpwood/dummy-call/'
107        if payload is None:
108            return self.request_get(
109                url=list_url, auth_header=auth_header)
110        else:
111            return self.request_post(
112                url=list_url, data=payload,
113                auth_header=auth_header)

Return a dummy call to ensure headers and payload reaching app.

The request just bounce on the server and return the headers and payload that reached the application. It is usefull for probing proxy servers, API gateways and other security and load balance tools.

Arguments:
  • payload: Payload to be returned by the dummy call end-point.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
Returns:

Return a dictonary with:

  • full_path: Full path of the request.
  • method: Method used at the call
  • headers: Headers at the request.
  • data: Post payload sent at the request.
def dummy_raise( self, exception_class: str, exception_deep: int, payload: dict = {}, auth_header: dict = None) -> None:
115    def dummy_raise(self, exception_class: str, exception_deep: int,
116                    payload: dict = {}, auth_header: dict = None) -> None:
117        """Raise an Pumpwood error with the payload.
118
119        This and point raises an Arbitrary PumpWoodException error, it can be
120        used for debuging error treatment.
121
122        Args:
123            exception_class:
124                Class of the exception to be raised.
125            exception_deep:
126                Deep of the exception in microservice calls. This arg will
127                make error recusive, calling the end-point it self for
128                `exception_deep` time before raising the error.
129            payload:
130                Payload that will be returned with error.
131            auth_header:
132                Auth header to substitute the microservice original
133                at the request (user impersonation).
134
135        Returns:
136            Should not return any results, all possible call should result
137            in raising the correspondent error.
138
139        Raises:
140            Should raise the correspondent error passed on exception_class
141            arg, with payload.
142        """
143        url = 'rest/pumpwood/dummy-raise/'
144        payload["exception_class"] = exception_class
145        payload["exception_deep"] = exception_deep
146        self.request_post(url=url, data=payload, auth_header=auth_header)

Raise an Pumpwood error with the payload.

This and point raises an Arbitrary PumpWoodException error, it can be used for debuging error treatment.

Arguments:
  • exception_class: Class of the exception to be raised.
  • exception_deep: Deep of the exception in microservice calls. This arg will make error recusive, calling the end-point it self for exception_deep time before raising the error.
  • payload: Payload that will be returned with error.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
Returns:

Should not return any results, all possible call should result in raising the correspondent error.

Raises:
  • Should raise the correspondent error passed on exception_class
  • arg, with payload.
def clear_service_cache(self, service: str, auth_header: dict = None) -> bool:
148    def clear_service_cache(self, service: str,
149                            auth_header: dict = None) -> bool:
150        """Clear disk cache associated with service container.
151
152        This request will ask service to clear disk cache. If there are
153        replicas at same service it is possible that the cache might be
154        invalidated on just one of the replicas leading to inconsistent
155        cache behavior.
156
157        Args:
158            service (str):
159                Pumpwood service to have its cache invalidated, it is expected
160                that the service implement the invalidation end-point
161                `/rest/[service-name]/clear-diskcache`.
162            auth_header (dict):
163                Auth header to substitute the microservice original
164                at the request (user impersonation).
165
166        Returns:
167            True if cache was invalidated.
168        """
169        # Service url begging are related to service general functions
170        url = 'service/{service}/clear-diskcache/'\
171            .format(service=service)
172        response = self.request_post(
173            url=url, data={}, auth_header=auth_header)
174        return response

Clear disk cache associated with service container.

This request will ask service to clear disk cache. If there are replicas at same service it is possible that the cache might be invalidated on just one of the replicas leading to inconsistent cache behavior.

Arguments:
  • service (str): Pumpwood service to have its cache invalidated, it is expected that the service implement the invalidation end-point /rest/[service-name]/clear-diskcache.
  • auth_header (dict): Auth header to substitute the microservice original at the request (user impersonation).
Returns:

True if cache was invalidated.

def health_check_service(self, service: str, raise_error: bool = False) -> bool:
176    def health_check_service(self, service: str,
177                             raise_error: bool = False) -> bool:
178        """Hits the health_check, it is not necessary authentication.
179
180        Helth check end-points are simple and does not involve connection
181        with database or storage. It will just check if container is up
182        and receiving requests. It is usefull for tests and for inter-service
183        calls.
184
185        If `raise_error` is set as True, function will raise
186        PumpWoodMicroserviceUnavailableError.
187
188        Args:
189            service (str):
190                Pumpwood service to have its cache invalidated, it is expected
191                that the service implement the invalidation end-point
192                `/rest/[service-name]/clear-diskcache`.
193            raise_error (bool):
194                If request does not return 200 status, it will raise a
195                PumpWoodMicroserviceUnavailableError.
196
197        Returns:
198            True if service is healthy.
199
200        Raises:
201            PumpWoodMicroserviceUnavailableError if request does not
202            return 200 code.
203        """
204        # Service url begging are related to service general functions
205        url = 'service/{service}/health-check/'.format(service=service)
206        get_url = urljoin(self.server_url, url)
207        response = requests.get(get_url, timeout=5)
208
209        # Raise a Pumpwood error if raise_if_not_healthy informing that the
210        # service health-check did not return a OK status
211        if not response.ok and raise_error:
212            msg = (
213                "Call on service [{service}] health-check end-point did not "
214                "return a OK status.")
215            raise PumpWoodMicroserviceUnavailableError(
216                msg, payload={'service': service})
217
218        return response.ok

Hits the health_check, it is not necessary authentication.

Helth check end-points are simple and does not involve connection with database or storage. It will just check if container is up and receiving requests. It is usefull for tests and for inter-service calls.

If raise_error is set as True, function will raise PumpWoodMicroserviceUnavailableError.

Arguments:
  • service (str): Pumpwood service to have its cache invalidated, it is expected that the service implement the invalidation end-point /rest/[service-name]/clear-diskcache.
  • raise_error (bool): If request does not return 200 status, it will raise a PumpWoodMicroserviceUnavailableError.
Returns:

True if service is healthy.

Raises:
  • PumpWoodMicroserviceUnavailableError if request does not
  • return 200 code.
class ABCPermissionMicroservice(abc.ABC, pumpwood_communication.microservice_abc.base.base.PumpWoodMicroServiceBase):
 12class ABCPermissionMicroservice(ABC, PumpWoodMicroServiceBase):
 13    """Abstract class for permission checking at pumpwood."""
 14
 15    def check_if_logged(self, auth_header: dict = None) -> bool:
 16        """Check if user is logged.
 17
 18        Args:
 19            auth_header (dict): = None
 20                AuthHeader to substitute the microservice original at
 21                request. If not passed, microservice object auth_header
 22                will be used.
 23
 24        Returns:
 25            Return True if auth_header is looged and False if not
 26        """
 27        try:
 28            check = self.request_get(
 29                url="rest/registration/check/",
 30                auth_header=auth_header)
 31        except PumpWoodUnauthorized:
 32            return False
 33        return check
 34
 35    def get_user_info(self, auth_header: dict = None,
 36                      use_disk_cache: bool = False,
 37                      disk_cache_expire: int = None) -> dict:
 38        """Get user info.
 39
 40        Args:
 41            auth_header (dict): = None
 42                AuthHeader to substitute the microservice original at
 43                request. If not passed, microservice object auth_header
 44                will be used.
 45            use_disk_cache (bool):
 46                It possible use disk cache.
 47            disk_cache_expire (int):
 48                Set a time to expire the cache. If not passed env variable
 49                ``PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT``
 50                env variable (legacy spelling supported), default 60 s.
 51
 52        Returns:
 53            A serialized user object with information of the logged user.
 54        """
 55        url = "rest/registration/retrieveauthenticateduser/"
 56        temp_disk_cache_expire = (
 57            disk_cache_expire
 58            if disk_cache_expire is not None else
 59            AUTHORIZATION_CACHE_TIMEOUT)
 60
 61        user_info = self.request_get(
 62            url=url, auth_header=auth_header, use_disk_cache=use_disk_cache,
 63            disk_cache_expire=temp_disk_cache_expire)
 64        return user_info
 65
 66    def check_permission(self, model_class: str, end_point: str,
 67                         extra_arg: str = None, allow_service_user: str = None,
 68                         allow_external: str = None, auth_header: dict = None,
 69                         ) -> dict:
 70        """Get user info.
 71
 72        Args:
 73            model_class (str):
 74                Model class associated to be checked for access.
 75            end_point (str):
 76                Name of the end-point that will be checked for permission. Ex.:
 77                retrieve, save, list, list-without-pag, ...
 78            extra_arg (str):
 79                Used on some end-points. On action end-point it is reponsible
 80                for setting the action associated with the call.
 81            allow_service_user: str = None:
 82
 83            allow_external: str = None:
 84
 85            auth_header (dict):
 86                AuthHeader to substitute the microservice original at
 87                request. If not passed, microservice object auth_header
 88                will be used.
 89
 90        Returns:
 91            A serialized user object with information of the logged user.
 92        """
 93        # user_info = self.request_post(
 94        #     url="rest/registration/check/",
 95        #     payload={
 96        #         'end_point': end_point,
 97        #         'first_arg': first_arg,
 98        #         'second_arg': second_arg,
 99        #         'api_config_allow': api_config_allow,
100        #         'api_config_deny': api_config_deny},
101        #     auth_header=auth_header, timeout=self.default_timeout)
102        # return user_info
103        return True
104
105    def get_user_row_permission(self, auth_header: dict = None,
106                                use_disk_cache: bool = False,
107                                disk_cache_expire: int = None,
108                                return_ids: bool = False
109                                ) -> list[dict] | list[int]:
110        """Get all user associated row pemission.
111
112        It will include row permissions associated directly to user or by
113        row permission associated with user groups that they are into.
114
115        Args:
116            auth_header (dict): = None
117                AuthHeader to substitute the microservice original at
118                request. If not passed, microservice object auth_header
119                will be used.
120            use_disk_cache (bool):
121                It possible use disk cache.
122            disk_cache_expire (int):
123                Set a time to expire the cache. If not passed env variable
124                ``PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT``
125                env variable (legacy spelling supported), default 60 s.
126            return_ids (bool):
127                Return only the ids of the row permissions.
128
129        Returns:
130            Return a list Row permissions associatef with user directly or
131            by a user group.
132        """
133        url = '/rest/userprofile/actions/self_row_permissions/'
134        temp_disk_cache_expire = (
135            disk_cache_expire
136            if disk_cache_expire is not None else
137            AUTHORIZATION_CACHE_TIMEOUT)
138
139        # Try to retrieve disk cache using authorization header to create
140        # the cache hash
141        request_header = self._check_auth_header(auth_header)
142        hash_dict = None
143        results = None
144        is_on_cache = False
145        if use_disk_cache:
146            hash_dict = {
147                'context': 'pumpwood_communication-get_user_row_permission',
148                'authorization': request_header['Authorization']}
149            results = default_cache.get(hash_dict=hash_dict)
150            if results is not None:
151                msg = "row_permission from cache url[{url}]".format(url=url)
152                logger.info(msg)
153                is_on_cache = True
154
155        if results is None:
156            results = self.request_post(
157                url=url, auth_header=auth_header, data={})['result']
158
159        if use_disk_cache and not is_on_cache:
160            default_cache.set(
161                hash_dict=hash_dict, value=results,
162                expire=temp_disk_cache_expire)
163
164        if return_ids:
165            return [x['pk'] for x in results]
166        else:
167            return results

Abstract class for permission checking at pumpwood.

def check_if_logged(self, auth_header: dict = None) -> bool:
15    def check_if_logged(self, auth_header: dict = None) -> bool:
16        """Check if user is logged.
17
18        Args:
19            auth_header (dict): = None
20                AuthHeader to substitute the microservice original at
21                request. If not passed, microservice object auth_header
22                will be used.
23
24        Returns:
25            Return True if auth_header is looged and False if not
26        """
27        try:
28            check = self.request_get(
29                url="rest/registration/check/",
30                auth_header=auth_header)
31        except PumpWoodUnauthorized:
32            return False
33        return check

Check if user is logged.

Arguments:
  • auth_header (dict): = None AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
Returns:

Return True if auth_header is looged and False if not

def get_user_info( self, auth_header: dict = None, use_disk_cache: bool = False, disk_cache_expire: int = None) -> dict:
35    def get_user_info(self, auth_header: dict = None,
36                      use_disk_cache: bool = False,
37                      disk_cache_expire: int = None) -> dict:
38        """Get user info.
39
40        Args:
41            auth_header (dict): = None
42                AuthHeader to substitute the microservice original at
43                request. If not passed, microservice object auth_header
44                will be used.
45            use_disk_cache (bool):
46                It possible use disk cache.
47            disk_cache_expire (int):
48                Set a time to expire the cache. If not passed env variable
49                ``PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT``
50                env variable (legacy spelling supported), default 60 s.
51
52        Returns:
53            A serialized user object with information of the logged user.
54        """
55        url = "rest/registration/retrieveauthenticateduser/"
56        temp_disk_cache_expire = (
57            disk_cache_expire
58            if disk_cache_expire is not None else
59            AUTHORIZATION_CACHE_TIMEOUT)
60
61        user_info = self.request_get(
62            url=url, auth_header=auth_header, use_disk_cache=use_disk_cache,
63            disk_cache_expire=temp_disk_cache_expire)
64        return user_info

Get user info.

Arguments:
  • auth_header (dict): = None AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
  • use_disk_cache (bool): It possible use disk cache.
  • disk_cache_expire (int): Set a time to expire the cache. If not passed env variable PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT env variable (legacy spelling supported), default 60 s.
Returns:

A serialized user object with information of the logged user.

def check_permission( self, model_class: str, end_point: str, extra_arg: str = None, allow_service_user: str = None, allow_external: str = None, auth_header: dict = None) -> dict:
 66    def check_permission(self, model_class: str, end_point: str,
 67                         extra_arg: str = None, allow_service_user: str = None,
 68                         allow_external: str = None, auth_header: dict = None,
 69                         ) -> dict:
 70        """Get user info.
 71
 72        Args:
 73            model_class (str):
 74                Model class associated to be checked for access.
 75            end_point (str):
 76                Name of the end-point that will be checked for permission. Ex.:
 77                retrieve, save, list, list-without-pag, ...
 78            extra_arg (str):
 79                Used on some end-points. On action end-point it is reponsible
 80                for setting the action associated with the call.
 81            allow_service_user: str = None:
 82
 83            allow_external: str = None:
 84
 85            auth_header (dict):
 86                AuthHeader to substitute the microservice original at
 87                request. If not passed, microservice object auth_header
 88                will be used.
 89
 90        Returns:
 91            A serialized user object with information of the logged user.
 92        """
 93        # user_info = self.request_post(
 94        #     url="rest/registration/check/",
 95        #     payload={
 96        #         'end_point': end_point,
 97        #         'first_arg': first_arg,
 98        #         'second_arg': second_arg,
 99        #         'api_config_allow': api_config_allow,
100        #         'api_config_deny': api_config_deny},
101        #     auth_header=auth_header, timeout=self.default_timeout)
102        # return user_info
103        return True

Get user info.

Arguments:
  • model_class (str): Model class associated to be checked for access.
  • end_point (str): Name of the end-point that will be checked for permission. Ex.: retrieve, save, list, list-without-pag, ...
  • extra_arg (str): Used on some end-points. On action end-point it is reponsible for setting the action associated with the call.
  • allow_service_user: str = None:
  • allow_external: str = None:
  • auth_header (dict): AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
Returns:

A serialized user object with information of the logged user.

def get_user_row_permission( self, auth_header: dict = None, use_disk_cache: bool = False, disk_cache_expire: int = None, return_ids: bool = False) -> list[dict] | list[int]:
105    def get_user_row_permission(self, auth_header: dict = None,
106                                use_disk_cache: bool = False,
107                                disk_cache_expire: int = None,
108                                return_ids: bool = False
109                                ) -> list[dict] | list[int]:
110        """Get all user associated row pemission.
111
112        It will include row permissions associated directly to user or by
113        row permission associated with user groups that they are into.
114
115        Args:
116            auth_header (dict): = None
117                AuthHeader to substitute the microservice original at
118                request. If not passed, microservice object auth_header
119                will be used.
120            use_disk_cache (bool):
121                It possible use disk cache.
122            disk_cache_expire (int):
123                Set a time to expire the cache. If not passed env variable
124                ``PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT``
125                env variable (legacy spelling supported), default 60 s.
126            return_ids (bool):
127                Return only the ids of the row permissions.
128
129        Returns:
130            Return a list Row permissions associatef with user directly or
131            by a user group.
132        """
133        url = '/rest/userprofile/actions/self_row_permissions/'
134        temp_disk_cache_expire = (
135            disk_cache_expire
136            if disk_cache_expire is not None else
137            AUTHORIZATION_CACHE_TIMEOUT)
138
139        # Try to retrieve disk cache using authorization header to create
140        # the cache hash
141        request_header = self._check_auth_header(auth_header)
142        hash_dict = None
143        results = None
144        is_on_cache = False
145        if use_disk_cache:
146            hash_dict = {
147                'context': 'pumpwood_communication-get_user_row_permission',
148                'authorization': request_header['Authorization']}
149            results = default_cache.get(hash_dict=hash_dict)
150            if results is not None:
151                msg = "row_permission from cache url[{url}]".format(url=url)
152                logger.info(msg)
153                is_on_cache = True
154
155        if results is None:
156            results = self.request_post(
157                url=url, auth_header=auth_header, data={})['result']
158
159        if use_disk_cache and not is_on_cache:
160            default_cache.set(
161                hash_dict=hash_dict, value=results,
162                expire=temp_disk_cache_expire)
163
164        if return_ids:
165            return [x['pk'] for x in results]
166        else:
167            return results

Get all user associated row pemission.

It will include row permissions associated directly to user or by row permission associated with user groups that they are into.

Arguments:
  • auth_header (dict): = None AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
  • use_disk_cache (bool): It possible use disk cache.
  • disk_cache_expire (int): Set a time to expire the cache. If not passed env variable PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT env variable (legacy spelling supported), default 60 s.
  • return_ids (bool): Return only the ids of the row permissions.
Returns:

Return a list Row permissions associatef with user directly or by a user group.