|
7 | 7 | from pytz import timezone, UTC |
8 | 8 |
|
9 | 9 | from odoo import fields |
10 | | -from odoo.exceptions import UserError, ValidationError |
| 10 | +from odoo.exceptions import AccessError, UserError, ValidationError |
11 | 11 | from odoo.tools import mute_logger |
12 | 12 | from odoo.tests.common import Form |
13 | 13 | from odoo.tests import tagged |
@@ -1182,3 +1182,101 @@ def test_leave_creation_without_allocation(self): |
1182 | 1182 | 'request_date_from': '2024-06-01', |
1183 | 1183 | 'request_date_to': '2024-06-02', |
1184 | 1184 | }) |
| 1185 | + |
| 1186 | + def test_calendar_event_create_access_rights(self): |
| 1187 | + """Test that a manager can validate a leave request for an employee linked to a portal user. |
| 1188 | + Customers defined custom ACLs and record rules to support the possibility to assign a portal user to employees |
| 1189 | + and still be able to manage their holidays. |
| 1190 | + """ |
| 1191 | + # Add the required ACLs and record rules to allow portal users to create `calendar.event`. |
| 1192 | + # This reflects the customization done by customers for the reason explained above. |
| 1193 | + self.env['ir.model.access'].create([ |
| 1194 | + # Read access on `mail.activity.type` for portal required for |
| 1195 | + # https://github.com/odoo/odoo/blob/cc0060e889603eb2e47fa44a8a22a70d7d784185/addons/calendar/models/calendar_event.py#L734 |
| 1196 | + { |
| 1197 | + 'name': 'Portal can read mail.activity.type', |
| 1198 | + 'model_id': self.env.ref('mail.model_mail_activity_type').id, |
| 1199 | + 'group_id': self.env.ref('base.group_portal').id, |
| 1200 | + 'perm_read': True, 'perm_create': False, 'perm_write': False, 'perm_unlink': False, |
| 1201 | + }, |
| 1202 | + # Read access on `mail.activity` for portal required for |
| 1203 | + # https://github.com/odoo/odoo/blob/cc0060e889603eb2e47fa44a8a22a70d7d784185/addons/calendar/models/calendar_event.py#L786 |
| 1204 | + # https://github.com/odoo/odoo/blob/cc0060e889603eb2e47fa44a8a22a70d7d784185/addons/calendar/models/calendar_event.py#L882 |
| 1205 | + { |
| 1206 | + 'name': 'Portal can read mail.activity', |
| 1207 | + 'model_id': self.env.ref('mail.model_mail_activity').id, |
| 1208 | + 'group_id': self.env.ref('base.group_portal').id, |
| 1209 | + 'perm_read': True, 'perm_create': False, 'perm_write': False, 'perm_unlink': False, |
| 1210 | + }, |
| 1211 | + # Read and create acess on `calendar.event` for portal required for |
| 1212 | + # https://github.com/odoo/odoo/blob/cc0060e889603eb2e47fa44a8a22a70d7d784185/addons/hr_holidays/models/hr_leave.py#L894-L898 |
| 1213 | + # Write and unlink added to match the customer customization + out of common sense, |
| 1214 | + # if you give create to portal for their own events, |
| 1215 | + # you give write and unlink so they can manage their own events |
| 1216 | + { |
| 1217 | + 'name': 'Portal all CRUD on calendar.event', |
| 1218 | + 'model_id': self.env.ref('calendar.model_calendar_event').id, |
| 1219 | + 'group_id': self.env.ref('base.group_portal').id, |
| 1220 | + 'perm_read': True, 'perm_create': True, 'perm_write': True, 'perm_unlink': True, |
| 1221 | + }, |
| 1222 | + # Read and create acess on `calendar.event` for portal required for |
| 1223 | + # https://github.com/odoo/odoo/blob/cc0060e889603eb2e47fa44a8a22a70d7d784185/addons/calendar/models/calendar_event.py#L760-L768 |
| 1224 | + # Write and unlink added to match the customer customization + out of common sense, |
| 1225 | + # if you give create to portal for their own events attendees, |
| 1226 | + # you give write and unlink so they can manage their own attendees |
| 1227 | + { |
| 1228 | + 'name': 'Portal all CRUD on calendar.attendee', |
| 1229 | + 'model_id': self.env.ref('calendar.model_calendar_attendee').id, |
| 1230 | + 'group_id': self.env.ref('base.group_portal').id, |
| 1231 | + 'perm_read': True, 'perm_create': True, 'perm_write': True, 'perm_unlink': True, |
| 1232 | + }]) |
| 1233 | + self.env['ir.rule'].create([ |
| 1234 | + # Restrict portals to their own activities |
| 1235 | + # so they cannot read the activities of other users |
| 1236 | + { |
| 1237 | + 'name': 'Portal own mail activity', |
| 1238 | + 'model_id': self.env.ref('mail.model_mail_activity').id, |
| 1239 | + 'groups': [(4, self.env.ref('base.group_portal').id)], |
| 1240 | + 'domain_force': "['|', ('user_id', '=', user.id), ('create_uid', '=', user.id)]", |
| 1241 | + }, |
| 1242 | + # Restrict portals to their own events |
| 1243 | + # so they cannot read the events of other users |
| 1244 | + { |
| 1245 | + 'name': 'Portal own calendar events', |
| 1246 | + 'model_id': self.env.ref('calendar.model_calendar_event').id, |
| 1247 | + 'groups': [(4, self.env.ref('base.group_portal').id)], |
| 1248 | + 'domain_force': "[('partner_ids', 'in', user.partner_id.id)]", |
| 1249 | + }, |
| 1250 | + # Restrict portals to their own attendees |
| 1251 | + # so they cannot read the attendees of other users |
| 1252 | + { |
| 1253 | + 'name': 'Portal own calendar attendees', |
| 1254 | + 'model_id': self.env.ref('calendar.model_calendar_attendee').id, |
| 1255 | + 'groups': [(4, self.env.ref('base.group_portal').id)], |
| 1256 | + 'domain_force': "[('partner_id', '=', user.partner_id.id)]", |
| 1257 | + } |
| 1258 | + ]) |
| 1259 | + |
| 1260 | + # Create a portal user and assign it to the employee |
| 1261 | + user_portal = self.env['res.users'].create({ |
| 1262 | + 'name': 'Portal', 'login': 'portal_user', 'password': 'portal_user', |
| 1263 | + 'groups_id': [(6, 0, [self.env.ref('base.group_portal').id])], |
| 1264 | + }) |
| 1265 | + self.employee_emp.user_id = user_portal |
| 1266 | + |
| 1267 | + # As a manager, create a leave request for the employee linked to a portal user |
| 1268 | + leave = self.env['hr.leave'].with_user(self.user_hrmanager_id).create({ |
| 1269 | + 'name': 'Holiday Request', |
| 1270 | + 'employee_id': self.employee_emp_id, |
| 1271 | + 'holiday_status_id': self.holidays_type_1.id, |
| 1272 | + 'date_from': (datetime.today() - relativedelta(days=1)), |
| 1273 | + 'date_to': datetime.today(), |
| 1274 | + 'number_of_days': 1, |
| 1275 | + }) |
| 1276 | + |
| 1277 | + # Assert the employee cannot approve his own leave request |
| 1278 | + with self.assertRaises(AccessError): |
| 1279 | + leave.with_user(self.user_employee_id).action_approve() |
| 1280 | + |
| 1281 | + # Assert the manager can approve the leave request assign to portal employee |
| 1282 | + leave.with_user(self.user_hrmanager_id).action_approve() |
0 commit comments