Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][ADD] hr_attendance_ip_check: IP-based attendance validation #189

Open
wants to merge 3 commits into
base: 16.0
Choose a base branch
from

Conversation

kongkea-aditi
Copy link

[16.0][ADD] hr_attendance_ip_check: IP-based attendance validation

This module extends HR attendance to validate check-in/check-out based on IP address.

Features:

  • IP whitelist configuration
  • Restrict attendance actions to allowed IPs
  • Easy enable/disable through settings

Test scenario:

  1. Install module
  2. Configure allowed IPs in Settings
  3. Try check-in from allowed and blocked IPs

@kongkea-aditi kongkea-aditi reopened this Oct 31, 2024
@kongkea-aditi kongkea-aditi changed the title [ADD] hr_attendance_ip_check: IP-based attendance validation [16.0][ADD] hr_attendance_ip_check: IP-based attendance validation Oct 31, 2024
Copy link

@ByteMeAsap ByteMeAsap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already an open PR for v17 for a module(also having the same module name - hr_attendance_ip_check) in this repo itself (#168). It was opened quite a while ago - in June.The changes added through that PR does implement the same feature in a much better way. It takes into account CIDR for IP address checking and also it manages the whitelisted IPs in a much more efficient way.Also, the overall coding standards are better. So, it could have been simply backported to v16 rather than having the module developed through this PR.

@@ -0,0 +1,2 @@
"""IP-based Attendance Check module initialization."""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this comment

import logging
from odoo import models, api, _

_logger = logging.getLogger(__name__)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused imports

def create(self, vals_list):
"""Override create to validate IP before creating attendance records."""
if not vals_list:
return {'warning': _('No valid attendance records to create')}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to return warning for missing vals_list , it would be there. Anyways warning cannot be used here like this

validation_result = employee._validate_ip_address('check_in')

if isinstance(validation_result, dict):
return validation_result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warnings cannot be returned from create functions its only used for onchange functions. Instead of warnings throw ValidationError directly from _validate_ip_address function


def write(self, vals):
"""Override write to validate IP before updating attendance records."""
if 'check_out' in vals:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for both check in and check out as check in time can also be modified later and accordingly pass check_in/check_out on line 31

for attendance in self:
validation_result = attendance.employee_id._validate_ip_address('check_out')
if isinstance(validation_result, dict):
return validation_result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to create functions, warnings cannot be returned


def _validate_ip_address(self, action_type='attendance'):
"""Validate if current IP is allowed for attendance actions."""
if not self.env['ir.config_parameter'].sudo().get_param(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make use of const_eval(after importing it)
from odoo.tools.safe_eval import const_eval

const_eval(self.env['ir.config_parameter'].sudo().get_param('hr_attendance.ip_check_enabled', 'False')))

_logger.error("Error getting IP address: %s", str(e), exc_info=True)
return None

def _validate_ip_address(self, action_type='attendance'):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return validation errors instead of warnings in this function


# Validate IP first
ip_validation = self._validate_ip_address(action_type)
if isinstance(ip_validation, dict):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validation error should be raised so no need to checking if its a dictionary

config_parameter='hr_attendance.ip_check_enabled',
help="Enable IP address validation for attendance check-in/check-out"
)
whitelist_ips = fields.Char(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having a comma separated list of IPs which would be very difficult to handle in long run, some other way should be used to handle the IPs.For ex: The allowed IPs can be stored on a per employee basis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants