forked from OCA/rest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] authenticated_partner_id in ServiceContextProvider
- Loading branch information
Showing
16 changed files
with
260 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from . import service | ||
from . import service_context_provider | ||
from . import cerberus_validator | ||
from . import user_component_context_provider |
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,12 @@ | ||
# Copyright 2021 ACSONE SA/NV | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
|
||
from odoo.addons.component.core import AbstractComponent | ||
|
||
|
||
class AbstractUserAuthenticatedPartnerProvider(AbstractComponent): | ||
_name = "abstract.user.authenticated.partner.provider" | ||
|
||
def _get_authenticated_partner_id(self): | ||
return self.env.user.partner_id.id |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from . import ir_rule | ||
from . import rest_service_registration |
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,27 @@ | ||
# Copyright 2021 ACSONE SA/NV | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import api, models | ||
|
||
|
||
class IrRule(models.Model): | ||
"""Add authenticated_partner_id in record rule evaluation context. | ||
This come from the env context, which is populated by the base_rest service layer | ||
context provider. | ||
""" | ||
|
||
_inherit = "ir.rule" | ||
|
||
@api.model | ||
def _eval_context(self): | ||
ctx = super()._eval_context() | ||
if "authenticated_partner_id" in self.env.context: | ||
ctx["authenticated_partner_id"] = self.env.context[ | ||
"authenticated_partner_id" | ||
] | ||
return ctx | ||
|
||
def _compute_domain_keys(self): | ||
"""Return the list of context keys to use for caching ``_compute_domain``.""" | ||
return super()._compute_domain_keys() + ["authenticated_partner_id"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Copyright 2021 ACSONE SA/NV | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
from odoo.addons.component.core import Component | ||
from odoo.addons.website.tools import MockRequest | ||
|
||
from .. import restapi | ||
from .common import TransactionRestServiceRegistryCase | ||
|
||
|
||
class TestServiceContextProvider(TransactionRestServiceRegistryCase): | ||
"""Test Odoo service context provider | ||
In this class we test the context provided by the service context provider | ||
""" | ||
|
||
def test_01(self): | ||
"""Test authenticated_partner_id | ||
In this case we check that the default service context provider provides | ||
no authenticated_partner_id | ||
""" | ||
|
||
# pylint: disable=R7980 | ||
class TestServiceNewApi(Component): | ||
_inherit = "base.rest.service" | ||
_name = "test.partner.service" | ||
_usage = "partner" | ||
_collection = self._collection_name | ||
_description = "test" | ||
|
||
@restapi.method( | ||
[(["/<int:id>/get", "/<int:id>"], "GET")], | ||
output_param=restapi.CerberusValidator("_get_partner_schema"), | ||
auth="public", | ||
) | ||
def get(self, _id): | ||
return {"name": self.env["res.partner"].browse(_id).name} | ||
|
||
self._build_services(self, TestServiceNewApi) | ||
controller = self._get_controller_for(TestServiceNewApi) | ||
with MockRequest(self.env), controller().service_component( | ||
"partner" | ||
) as service: | ||
self.assertFalse(service.work.authenticated_partner_id) | ||
|
||
def test_02(self): | ||
"""Test authenticated_partner_id | ||
In this case we check that the 'abstract.user.authenticated.partner.provider' | ||
service context provider provides the current user's partner as | ||
authenticated_partner_id | ||
""" | ||
|
||
# pylint: disable=R7880 | ||
class TestComponentContextprovider(Component): | ||
_name = "test.component.context.provider" | ||
_inherit = [ | ||
"abstract.user.authenticated.partner.provider", | ||
"base.rest.service.context.provider", | ||
] | ||
_usage = "test_component_context_provider" | ||
|
||
self._BaseTestController._component_context_provider = ( | ||
"test_component_context_provider" | ||
) | ||
|
||
# pylint: disable=R7980 | ||
class TestServiceNewApi(Component): | ||
_inherit = "base.rest.service" | ||
_name = "test.partner.service" | ||
_usage = "partner" | ||
_collection = self._collection_name | ||
_description = "test" | ||
|
||
@restapi.method( | ||
[(["/<int:id>/get", "/<int:id>"], "GET")], | ||
output_param=restapi.CerberusValidator("_get_partner_schema"), | ||
auth="public", | ||
) | ||
def get(self, _id): | ||
return {"name": self.env["res.partner"].browse(_id).name} | ||
|
||
self._build_components(TestComponentContextprovider) | ||
self._build_services(self, TestServiceNewApi) | ||
controller = self._get_controller_for(TestServiceNewApi) | ||
with MockRequest(self.env), controller().service_component( | ||
"partner" | ||
) as service: | ||
self.assertEqual( | ||
service.work.authenticated_partner_id, self.env.user.partner_id.id | ||
) | ||
|
||
def test_03(self): | ||
"""Test authenticated_partner_id | ||
In this case we check that redefining the method _get_authenticated_partner_id | ||
changes the authenticated_partner_id provided by the service context provider | ||
""" | ||
|
||
# pylint: disable=R7880 | ||
class TestComponentContextprovider(Component): | ||
_name = "test.component.context.provider" | ||
_inherit = "base.rest.service.context.provider" | ||
_usage = "test_component_context_provider" | ||
|
||
def _get_authenticated_partner_id(self): | ||
return 9999 | ||
|
||
self._BaseTestController._component_context_provider = ( | ||
"test_component_context_provider" | ||
) | ||
|
||
# pylint: disable=R7980 | ||
class TestServiceNewApi(Component): | ||
_inherit = "base.rest.service" | ||
_name = "test.partner.service" | ||
_usage = "partner" | ||
_collection = self._collection_name | ||
_description = "test" | ||
|
||
@restapi.method( | ||
[(["/<int:id>/get", "/<int:id>"], "GET")], | ||
output_param=restapi.CerberusValidator("_get_partner_schema"), | ||
auth="public", | ||
) | ||
def get(self, _id): | ||
return {"name": self.env["res.partner"].browse(_id).name} | ||
|
||
self._build_components(TestComponentContextprovider) | ||
self._build_services(self, TestServiceNewApi) | ||
controller = self._get_controller_for(TestServiceNewApi) | ||
with MockRequest(self.env), controller().service_component( | ||
"partner" | ||
) as service: | ||
self.assertEqual(service.work.authenticated_partner_id, 9999) |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from . import auth_jwt_component_context_provider | ||
from . import service |
23 changes: 23 additions & 0 deletions
23
base_rest_auth_jwt/components/auth_jwt_component_context_provider.py
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,23 @@ | ||
# Copyright 2021 ACSONE SA/NV | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
|
||
from odoo.http import request | ||
|
||
from odoo.addons.component.core import AbstractComponent, Component | ||
|
||
|
||
class AbstractAuthJwtAuthenticatedPartnerProvider(AbstractComponent): | ||
_name = "abstract.auth.jwt.authenticated.partner.provider" | ||
|
||
def _get_authenticated_partner_id(self): | ||
return request.jwt_partner_id | ||
|
||
|
||
class BaseRestAuthJwtComponentContextProvider(Component): | ||
_name = "base.rest.auth.jwt.component.context.provider" | ||
_inherit = [ | ||
"abstract.auth.jwt.authenticated.partner.provider", | ||
"base.rest.service.context.provider", | ||
] | ||
_usage = "auth_jwt_component_context_provider" |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from . import ping_services | ||
from . import partner_services | ||
from . import partner_image_services | ||
from . import partner_jwt_services | ||
from . import exception_services | ||
from . import partner_new_api_services |
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,10 @@ | ||
# Copyright 2021 ACSONE SA/NV | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
from odoo.addons.component.core import Component | ||
|
||
|
||
class PingJwtService(Component): | ||
_inherit = "ping.service" | ||
_name = "ping.jwt.service" | ||
_collection = "base.rest.demo.jwt.services" |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# generated from manifests external_dependencies | ||
apispec | ||
apispec>=4.0.0 | ||
cerberus | ||
jsondiff | ||
|