Ejemplo n.º 1
0
        /// <summary>
        /// Creates the community details for the given community. User id is used to check the user role
        /// on the community,based on that, CommunityDetails might be null in case is user is not having permission.
        /// </summary>
        /// <param name="community">Community for which community details has to be created</param>
        /// <param name="userId">Current user id</param>
        /// <param name="checkPendingRequest">Check for pending requests of the user</param>
        /// <returns>Community details instance</returns>
        private CommunityDetails CreateCommunityDetails(Community community, long? userId, bool checkPendingRequest)
        {
            CommunityDetails communityDetails = null;

            if (community != null)
            {
                Permission permission;
                var userRole = GetCommunityUserRole(community.CommunityID, userId);

                if (!CanReadCommunity(userRole))
                {
                    throw new HttpException(401, Resources.NoPermissionReadCommunityMessage);
                }

                permission = userRole.GetPermission();

                // 1. For visitors (not assigned any roles for the community) who are logged in, need to find if any role request is pending approval.
                // 2. Pending approval is needed only for community details, not to be added in other places.
                if (checkPendingRequest &&
                        userRole == UserRole.Visitor &&
                        userId.HasValue &&
                        _userRepository.PendingPermissionRequests(userId.Value, community.CommunityID))
                {
                    permission = Permission.PendingApproval;
                }

                communityDetails = new CommunityDetails(permission);

                // Some of the values which comes from complex objects need to be set through this method.
                communityDetails.SetValuesFrom(community);

                communityDetails.ViewCount = community.ViewCount.HasValue ? community.ViewCount.Value : 0;

                // Update parent details based on the permission.
                var parent = Enumerable.FirstOrDefault(community.CommunityRelation1);
                if (parent != null && parent.Community != null)
                {
                    var parentUserRole = GetCommunityUserRole(parent.Community.CommunityID, userId);

                    if (!CanReadCommunity(parentUserRole))
                    {
                        communityDetails.ParentName = string.Empty;
                        communityDetails.ParentID = -1;
                        communityDetails.ParentType = CommunityTypes.None;
                    }
                }
            }

            return communityDetails;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieves the sub communities of a given community. This only retrieves the immediate children.
        /// </summary>
        /// <param name="communityId">ID of the community.</param>
        /// <param name="userId">Id of the user who is accessing</param>
        /// <param name="pageDetails">Details about the pagination</param>
        /// <param name="onlyItemCount">To get only item count, not the entities. When community details page is loaded first time, 
        /// no need to get the communities, only count is enough.</param>
        /// <returns>Collection of sub communities</returns>
        public IEnumerable<CommunityDetails> GetSubCommunities(long communityId, long userId, PageDetails pageDetails, bool onlyItemCount)
        {
            this.CheckNotNull(() => new { pageDetails });

            IList<CommunityDetails> subCommunities = new List<CommunityDetails>();

            var subCommunityIDs = _communityRepository.GetSubCommunityIDs(communityId, userId);

            // Gets the total number of direct sub communities of the community
            pageDetails.TotalCount = subCommunityIDs.Count();

            pageDetails.TotalPages = (pageDetails.TotalCount / pageDetails.ItemsPerPage) + ((pageDetails.TotalCount % pageDetails.ItemsPerPage == 0) ? 0 : 1);

            // When community details page is loaded first time, no need to get the communities, only count is enough.
            if (!onlyItemCount && subCommunityIDs != null && subCommunityIDs.Count() > 0)
            {
                subCommunityIDs = subCommunityIDs.Skip((pageDetails.CurrentPage - 1) * pageDetails.ItemsPerPage).Take(pageDetails.ItemsPerPage);
                foreach (var community in _communityRepository.GetItems(subCommunityIDs))
                {
                    var userRole = _userRepository.GetUserRole(userId, community.CommunityID);
                    var communityDetails = new CommunityDetails(userRole.GetPermission());

                    // Some of the values which comes from complex objects need to be set through this method.
                    communityDetails.SetValuesFrom(community);

                    subCommunities.Add(communityDetails);
                }
            }

            return subCommunities;
        }