PermissionsChange Method (AccessTo, AccessLevel) |
Namespace: AskiaPortalCmn.Common
The user A (in the context) have the following permissions:
- users : Create and edit
- groups : Create and edit
The user B (the user to edit) have the following permissions:
- users : Full control
- groups: Read only
using system; using AskiaPortalCmn; namespace MyApp { class Program { static void Main(string[] args) { // 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 `users` // The userB have the FullControl permission on `users` var changeResult1 = userB.Permissions.Change(AccessTo.Users, AccessLevel.ReadOnly); if (changeResult1.Success) { Console.WriteLine("Permission `users` successfully changed to read-only"); } else { Console.Error.WriteLine("You're permission item `users` is lower than the userB permission"); } // --> Produce // "You're permission item `users` 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 `groups` // The userB have the permission to ReadOnly `groups` var changeResult2 = userB.Permissions.Change(AccessTo.Groups, AccessLevel.FullControl); if (changeResult2.Success) { Console.WriteLine("Permission `groups` successfully changed to FullControl"); } else { Console.Error.WriteLine("You're permission item `groups` is lower than the permission you want to set"); } // --> Produce // "You're permission item `groups` 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 `groups` // The userB have the permission to ReadOnly `groups` var changeResult3 = userB.Permissions.Change(AccessTo.Groups, AccessLevel.CreateAndEdit); if (changeResult3.Success) { Console.WriteLine("Permission `groups` successfully changed to CreateAndEdit"); } else { Console.Error.WriteLine("You don't have enough right to change the userB"); } // --> Produce // "Permission `groups` successfully changed to CreateAndEdit" } } }