Click or drag to resize

PermissionsChange Method (String, AccessLevel, NullableGuid)

Change the value of the specified 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
public IReturnValue Change(
	string key,
	AccessLevel level,
	Nullable<Guid> moduleGuid = null
)

Parameters

key
Type: SystemString
Indicates the permission to modify
level
Type: AskiaPortalCmnAccessLevel
Indicates the value to set
moduleGuid (Optional)
Type: SystemNullableGuid
Indicates the GUID of the module that contains the permission
It uses the current module CurrentModule if the value is null

Return Value

Type: IReturnValue
It return unsuccess value, if the user in the context doesn't have enough permission to do this action

Implements

IPermissionsChange(String, AccessLevel, NullableGuid)
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.
Examples

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