Granting a user access to a task with a script

In ELMA, access to a task is granted to its author, executor, co-executors and executor’s superiors. In reality, sometimes these access permissions are insufficient for convenient work in the system. For example, a head of a department is assigned a task, and the substitute user has to be able to view this task. This being said, reassigning the task to the substitute user is not an option, since it is required that the head of a department is the executor of the task. Since in ELMA by default the substitute user has nothing to do with this task (they are not the author, executor, etc.), they will not have access to the task. However, you can use a script in a business process to implement that. Here is the script:

The script uses the following variables:
task – a variable of the Task Base Class type. It contains the task, permissions to which are granted to the substitute user.
head – a User-type variable, contains the user, who occupies the department head position. 
substitute – a User-type variable, contains the user, who substitutes the department head. 

Script with Public API

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.
Namespace:
using EleWise.ELMA.API;
 
Script text:
//if the substitute user already has access to the task
if (task.Permissions.Any(p => p.User == substitute))
  //interrupt the script
  return;
//get the department head's access permissions for the task
var permissions = task.Permissions.Where(p => p.User == head).ToList();
//if permissions are not found, interrupt the script
if (permissions.Count == 0)
  return;
//for each permission
foreach (var permission in permissions)
{
  //create a new permission set item for the Substitute and grant them the same permissions
     var newPermissin = PublicAPI.Portal.Objects.Tasks.TaskBasePermission.Create();
  newPermissin.Target = permission.Target;
  newPermissin.TypeRole = permission.TypeRole;
  newPermissin.AllowChief = permission.AllowChief; // you can set false, to avoid granting access to the substitute's superiors
  newPermissin.User = substitute;  
//add the created item to the task access permissions set
task.Permissions.Add(newPermissin);
}
//save the changes in the task
task.Save();

Script without PublicAPI

Namespaces:

using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Tasks.Models;

Script text:

//if the substitute user already has access to the task
if (task.Permissions.Any(p => p.User == substitute))
  //interrupt the script
  return; 
//get the department head’s permissions to the task
var permissions = task.Permissions.Where(p => p.User == head).ToList();
//if permissions are not found, interrupt the script
if (permissions.Count == 0)
  return;
//for each permission
foreach (var permission in permissions)
{
  //create a new permission for the substitute user and grant them the same 
permissions 
  var newPermissin = InterfaceActivator.Create<TaskBasePermission>();
  newPermissin.Target = permission.Target;
  newPermissin.TypeRole = permission.TypeRole;
  newPermissin.AllowChief = permission.AllowChief; 
  newPermissin.User = substitute;
  //add the created permission to the task
  task.Permissions.Add(newPermissin);
}
//save the changes made to the task
task.Save();

As the result of executing the script the user, contained in the substitute variable will be granted access to the task, stored in the task variable.