-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] hr_attendance_ip_check: Migration to 17.0
- Loading branch information
sonhd91
committed
Jul 2, 2024
1 parent
8af1d3c
commit 8c1a764
Showing
14 changed files
with
182 additions
and
728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,86 @@ | ||
.. image:: https://img.shields.io/badge/licence-GPL--3-blue.svg | ||
:target: http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
:alt: License: GPL-3 | ||
|
||
====================== | ||
HR Attendance IP Check | ||
====================== | ||
|
||
.. | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! This file is generated by oca-gen-addon-readme !! | ||
!! changes will be overwritten. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! source digest: sha256:9c42d93221a4eaaee6cef7fc24f794aa874329b16f87e541023fff7a066eac98 | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png | ||
:target: https://odoo-community.org/page/development-status | ||
:alt: Beta | ||
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png | ||
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
:alt: License: AGPL-3 | ||
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhr--attendance-lightgray.png?logo=github | ||
:target: https://github.com/OCA/hr-attendance/tree/17.0/hr_attendance_ip_check | ||
:alt: OCA/hr-attendance | ||
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png | ||
:target: https://translation.odoo-community.org/projects/hr-attendance-17-0/hr-attendance-17-0-hr_attendance_ip_check | ||
:alt: Translate me on Weblate | ||
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png | ||
:target: https://runboat.odoo-community.org/builds?repo=OCA/hr-attendance&target_branch=17.0 | ||
:alt: Try me on Runboat | ||
|
||
|badge1| |badge2| |badge3| |badge4| |badge5| | ||
|
||
Allow attendance check in from specified ip networks only. | ||
|
||
For a detailed documentation have a look at https://www.odoo-wiki.org/. | ||
**Table of contents** | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Bug Tracker | ||
=========== | ||
|
||
Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr-attendance/issues>`_. | ||
In case of trouble, please check there if your issue has already been reported. | ||
If you spotted it first, help us to smash it by providing a detailed and welcomed | ||
`feedback <https://github.com/OCA/hr-attendance/issues/new?body=module:%20hr_attendance_ip_check%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
||
Do not contact contributors directly about support or help with technical issues. | ||
|
||
Credits | ||
======= | ||
|
||
Authors | ||
------- | ||
|
||
* Mint System GmbH | ||
|
||
Contributors | ||
------------ | ||
|
||
- Janik von Rotz [email protected] | ||
- Son Ho [email protected] | ||
|
||
Other credits | ||
------------- | ||
|
||
The original development of this module has been done by Mint System. It | ||
can be found in: | ||
https://github.com/Mint-System/Odoo-Apps-HR/tree/15.0/hr_attendance_ip_check | ||
|
||
.. image:: https://raw.githubusercontent.com/Mint-System/Wiki/master/assets/icon-box.png | ||
:height: 100 | ||
:width: 100 | ||
:alt: Icon | ||
This module has been ported to the OCA with their agreement. | ||
|
||
Configuration | ||
~~~~~~~~~~~~~ | ||
Maintainers | ||
----------- | ||
|
||
* No additional configurations needed | ||
This module is maintained by the OCA. | ||
|
||
Maintainer | ||
========== | ||
.. image:: https://odoo-community.org/logo.png | ||
:alt: Odoo Community Association | ||
:target: https://odoo-community.org | ||
|
||
.. image:: https://raw.githubusercontent.com/Mint-System/Wiki/master/assets/mint-system-logo.png | ||
:target: https://www.mint-system.ch | ||
OCA, or the Odoo Community Association, is a nonprofit organization whose | ||
mission is to support the collaborative development of Odoo features and | ||
promote its widespread use. | ||
|
||
This module is maintained by Mint System GmbH. | ||
This module is part of the `OCA/hr-attendance <https://github.com/OCA/hr-attendance/tree/17.0/hr_attendance_ip_check>`_ project on GitHub. | ||
|
||
For support and more information, please visit `our Website <https://www.mint-system.ch>`__. | ||
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Copyright 2024 Janik von Rotz <[email protected]> | ||
# Copyright 2024 Camptocamp | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
{ | ||
"name": "HR Attendance IP Check", | ||
"summary": """ | ||
|
@@ -6,7 +9,7 @@ | |
"author": "Mint System GmbH, Odoo Community Association (OCA)", | ||
"website": "https://github.com/OCA/hr-attendance", | ||
"category": "Technical", | ||
"version": "15.0.1.1.0", | ||
"version": "17.0.1.0.0", | ||
"license": "AGPL-3", | ||
"depends": ["hr_attendance"], | ||
"data": ["views/attendance_cidr.xml", "security/ir.model.access.csv"], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Copyright 2024 Janik von Rotz <[email protected]> | ||
# Copyright 2024 Camptocamp | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
import ipaddress | ||
import logging | ||
|
||
|
@@ -25,5 +28,5 @@ def _validate_cidr(self): | |
for rec in self: | ||
try: | ||
ipaddress.IPv4Network(rec.cidr) | ||
except: | ||
raise ValidationError(_("This is not a valid CIDR.")) | ||
except Exception: | ||
raise ValidationError(_("This is not a valid CIDR.")) from None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Copyright 2024 Janik von Rotz <[email protected]> | ||
# Copyright 2024 Camptocamp | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo import api, models | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
# Copyright 2024 Janik von Rotz <[email protected]> | ||
# Copyright 2024 Camptocamp | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
import ipaddress | ||
import logging | ||
|
||
from odoo import _, exceptions, fields, models | ||
from odoo.http import request | ||
from odoo.tools import config | ||
|
||
_logger = logging.getLogger(__name__) | ||
import ipaddress | ||
|
||
|
||
class HrEmployee(models.Model): | ||
|
@@ -14,36 +18,46 @@ class HrEmployee(models.Model): | |
|
||
def _attendance_ip_check(self): | ||
"""Return if client ip is not in totp cidrs.""" | ||
|
||
test_enable = config["test_enable"] | ||
# Get remote ip | ||
ip_address = ipaddress.IPv4Address(request.httprequest.environ["REMOTE_ADDR"]) | ||
if test_enable: | ||
ip_address = ipaddress.IPv4Address("127.0.0.1") | ||
else: | ||
ip_address = ipaddress.IPv4Address( | ||
request.httprequest.environ["REMOTE_ADDR"] | ||
) | ||
|
||
# Get cidrs from employee | ||
employee_cidrs = self.sudo().attendance_cidr_ids | ||
for employee in self: | ||
# Get cidrs from employee | ||
employee_cidrs = employee.sudo().attendance_cidr_ids | ||
|
||
# Get single check cidrs | ||
allowed_cidrs = employee_cidrs.filtered("single_check")[:1] | ||
# Get single check cidrs | ||
allowed_cidrs = employee_cidrs.filtered("single_check")[:1] | ||
|
||
# Combine global and employee cidrs | ||
if not allowed_cidrs: | ||
allowed_cidrs = self.env["attendance.cidr"].search( | ||
[("employee_ids", "=", False)] | ||
) | ||
allowed_cidrs += employee_cidrs | ||
|
||
# Chech if ip is in allowed_cidrs | ||
in_cidr = any( | ||
ip_address in cidr | ||
for cidr in allowed_cidrs.mapped(lambda r: ipaddress.IPv4Network(r.cidr)) | ||
) | ||
|
||
# Raise exception if there is a cidr list and the client ip is not in cidr | ||
if allowed_cidrs and not in_cidr: | ||
raise exceptions.ValidationError( | ||
_( | ||
"Not allowed to create new attendance record for %(empl_name)s, the client ip is not in the list of allowed ip networks." | ||
# Combine global and employee cidrs | ||
if not allowed_cidrs: | ||
allowed_cidrs = self.env["attendance.cidr"].search( | ||
[("employee_ids", "=", False)] | ||
) | ||
allowed_cidrs += employee_cidrs | ||
|
||
# Chech if ip is in allowed_cidrs | ||
in_cidr = any( | ||
ip_address in cidr | ||
for cidr in allowed_cidrs.mapped( | ||
lambda r: ipaddress.IPv4Network(r.cidr) | ||
) | ||
% { | ||
"empl_name": self.name, | ||
} | ||
) | ||
|
||
# Raise exception if there is a cidr list and the client ip is not in cidr | ||
if allowed_cidrs and not in_cidr: | ||
raise exceptions.ValidationError( | ||
_( | ||
"Not allowed to create new attendance record for " | ||
"%(empl_name)s, the client ip is not in the " | ||
"list of allowed ip networks." | ||
) | ||
% { | ||
"empl_name": employee.name, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build-system] | ||
requires = ["whool"] | ||
build-backend = "whool.buildapi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* Janik von Rotz <[email protected]> | ||
* Son Ho <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
The original development of this module has been done by Mint System. | ||
It can be found in: https://github.com/Mint-System/Odoo-Apps-HR/tree/15.0/hr_attendance_ip_check | ||
|
||
This module has been ported to the OCA with their agreement. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow attendance check in from specified ip networks only. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_hr_attendance_ip_check |
37 changes: 37 additions & 0 deletions
37
hr_attendance_ip_check/tests/test_hr_attendance_ip_check.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2024 Camptocamp | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
import time | ||
|
||
from odoo.exceptions import ValidationError | ||
from odoo.tests.common import TransactionCase | ||
|
||
|
||
class TestHrAttendanceIPcheck(TransactionCase): | ||
"""Tests for attendance date ranges validity""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.employee = cls.env["hr.employee"].create({"name": "Test Emp"}) | ||
|
||
def test_valid_cidr(self): | ||
with self.assertRaises(ValidationError): | ||
self.attendance_cidr = self.env["attendance.cidr"].create( | ||
{"employee_ids": [self.employee.id], "cidr": "invalid"} | ||
) | ||
|
||
def test_ip_check(self): | ||
self.attendance_cidr = self.env["attendance.cidr"].create( | ||
{"employee_ids": [self.employee.id], "cidr": "127.0.0.1"} | ||
) | ||
Attendance = self.env["hr.attendance"].with_context(test_enable=True) | ||
self.attendance = Attendance.create( | ||
{ | ||
"employee_id": self.employee.id, | ||
"check_in": time.strftime("%Y-%m-10 10:00"), | ||
} | ||
) | ||
emp_attendance = self.env["hr.attendance"].search( | ||
[("employee_id", "=", self.employee.id)] | ||
) | ||
self.assertTrue(self.attendance in emp_attendance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters