Exemple #1
0
        protected async Task <IActionResult> GetDocuments(HttpRequest req, DocumentClient client, ILogger log, string collectionName)
        {
            log.LogInformation($"Getting {collectionName} list");

            var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers);

            if (authorizationResult.Failed)
            {
                log.LogWarning(authorizationResult.FailureReason);
                return(new UnauthorizedResult());
            }

            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, collectionName);
            var query         = client.CreateDocumentQuery <T>(collectionUri).AsDocumentQuery();
            var documents     = new List <T>();

            while (query.HasMoreResults)
            {
                var result = await query.ExecuteNextAsync <T>();

                documents.AddRange(result);
            }

            return(new OkObjectResult(documents));
        }
        public async Task <IActionResult> GetList(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]
            HttpRequest req,
            [CosmosDB(databaseName: "2wr", collectionName: "emergencykits", ConnectionStringSetting = "CosmosDBConnection")]
            DocumentClient client,
            ILogger log)
        {
            log.LogInformation($"Getting list of emergency kits");
            var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers);

            if (authorizationResult.Failed)
            {
                log.LogWarning(authorizationResult.FailureReason);
                return(new UnauthorizedResult());
            }
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("2wr", "emergencykits");
            var query         = client.CreateDocumentQuery <EmergencyKit>(collectionUri, new FeedOptions {
                EnableCrossPartitionQuery = true
            })                                                                                                                     // REVIEW: Why is this required when my where clause includes the partition id?
                                .Where(e => e.UserId == authorizationResult.User.Identity.Name)
                                .AsDocumentQuery();

            var emergencyKits = new List <EmergencyKit>();

            while (query.HasMoreResults)
            {
                var result = await query.ExecuteNextAsync <EmergencyKit>();

                emergencyKits.AddRange(result);
            }

            return(new OkObjectResult(emergencyKits));
        }
        public async Task <IActionResult> GetKits(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]
            HttpRequest req,
            [CosmosDB(databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")]
            DocumentClient client,
            ILogger log)
        {
            log.LogInformation($"Getting list of base kits");
            var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers);

            if (authorizationResult.Failed)
            {
                log.LogWarning(authorizationResult.FailureReason);
                return(new UnauthorizedResult());
            }
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("2wr", "basekits");
            var query         = client.CreateDocumentQuery <BaseKit>(collectionUri, new FeedOptions {
                EnableCrossPartitionQuery = true
            })
                                .AsDocumentQuery();

            var baseKits = new List <BaseKit>();

            while (query.HasMoreResults)
            {
                var result = await query.ExecuteNextAsync <BaseKit>();

                baseKits.AddRange(result);
            }

            return(new OkObjectResult(baseKits));
        }
Exemple #4
0
        private async Task <IActionResult> RunFilteredRequest <T>(IHeaderDictionary headers, System.Func <IUserRepository, Task <T> > work)
        {
            var authResult = await _apiAuthentication.AuthenticateAsync(headers);

            if (authResult.Failed)
            {
                return(new UnauthorizedObjectResult(authResult.FailureReason));
            }

            var orgId = authResult.User.Claims.SingleOrDefault(x => x.Type == ORGID_EXTENSION);

            if (orgId == null)
            {
                return(new UnauthorizedObjectResult(new { Message = "User is not a member of an organization" }));
            }

            var repo = _repoFactory.CreateForOrgId(authResult.User);

            return(new OkObjectResult(await work(repo)));
        }
Exemple #5
0
        private async Task <bool> Authorized(HttpRequest req, ILogger log)
        {
            var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers);

            if (authorizationResult.Failed)
            {
                log.LogWarning(authorizationResult.FailureReason);
                return(false);
            }

            _user = authorizationResult.User;
            return(true);
        }