Files
jikimo_sf/yizuo_login_background_and_styles/controllers/main.py
2023-01-31 11:02:53 +08:00

150 lines
8.0 KiB
Python

# -*- coding: utf-8 -*-
#############################################################################
#
# Changsha Yizuo Intelligent Technology Co., Ltd.
#
# Copyright (C) 2020-TODAY Yizuo Intelligent Technology.(<https://www.yizuo.ltd>).
# Author: Van(v16)(pengyb@yizuo.ltd)
#
# You can modify it under the terms of the GNU AFFERO
# GENERAL PUBLIC LICENSE (AGPL 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 AFFERO GENERAL PUBLIC LICENSE (AGPL v3) for more details.
#
# You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
# (AGPL v3) along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
try:
import httpagentparser
except ImportError:
pass
from time import gmtime, strftime
from odoo.addons.web.controllers import home
from odoo.http import request
from odoo.exceptions import Warning
import odoo
import odoo.modules.registry
from odoo.tools.translate import _
from odoo import http
class Home(home.Home):
@http.route('/web/login', type='http', auth="public")
def web_login(self, redirect=None, **kw):
home.ensure_db()
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.uid = odoo.SUPERUSER_ID
values = request.params.copy()
try:
values['databases'] = http.db_list()
except odoo.exceptions.AccessDenied:
values['databases'] = None
if request.httprequest.method == 'POST':
old_uid = request.uid
uid = request.session.authenticate(request.session.db,
request.params['login'],
request.params['password'])
if uid is not False:
# user_rec = request.env['res.users'].sudo().search(
# [('id', '=', uid)])
# if user_rec.partner_id.email and user_rec.has_group(
# 'user_login_alert.receive_login_notification'):
# send_mail = 0
# agent = request.httprequest.environ.get('HTTP_USER_AGENT')
# agent_details = httpagentparser.detect(agent)
# user_os = agent_details['os']['name']
# browser_name = agent_details['browser']['name']
# ip_address = request.httprequest.environ['REMOTE_ADDR']
# if user_rec.last_logged_ip and user_rec.last_logged_browser and user_rec.last_logged_os:
# if user_rec.last_logged_ip != ip_address or user_rec.last_logged_browser != browser_name or user_rec.last_logged_os != user_os:
# send_mail = 1
# user_rec.last_logged_ip = ip_address
# user_rec.last_logged_browser = browser_name
# user_rec.last_logged_os = user_os
# else:
# send_mail = 0
# else:
# send_mail = 1
# user_rec.last_logged_ip = ip_address
# user_rec.last_logged_browser = browser_name
# user_rec.last_logged_os = user_os
# if send_mail == 1:
# email_to = user_rec.partner_id.email
# current_date_time = strftime("%Y-%m-%d %H:%M:%S",
# gmtime())
# message_body = 'Hi ' + user_rec.name + ' , Your account has been ' \
# 'accessed successfully. The details of the ' \
# 'system from which the account is accessed ...,'
# message_body += '<table border="1" width="100%" cellpadding="0" bgcolor="#ededed">'
# message_body += '<tr><td>' + 'OS' + '</td>' \
# '<td>' + user_os + '</td>' \
# '</tr>' \
# '<tr><td>' + 'Browser' + '</td>' \
# '<td>' + browser_name + '</td>' \
# '</tr>' \
# '<tr><td>' + 'IP Address' + '</td>' \
# '<td>' + ip_address + '</td>' \
# '</tr>'
# message_body += '</table>'
# message_body += 'Thank you'
# template_obj = request.env['mail.mail']
# template_data = {
# 'subject': 'Login Alert : ' + current_date_time,
# 'body_html': message_body,
# 'email_from': request.env.user.company_id.email,
# 'email_to': email_to
# }
# template_id = template_obj.create(template_data)
# template_obj.send(template_id)
request.params['login_success'] = True
if not redirect:
redirect = '/web'
return request.redirect(
self._login_redirect(uid, redirect=redirect))
request.uid = old_uid
values['error'] = _("Wrong login/password")
# get confi login set
param_obj = request.env['ir.config_parameter'].sudo()
values['reset_password_enabled'] = param_obj.get_param('auth_signup.reset_password')
values['signup_enabled'] = param_obj.get_param('auth_signup.invitation_scope') == 'b2c'
values['disable_footer'] = param_obj.get_param('disable_footer')
style = param_obj.get_param('login_background.style')
background = param_obj.get_param('login_background.background')
values['background_color'] = param_obj.get_param('login_background.color')
background_image = param_obj.get_param('login_background.background_image')
if background == 'image':
image_url = ''
if background_image:
base_url = param_obj.get_param('web.base.url')
image_url = base_url + '/web/image?' + 'model=login.image&id=' + background_image + '&field=image'
values['background_src'] = image_url or ''
values['background_color'] = ''
if background == 'color':
values['background_src'] = ''
if style == 'default' or style is False:
response = request.render('web.login', values)
elif style == 'left':
response = request.render('yizuo_login_background_and_styles.left_login_template', values)
elif style == 'right':
response = request.render('yizuo_login_background_and_styles.right_login_template', values)
else:
response = request.render('yizuo_login_background_and_styles.middle_login_template', values)
response.headers['X-Frame-Options'] = 'DENY'
return response