[UPD] odex_web_app: add feature multiple databases

This commit is contained in:
Samir Ladoui 2024-11-12 15:30:19 +01:00
parent 38c18d85ee
commit 5ed981fb4a
1 changed files with 26 additions and 22 deletions

View File

@ -3,6 +3,7 @@ import werkzeug
from odoo import http, tools
from odoo.http import request, Response
from odoo.addons.auth_signup.models.res_users import SignupError
from odoo.addons.web.controllers.main import ensure_db
from odoo.exceptions import UserError
import base64
from ..validator import validator
@ -20,44 +21,47 @@ class AuthenticationController(http.Controller):
@http.route('/rest_api/web/login', type='http', auth='none', csrf=False, cors='*', methods=['POST'])
def login_phone(self, **kw):
if not kw:
kw = json.loads(http.request.httprequest.data)
db = kw.get('db')
login = kw.get('login')
password = kw.get('password')
if not login:
return http_helper.response(code=400, message=_('username or email is missing'), success=False)
# Check for required fields
if not login:
return http_helper.response(code=400, message=_('Username or email is missing'), success=False)
if not password:
return http_helper.response(code=400, message=_('Password is missing'), success=False)
if not kw.get('device_id'):
return http_helper.response(code=400, message=_('Device id is missing'), success=False)
# check fcm_token
if not kw.get('fcm_token_web'):
return http_helper.response(code=400, message=_('FCM Token is missing'), success=False)
user = request.env['res.users'].sudo().search([('login', '=', login)], limit=1)
if not user or not user.login:
return http_helper.response(code=400, message=_('User account with login {} not found').format(login),
success=False)
# Set the database for the request environment
if db:
ensure_db()
# Authenticate user
uid = http_helper.is_authentic(login, password)
if not uid:
return http_helper.errcode(code=400, message=_('Unable to Sign In. invalid user password'))
token = validator.create_token(request.env.user)
dic = request.env.user.to_dict(True)
employee = http.request.env['hr.employee'].sudo().search([('user_id', '=', user.id)], limit=1)
if employee and kw.get('device_id') and not employee.device_id:
employee.sudo().write({'device_id': kw.get('device_id')})
# write fcm_token and fcm_token_web in employee
fcm_token_web = kw.get('fcm_token_web')
if employee and fcm_token_web:
employee.sudo().write({'fcm_token_web': fcm_token_web})
# Generate token and prepare response
user = request.env['res.users'].browse(uid)
token = validator.create_token(user)
dic = user.sudo().to_dict(True)
employee = request.env['hr.employee'].sudo().search([('user_id', '=', user.id)], limit=1)
# Update device_id and fcm_token_web if present
if employee:
if kw.get('device_id') and not employee.device_id:
employee.sudo().write({'device_id': kw.get('device_id')})
if kw.get('fcm_token_web'):
employee.sudo().write({'fcm_token_web': kw.get('fcm_token_web')})
dic['token'] = token
http_helper.cleanup();
return http_helper.response(data=dic, message=_("User log in successfully"))
http_helper.cleanup()
return http_helper.response(data=dic, message=_("User logged in successfully"))
@http.route('/rest_api/web/validate',type='http', auth='none', csrf=False, cors='*',methods=['POST'])