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:

//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();

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.

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.