Advertisement
tonyschoborg

Permissions (C# Shell App Paste)

Apr 11th, 2021
2,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. //Disclaimer: The creator of 'C# Shell (C# Offline Compiler)' is in no way responsible for the code posted by any user.
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6.  
  7. namespace CSharp_Shell
  8. {
  9.  
  10.     public class Program
  11.     {
  12.         public static void Main()
  13.         {
  14.            
  15.         }
  16.        
  17.         public class BasePermissions
  18.         {
  19.             public int Permission {get;set;}
  20.      
  21.         }
  22.        
  23.         public class DivisionsPermissions : BasePermissions
  24.         {
  25.             public enum Permissions
  26.             {
  27.                 Create = 1, Read = 2, Update = 4, Delete = 8, Custom = 16
  28.             }
  29.            
  30.             public Dictionary<Permissions, bool> GetPermissions()
  31.             {
  32.                return new Dictionary<Permissions, bool>
  33.                {
  34.                 {Permissions.Create, (Permission & (int)Permissions.Create)==(int)Permissions.Create},
  35.                 {Permissions.Read, (Permission & (int)Permissions.Read)==(int)Permissions.Read},
  36.                 {Permissions.Update, (Permission & (int)Permissions.Update)==(int)Permissions.Update},
  37.                 {Permissions.Delete, (Permission & (int)Permissions.Delete)==(int)Permissions.Delete},
  38.                 {Permissions.Custom, (Permission & (int)Permissions.Custom)==(int)Permissions.Custom}
  39.                };
  40.             }
  41.            
  42.             public bool GetPermission(Permissions permission)
  43.             {
  44.                 return GetPermissions()[permission];
  45.             }
  46.             public void SetPermission(Permissions permission, bool value)
  47.         {
  48.  
  49.             //bitwise NOT ~
  50.             // NOT permission if value is false
  51.             // then and with Permission property
  52.            
  53.             //if value true OR with property
  54.             //if value false NOT permission the OR with Permission Property
  55.            
  56.             var perm = (int)permission;
  57.             if (!value)
  58.                 perm = ~perm;
  59.                
  60.              Permission |= (int)perm;
  61.         }
  62.         }
  63.        
  64.        
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement