mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[REF] pms-api-rest: add controller 4 login
This commit is contained in:
committed by
Darío Lodeiros
parent
b3ed4109bb
commit
6f672029f5
@@ -16,8 +16,6 @@
|
||||
"external_dependencies": {
|
||||
"python": ["jwt", "simplejson", "marshmallow"],
|
||||
},
|
||||
"data": [
|
||||
"security/ir.model.access.csv", "data/auth_jwt_validator.xml"
|
||||
],
|
||||
"data": ["data/auth_jwt_validator.xml"],
|
||||
"installable": True,
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
from odoo.addons.base_rest.controllers import main
|
||||
|
||||
|
||||
class BaseRestDemoPublicApiController(main.RestController):
|
||||
class BaseRestPrivateApiController(main.RestController):
|
||||
_root_path = "/api/"
|
||||
_collection_name = "pms.reservation.service"
|
||||
_collection_name = "pms.private.services"
|
||||
_default_auth = "jwt_api_pms"
|
||||
|
||||
|
||||
class BaseRestPublicApiController(main.RestController):
|
||||
_root_path = "/auth/"
|
||||
_collection_name = "pms.public.services"
|
||||
_default_auth = "public"
|
||||
|
||||
@@ -20,3 +20,5 @@ from . import pms_property_info
|
||||
from . import pms_property_search_param
|
||||
from . import pms_account_journal_info
|
||||
from . import pms_payment_info
|
||||
from . import user_input
|
||||
from . import user_output
|
||||
|
||||
9
pms_api_rest/datamodels/user_input.py
Normal file
9
pms_api_rest/datamodels/user_input.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from marshmallow import fields
|
||||
|
||||
from odoo.addons.datamodel.core import Datamodel
|
||||
|
||||
|
||||
class PmsApiRestUserInput(Datamodel):
|
||||
_name = "pms.api.rest.user.input"
|
||||
username = fields.String(required=False, allow_none=True)
|
||||
password = fields.String(required=False, allow_none=True)
|
||||
10
pms_api_rest/datamodels/user_output.py
Normal file
10
pms_api_rest/datamodels/user_output.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from marshmallow import fields
|
||||
|
||||
from odoo.addons.datamodel.core import Datamodel
|
||||
|
||||
|
||||
class PmsApiRestUserOutput(Datamodel):
|
||||
_name = "pms.api.rest.user.output"
|
||||
# user = fields.String(required=False, allow_none=True)
|
||||
# exp = fields.String(required=False, allow_none=True)
|
||||
token = fields.String(required=False, allow_none=True)
|
||||
@@ -1,2 +0,0 @@
|
||||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||
access_jwt_access_token,Read jwt access token,model_jwt_provider_access_token,,1,0,0,0
|
||||
|
@@ -5,4 +5,8 @@ from . import calendar_service
|
||||
from . import partner_services
|
||||
|
||||
from . import reservation_services
|
||||
<<<<<<< HEAD
|
||||
from . import property_services
|
||||
=======
|
||||
from . import login_service
|
||||
>>>>>>> a4394db3... [REF] pms-api-rest: add controller 4 login
|
||||
|
||||
@@ -7,7 +7,7 @@ from odoo.addons.component.core import Component
|
||||
|
||||
class PmsCalendarService(Component):
|
||||
_inherit = "base.rest.service"
|
||||
_name = "pms.calendar.service"
|
||||
_name = "pms.private.services"
|
||||
_usage = "calendar"
|
||||
_collection = "pms.reservation.service"
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ class PmsFolioService(Component):
|
||||
_inherit = "base.rest.service"
|
||||
_name = "pms.folio.service"
|
||||
_usage = "folios"
|
||||
_collection = "pms.reservation.service"
|
||||
_collection = "pms.private.services"
|
||||
|
||||
@restapi.method(
|
||||
[
|
||||
|
||||
116
pms_api_rest/services/login_service.py
Normal file
116
pms_api_rest/services/login_service.py
Normal file
@@ -0,0 +1,116 @@
|
||||
import time
|
||||
|
||||
from jose import jwt
|
||||
|
||||
from odoo import _
|
||||
from odoo.exceptions import ValidationError, AccessDenied, UserError
|
||||
|
||||
from odoo.addons.base_rest import restapi
|
||||
from odoo.addons.base_rest_datamodel.restapi import Datamodel
|
||||
from odoo.addons.component.core import Component
|
||||
|
||||
|
||||
class PmsPartnerService(Component):
|
||||
_inherit = "base.rest.service"
|
||||
_name = "pms.auth.service"
|
||||
_usage = "login"
|
||||
_collection = "pms.public.services"
|
||||
|
||||
@restapi.method(
|
||||
[
|
||||
(
|
||||
[
|
||||
"/",
|
||||
],
|
||||
"POST",
|
||||
)
|
||||
],
|
||||
input_param=Datamodel("pms.api.rest.user.input", is_list=False),
|
||||
# output_param=Datamodel("pms.api.rest.user.output", is_list=False),
|
||||
)
|
||||
def aa(self, user):
|
||||
|
||||
user_record = (
|
||||
self.env["res.users"].sudo().search([("login", "=", user.username)])
|
||||
)
|
||||
|
||||
if not user_record:
|
||||
ValidationError(_("user or password not valid"))
|
||||
try:
|
||||
user_record.with_user(user_record)._check_credentials(user.password, None)
|
||||
|
||||
except Exception as e:
|
||||
raise UserError("")
|
||||
|
||||
PmsApiRestUserOutput = self.env.datamodels["pms.api.rest.user.output"]
|
||||
expiration_date = time.time() + 36660
|
||||
token = jwt.encode(
|
||||
{
|
||||
"aud": "api_pms",
|
||||
"iss": "pms",
|
||||
"exp": expiration_date,
|
||||
"username": user.username,
|
||||
"password": user.password,
|
||||
},
|
||||
key="pms_secret_key_example",
|
||||
algorithm=jwt.ALGORITHMS.HS256,
|
||||
)
|
||||
# return PmsApiRestUserOutput(token=token)
|
||||
return token
|
||||
|
||||
def user_error(self):
|
||||
"""
|
||||
Simulate an odoo.exceptions.UserError
|
||||
Should be translated into BadRequest with a description into the json
|
||||
body
|
||||
"""
|
||||
raise UserError(_("UserError message"))
|
||||
|
||||
# Validator
|
||||
def _validator_user_error(self):
|
||||
return {}
|
||||
|
||||
def _validator_return_user_error(self):
|
||||
return {}
|
||||
|
||||
def _validator_validation_error(self):
|
||||
return {}
|
||||
|
||||
def _validator_return_validation_error(self):
|
||||
return {}
|
||||
|
||||
def _validator_session_expired(self):
|
||||
return {}
|
||||
|
||||
def _validator_return_session_expired(self):
|
||||
return {}
|
||||
|
||||
def _validator_missing_error(self):
|
||||
return {}
|
||||
|
||||
def _validator_return_missing_error(self):
|
||||
return {}
|
||||
|
||||
def _validator_access_error(self):
|
||||
return {}
|
||||
|
||||
def _validator_return_access_error(self):
|
||||
return {}
|
||||
|
||||
def _validator_access_denied(self):
|
||||
return {}
|
||||
|
||||
def _validator_return_access_denied(self):
|
||||
return {}
|
||||
|
||||
def _validator_http_exception(self):
|
||||
return {}
|
||||
|
||||
def _validator_return_http_exception(self):
|
||||
return {}
|
||||
|
||||
def _validator_bare_exception(self):
|
||||
return {}
|
||||
|
||||
def _validator_return_bare_exception(self):
|
||||
return {}
|
||||
@@ -7,7 +7,7 @@ class PmsPartnerService(Component):
|
||||
_inherit = "base.rest.service"
|
||||
_name = "pms.partner.service"
|
||||
_usage = "partners"
|
||||
_collection = "pms.reservation.service"
|
||||
_collection = "pms.private.services"
|
||||
|
||||
@restapi.method(
|
||||
[
|
||||
|
||||
@@ -9,7 +9,7 @@ class PmsRoomService(Component):
|
||||
_inherit = "base.rest.service"
|
||||
_name = "pms.reservation.service"
|
||||
_usage = "reservations"
|
||||
_collection = "pms.reservation.service"
|
||||
_collection = "pms.private.services"
|
||||
|
||||
@restapi.method(
|
||||
[
|
||||
|
||||
@@ -7,7 +7,7 @@ class PmsRoomService(Component):
|
||||
_inherit = "base.rest.service"
|
||||
_name = "pms.room.service"
|
||||
_usage = "rooms"
|
||||
_collection = "pms.reservation.service"
|
||||
_collection = "pms.private.services"
|
||||
|
||||
@restapi.method(
|
||||
[
|
||||
|
||||
@@ -7,7 +7,7 @@ class PmsRoomTypeService(Component):
|
||||
_inherit = "base.rest.service"
|
||||
_name = "pms.room.type.service"
|
||||
_usage = "room-types"
|
||||
_collection = "pms.reservation.service"
|
||||
_collection = "pms.private.services"
|
||||
|
||||
@restapi.method(
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user