391 lines
16 KiB
Python
391 lines
16 KiB
Python
# -*- coding: utf-8 -*-
|
|
###############################################################################
|
|
#
|
|
# Cybrosys Technologies Pvt. Ltd.
|
|
#
|
|
# Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>)
|
|
# Author: MAyana KP (<https://www.cybrosys.com>)
|
|
#
|
|
# You can modify it under the terms of the GNU LESSER
|
|
# GENERAL PUBLIC LICENSE (LGPL v3), Version 3.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details.
|
|
#
|
|
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
|
|
# (LGPL v3) along with this program.
|
|
# If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
###############################################################################
|
|
import hashlib
|
|
import odoo
|
|
from odoo import http
|
|
from odoo.tools import pycompat
|
|
from odoo.tools.translate import _
|
|
from odoo.http import request
|
|
from odoo.addons.auth_signup.controllers.main import AuthSignupHome as AuthSignupHomeBase
|
|
from odoo.addons.web.controllers.home import Home as WebHome
|
|
from odoo.addons.web.controllers.utils import ensure_db
|
|
|
|
# Shared parameters for all login/signup flows
|
|
SIGN_UP_REQUEST_PARAMS = {'db', 'login', 'debug', 'token', 'message', 'error',
|
|
'scope', 'mode', 'redirect', 'redirect_hostname',
|
|
'email', 'name', 'partner_id', 'password',
|
|
'confirm_password', 'city', 'country_id', 'lang'}
|
|
|
|
|
|
def _get_login_style_values():
|
|
conf_param = request.env['ir.config_parameter'].sudo()
|
|
background_type = conf_param.get_param('web_login_styles.background')
|
|
image = conf_param.get_param('web_login_styles.image')
|
|
url = conf_param.get_param('web_login_styles.url')
|
|
|
|
values = {
|
|
'bg_img': '',
|
|
'content_title': conf_param.get_param('web_login_styles.note_title'),
|
|
'content_description': conf_param.get_param(
|
|
'web_login_styles.note_description'
|
|
),
|
|
}
|
|
|
|
if background_type == 'color':
|
|
values['bg'] = ''
|
|
values['color'] = conf_param.get_param('web_login_styles.color')
|
|
|
|
elif background_type == 'image':
|
|
exist_rec = request.env['ir.attachment'].sudo().search(
|
|
[('is_background', '=', True)]
|
|
)
|
|
|
|
if exist_rec:
|
|
exist_rec.unlink()
|
|
|
|
attachment = request.env['ir.attachment'].sudo().create({
|
|
'name': 'Background Image',
|
|
'datas': image,
|
|
'type': 'binary',
|
|
'mimetype': 'image/png',
|
|
'public': True,
|
|
'is_background': True,
|
|
})
|
|
|
|
base_url = conf_param.get_param('web.base.url')
|
|
|
|
values['bg_img'] = (
|
|
f"{base_url}/web/image?"
|
|
f"model=ir.attachment&id={attachment.id}&field=datas"
|
|
)
|
|
|
|
elif background_type == 'url':
|
|
attachment = request.env['ir.attachment'].sudo().search(
|
|
[('url', '=', url)],
|
|
limit=1,
|
|
)
|
|
|
|
if not attachment:
|
|
attachment = request.env['ir.attachment'].sudo().create({
|
|
'name': 'Background Image URL',
|
|
'url': url,
|
|
'type': 'url',
|
|
'public': True,
|
|
})
|
|
|
|
encode = hashlib.md5(
|
|
pycompat.to_text(attachment.url).encode("utf-8")
|
|
).hexdigest()[:7]
|
|
|
|
values['bg_img'] = f"/web/image/{attachment.id}-{encode}"
|
|
|
|
return values
|
|
|
|
|
|
def _get_login_template(orientation):
|
|
return {
|
|
'right': 'web_login_styles.login_template_right',
|
|
'left': 'web_login_styles.login_template_left',
|
|
'middle': 'web_login_styles.login_template_middle',
|
|
'custom': 'web_login_styles.login_template_custom',
|
|
'custom_left': 'web_login_styles.login_template_unique_custom_left',
|
|
'custom_right': 'web_login_styles.login_template_unique_custom_right',
|
|
}.get(
|
|
orientation,
|
|
'web_login_styles.login_template_unique_custom_left',
|
|
)
|
|
|
|
|
|
class Home(WebHome):
|
|
# @http.route(route='/web/login', type='http', auth="none")
|
|
# def web_login(self, redirect=None, **kw):
|
|
# """Override web_login function to add features of this module."""
|
|
# ensure_db()
|
|
# request.env['ir.ui.menu'].sudo().clear_caches()
|
|
# master_selected = kw.get('master_select')
|
|
# request.params['login_success'] = False
|
|
# if request.httprequest.method == 'GET' and redirect and request.session.uid:
|
|
# return request.redirect(redirect)
|
|
# if not request.uid:
|
|
# request.update_env(user=odoo.SUPERUSER_ID)
|
|
# values = {val: item for val, item in request.params.items() if
|
|
# val in SIGN_UP_REQUEST_PARAMS}
|
|
# try:
|
|
# values['databases'] = http.db_list()
|
|
# except odoo.exceptions.AccessDenied:
|
|
# values['databases'] = None
|
|
# if request.httprequest.method == 'POST':
|
|
# old_uid = request.update_env(user=request.session.uid)
|
|
# try:
|
|
# credential = {'login': request.params['login'], 'password': request.params['password'],
|
|
# 'type': 'password'}
|
|
# uid = request.session.authenticate(request.session.db, credential)
|
|
# request.params['login_success'] = True
|
|
# return request.redirect(
|
|
# self._login_redirect(uid, redirect=redirect))
|
|
# except odoo.exceptions.AccessDenied as e:
|
|
# request.update_env = old_uid
|
|
# if e.args == odoo.exceptions.AccessDenied().args:
|
|
# values['error'] = _("Wrong login/password")
|
|
# else:
|
|
# values['error'] = e.args[0]
|
|
# else:
|
|
# if 'error' in request.params and request.params.get(
|
|
# 'error') == 'access':
|
|
# values['error'] = _(
|
|
# 'Only employees can access this database. '
|
|
# 'Please contact the administrator.')
|
|
# if 'login' not in values and request.session.get('auth_login'):
|
|
# values['login'] = request.session.get('auth_login')
|
|
# if not odoo.tools.config['list_db']:
|
|
# values['disable_database_manager'] = True
|
|
# conf_param = request.env['ir.config_parameter'].sudo()
|
|
# orientation = conf_param.get_param('web_login_styles.orientation')
|
|
# image = conf_param.get_param('web_login_styles.image')
|
|
# url = conf_param.get_param('web_login_styles.url')
|
|
# background_type = conf_param.get_param('web_login_styles.background')
|
|
# content_title = conf_param.get_param('web_login_styles.note_title')
|
|
# content_description = conf_param.get_param('web_login_styles.note_description')
|
|
# values['content_title'] = content_title
|
|
# values['content_description'] = content_description
|
|
# values['masters'] = request.env['master.control'].sudo().search([], order='sequence asc, id asc')
|
|
# request.env['ir.ui.menu'].sudo().clear_caches()
|
|
# request.env['ir.ui.menu'].sudo()._visible_menu_ids()
|
|
# if request.session.uid and master_selected:
|
|
# user = request.env.user
|
|
# master = request.env['master.control'].sudo().search(
|
|
# [('code', '=', master_selected)], limit=1
|
|
# )
|
|
#
|
|
# if master.exists() and master.user_ids:
|
|
# if user not in master.user_ids:
|
|
# request.session.logout(keep_db=True)
|
|
#
|
|
# # Create a response with JavaScript alert
|
|
# html = f"""
|
|
# <html>
|
|
# <body>
|
|
# <script>
|
|
# alert("{_("You don't have access to login to '%s'. Please contact the administrator.") % master.display_name}");
|
|
# window.location.href = '/web/login';
|
|
# </script>
|
|
# </body>
|
|
# </html>
|
|
# """
|
|
# return http.Response(html, content_type='text/html')
|
|
# # request.session.uid = None
|
|
# # request.params['login_success'] = False
|
|
# # response.qcontext['error'] = _(
|
|
# # "You don't have access to login to '%s'. "
|
|
# # "Please contact the administrator."
|
|
# # ) % master.display_name
|
|
# # return response
|
|
#
|
|
# request.session['active_master'] = master.code
|
|
# if background_type == 'color':
|
|
# values['bg'] = ''
|
|
# values['color'] = conf_param.sudo().get_param(
|
|
# 'web_login_styles.color')
|
|
# elif background_type == 'image':
|
|
# exist_rec = request.env['ir.attachment'].sudo().search(
|
|
# [('is_background', '=', True)])
|
|
# if exist_rec:
|
|
# exist_rec.unlink()
|
|
# attachments = request.env['ir.attachment'].sudo().create({
|
|
# 'name': 'Background Image',
|
|
# 'datas': image,
|
|
# 'type': 'binary',
|
|
# 'mimetype': 'image/png',
|
|
# 'public': True,
|
|
# 'is_background': True
|
|
# })
|
|
# base_url = conf_param.sudo().get_param('web.base.url')
|
|
# url = base_url + '/web/image?' + 'model=ir.attachment&id=' + str(
|
|
# attachments.id) + '&field=datas'
|
|
# values['bg_img'] = url or ''
|
|
# elif background_type == 'url':
|
|
# pre_exist = request.env['ir.attachment'].sudo().search(
|
|
# [('url', '=', url)])
|
|
# if not pre_exist:
|
|
# attachments = request.env['ir.attachment'].sudo().create({
|
|
# 'name': 'Background Image URL',
|
|
# 'url': url,
|
|
# 'type': 'url',
|
|
# 'public': True
|
|
# })
|
|
# else:
|
|
# attachments = pre_exist
|
|
# encode = hashlib.md5(
|
|
# pycompat.to_text(attachments.url).encode("utf-8")).hexdigest()[
|
|
# 0:7]
|
|
# encode_url = "/web/image/{}-{}".format(attachments.id, encode)
|
|
# values['bg_img'] = encode_url or ''
|
|
# if orientation == 'right':
|
|
# response = request.render('web_login_styles.login_template_unique_custom',
|
|
# values)
|
|
# elif orientation == 'left':
|
|
# response = request.render('web_login_styles.login_template_left',
|
|
# values)
|
|
# elif orientation == 'middle':
|
|
# response = request.render('web_login_styles.login_template_middle',
|
|
# values)
|
|
# else:
|
|
# response = request.render('web_login_styles.login_template_unique_custom', values)
|
|
# response.headers['X-Frame-Options'] = 'DENY'
|
|
# return response
|
|
|
|
@http.route(route='/web/login', type='http', auth="none", website=True)
|
|
def web_login(self, redirect=None, **kw):
|
|
"""Override web_login function to add features of this module."""
|
|
ensure_db()
|
|
|
|
request.env['ir.ui.menu'].sudo().clear_caches()
|
|
master_selected = kw.get('master_select')
|
|
request.params['login_success'] = False
|
|
|
|
if request.httprequest.method == 'GET' and redirect and request.session.uid:
|
|
return request.redirect(redirect)
|
|
|
|
if not request.uid:
|
|
request.update_env(user=odoo.SUPERUSER_ID)
|
|
|
|
values = {
|
|
val: item
|
|
for val, item in request.params.items()
|
|
if val in SIGN_UP_REQUEST_PARAMS
|
|
}
|
|
|
|
try:
|
|
values['databases'] = http.db_list()
|
|
except odoo.exceptions.AccessDenied:
|
|
values['databases'] = None
|
|
|
|
if request.httprequest.method == 'POST':
|
|
old_uid = request.uid
|
|
|
|
try:
|
|
request.update_env(user=request.session.uid)
|
|
|
|
credential = {
|
|
'login': request.params['login'],
|
|
'password': request.params['password'],
|
|
'type': 'password',
|
|
}
|
|
|
|
auth_info = request.session.authenticate(
|
|
request.session.db,
|
|
credential,
|
|
)
|
|
uid = auth_info["uid"]
|
|
|
|
# -------------------------------
|
|
# Master validation
|
|
# -------------------------------
|
|
if master_selected:
|
|
user = request.env.user
|
|
|
|
master = request.env['master.control'].sudo().search(
|
|
[('code', '=', master_selected)],
|
|
limit=1,
|
|
)
|
|
|
|
if master and master.user_ids and user not in master.user_ids:
|
|
request.session.logout(keep_db=True)
|
|
|
|
values['error'] = _(
|
|
"You don't have access to login to '%s'. "
|
|
"Please contact the administrator."
|
|
) % master.display_name
|
|
|
|
values['masters'] = request.env[
|
|
'master.control'
|
|
].sudo().search([], order='sequence asc, id asc')
|
|
values.update(_get_login_style_values())
|
|
orientation = request.env[
|
|
'ir.config_parameter'
|
|
].sudo().get_param('web_login_styles.orientation')
|
|
|
|
return request.render(
|
|
_get_login_template(orientation),
|
|
values,
|
|
)
|
|
|
|
if master:
|
|
request.session['active_master'] = master.code
|
|
|
|
request.params['login_success'] = True
|
|
|
|
return request.redirect(
|
|
self._login_redirect(uid, redirect=redirect)
|
|
)
|
|
|
|
except odoo.exceptions.AccessDenied as e:
|
|
request.update_env(user=old_uid)
|
|
|
|
if e.args == odoo.exceptions.AccessDenied().args:
|
|
values['error'] = _("Wrong login/password")
|
|
else:
|
|
values['error'] = e.args[0]
|
|
|
|
else:
|
|
if request.params.get('error') == 'access':
|
|
values['error'] = _(
|
|
'Only employees can access this database. '
|
|
'Please contact the administrator.'
|
|
)
|
|
|
|
if 'login' not in values and request.session.get('auth_login'):
|
|
values['login'] = request.session.get('auth_login')
|
|
|
|
if not odoo.tools.config['list_db']:
|
|
values['disable_database_manager'] = True
|
|
|
|
conf_param = request.env['ir.config_parameter'].sudo()
|
|
|
|
orientation = conf_param.get_param('web_login_styles.orientation')
|
|
values.update(_get_login_style_values())
|
|
|
|
values['masters'] = request.env['master.control'].sudo().search(
|
|
[],
|
|
order='sequence asc, id asc',
|
|
)
|
|
|
|
response = request.render(_get_login_template(orientation), values)
|
|
response.headers['X-Frame-Options'] = 'DENY'
|
|
|
|
return response
|
|
|
|
|
|
class AuthSignupHome(AuthSignupHomeBase, Home):
|
|
@http.route()
|
|
def web_auth_signup(self, *args, **kw):
|
|
return super().web_auth_signup(*args, **kw)
|
|
|
|
@http.route()
|
|
def web_auth_reset_password(self, *args, **kw):
|
|
return super().web_auth_reset_password(*args, **kw)
|
|
|
|
def get_auth_signup_qcontext(self):
|
|
qcontext = super().get_auth_signup_qcontext()
|
|
qcontext.update(_get_login_style_values())
|
|
return qcontext
|