Example #1
0
        public async Task <List <Organization> > ListOrganizationsAsync(
            OrganizationListRequest request,
            OrganizationFilter filter,
            OrganizationRetrievalOptions options,
            CancellationToken cancellationToken)
        {
            var user = _httpContextAccessor.HttpContext.User;

            if (!user.Identity.IsAuthenticated)
            {
                return(new List <Organization>());
            }

            var query = _context.Organizations.AsNoTracking();

            if (!user.IsPowerAdmin()) // Power admin can see all orgs.
            {
                query = query.HasOrganizationMember(user);
            }

            return(await query
                   .AddFilter(filter ?? new OrganizationFilter())
                   .AddOrder(request.OrderBy, request.Descending)
                   .WithOptions(options ?? new OrganizationRetrievalOptions())
                   .ToListAsync(cancellationToken));
        }
Example #2
0
        public async Task <Organization> GetOrganizationByIdAsync(
            int id,
            OrganizationRetrievalOptions options,
            CancellationToken cancellationToken)
        {
            options ??= new OrganizationRetrievalOptions();

            var query = _context.Organizations
                        .AsNoTracking()
                        .Where(m => m.OrganizationId == id);

            var org = await query
                      .WithOptions(options)
                      .FirstOrDefaultAsync(cancellationToken);

            if (org == null || !org.Active)
            {
                throw new NotFoundException($"Organization {id} not found");
            }

            await _organizationAccessControlService
            .CheckOrganizationReadAccessAsync(id);

            return(org);
        }
Example #3
0
 public async Task <Organization> RequireCurrentOrganizationAsync(
     OrganizationRetrievalOptions options,
     CancellationToken cancellationToken)
 {
     return(await GetCurrentOrganizationAsync(options, cancellationToken) ??
            throw new OrganizationMisconfigurationException(_httpContextAccessor.HttpContext.Request.Host.Value));
 }
        public static IQueryable <Organization> WithOptions(this IQueryable <Organization> query,
                                                            OrganizationRetrievalOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.LoadMembers)
            {
                query = query.Include(o => o.Members)
                        .ThenInclude(m => m.Roles);
            }

            if (options.LoadHostnames)
            {
                query = query.Include(o => o.Hostnames);
            }

            if (options.LoadSettings)
            {
                query = query.Include(o => o.Settings);
            }

            return(query);
        }
Example #5
0
        public async Task <Organization> GetCurrentOrganizationAsync(
            OrganizationRetrievalOptions options,
            CancellationToken cancellationToken)
        {
            // Retrieve current organization by orgId param first
            var orgIdParamValue = _httpContextAccessor.HttpContext.Request.Query[OrgIdParamName];

            if (!string.IsNullOrEmpty(orgIdParamValue) && int.TryParse(orgIdParamValue, out var organizationId))
            {
                return(await _context.Organizations
                       .AsNoTracking()
                       .WithOptions(options ?? new OrganizationRetrievalOptions())
                       .Where(o => o.OrganizationId == organizationId)
                       .FirstOrDefaultAsync(cancellationToken));
            }

            // Try hostname approach, if no orgId is present in the query
            var host = _httpContextAccessor.HttpContext.Request.Host;

            if (!host.HasValue)
            {
                return(null);
            }

            return(await _context.Organizations
                   .AsNoTracking()
                   .WithOptions(options ?? new OrganizationRetrievalOptions())
                   .Where(o => o.Hostnames.Any(h => h.Active && h.Hostname == host.Value))
                   .FirstOrDefaultAsync(cancellationToken));
        }
        public async Task <Organization> GetCurrentOrganizationAsync(OrganizationRetrievalOptions options = null)
        {
            var host = _httpContextAccessor.HttpContext.Request.Host;

            if (!host.HasValue)
            {
                return(null);
            }

            return(await _context.Organizations
                   .AsNoTracking()
                   .UseOptions(options ?? new OrganizationRetrievalOptions())
                   .Where(o => o.Hostnames.Any(h => h.Active && h.Hostname == host.Value))
                   .FirstOrDefaultAsync());
        }
        public async Task <List <Organization> > ListOrganizationsAsync(
            OrganizationFilter filter,
            OrganizationRetrievalOptions options)
        {
            var user = _httpContextAccessor.HttpContext.User;

            if (!user.Identity.IsAuthenticated)
            {
                return(new List <Organization>());
            }

            var query = _context.Organizations.AsNoTracking();

            if (!user.IsInRole(Roles.SuperAdmin)) // Super admin can see all orgs.
            {
                query = query.HasOrganizationMember(user);
            }

            return(await query
                   .UseFilter(filter ?? new OrganizationFilter())
                   .UseOptions(options ?? new OrganizationRetrievalOptions())
                   .ToListAsync());
        }
        public async Task <Organization> GetOrganizationByIdAsync(int id, OrganizationRetrievalOptions options)
        {
            options ??= new OrganizationRetrievalOptions();

            var user = _httpContextAccessor.HttpContext.User;

            if (!user.Identity.IsAuthenticated)
            {
                throw new AccessViolationException("Not authenticated.");
            }

            var query = _context.Organizations
                        .AsNoTracking()
                        .Where(m => m.OrganizationId == id);

            if (!user.IsInRole(Roles.SuperAdmin)) // Super admin can see all orgs.
            {
                query = query.HasOrganizationMember(user);
            }

            return(await query
                   .UseOptions(options)
                   .SingleOrDefaultAsync());
        }