Esempio n. 1
0
        /// <summary>
        /// Gets the Permissiones.
        /// </summary>
        /// <returns></returns>
        public static PermissionDto GetPermissionDto(string roleName)
        {
            PermissionAdmin admin = new PermissionAdmin();

            admin.Load(new string[] { roleName });

            return(admin.CurrentDto);
        }
Esempio n. 2
0
        /// <summary>
        /// Checks the permission. Results are cached.
        /// </summary>
        /// <param name="permission">The permission.</param>
        /// <param name="cacheResults">if set to <c>true</c> [cache results].</param>
        /// <returns></returns>
        public bool CheckPermission(string permission, bool cacheResults)
        {
            if (!ProfileConfiguration.Instance.EnablePermissions)
            {
                return(true);
            }

            if (!Roles.Enabled)
            {
                return(true);
            }

            if (Roles.GetRolesForUser().Length == 0)
            {
                return(false);
            }

            // Always allow admin
            if (Roles.IsUserInRole(AppRoles.AdminRole))
            {
                return(true);
            }

            // Now check permissions for all current user roles
            string cacheKey = String.Empty;

            PermissionDto dto = null;

            if (cacheResults)
            {
                cacheKey = String.Format("ecf-pr-{0}", GetCurrentUserName());
                // check cache first
                object cachedObject = CacheHelper.Get(cacheKey);

                if (cachedObject != null)
                {
                    dto = (PermissionDto)cachedObject;
                }
            }

            // Get data from database and cache results if cache is enabled
            if (dto == null)
            {
                string[]        roles = GetRolesForUser();
                PermissionAdmin admin = new PermissionAdmin();
                admin.Load(roles);
                dto = admin.CurrentDto;

                if (cacheResults)
                {
                    CacheHelper.Insert(cacheKey, dto, new TimeSpan(0, 0, 30));
                }
            }

            // Now find if the permission we requested is availabe for current user
            // basically if receord for specified permission exists, then it is allowed, otherwise it is denied
            if (dto.RolePermission.Count == 0)
            {
                return(false);
            }

            DataRow[] rows = dto.RolePermission.Select(String.Format("permission like '{0}'", permission.Trim()));

            if (rows.Length > 0)
            {
                return(true);
            }

            return(false);
        }