コード例 #1
0
        /// <summary>
        /// Returns whether the current user passes authorization on the rights based on the given AuthorizationCheck.
        /// </summary>
        /// <param name="authCheck"></param>
        /// <param name="rights"></param>
        /// <returns></returns>
        public static bool IsAuthorizedTo(AuthorizationCheck authCheck, IEnumerable <Rights> rights)
        {
            if (rights.Count() == 0)
            {
                // Always return false for this. If there's a mistake where authorization
                // is being checked for on an empty collection, we don't want to return
                // true.
                return(false);
            }
            else
            {
                var roles = Security.GetCurrentUserRoles();

                if (authCheck == AuthorizationCheck.HasAny)
                {
                    foreach (var right in rights)
                    {
                        if (Right.HasRight(right, roles))
                        {
                            return(true);
                        }
                    }

                    return(false);
                }
                else if (authCheck == AuthorizationCheck.HasAll)
                {
                    bool authCheckPassed = true;

                    foreach (var right in rights)
                    {
                        if (!Right.HasRight(right, roles))
                        {
                            authCheckPassed = false;
                            break;
                        }
                    }
                    return(authCheckPassed);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Returns whether or not the current user has the passed in Right.
 /// </summary>
 /// <param name="right"></param>
 /// <returns></returns>
 public static bool IsAuthorizedTo(Rights right)
 {
     return(Right.HasRight(right, Security.GetCurrentUserRoles()));
 }