예제 #1
0
        internal Access GetAccess(AccessRight right, string role)
        {
            AccessPermission permission =
                //AccessPermissions.SingleOrDefault(x => x.AccessRight == right.Value && x.Role == role) ??
                AccessPermissions
                .Where(x => x.AccessRight >= right.Value && x.Access != Access.Inherit && x.Role == role)
                .OrderBy(x => x.AccessRight)
                .FirstOrDefault();

            return(permission == null ? Access.Inherit : permission.Access);
        }
예제 #2
0
        /// <summary>
        ///   Checks if principal Users has access to some action (AccessRight).
        /// </summary>
        /// <param name="secured"> Secured entity. </param>
        /// <param name="right"> AccessRight action </param>
        /// <param name="principal"> Current principal. </param>
        /// <returns> Value indicates if action is approved for selected role. </returns>
        //public static bool IsAllowed(this ISecured secured, AccessRight right, IPrincipal principal, IUsersInRolesRepository uic)
        //{
        //    var user = principal.Identity.Name;
        //    var roles = uic.GetAll();
        //    var v = roles.Any(role => secured.IsAllowed(right, user));
        //    return v;
        //}

        private static Access GetAccess(ISecured secured, AccessRight right, string role)
        {
            var access = secured.Permissions.GetAccess(right, role);

            if (access != Access.Inherit)
            {
                return(access);
            }

            var securedChild = secured as ISecuredChild;

            return(securedChild == null || securedChild.Parent == null
                       ? Access.Inherit
                       : GetAccess(securedChild.Parent, right, role));
        }
예제 #3
0
        /// <summary>
        ///   Checks if role has access to some action (AccessRight).
        /// </summary>
        /// <param name="secured"> Secured entity. </param>
        /// <param name="right"> AccessRight action </param>
        /// <param name="role"> Role to check against. </param>
        /// <returns> Value indicates if action is approved for selected role. </returns>
        public static bool IsAllowed(this ISecured secured, AccessRight right, string role)
        {
            if (CmsContext.UnrestrictedRoles.Any(x => x == role))
            {
                return(true);
            }

            var access = GetAccess(secured, right, role);

            if (access == Access.Inherit)
            {
                access = GetApplicationAccess(secured, right, role);
            }

            return(access == Access.Allow);
        }
예제 #4
0
        public IList <PermissionInput> GetPermissions(string role, long securedId)
        {
            var permissions = securedId == 0
                                  ? _cmsContext.GetPermissionsFor <TApplication>()
                                  : _repository.GetById(securedId).Permissions;

            var rights = AccessRight.GetAllAccessRights <TAccessRight>();

            return(rights
                   .Select(x => new PermissionInput
            {
                AccessRightName = x.DisplayName,
                AccessRightValue = x.Value,
                AccessValue = GetAccess(permissions, x, role).Value
            })
                   .ToList());
        }
예제 #5
0
        public void SetPermissions(string role, IEnumerable <PermissionInput> permissionList, long securedId)
        {
            var entity = securedId == null ? null : _repository.GetById(securedId);

            var permissions = entity == null
                                  ? _cmsContext.GetPermissionsFor <TApplication>()
                                  : entity.Permissions;

            permissions.InheritFor(role);

            foreach (var permissionInput in permissionList)
            {
                var access = Enumeration.FromValue <Access>(permissionInput.AccessValue);
                if (access == Access.Inherit)
                {
                    continue;
                }

                var accessRight = AccessRight.FromAccessRightValue <TAccessRight>(permissionInput.AccessRightValue);

                if (access == Access.Allow)
                {
                    permissions.Allow(accessRight).To(role);
                }
                else
                {
                    permissions.Deny(accessRight).To(role);
                }
            }

            if (entity == null)
            {
                _cmsContext.SetPermissionsFor <TApplication>(permissions);
            }
            else
            {
                entity.Permissions = permissions;
                _repository.Save(entity);
            }
        }
예제 #6
0
 internal AccessPermissionBuilder(IList <AccessPermission> permissions, AccessRight right, Access access)
 {
     _permissions = permissions;
     _right       = right;
     _access      = access;
 }
예제 #7
0
        private static Access GetApplicationAccess(ISecured secured, AccessRight right, string role)
        {
            var application = CmsContext.Current.GetApplication(secured.GetType()) as ISecured;

            return(application == null ? Access.Deny : application.Permissions.GetAccess(right, role));
        }
예제 #8
0
 public bool IsDefined(AccessRight right, string role)
 {
     return(AccessPermissions.Any(x => x.AccessRight == right.Value && x.Role == role));
     //return (AccessPermissions.Count(x => x.AccessRight == right.Value && x.Role == role) == 0);
 }
예제 #9
0
 public AccessPermissionBuilder Deny(AccessRight right)
 {
     return(new AccessPermissionBuilder(AccessPermissions, right, Access.Deny));
 }
예제 #10
0
 public AccessPermissionBuilder Allow(AccessRight right)
 {
     return(new AccessPermissionBuilder(AccessPermissions, right, Access.Allow));
 }