Esempio n. 1
0
        /// <summary>
        /// Merge user roles from another <see cref="UserRoleCache"/>, other cache taking precedence.
        /// </summary>
        /// <param name="other">Other <see cref="UserRoleCache"/> to merge with.</param>
        public void MergeRight(UserRoleCache other)
        {
            // Merge other roles into local ones
            Dictionary <string, string[]> mergedUserRoles = other.UserRoles.Merge(UserRoles);

            // Wait for thread level lock on dictionary
            lock (m_userRolesLock)
            {
                // Replace local user roles dictionary with merged roles
                m_userRoles = mergedUserRoles;
            }

            // Queue up a serialization for any newly added roles
            Save();
        }
Esempio n. 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,
#if DNF45 && !MONO
                ReloadOnChange = true,
#else
                // Reload on change is disabled to eliminate GC handle leaks on .NET 4.0, this prevents
                // automatic runtime reloading of key/iv data cached by another application.
                ReloadOnChange = false,
#endif
                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;
            }
            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,
#if DNF45 && !MONO
                    ReloadOnChange = true,
#else
                    // Reload on change is disabled to eliminate GC handle leaks on .NET 4.0, this prevents
                    // automatic runtime reloading of key/iv data cached by another application.
                    ReloadOnChange = false,
#endif
                    AutoSave = true
                };

                // Load initial roles
                currentCache.Load();

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

            return(currentCache);
        }
Esempio n. 3
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;
        }
Esempio n. 4
0
        /// <summary>
        /// Merge user roles from another <see cref="UserRoleCache"/>, other cache taking precedence.
        /// </summary>
        /// <param name="other">Other <see cref="UserRoleCache"/> to merge with.</param>
        public void MergeRight(UserRoleCache other)
        {
            // Merge other roles into local ones
            Dictionary<string, string[]> mergedUserRoles = other.UserRoles.Merge(UserRoles);

            // Wait for thread level lock on dictionary
            lock (m_userRolesLock)
            {
                // Replace local user roles dictionary with merged roles
                m_userRoles = mergedUserRoles;
            }

            // Queue up a serialization for any newly added roles
            Save();
        }