diff --git a/addons/project/models/project.py b/addons/project/models/project.py index 5cc110fbe31..a227c6a3bb3 100644 --- a/addons/project/models/project.py +++ b/addons/project/models/project.py @@ -1764,6 +1764,8 @@ class Task(models.Model): vals['analytic_account_id'] = project.analytic_account_id.id if project.analytic_tag_ids: vals['analytic_tag_ids'] = [Command.set(project.analytic_tag_ids.ids)] + else: + vals['user_ids'] = [Command.link(self.env.user.id)] return vals @@ -1874,6 +1876,10 @@ class Task(models.Model): # user_ids change: update date_assign if vals.get('user_ids'): vals['date_assign'] = fields.Datetime.now() + if not project_id: + user_ids = self._fields['user_ids'].convert_to_cache(vals.get('user_ids', []), self) + if self.env.user.id not in user_ids: + vals['user_ids'] = [Command.set(list(user_ids) + [self.env.user.id])] # Stage change: Update date_end if folded stage and date_last_stage_update if vals.get('stage_id'): vals.update(self.update_date_end(vals['stage_id'])) diff --git a/addons/project/tests/test_access_rights.py b/addons/project/tests/test_access_rights.py index 8bab7f6f5bb..bf9dba4dd26 100644 --- a/addons/project/tests/test_access_rights.py +++ b/addons/project/tests/test_access_rights.py @@ -366,9 +366,37 @@ class TestAccessRightsPrivateTask(TestAccessRights): self.assertEqual(vals[0]['name'], private_task.name) @users('Project user') - def test_project_user_cannot_create_private_task_for_another_user(self): - with self.assertRaises(AccessError): - self.create_private_task('test private for another user', self.env.user, user_ids=[Command.set(self.user_projectuser.ids)]) + def test_project_user_can_create_private_task_for_another_user(self): + self.create_private_task('Private task', user_ids=[Command.set(self.user_projectuser.ids)]) + + @users('Project user') + def test_project_current_user_is_added_in_private_task_assignees(self): + task_values = {'name': 'Private task'} + my_private_task = self.env['project.task'].create(task_values) + self.assertEqual(my_private_task.user_ids, self.env.user, 'When no assignee is set on a private task, the task should be assigned to the current user.') + user_projectuser_private_task = self.env['project.task'].create({**task_values, 'user_ids': [Command.set(self.user_projectuser.ids)]}) + self.assertTrue(self.env.user in user_projectuser_private_task.user_ids, 'When creating a private task for another user, the current user should be added to the assignees.') + + @users('Project user') + def test_project_current_user_is_added_in_task_assignees_when_project_id_is_set(self): + task_values = {'name': 'Private task', 'project_id': self.project_pigs.id, 'user_ids': [Command.set(self.user_projectuser.ids)]} + user_projectuser_task = self.env['project.task'].create(task_values) + self.assertFalse(self.env.user in user_projectuser_task.user_ids, "When creating a task that has a project for another user, the current user should not be added to the assignees.") + + @users('Project user') + def test_project_current_user_is_set_as_assignee_in_task_when_project_id_is_set_with_no_assignees(self): + task = self.env['project.task'].create({'name': 'Private task', 'project_id': self.project_pigs.id}) + self.assertEqual(task.user_ids, self.env.user, "When creating a task that has a project without assignees, the task will be assigned to the current user if no default_project_id is provided in the context (which is handled in _default_personal_stage_type_id).") + + @users('Project user') + def test_project_current_user_is_not_added_in_private_task_assignees_when_default_project_id_is_in_the_context(self): + task_values = {'name': 'Private task'} + context = {'default_project_id': self.project_pigs.id} + ProjectTask_with_default_project_id = self.env['project.task'].with_context(context) + task = ProjectTask_with_default_project_id.create(task_values) + self.assertNotEqual(task.user_ids, self.env.user, "When creating a task without assignees and providing default_project_id in the context, the task should not be assigned to the current user.") + user_projectuser_task = ProjectTask_with_default_project_id.create({**task_values, 'user_ids': [Command.set(self.user_projectuser.ids)]}) + self.assertFalse(self.env.user in user_projectuser_task.user_ids, "When creating a task for another user and providing default_project_id in the context, the current user should not be added to the assignees.") @users('Project user') def test_project_user_cannot_write_private_task_of_another_user(self):