Search code examples
asp.netsensenet

SetPermission With sensenet API - version 6.3


I am working on sensenet API. I faced an issue with setPermission on sensenetAPI security.

As per concern, when I create a document I would like to give See, open, Save and RunApplication permission as a default for newly created document to the user(User is taken from the function parameter).

To achieve this I use below code

   public static void SetCollabUserSecurity(string myUserEmailId, Node myNodetToSetSecurity)
    {
        var domainName = "Builtin";
        var strUsername = GetNameFromEmail(myUserEmailId);
        User user;
        using (new SystemAccount())
        {
                user = User.Load(domainName, strUsername);
                if (user != null && user.Enabled)
                {
                    var myUser = user;
                    myNodetToSetSecurity.Security.SetPermission(myUser, true, PermissionType.See,
                        PermissionValue.Allow);
                    myNodetToSetSecurity.Security.SetPermission(myUser, true, PermissionType.Open,
                        PermissionValue.Allow);
                    myNodetToSetSecurity.Security.SetPermission(myUser, true, PermissionType.Save,
                        PermissionValue.Allow);
                    myNodetToSetSecurity.Security.SetPermission(myUser, true, PermissionType.RunApplication,
                       PermissionValue.Allow);
                }
        }
    }

While I am using this function, my process for creating document becomes time consuming. It takes around 40 second time for execution.

So in case of, if I would like to share the same newly created document with multiple users, lets say there are 3 user and I want to give the above permission to all of them then my single function call takes 120 second (2 minute) time to simply assign permission.

Is there any Odata REST API call available or any sensenet library call available through which I can assign...

1) multiple permission to multiple user for single document or 2) multiple permission to single user for single document

Can anyone help to come out from this issue?

Thanks!


Solution

  • C# api

    On the server there is a c# api for managing permissions, please check this article for details. You may use the AclEditor class for setting multiple permissions in one round. Please note that you have to call the Apply method at the end to actually perform the operation.

    // set permissions on folder1, folder2 and file1 for two users and a group
    SecurityHandler.CreateAclEditor()
       .Allow(folder1.Id, user1.Id, false, PermissionType.Open, PermissionType.Custom01)
       .Allow(folder2.Id, user2.Id, false, PermissionType.Open)
       .Allow(file1.Id, editorsGroup.Id, false, PermissionType.Save)
       .Apply();
    

    As a side note: in most cases it is better to work with groups than users when assigning permissions. So it is advisable to give permissions to a group and put users into the group as members instead of assigning permissions to users directly.

    Also: it is easier to maintain a simpler security structure, for example if you assign a permission on the parent container (e.g. a folder) instead of on individual files. Of course if you have to set permission per file, then it is fine.

    OData api

    The same api is available from the client through the REST api. Please take a look at the SetPermissions action in this article or the similar api in the JavaScript client library of sensenet.