-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
base: 16.0
Are you sure you want to change the base?
[16.0][ADD] hr_attendance_ip_check: IP-based attendance validation #189
Conversation
There was a problem hiding this 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.""" |
There was a problem hiding this comment.
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__) |
There was a problem hiding this comment.
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')} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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'): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
[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:
Test scenario: