Skip to content

Commit

Permalink
[MIG] hr_attendance_ip_check: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sonhd91 committed Jul 2, 2024
1 parent 8af1d3c commit 8c1a764
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 728 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ TODO: add repo description.

[//]: # (addons)

This part will be replaced when running the oca-gen-addons-table script from OCA/maintainer-tools.
Available addons
----------------
addon | version | maintainers | summary
--- | --- | --- | ---
[hr_attendance_ip_check](hr_attendance_ip_check/) | 17.0.1.0.0 | | Allow attendance check in from specified ip networks only.

[//]: # (end addons)

Expand Down
674 changes: 0 additions & 674 deletions hr_attendance_ip_check/LICENSE

This file was deleted.

91 changes: 73 additions & 18 deletions hr_attendance_ip_check/README.rst
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.
5 changes: 4 additions & 1 deletion hr_attendance_ip_check/__manifest__.py
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": """
Expand All @@ -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"],
Expand Down
7 changes: 5 additions & 2 deletions hr_attendance_ip_check/models/allowed_cidrs.py
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

Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions hr_attendance_ip_check/models/hr_attendance.py
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


Expand Down
70 changes: 42 additions & 28 deletions hr_attendance_ip_check/models/hr_employee.py
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):
Expand All @@ -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(

Check warning on line 26 in hr_attendance_ip_check/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_attendance_ip_check/models/hr_employee.py#L26

Added line #L26 was not covered by tests
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(

Check warning on line 54 in hr_attendance_ip_check/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_attendance_ip_check/models/hr_employee.py#L54

Added line #L54 was not covered by tests
_(
"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,
}
)
3 changes: 3 additions & 0 deletions hr_attendance_ip_check/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions hr_attendance_ip_check/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Janik von Rotz <[email protected]>
* Son Ho <[email protected]>
5 changes: 5 additions & 0 deletions hr_attendance_ip_check/readme/CREDITS.md
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.
1 change: 1 addition & 0 deletions hr_attendance_ip_check/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow attendance check in from specified ip networks only.
1 change: 1 addition & 0 deletions hr_attendance_ip_check/tests/__init__.py
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 hr_attendance_ip_check/tests/test_hr_attendance_ip_check.py
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)
5 changes: 1 addition & 4 deletions hr_attendance_ip_check/views/attendance_cidr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
<group>
<field name="employee_ids" widget="many2many_tags" />
<field name="cidr" placeholder="192.168.2.0/24" />
<field
name="single_check"
attrs="{'readonly': [('employee_ids', '=', [])]}"
/>
<field name="single_check" readonly="not employee_ids" />
</group>
</group>
</sheet>
Expand Down

0 comments on commit 8c1a764

Please sign in to comment.