예제 #1
0
        private PermissionManager(string store, string connection)
        {
            cache = new StorageCache(connection);

            try
            {
                cache.BuildStorageCache();
                //cache.BuildStorageCache(store);
            }
            catch (Exception exception)
            {
                throw new ApplicationException("Unable to initialize NetSqlAzMan security store", exception);
            }
        }
예제 #2
0
 internal static void startStorageBuildCache(string storeName, string applicationName)
 {
     //Design Feature 1: If Not already building cache ... ignore new InvalidateCache
     lock (CacheService.locker)
     {
         if (!CacheService.buildingCache)
         {
             CacheService.buildingCache = true;
             WindowsCacheService.writeEvent(String.Format("Invalidate Cache invoked from user '{0}'. Store: '{1}' - Application: '{2}'.", ((System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent()).Name, storeName ?? String.Empty, applicationName ?? String.Empty), System.Diagnostics.EventLogEntryType.Information);
             Boolean AsyncCacheBuilding;
             if (!Boolean.TryParse(ConfigurationManager.AppSettings["AsyncCacheBuilding"], out AsyncCacheBuilding))
                 AsyncCacheBuilding = true;
             //Design Feature 2: Async Cache Building
             if (AsyncCacheBuilding)
             {
                 System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(o =>
                     {
                         try
                         {
                             StorageCache sc = new StorageCache(Properties.Settings.Default.NetSqlAzManStorageCacheConnectionString);
                             sc.BuildStorageCache(storeName, applicationName);
                             if (CacheService.storageCache != null)
                             {
                                 //Design Feature 3: When Build ... replace the old cache with new one
                                 //3.1) This means that while building ... User can invoke CheckAccess on the OLD cache
                                 //3.2) Replacement is thread safe
                                 lock (CacheService.storageCache)
                                 {
                                     CacheService.storageCache = sc;
                                 }
                             }
                             else
                             {
                                 CacheService.storageCache = sc;
                             }
                             WindowsCacheService.writeEvent("Cache Built (Async).", System.Diagnostics.EventLogEntryType.Information);
                         }
                         catch (Exception ex)
                         {
                             WindowsCacheService.writeEvent(String.Format("Cache building error (Async):\r\n{0}\r\n\r\nStack Track:\r\n{1}", ex.Message, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error);
                         }
                         finally
                         {
                             lock (CacheService.locker)
                             {
                                 CacheService.buildingCache = false;
                             }
                         }
                     }
                         ));
             }
             else
             {
                 //Sync Cache Build
                 try
                 {
                     StorageCache sc = new StorageCache(Properties.Settings.Default.NetSqlAzManStorageCacheConnectionString);
                     sc.BuildStorageCache(storeName, applicationName);
                     if (CacheService.storageCache != null)
                     {
                         //Design Feature 3: When Build ... replace the old cache with new one
                         //3.1) This means that while building ... User can invoke CheckAccess on the OLD cache
                         //3.2) Replacement is thread safe
                         lock (CacheService.storageCache)
                         {
                             CacheService.storageCache = sc;
                         }
                     }
                     else
                     {
                         CacheService.storageCache = sc;
                     }
                     WindowsCacheService.writeEvent("Cache Built (Sync).", System.Diagnostics.EventLogEntryType.Information);
                 }
                 catch (Exception ex)
                 {
                     WindowsCacheService.writeEvent(String.Format("Cache building error (Sync):\r\n{0}\r\n\r\nStack Track:\r\n{1}", ex.Message, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error);
                 }
                 finally
                 {
                     lock (CacheService.locker)
                     {
                         CacheService.buildingCache = false;
                     }
                 }
             }
         }
         else
         {
             WindowsCacheService.writeEvent("Invalidate Cache invoked while building cache. Command ignored.", System.Diagnostics.EventLogEntryType.Warning);
         }
     }
 }
예제 #3
0
        private void CheckNodeAccess(TreeNode tn)
        {
            var    tag       = (ItemType)tn.Tag;
            string sItemType = String.Empty;

            switch (tag)
            {
            case ItemType.Role:
                sItemType = "Role";
                break;

            case ItemType.Task:
                sItemType = "Task";
                break;

            case ItemType.Operation:
                sItemType = "Operation";
                break;
            }

            AuthorizationType auth        = AuthorizationType.Neutral;
            string            sAuth       = String.Empty;
            DateTime          chkStart    = DateTime.Now;
            TimeSpan          elapsedTime = TimeSpan.Zero;
            DateTime          chkEnd      = DateTime.Now;
            List <KeyValuePair <string, string> > attributes = null;

            //Cache Build
            if (chkUserPermisisonCache.Checked && _UserPermissionCache == null)
            {
                txtDetails.Text     += "Building UserPermissionCache ... " + Environment.NewLine;
                _UserPermissionCache = new UserPermissionCache(_Storage, _Store.Name, _Application.Name, _DbUser, true, false);
                chkEnd           = DateTime.Now;
                elapsedTime      = (TimeSpan)chkEnd.Subtract(chkStart);
                txtDetails.Text += String.Format("[{0} mls.]\r\n", elapsedTime.TotalMilliseconds) + Environment.NewLine;
            }
            else if (chkStorageCache.Checked && _StorageCache == null)
            {
                txtDetails.Text += "Building StorageCache ... " + Environment.NewLine;
                _StorageCache    = new StorageCache(_Storage.ConnectionString);
                _StorageCache.BuildStorageCache(_Store.Name, _Application.Name);
                chkEnd           = DateTime.Now;
                elapsedTime      = (TimeSpan)chkEnd.Subtract(chkStart);
                txtDetails.Text += String.Format("[{0} mls.]\r\n", elapsedTime.TotalMilliseconds) + Environment.NewLine;
            }
            chkStart         = DateTime.Now;
            elapsedTime      = TimeSpan.Zero;
            txtDetails.Text += String.Format("{0} {1} '{2}' ... ", "Check Access Test on", sItemType, tn.Text);

            try
            {
                if (chkUserPermisisonCache.Checked)
                {
                    auth = _UserPermissionCache.CheckAccess(
                        tn.Text,
                        !String.IsNullOrEmpty(txtValidFor.Text) ? Convert.ToDateTime(txtValidFor.Text) : DateTime.Now,
                        out attributes);
                }
                else if (this.chkStorageCache.Checked)
                {
                    auth = _StorageCache.CheckAccess(
                        _Store.Name,
                        _Application.Name,
                        tn.Text, _DbUser.CustomSid.StringValue,
                        !String.IsNullOrEmpty(txtValidFor.Text) ? Convert.ToDateTime(txtValidFor.Text) : DateTime.Now,
                        false,
                        out attributes);
                }
                else
                {
                    auth = _Storage.CheckAccess(
                        _Store.Name,
                        _Application.Name,
                        tn.Text, _DbUser,
                        !String.IsNullOrEmpty(txtValidFor.Text) ? Convert.ToDateTime(txtValidFor.Text) : DateTime.Now,
                        false,
                        out attributes);
                }

                chkEnd      = DateTime.Now;
                elapsedTime = (TimeSpan)chkEnd.Subtract(chkStart);
                sAuth       = "Neutral";
                switch (auth)
                {
                case AuthorizationType.AllowWithDelegation:
                    sAuth = "Allow with Delegation";
                    break;

                case AuthorizationType.Allow:
                    sAuth = "Allow";
                    break;

                case AuthorizationType.Deny:
                    sAuth = "Deny";
                    break;

                case AuthorizationType.Neutral:
                    sAuth = "Neutral";
                    break;
                }
                //tn.ToolTip = sAuth;
                txtDetails.Text += String.Format("{0} [{1} mls.]", sAuth, elapsedTime.TotalMilliseconds) + Environment.NewLine;
                if (attributes != null && attributes.Count > 0)
                {
                    txtDetails.Text += String.Format(" {0} attribute(s) found:", attributes.Count) + Environment.NewLine;
                    int attributeIndex = 0;
                    foreach (KeyValuePair <string, string> attr in attributes)
                    {
                        txtDetails.Text += String.Format("  {0}) Key: {1} Value: {2}", ++attributeIndex, attr.Key, attr.Value) + Environment.NewLine;
                    }
                }
            }
            catch (Exception ex)
            {
                sAuth            = "Check Access Test Error";
                txtDetails.Text += String.Format("{0} [{1} mls.]", ex.Message, elapsedTime.TotalMilliseconds) + Environment.NewLine;
            }
            tn.Text = String.Format("{0} - ({1})", tn.Text, sAuth.ToUpper());
            foreach (TreeNode tnChild in tn.Nodes)
            {
                CheckNodeAccess(tnChild);
            }
        }
예제 #4
0
 internal static void startStorageBuildCache(string storeName, string applicationName)
 {
     //Design Feature 1: If Not already building cache ... ignore new InvalidateCache
     lock (CacheService.locker)
     {
         if (!CacheService.buildingCache)
         {
             CacheService.buildingCache = true;
             WindowsCacheService.writeEvent(String.Format("Invalidate Cache invoked from user '{0}'. Store: '{1}' - Application: '{2}'.", ((System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent()).Name, storeName ?? String.Empty, applicationName ?? String.Empty), System.Diagnostics.EventLogEntryType.Information);
             Boolean AsyncCacheBuilding;
             if (!Boolean.TryParse(ConfigurationManager.AppSettings["AsyncCacheBuilding"], out AsyncCacheBuilding))
             {
                 AsyncCacheBuilding = true;
             }
             //Design Feature 2: Async Cache Building
             if (AsyncCacheBuilding)
             {
                 System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(o =>
                 {
                     try
                     {
                         StorageCache sc = new StorageCache(Properties.Settings.Default.NetSqlAzManStorageCacheConnectionString);
                         sc.BuildStorageCache(storeName, applicationName);
                         if (CacheService.storageCache != null)
                         {
                             //Design Feature 3: When Build ... replace the old cache with new one
                             //3.1) This means that while building ... User can invoke CheckAccess on the OLD cache
                             //3.2) Replacement is thread safe
                             lock (CacheService.storageCache)
                             {
                                 CacheService.storageCache = sc;
                             }
                         }
                         else
                         {
                             CacheService.storageCache = sc;
                         }
                         WindowsCacheService.writeEvent("Cache Built (Async).", System.Diagnostics.EventLogEntryType.Information);
                     }
                     catch (Exception ex)
                     {
                         WindowsCacheService.writeEvent(String.Format("Cache building error (Async):\r\n{0}\r\n\r\nStack Track:\r\n{1}", ex.Message, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error);
                     }
                     finally
                     {
                         lock (CacheService.locker)
                         {
                             CacheService.buildingCache = false;
                         }
                     }
                 }
                                                                                                 ));
             }
             else
             {
                 //Sync Cache Build
                 try
                 {
                     StorageCache sc = new StorageCache(Properties.Settings.Default.NetSqlAzManStorageCacheConnectionString);
                     sc.BuildStorageCache(storeName, applicationName);
                     if (CacheService.storageCache != null)
                     {
                         //Design Feature 3: When Build ... replace the old cache with new one
                         //3.1) This means that while building ... User can invoke CheckAccess on the OLD cache
                         //3.2) Replacement is thread safe
                         lock (CacheService.storageCache)
                         {
                             CacheService.storageCache = sc;
                         }
                     }
                     else
                     {
                         CacheService.storageCache = sc;
                     }
                     WindowsCacheService.writeEvent("Cache Built (Sync).", System.Diagnostics.EventLogEntryType.Information);
                 }
                 catch (Exception ex)
                 {
                     WindowsCacheService.writeEvent(String.Format("Cache building error (Sync):\r\n{0}\r\n\r\nStack Track:\r\n{1}", ex.Message, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error);
                 }
                 finally
                 {
                     lock (CacheService.locker)
                     {
                         CacheService.buildingCache = false;
                     }
                 }
             }
         }
         else
         {
             WindowsCacheService.writeEvent("Invalidate Cache invoked while building cache. Command ignored.", System.Diagnostics.EventLogEntryType.Warning);
         }
     }
 }