Browse Source

Aug 17 : [UPDT] Bug Fixed 'login_using_qr'

pull/277/head
AjmalCybro 2 years ago
parent
commit
de36430bc6
  1. 43
      login_using_qr/controllers/qr_scanner.py
  2. 7
      login_using_qr/models/res_users.py
  3. 15
      login_using_qr/views/redirect_page_templates.xml
  4. 28
      login_using_qr/views/res_users_views.xml

43
login_using_qr/controllers/qr_scanner.py

@ -1,36 +1,22 @@
# -*- coding: utf-8 -*-
###############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2023-TODAY Cybrosys Technologies(<https://www.cybrosys.com>)
# Author: Mohamed Muzammil VP (odoo@cybrosys.com)
#
# 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/>.
#
###############################################################################
import cv2
from pyzbar.pyzbar import decode
from odoo.http import Controller, request
from odoo import http
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', 'signup_email'}
class LoginController(Controller):
"""controller that works when Login With QR clicked"""
@http.route(['/web/redirect'], type='http', auth='none', website=True,
csrf=False, csrf_token=None)
def open_scanner(self):
def open_scanner(self, *args, **kw):
"""This code scan the QR provided and Login to the corresponding user
note: Only Internal User can login through it"""
try:
@ -58,8 +44,16 @@ class LoginController(Controller):
else:
cap.release()
cv2.destroyAllWindows()
# Rendering the template of redirect
return request.render("login_using_qr.redirect_to")
# Use the overridden web_login method to show error message
values = {k: v for k, v in request.params.items() if
k in SIGN_UP_REQUEST_PARAMS}
values['error'] = ("Wrong QR Code")
request.update_env(user=request.session.uid)
request.env["ir.http"]._auth_method_public()
response = request.render('web.login', values)
return response
# Display the resulting frame
cv2.imshow('scanner- to exit press "q"', frame)
@ -71,3 +65,4 @@ class LoginController(Controller):
return request.redirect('/web/login')
except Exception:
return request.render("login_using_qr.be_patient")

7
login_using_qr/models/res_users.py

@ -19,6 +19,7 @@
# If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
try:
import qrcode
except ImportError:
@ -45,14 +46,14 @@ class ResUsers(models.Model):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=3,
border=4,
box_size=10,
border=1,
)
qr.add_data(detail.login)
qr.make(fit=True)
img = qr.make_image()
temp = BytesIO()
img.save(temp, format="PNG")
img.save(temp, format="PNG",optimise=True)
qr_image = base64.b64encode(temp.getvalue())
detail.update({'qr_code': qr_image})
else:

15
login_using_qr/views/redirect_page_templates.xml

@ -1,20 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<!-- template to show if the scanned QR Code is not accepted -->
<template id="redirect_to">
<div>
<center>
<h5>
This feature currently not available for you
</h5>
<a href="/web/login">
<h5>
redirect to login page
</h5>
</a>
</center>
</div>
</template>
<!-- template to show if any error occur in opening of scanner -->
<template id="be_patient">
<div>

28
login_using_qr/views/res_users_views.xml

@ -1,14 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<!--adding field in res.users-->
<record id="view_users_form" model="ir.ui.view">
<field name="name">res.users.view.form.inherit.login.using.qr</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name = 'login']" position="after">
<field name="qr_code" widget='image' class="oe_avatar"/>
<!--adding field in res.users-->
<record id="view_users_form" model="ir.ui.view">
<field name="name">res.users.view.form.inherit.login.using.qr</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<data>
<xpath expr="//page[@name='access_rights']" position="after">
<page string="Login QR Code" name="qr_page">
<span>LOGIN QR</span><br/>
<field name="qr_code" class="oe_form_binary_file" widget="image"/>
</page>
</xpath>
</field>
</record>
</odoo>
</data>
</field>
</record>
</odoo>

Loading…
Cancel
Save