コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public List <UserGroupPermissionsPoco> GetRecursivePermissionsForNode(IPublishedContent node)
        {
            if (node == null)
            {
                return(null);
            }

            // skip 1 to ignore root (-1)
            string[] path = node.Path.Split(',').Skip(1).ToArray();
            // get all permissions matching either this node, its type, or an ancestor id
            List <UserGroupPermissionsPoco> allPermissions = _repo.AllPermissionsForNode(path, node.ContentType.Id);

            List <UserGroupPermissionsPoco> forNode = allPermissions.Where(p => p.NodeId == node.Id).ToList();

            if (forNode.Any())
            {
                return(forNode);
            }

            List <UserGroupPermissionsPoco> forType =
                allPermissions.Where(p => p.ContentTypeId == node.ContentType.Id).ToList();

            if (forType.Any())
            {
                return(forType);
            }

            // if we're here, reverse the path and check each node in turn
            IEnumerable <int> reversedPath = path.Reverse().Select(int.Parse);

            foreach (int ancestorId in reversedPath)
            {
                List <UserGroupPermissionsPoco> forAncestor = allPermissions.Where(x => x.NodeId == ancestorId).ToList();
                if (forAncestor.Any())
                {
                    return(forAncestor);
                }
            }

            return(new List <UserGroupPermissionsPoco>());
        }