Adding/removing permissions to a document using a script

Example of a script for adding full access to a document to a specific user

Script without using PublicAPI

Context variables:

context.Document - variable of the Document type
context.User - variable of the User type

Important: In this example, a user will get full access to a document, since all the permissions will be added: permissions to view, permissions to edit and full access. If you need to grant permissions to edit, add permissions to view and edit; If you need to grant permissions to view, add permissions to view only. 

Add assemblies:
EleWise.ELMA.Documents
EleWise.ELMA.Documents.Docflow

Declare namespaces:
using EleWise.ELMA.Documents.Models; 
using EleWise.ELMA.Security; 
using EleWise.ELMA.Services; 
using NHibernate.Bytecode;
 
Script text:
context.Document.Permissions.Add(new InstanceOf<DmsObjectPermission>
 {
  New = 
  {
   DmsObject = context.Document,
   PermissionId = EleWise.ELMA.Documents.PermissionProvider.DocumentViewPermission.Id, // - Permissions to view
   TypeRoleId = CommonRoleTypes.User.Id,
   Assigned = context.User    
  }
 }.New);
 context.Document.Permissions.Add(new InstanceOf<DmsObjectPermission>
 {
  New = 
  {
   DmsObject = context.Document,
   PermissionId = EleWise.ELMA.Documents.PermissionProvider.DocumentEditPermission.Id, // - Permissions to edit
   TypeRoleId = CommonRoleTypes.User.Id,
   Assigned = context.User    
  }
 }.New);
    context.Document.Permissions.Add(new InstanceOf<DmsObjectPermission>
 {
  New = 
  {
   DmsObject = context.Document,
   PermissionId = EleWise.ELMA.Documents.PermissionProvider.DocumentManagerAccessPermission.Id, // - Assign permissions
   TypeRoleId = CommonRoleTypes.User.Id,
   Assigned = context.User
  }
     }.New);
 context.Document.Permissions.Add(new InstanceOf<DmsObjectPermission>
 {
  New = 
  {
   DmsObject = context.Document,
   PermissionId = EleWise.ELMA.Documents.PermissionProvider.DocumentFullAccessPermission.Id, // - Full access
   TypeRoleId = CommonRoleTypes.User.Id,
   Assigned = context.User    
  }
 }.New);
 context.Document.Save();
 
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here

Context variables:

context.Document - variable of the Document type
context.User - variable of the User type

Add namespace:
using EleWise.ELMA.API;​

 
Script text:
PublicAPI.Docflow.Document.AddViewPermission(context.Document, context.User);       //Permissions to view
PublicAPI.Docflow.Document.AddEditPermission(context.Document, context.User);           //Permissions to edit
PublicAPI.Docflow.Document.AddManageAccessPermission(context.Document, context.User);   //Permissions to manage access
PublicAPI.Docflow.Document.AddFullAccessPermission(context.Document, context.User); //Full access
 

 

Example of a script for removing access to a document from a user

Script without using PublicAPI
Assemblies and namespaces are the same, as above.

Script text:

var item = context.Document.Permissions.ToList().FindAll(p => p.User == (User)UserManager.Instance.LoadByLogin("admin"));// get document permissions of the admin user
if (item != null) {
            context.Document.Permissions.RemoveAll(item); //remove all the permissions
            context.Document.Save();
        }
 
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
Context variables and namespaces are the same, as above.

Script text:

PublicAPI.Docflow.Document.DeletePermissions(context.Document, context.User);