IPermissionsChange Method (String, AccessLevel, NullableGuid) |
Namespace: AskiaPortalCmn.Common
The user A (in the context) have the following
permissions on the current module:
- resources: Create and edit
- filters: Create and edit
The user B (the user to edit) have the following permissions:
- resources: Full control
- filters: Read only
using system; using AskiaPortalCmn; namespace MyApp { class Program { static void Main(string[] args) { // Initialize the current module AskiaPortalCmnApi.InitializeModule(Guid.Parse("26f6ab8b-2fdb-4f4f-ad14-7507892846a5")); // Authenticate the userA in the context var context = ContextFactory.CreateByLoginAndPassword("userA", "secret"); var contextValidation = context.Validate(); if (!contextValidation.Success) { Console.Error.Write(contextValidation.Exception.Message); return; } // Search the userB IUser userB = UserFactory.FindByGuid(context, Guid.Parse("20BE5F84-F006-49A0-9674-4B16A090AF2D")); if (userB == null) { Console.Error.WriteLine("Could not find the user with the specified guid"); return; } // Case 1. The user in the context try to modify the permission // of a user that have higher permission // The userA (in the context) have the permission to CreateAndEdit `resources` // The userB have the FullControl permission on `resources` var changeResult1 = userB.Permissions.Change("resources", AccessLevel.ReadOnly, AskiaPortalCmnApi.CurrentModule.Guid); if (changeResult1.Success) { Console.WriteLine("Permission `resources` successfully changed to read-only"); } else { Console.Error.WriteLine("You're permission item `resources` is lower than the userB permission"); } // --> Produce // "You're permission item `resources` is lower than the userB permission" // Case 2. The user in the context try to set a permission // higher than it's own permission // The userA (in the context) have the permission to CreateAndEdit `filters` // The userB have the permission to ReadOnly `filters` var changeResult2 = userB.Permissions.Change("filters", AccessLevel.FullControl, AskiaPortalCmnApi.CurrentModule.Guid); if (changeResult2.Success) { Console.WriteLine("Permission `filters` successfully changed to FullControl"); } else { Console.Error.WriteLine("You're permission item `filters` is lower than the permission you want to set"); } // --> Produce // "You're permission item `filters` is lower than the permission you want to set" // Case 3. The user in the context try to set a permission // equal to it's own permission // The userA (in the context) have the permission to CreateAndEdit `filters` // The userB have the permission to ReadOnly `filters` var changeResult3 = userB.Permissions.Change("filters", AccessLevel.CreateAndEdit, AskiaPortalCmnApi.CurrentModule.Guid); if (changeResult3.Success) { Console.WriteLine("Permission `filters` successfully changed to CreateAndEdit"); } else { Console.Error.WriteLine("You don't have enough right to change the userB"); } // --> Produce // "Permission `filters` successfully changed to CreateAndEdit" } } }