Ejemplo n.º 1
0
        /// <summary>
        /// Gets the role memberships.
        /// </summary>
        /// <param name="accountId">The user identifier.</param>
        /// <param name="ecoSpaceId">The eco space identifier.</param>
        /// <returns>MembershipContainer.</returns>
        public MembershipContainer GetRoleMemberships(string accountId, string ecoSpaceId = null)
        {
            if (String.IsNullOrWhiteSpace(ecoSpaceId))
            {
                ecoSpaceId = MasterEcoSpace.ID;
            }
            var res = new MembershipContainer();

            try
            {
                var req = new RequestBase()
                {
                    Function = String.Format("if( doc.TargetId=='{0}' " +
                                             "&& doc.RelationType=='{1}' " +
                                             "&& doc.EcoSpaceId=='{2}' ) emit(doc.Id,doc)",
                                             accountId,
                                             RoleMembership.Relation,
                                             ecoSpaceId)
                };
                var links = Provider <RoleMembership> .Query(req);

                foreach (var roleMembership in links)
                {
                    res.AddPart(roleMembership);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex.GetCombinedMessages());
                res.AddError(ex.GetCombinedMessages());
            }
            return(res);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        /// <summary>
        /// Returns members of the course.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>The members of the sample course.</returns>
        protected override async Task <ActionResult <MembershipContainer> > OnGetMembershipAsync(GetMembershipRequest request)
        {
            // In this sample app, each registered app user has an associated platform,
            // course, and membership. So look up the user that owns the requested course.

            if (!int.TryParse(request.ContextId, out var contextId))
            {
                var name = $"{nameof(request)}.{nameof(request.ContextId)}";
                ModelState.AddModelError(name, $"The {name} field cannot be converted into a course id.");
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }

            if (!await _courseValidator.UserHasAccess(contextId))
            {
                return(Unauthorized(new ProblemDetails
                {
                    Title = "Not authorized",
                    Detail = "User not authorized to access the requested course."
                }));
            }

            var course = await _context.GetCourseAsync(contextId);

            if (course == null)
            {
                return(NotFound(new ProblemDetails
                {
                    Title = ReasonPhrases.GetReasonPhrase(StatusCodes.Status404NotFound),
                    Detail = "Course not found"
                }));
            }

            var user = await _context.GetUserByCourseIdAsync(course.Id);

            var membership = new MembershipContainer
            {
                Id      = Request.GetDisplayUrl(),
                Context = new Context
                {
                    Id    = user.Course.Id.ToString(),
                    Title = user.Course.Name
                }
            };

            if (user.People.Any())
            {
                var people = user.People
                             .Select(p => new Member
                {
                    FamilyName         = p.LastName,
                    GivenName          = p.FirstName,
                    Roles              = PeopleModel.ParsePersonRoles(p.Roles),
                    Status             = MemberStatus.Active,
                    LisPersonSourcedId = p.SisId,
                    UserId             = p.Id.ToString(),
                    Email              = p.Email
                });

                if (request.Rlid.IsPresent())
                {
                    if (!int.TryParse(request.Rlid, out var resourceLinkId))
                    {
                        var name = $"{nameof(request)}.{nameof(request.ContextId)}";
                        ModelState.AddModelError(name, $"The {name} field cannot be converted into a resource linkid id.");
                        return(BadRequest(new ValidationProblemDetails(ModelState)));
                    }

                    people = people.Where(p => user.Course.ResourceLinks.Any(l => l.Id == resourceLinkId));
                }

                if (request.Role.HasValue)
                {
                    people = people.Where(p => p.Roles.Any(r => r == request.Role.Value));
                }

                membership.Members = people.ToList();
            }

            return(membership);
        }