コード例 #1
0
        public static MembershipHelperUser GetUser(string userName)
        {
            string cachedKey = string.Format("UserByName_{0}", userName);
            object result    = CacheManager.GetCached(cachedKey);

            if (result == null)
            {
                MembershipUser mu = Membership.GetUser(userName);
                if (mu != null)
                {
                    result = new MembershipHelperUser(mu);
                    CacheManager.AddItem(cachedKey, result);
                }
                else
                {
                    result = null;
                }
            }
            return(result as MembershipHelperUser);
        }
コード例 #2
0
        /// <summary>
        /// Validates the current object has permission to be executed.
        /// If there is no permission setup for this object and user, return false to enforce security.
        /// </summary>
        /// <param name="o">current object</param>
        /// <param name="action">action to be validated</param>
        /// <returns>
        /// True if it has the corresponding permission.
        /// False if no valid permissions are set for this object.
        /// </returns>
        public static bool Check(object o, Enum action)
        {
            MembershipHelperUser mu = MembershipHelper.GetUser();

            if (mu == null)
            {
                return(false);
            }

            // Check if there are permissions for this object cached for this user.
            string checkAction = ((action != null) ? action.ToString() : ALLPERMISSIONS);

            string path = string.Empty;

            if ((typeof(HtmlControl).IsInstanceOfType(o) || typeof(WebControl).IsInstanceOfType(o)) && (o as Control) != null)
            {
                if ((o as Control).Page != null)
                {
                    path = (o as Control).Page.AppRelativeVirtualPath;
                }
                else
                {
                    path = (o as Control).AppRelativeTemplateSourceDirectory;
                }
            }

            if (logger.IsDebugEnabled)
            {
                logger.DebugFormat("Checking cache permission for: User:{0} Action:{1} Object:{2} Type:{3} Path:{4}", mu.UserId, checkAction, o, o.GetType().ToString(), path);
            }
            string cachedKey = string.Format("PERM_{0}_{1}_{2}_{3}_{4}", mu.UserId, o, checkAction, o.GetType().ToString(), path);

            object result = CacheManager.GetCached(cachedKey);

            if (result == null)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("Cache not found.");
                }

                // Obtain the list of permissions of the logged user
                List <Permission> lst = (ControllerManager.Permission.ListForCurrentUserAndSite(mu.UserId, action, Configuration.SiteCode) as List <Permission>);
                lst.AddRange(ControllerManager.Permission.ListPermisionsByUser(mu.UserId, Configuration.SiteCode));

                if (logger.IsDebugEnabled)
                {
                    logger.Debug("Reviewing list of possible permissions.");
                    foreach (Permission p in lst)
                    {
                        logger.DebugFormat("Permission found: {0}", p.ToString());
                    }
                    logger.Debug("End reviewing list of possible permissions.");
                }
                // Review which permission can be validated on the current object and validate each of them
                bool isChecked = false;
                foreach (Permission p in lst)
                {
                    if (p.CanCheck(o) && p.Check(o, action))
                    {
                        isChecked = true;
                    }
                }

                CacheManager.AddItem(cachedKey, isChecked);

                if (logger.IsDebugEnabled)
                {
                    logger.DebugFormat("Result: {0}", isChecked);
                }

                return(isChecked);
            }
            else
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("Cache found.");
                    logger.DebugFormat("Result: {0}", (bool)result);
                }

                return((bool)result);
            }
        }