diff --git a/addons/hr/models/res_users.py b/addons/hr/models/res_users.py index 2dc47730b9d..b75d541f4e0 100644 --- a/addons/hr/models/res_users.py +++ b/addons/hr/models/res_users.py @@ -175,9 +175,19 @@ class User(models.Model): # Note: limit the `sudo` to the only action of "editing own profile" action in order to # avoid breaking `groups` mecanism on res.users form view. profile_view = self.env.ref("hr.res_users_view_form_profile") + original_user = self.env.user if profile_view and view_id == profile_view.id: self = self.with_user(SUPERUSER_ID) - return super(User, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) + result = super(User, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) + # Due to using the SUPERUSER the result will contain action that the user may not have access too + # here we filter out actions that requires special implicit rights to avoid having unusable actions + # in the dropdown menu. + if toolbar and self.env.user != original_user: + self = self.with_user(original_user.id) + if not self.user_has_groups("base.group_erp_manager"): + change_password_action = self.env.ref("base.change_password_wizard_action") + result['toolbar']['action'] = [act for act in result['toolbar']['action'] if act['id'] != change_password_action.id] + return result def write(self, vals): """ diff --git a/addons/hr/tests/test_self_user_access.py b/addons/hr/tests/test_self_user_access.py index 6458deac69e..539476e48b0 100644 --- a/addons/hr/tests/test_self_user_access.py +++ b/addons/hr/tests/test_self_user_access.py @@ -76,6 +76,32 @@ class TestSelfAccessProfile(TestHrCommon): # Compare both self.assertEqual(full_fields.keys(), fields.keys(), "View fields should not depend on user's groups") + def test_access_my_profile_toolbar(self): + """ A simple user shouldn't have the possibilities to see the 'Change Password' action""" + james = new_test_user(self.env, login='jam', groups='base.group_user', name='Simple employee', email='jam@example.com') + james = james.with_user(james) + self.env['hr.employee'].create({ + 'name': 'James', + 'user_id': james.id, + }) + view = self.env.ref('hr.res_users_view_form_profile') + available_actions = james.fields_view_get(view_id=view.id, toolbar=True)['toolbar']['action'] + change_password_action = self.env.ref("base.change_password_wizard_action") + + self.assertFalse(any(x['id'] == change_password_action.id for x in available_actions)) + + # An ERP manager should have the possibilities to see the 'Change Password' + john = new_test_user(self.env, login='joh', groups='base.group_erp_manager', name='ERP Manager', email='joh@example.com') + john = john.with_user(john) + self.env['hr.employee'].create({ + 'name': 'John', + 'user_id': john.id, + }) + view = self.env.ref('hr.res_users_view_form_profile') + available_actions = john.fields_view_get(view_id=view.id, toolbar=True)['toolbar']['action'] + self.assertTrue(any(x['id'] == change_password_action.id for x in available_actions)) + + class TestSelfAccessRights(TestHrCommon): @classmethod diff --git a/addons/hr/views/res_users.xml b/addons/hr/views/res_users.xml index a60024152d3..d61884c35bb 100644 --- a/addons/hr/views/res_users.xml +++ b/addons/hr/views/res_users.xml @@ -47,6 +47,7 @@
false + false hr_employee_profile_form