Skip to content

Commit

Permalink
[MIG] access_restriction_by_ip
Browse files Browse the repository at this point in the history
  • Loading branch information
yibudak committed Jan 13, 2025
1 parent f109fae commit 0ba4fea
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 82 deletions.
2 changes: 1 addition & 1 deletion access_restriction_by_ip/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ Cybrosys Techno Solutions
Authors
-------
* Niyas Raphy <[email protected]>
* Ahmet Altinisik <[email protected]>
* Ahmet Altinisik <[email protected]>
3 changes: 0 additions & 3 deletions access_restriction_by_ip/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
Expand All @@ -19,5 +18,3 @@
##############################################################################
from . import controllers
from . import models


39 changes: 18 additions & 21 deletions access_restriction_by_ip/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
Expand All @@ -18,26 +17,24 @@
#
##############################################################################
{
'name': 'Access Restriction By IP',
'summary': """User Can Access His Account Only From Specified IP Address""",
'version': '12.0.1.0.0',
'description': """User Can Access His Account Only From Specified IP Address""",
'author': 'Cybrosys Techno Solutions',
'company': 'Cybrosys Techno Solutions',
'website': 'https://www.cybrosys.com',
'category': 'Tools',
'depends': ['base', 'mail'],
'external_dependencies': {
'python': ['ipaddress'],
"name": "Access Restriction By IP",
"summary": """User Can Access His Account Only From Specified IP Address""",
"version": "16.0.1.0.0",
"author": "Cybrosys Techno Solutions, Altinkaya Enclosures",
"company": "Cybrosys Techno Solutions",
"website": "https://github.com/altinkaya-opensource/odoo-addons",
"category": "Tools",
"depends": ["base", "mail"],
"external_dependencies": {
"python": ["ipaddress"],
},
'license': 'LGPL-3',
'data': [
'security/ir.model.access.csv',
'views/allowed_ips_view.xml',
"license": "LGPL-3",
"data": [
"security/ir.model.access.csv",
"views/allowed_ips_view.xml",
],
'images': ['static/description/banner.jpg'],
'demo': [],
'installable': True,
'auto_install': False,
"images": ["static/description/banner.jpg"],
"demo": [],
"installable": True,
"auto_install": False,
}

2 changes: 0 additions & 2 deletions access_restriction_by_ip/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
Expand All @@ -18,4 +17,3 @@
#
##############################################################################
from . import main

70 changes: 47 additions & 23 deletions access_restriction_by_ip/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
Expand All @@ -17,40 +16,62 @@
# If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from odoo.addons.web.controllers.main import Home
from odoo.http import request
from odoo.exceptions import Warning
import ipaddress

import odoo
import odoo.modules.registry
from odoo.tools.translate import _
from odoo import http
import ipaddress
from odoo.http import request
from odoo.tools.translate import _

from odoo.addons.web.controllers.main import Home

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'}
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",
}


class IPRestriction(Home):

@http.route()
def web_login(self, *args, **kwargs):
request.params['login_success'] = False
request.params["login_success"] = False
if not request.uid:
request.uid = odoo.SUPERUSER_ID

values = {k: v for k, v in request.params.items() if k in SIGN_UP_REQUEST_PARAMS}
values = {
k: v for k, v in request.params.items() if k in SIGN_UP_REQUEST_PARAMS
}
try:
values['databases'] = http.db_list()
values["databases"] = http.db_list()
except odoo.exceptions.AccessDenied:
values['databases'] = None
values["databases"] = None

if request.httprequest.method == 'POST':
if request.httprequest.method == "POST":
old_uid = request.uid
ip_address = request.httprequest.environ['REMOTE_ADDR']
ip_address = request.httprequest.environ["REMOTE_ADDR"]
login_ip = ipaddress.ip_network(ip_address)
user_rec = request.env['res.users'].sudo().search([('login', '=', request.params.get('login', False))])
user_rec = (
request.env["res.users"]
.sudo()
.search([("login", "=", request.params.get("login", False))])
)
if user_rec.allowed_ips:
ip_ok = False
for rec in user_rec.allowed_ips:
Expand All @@ -60,13 +81,16 @@ def web_login(self, *args, **kwargs):

if not ip_ok:
request.uid = old_uid
values['error'] = _("Not allowed to login from this IP (%s)" % ip_address)
values["error"] = _(
"Not allowed to login from this IP (%(ip_addr)s)",
ip_addr=ip_address,
)

if not odoo.tools.config['list_db']:
values['disable_database_manager'] = True
if not odoo.tools.config["list_db"]:
values["disable_database_manager"] = True

response = request.render('web.login', values)
response.headers['X-Frame-Options'] = 'DENY'
response = request.render("web.login", values)
response.headers["X-Frame-Options"] = "DENY"
return response

return super(IPRestriction, self).web_login(*args, **kwargs)
return super().web_login(*args, **kwargs)
2 changes: 2 additions & 0 deletions access_restriction_by_ip/doc/RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Module <access_restriction_by_ip>

#### 20.02.2019

#### Version 12.0.1.0.0

#### Module Migrated
3 changes: 0 additions & 3 deletions access_restriction_by_ip/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
Expand All @@ -18,5 +17,3 @@
#
##############################################################################
from . import allowed_ips


27 changes: 16 additions & 11 deletions access_restriction_by_ip/models/allowed_ips.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
Expand All @@ -17,30 +16,36 @@
# If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from odoo import api, models, fields, _
from odoo.exceptions import ValidationError
import ipaddress

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class ResUsersInherit(models.Model):
_inherit = 'res.users'
_inherit = "res.users"

allowed_ips = fields.One2many('allowed.ips', 'users_ip', string='IP')
allowed_ips = fields.One2many("allowed.ips", "users_ip", string="IP")


class AllowedIPs(models.Model):
_name = 'allowed.ips'
_name = "allowed.ips"
_description = "Allowed IP address for Odoo Login"

users_ip = fields.Many2one('res.users', string='IP')
ip_address = fields.Char(string='Allowed IP')
users_ip = fields.Many2one("res.users", string="IP")
ip_address = fields.Char(string="Allowed IP")

@api.constrains('ip_address')
@api.constrains("ip_address")
def _check_ip(self):
for rec in self:
if rec.ip_address:
try:
ipaddress.ip_network(rec.ip_address)
except ValueError:
raise ValidationError(_('Please enter a valid IP address or'
' subnet mask (%s)' % rec.ip_address))
raise ValidationError( # noqa: B904
_(
"Please enter a valid IP address or"
" subnet mask (%(ip_addr)s)",
ip_addr=rec.ip_address,
)
)
34 changes: 16 additions & 18 deletions access_restriction_by_ip/views/allowed_ips_view.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record model="ir.ui.view" id="res_users_allowed_ips">
<field name="name">res.users</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<xpath expr="//page[1]" position='after'>
<page string="Allowed IP">
<field name="allowed_ips">
<tree editable="bottom">
<field name="ip_address"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>
</data>
<record model="ir.ui.view" id="res_users_allowed_ips">
<field name="name">res.users</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form" />
<field name="arch" type="xml">
<xpath expr="//page[1]" position='after'>
<page string="Allowed IP">
<field name="allowed_ips">
<tree editable="bottom">
<field name="ip_address" />
</tree>
</field>
</page>
</xpath>
</field>
</record>
</odoo>

0 comments on commit 0ba4fea

Please sign in to comment.