Beispiel #1
0
        /// <summary>
        /// Gets the user requests for the given community and for the given page. User should have moderator
        /// or owner/site admin permission on the community to get user request.
        /// </summary>
        /// <param name="userId">User who is reading the requests</param>
        /// <param name="communityId">Community for which requests are fetched</param>
        /// <param name="pageDetails">Page for which requests are fetched</param>
        /// <returns>List of user role requests</returns>
        public async Task<PermissionDetails> GetUserPemissionRequests(long userId, long? communityId, PageDetails pageDetails)
        {
            this.CheckNotNull(() => new { pageDetails });

            // Condition to get all the pending requests irrespective of community.
            Expression<Func<PermissionRequest, bool>> condition = (PermissionRequest pr) => pr.Approved == null;
            Func<PermissionRequest, object> orderBy = (PermissionRequest c) => c.RoleID;

            if (communityId.HasValue)
            {
                // If community is specified, get all the pending requests of the specified community.
                condition = (PermissionRequest pr) => pr.Approved == null && pr.CommunityID == communityId.Value;
            }
            else
            {
                // If no community id is specified, get all the community ids to which user is given role of moderator or 
                // higher and get their pending requests.
                var userCommunityIds = _userRepository.GetUserCommunitiesForRole(userId, UserRole.Moderator, false);

                condition = (PermissionRequest pr) => pr.Approved == null && userCommunityIds.Contains(pr.CommunityID);
            }

            // Gets the total items satisfying the condition
            pageDetails.TotalCount =  _permissionRequestRepository.GetItemsCount(condition);
            pageDetails.TotalPages = (pageDetails.TotalCount / pageDetails.ItemsPerPage) + ((pageDetails.TotalCount % pageDetails.ItemsPerPage == 0) ? 0 : 1);

            var permissionDetails = new PermissionDetails();

            foreach (var item in  _permissionRequestRepository.GetItems(condition, orderBy, true, (pageDetails.CurrentPage - 1) * pageDetails.ItemsPerPage, pageDetails.ItemsPerPage))
            {
                var userRole = _userRepository.GetUserRole(userId, item.CommunityID);

                // 1. User has to be at least Moderator to know the permission request details of the community.
                // 2. In case of profile page, user might be moderator for few communities and not for others. So, need to send only the requests
                //    of community to which user is moderator or higher.
                if (userRole >= UserRole.Moderator)
                {
                    var permissionItem = new PermissionItem();
                    Mapper.Map(item, permissionItem);
                    permissionItem.CurrentUserRole = userRole;
                    permissionDetails.PermissionItemList.Add(permissionItem);
                    permissionDetails.CurrentUserPermission = userRole.GetPermission();
                }
                else if (communityId.HasValue)
                {
                    // If user is not having contributor or higher role, he will get item not found or don't have permission exception page.
                    // This message to be shown only in case of permissions page not for profile page.
                    permissionDetails = null;
                }
            }

            return permissionDetails;
        }
Beispiel #2
0
        /// <summary>
        /// Gets the user permissions for the given community and for the given page. User should have at least
        /// contributor permission on the community to get user permissions.
        /// </summary>
        /// <param name="userId">User who is reading the permissions</param>
        /// <param name="communityId">Community for which permissions are fetched</param>
        /// <param name="pageDetails">Page for which permissions are fetched</param>
        /// <returns>List of permissions/user roles</returns>
        
        public async Task<PermissionDetails> GetUserPemissions(long userId, long communityId, PageDetails pageDetails)
        {
            this.CheckNotNull(() => new { pageDetails });

            Expression<Func<UserCommunities, bool>> condition = c => c.CommunityId == communityId;
            Func<UserCommunities, object> orderBy = c => c.RoleID;

            // Gets the total items satisfying the condition
            pageDetails.TotalCount =  _userCommunitiesRepository.GetItemsCount(condition);
            pageDetails.TotalPages = (pageDetails.TotalCount / pageDetails.ItemsPerPage) + ((pageDetails.TotalCount % pageDetails.ItemsPerPage == 0) ? 0 : 1);

            // TODO: Passing the condition in a variable doesn't add the WHERE clause in SQL server. Need to work on this later.
            
            var items =  _userCommunitiesRepository.GetItems(condition, orderBy, true, (pageDetails.CurrentPage - 1) * pageDetails.ItemsPerPage, pageDetails.ItemsPerPage);

            var permissionDetails = new PermissionDetails();

            if (items != null && items.Any())
            {
                var userRole = _userRepository.GetUserRole(userId, communityId);

                // User has to be at least contributor to know the permission details of the community.
                if (userRole >= UserRole.Contributor)
                {
                    permissionDetails.CurrentUserPermission = userRole.GetPermission();

                    foreach (var item in items)
                    {
                        var permissionItem = new PermissionItem();
                        Mapper.Map(item, permissionItem);
                        permissionItem.CurrentUserRole = userRole;
                        permissionDetails.PermissionItemList.Add(permissionItem);
                    }
                }
                else
                {
                    // If user is not having contributor or higher role, he will get item not found or don't have permission exception page.
                    permissionDetails = null;
                }
            }

            return permissionDetails;
        }