Browse Source

Jun 21 [UPDT] Updated 'access_restriction_by_ip'

pull/195/merge
AjmalCybro 1 year ago
parent
commit
743618efd9
  1. 6
      access_restriction_by_ip/__manifest__.py
  2. 14
      access_restriction_by_ip/controllers/main.py
  3. 3
      access_restriction_by_ip/models/allowed_ips.py
  4. 7
      access_restriction_by_ip/static/description/index.html
  5. 33
      access_restriction_by_ip/static/src/js/widget.js

6
access_restriction_by_ip/__manifest__.py

@ -31,8 +31,12 @@
'security/ir.model.access.csv', 'security/ir.model.access.csv',
'views/allowed_ips_view.xml', 'views/allowed_ips_view.xml',
], ],
'assets': {
'web.assets_backend': [
'/access_restriction_by_ip/static/src/js/widget.js',
],
},
'images': ['static/description/banner.png'], 'images': ['static/description/banner.png'],
'installable': True, 'installable': True,
'auto_install': False, 'auto_install': False,
} }

14
access_restriction_by_ip/controllers/main.py

@ -34,10 +34,8 @@ class Home(main.Home):
request.params['login_success'] = False request.params['login_success'] = False
if request.httprequest.method == 'GET' and redirect and request.session.uid: if request.httprequest.method == 'GET' and redirect and request.session.uid:
return request.redirect(redirect) return request.redirect(redirect)
if not request.uid: if not request.uid:
request.uid = odoo.SUPERUSER_ID request.uid = odoo.SUPERUSER_ID
values = request.params.copy() values = request.params.copy()
try: try:
values['databases'] = http.db_list() values['databases'] = http.db_list()
@ -85,5 +83,15 @@ class Home(main.Home):
request.uid = old_uid request.uid = old_uid
if e.args == odoo.exceptions.AccessDenied().args: if e.args == odoo.exceptions.AccessDenied().args:
values['error'] = _("Wrong login/password") values['error'] = _("Wrong login/password")
return request.render('web.login', values) return request.render('web.login', values)
@http.route('/get_ip', auth='user', type='json')
def get_ip(self):
ip_address = request.httprequest.environ['REMOTE_ADDR']
ip_addresses = []
for ips in request.env.user.allowed_ips:
ip_addresses.append(ips.ip_address)
if ip_addresses:
if ip_address not in ip_addresses:
return False
return True

3
access_restriction_by_ip/models/allowed_ips.py

@ -17,7 +17,7 @@
# If not, see <http://www.gnu.org/licenses/>. # If not, see <http://www.gnu.org/licenses/>.
# #
############################################################################## ##############################################################################
from odoo import models, fields from odoo import api, models, fields
class ResUsersInherit(models.Model): class ResUsersInherit(models.Model):
@ -31,3 +31,4 @@ class AllowedIPs(models.Model):
users_ip = fields.Many2one('res.users', string='IP') users_ip = fields.Many2one('res.users', string='IP')
ip_address = fields.Char(string='Allowed IP') ip_address = fields.Char(string='Allowed IP')

7
access_restriction_by_ip/static/description/index.html

@ -1,4 +1,3 @@
<div class="container" style="padding: 1rem !important; margin-bottom: 1rem !important;"> <div class="container" style="padding: 1rem !important; margin-bottom: 1rem !important;">
<div class="row"> <div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 d-flex justify-content-between" <div class="col-sm-12 col-md-12 col-lg-12 d-flex justify-content-between"
@ -210,7 +209,8 @@
Userwise IP</h4> Userwise IP</h4>
<p <p
style="font-family: 'Roboto', sans-serif !important; font-weight: 400 !important; color: #282F33 !important; font-size: 1rem !important;"> style="font-family: 'Roboto', sans-serif !important; font-weight: 400 !important; color: #282F33 !important; font-size: 1rem !important;">
If no IP is set to user means there is not any restriction by IP. IP Address for each users can be set from users form view</p> If no IP is set to user means there is not any restriction by IP. IP Address for each users can be
set from users form view</p>
</div> </div>
</div> </div>
@ -243,7 +243,8 @@
User accessing his account</h4> User accessing his account</h4>
<p <p
style="font-family: 'Roboto', sans-serif !important; font-weight: 400 !important; color: #282F33 !important; font-size: 1rem !important;"> style="font-family: 'Roboto', sans-serif !important; font-weight: 400 !important; color: #282F33 !important; font-size: 1rem !important;">
On accessing account from a non specified IP. On accessing account from a non specified IP.(If the user is already logged in he/she will be logged out
after the first reload)
</p> </p>
<img src="assets/screenshots/access_restriction_by_ip_2.png" class="img-responsive img-thumbnail border" <img src="assets/screenshots/access_restriction_by_ip_2.png" class="img-responsive img-thumbnail border"
width="100%" height="auto"/> width="100%" height="auto"/>

33
access_restriction_by_ip/static/src/js/widget.js

@ -0,0 +1,33 @@
/** @odoo-module **/
import SystrayMenu from 'web.SystrayMenu';
import Widget from 'web.Widget';
var ajax = require('web.ajax');
var core = require('web.core');
var qweb = core.qweb;
var GetUser = Widget.extend({
/**
function run before loading the page to call methode "get_idle_time"
*/
willStart: function() {
var self = this;
return this._super().then(function() {
self.get_user();
});
},
/**
Getting minutes through python for the corresponding user in the backend
*/
get_user: function() {
var self = this
var now = new Date().getTime();
ajax.rpc('/get_ip').then(function(data) {
console.log(data)
if (data == false){
location.replace("/web/session/logout")
}
})
},
});
SystrayMenu.Items.push(GetUser);
export default GetUser;
Loading…
Cancel
Save