Click or drag to resize

IPermissionsChange Method (AccessTo, AccessLevel)

Change the value of the specified `AskiaPortal` permission.
It will not save the change in the database unless you explicitly call the Save method.

Namespace:  AskiaPortalCmn.Common
Assembly:  AskiaPortalCmn (in AskiaPortalCmn.dll) Version: 1.7.0-build068
Syntax
IReturnValue Change(
	AccessTo item,
	AccessLevel level
)

Parameters

item
Type: AskiaPortalCmnAccessTo
Indicates the `AskiaPortal` permission to modify
level
Type: AskiaPortalCmnAccessLevel
Indicates the value to set

Return Value

Type: IReturnValue
It return unsuccess value, if the user in the context doesn't have enough permission to do this action
Remarks
Administrator allowed to modify the permission of a user, could not change permission higher than his own permission.
It will not save the change in the database unless you explicitly call the Save method.
Remarks
Examples

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"
         }
    }
}
See Also