/// <summary> /// Gets the connection opportunities queryable that matches the specified /// options. /// </summary> /// <param name="options">The filter to apply to the query.</param> /// <returns>A queryable of <see cref="ConnectionOpportunity"/> objects.</returns> /// <exception cref="System.InvalidOperationException">Context is not a RockContext.</exception> public IQueryable <ConnectionOpportunity> GetConnectionOpportunitiesQuery(ConnectionOpportunityQueryOptions options = null) { if (!(Context is RockContext rockContext)) { throw new InvalidOperationException("Context is not a RockContext."); } options = options ?? DefaultGetConnectionTypesOptions; var qry = Queryable(); if (options.ConnectionTypeGuids != null && options.ConnectionTypeGuids.Any()) { qry = qry.Where(o => options.ConnectionTypeGuids.Contains(o.ConnectionType.Guid)); } if (options.ConnectorPersonIds != null && options.ConnectorPersonIds.Any()) { var connectorRequestsQry = new ConnectionRequestService(rockContext).Queryable() .Where(r => r.ConnectionState != ConnectionState.Connected && r.ConnectorPersonAliasId.HasValue && options.ConnectorPersonIds.Contains(r.ConnectorPersonAlias.PersonId)) .Select(r => r.Id); qry = qry.Where(o => o.ConnectionRequests.Any(r => connectorRequestsQry.Contains(r.Id))); } if (!options.IncludeInactive) { qry = qry.Where(o => o.IsActive && o.ConnectionType.IsActive); } return(qry); }
/// <summary> /// Filters the collection of <see cref="ConnectionType"/> objects to those /// that <paramref name="person"/> is authorized to view. This handles special /// security considerations such as <see cref="ConnectionType.EnableRequestSecurity"/>. /// </summary> /// <param name="connectionTypes">The connection types to be filtered.</param> /// <param name="person">The person that will be used for the authorization check.</param> /// <returns>A list of <see cref="ConnectionType"/> objects that the person is allowed to see.</returns> /// <exception cref="System.InvalidOperationException">Context is not a RockContext.</exception> public List <ConnectionType> GetViewAuthorizedConnectionTypes(IEnumerable <ConnectionType> connectionTypes, Person person) { if (!(Context is RockContext rockContext)) { throw new InvalidOperationException("Context is not a RockContext."); } // Make a list of any type identifiers that are configured // for request security and the person is assigned as the // connector to any request. var currentPersonId = person?.Id; var selfAssignedSecurityTypes = new ConnectionRequestService(rockContext) .Queryable() .Where(r => r.ConnectorPersonAlias.PersonId == currentPersonId && r.ConnectionOpportunity.ConnectionType.EnableRequestSecurity) .Select(r => r.ConnectionOpportunity.ConnectionTypeId) .Distinct() .ToList(); // Put all the types in memory so we can check security. var types = connectionTypes.ToList() .Where(o => o.IsAuthorized(Authorization.VIEW, person) || selfAssignedSecurityTypes.Contains(o.Id)) .ToList(); return(types); }