Exemple #1
0
        // Static Methods

        /// <summary>
        /// Loads the <see cref="UserRoleCache"/> for the current local user.
        /// </summary>
        /// <returns>Loaded instance of the <see cref="UserRoleCache"/>.</returns>
        public static UserRoleCache GetCurrentCache()
        {
            UserRoleCache currentCache;
            UserRoleCache localUserRoleCache;
            string        localCacheFileName = FilePath.GetAbsolutePath(DefaultCacheFileName);

            // Initialize local user role cache (application may only have read-only access to this cache)
            localUserRoleCache = new UserRoleCache
            {
                FileName       = localCacheFileName,
                ReloadOnChange = false,
                AutoSave       = false
            };

            // Load initial user roles
            localUserRoleCache.Load();

            try
            {
                // Validate that user has write access to the local cache folder
                string tempFile = FilePath.GetDirectoryName(localCacheFileName) + Guid.NewGuid() + ".tmp";

                using (File.Create(tempFile))
                {
                }

                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }

                // No access issues exist, use local cache as the primary cache
                currentCache          = localUserRoleCache;
                currentCache.AutoSave = true;
                localUserRoleCache    = null;
            }
            catch (UnauthorizedAccessException)
            {
                // User does not have needed serialization access to common cache folder,
                // use a path where user will have rights
                string userCacheFolder   = FilePath.AddPathSuffix(FilePath.GetApplicationDataFolder());
                string userCacheFileName = userCacheFolder + FilePath.GetFileName(localCacheFileName);

                // Make sure user directory exists
                if (!Directory.Exists(userCacheFolder))
                {
                    Directory.CreateDirectory(userCacheFolder);
                }

                // Copy existing common cache if none exists
                if (File.Exists(localCacheFileName) && !File.Exists(userCacheFileName))
                {
                    File.Copy(localCacheFileName, userCacheFileName);
                }

                // Initialize primary cache within user folder
                currentCache = new UserRoleCache
                {
                    FileName       = userCacheFileName,
                    ReloadOnChange = false,
                    AutoSave       = true
                };

                // Load initial roles
                currentCache.Load();

                // Merge new or updated roles, protected folder roles taking precedence over user folder roles
                currentCache.MergeRight(localUserRoleCache);
            }

            if ((object)localUserRoleCache != null)
            {
                localUserRoleCache.Dispose();
            }

            return(currentCache);
        }
Exemple #2
0
        // Static Methods

        /// <summary>
        /// Loads the <see cref="UserRoleCache"/> for the current local user.
        /// </summary>
        /// <returns>Loaded instance of the <see cref="UserRoleCache"/>.</returns>
        public static UserRoleCache GetCurrentCache()
        {
            UserRoleCache currentCache;
            UserRoleCache localUserRoleCache;
            string localCacheFileName = FilePath.GetAbsolutePath(DefaultCacheFileName);

            // Initialize local user role cache (application may only have read-only access to this cache)
            localUserRoleCache = new UserRoleCache
            {
                FileName = localCacheFileName,
                ReloadOnChange = false,
                AutoSave = false
            };

            // Load initial user roles
            localUserRoleCache.Load();

            try
            {
                // Validate that user has write access to the local cache folder
                string tempFile = FilePath.GetDirectoryName(localCacheFileName) + Guid.NewGuid() + ".tmp";

                using (File.Create(tempFile))
                {
                }

                if (File.Exists(tempFile))
                    File.Delete(tempFile);

                // No access issues exist, use local cache as the primary cache
                currentCache = localUserRoleCache;
                currentCache.AutoSave = true;
                localUserRoleCache = null;
            }
            catch (UnauthorizedAccessException)
            {
                // User does not have needed serialization access to common cache folder,
                // use a path where user will have rights
                string userCacheFolder = FilePath.AddPathSuffix(FilePath.GetApplicationDataFolder());
                string userCacheFileName = userCacheFolder + FilePath.GetFileName(localCacheFileName);

                // Make sure user directory exists
                if (!Directory.Exists(userCacheFolder))
                    Directory.CreateDirectory(userCacheFolder);

                // Copy existing common cache if none exists
                if (File.Exists(localCacheFileName) && !File.Exists(userCacheFileName))
                    File.Copy(localCacheFileName, userCacheFileName);

                // Initialize primary cache within user folder
                currentCache = new UserRoleCache
                {
                    FileName = userCacheFileName,
                    ReloadOnChange = false,
                    AutoSave = true
                };

                // Load initial roles
                currentCache.Load();

                // Merge new or updated roles, protected folder roles taking precedence over user folder roles
                currentCache.MergeRight(localUserRoleCache);
            }

            if ((object)localUserRoleCache != null)
                localUserRoleCache.Dispose();

            return currentCache;
        }