Beispiel #1
0
        public async Task <IHttpActionResult> Order([FromBody] OrderParams orderParams)
        {
            // Diese Prüfung soll immer für den aktuellen Benutzer (also nicht für den Besteller) ablaufen gemäss Telefonat mit Marlies Hertig
            var basket = await GetBasket();

            if (basket == null || basket.Count(i => !i.EinsichtsbewilligungNotwendig) == 0)
            {
                throw new BadRequestException("Order not is allowed, because there are no items in basket");
            }

            var userAccess = GetUserAccess();

            var bestellerId = !string.IsNullOrWhiteSpace(orderParams.UserId)
                ? orderParams.UserId
                : ControllerHelper.GetCurrentUserId();
            var bestellungIstFuerAndererBenutzer = ControllerHelper.GetCurrentUserId() != bestellerId;

            if (bestellungIstFuerAndererBenutzer)
            {
                if (GetCurrentUserAccess().RolePublicClient != AccessRoles.RoleBAR)
                {
                    throw new BadRequestException(
                              "Es wird versucht, für einen anderen Benutzer als den aktuellen Benutzer zu bestellen. Dazu fehlt aber die Berechtigung.");
                }

                orderParams.Comment = string.Join(" ", orderParams.Comment,
                                                  FrontendSettingsViaduc.Instance.GetTranslation(WebHelper.GetClientLanguage(Request),
                                                                                                 "order.frombar", "(Diese Bestellung wurde durch das Bundesarchiv ausgelöst.)"));
            }

            var orderItemIdsToExclude = basket
                                        .Where(basketItem => basketItem.EinsichtsbewilligungNotwendig ||
                                               orderParams.Type == OrderType.Digitalisierungsauftrag &&
                                               orderParams.OrderIdsToExclude != null && orderParams.OrderIdsToExclude.Contains(basketItem.Id))
                                        .Select(item => item.Id)
                                        .ToList();

            DateTime?leseSaalDateAsDateTime = null;

            switch (orderParams.Type)
            {
            case OrderType.Verwaltungsausleihe:
                ValidateVerwaltungsausleiheBestellung(userAccess);
                break;

            case OrderType.Digitalisierungsauftrag:
                await ValidateDigitalisierungsauftragBestellung(bestellerId, bestellungIstFuerAndererBenutzer,
                                                                userAccess, basket, orderItemIdsToExclude);

                break;

            case OrderType.Lesesaalausleihen:
                leseSaalDateAsDateTime = orderParams.LesesaalDate.ParseDateTimeSwiss();
                ValidateLesesaalBestellung(leseSaalDateAsDateTime);
                break;

            default:
                throw new BadRequestException(
                          $"Bestelltyp {orderParams.Type.ToString()} ist hier nicht unterstützt.");
            }

            if (userAccess.RolePublicClient == AccessRoles.RoleAS)
            {
                var settings = NinjectWebCommon.Kernel.Get <VerwaltungsausleiheSettings>();
                orderParams.ArtDerArbeit = (int)settings.ArtDerArbeitFuerAmtsBestellung;
            }

            var creationRequest = new OrderCreationRequest
            {
                OrderItemIdsToExclude = orderItemIdsToExclude,
                Type          = orderParams.Type,
                Comment       = orderParams.Comment,
                ArtDerArbeit  = orderParams.ArtDerArbeit,
                LesesaalDate  = leseSaalDateAsDateTime,
                CurrentUserId = ControllerHelper.GetCurrentUserId(),
                BestellerId   = bestellerId
            };

            var veInfoList = basket.Where(item => item.VeId.HasValue && !orderItemIdsToExclude.Contains(item.Id))
                             .Select(item => new VeInfo((int)item.VeId, item.Reason)).ToList();

            await kontrollstellenInformer.InformIfNecessary(userAccess, veInfoList);

            await client.CreateOrderFromBasket(creationRequest);

            return(Content <object>(HttpStatusCode.NoContent, null));
        }
Beispiel #2
0
        public async Task <IHttpActionResult> DownloadFile(int id, string token, int reason = 0)
        {
            var archiveRecordId = id;

            if (string.IsNullOrWhiteSpace(token))
            {
                return(Content(HttpStatusCode.Forbidden, "Invalid token"));
            }

            var ipAdress = downloadHelper.GetClientIp(Request);

            if (!downloadTokenDataAccess.CheckTokenIsValidAndClean(token, archiveRecordId, DownloadTokenType.ArchiveRecord, ipAdress))
            {
                return(BadRequest("Token expired or is not valid"));
            }

            var userId = downloadTokenDataAccess.GetUserIdByToken(token, archiveRecordId, DownloadTokenType.ArchiveRecord, ipAdress);

            if (string.IsNullOrWhiteSpace(userId))
            {
                return(Content(HttpStatusCode.Forbidden, "No User found for the requested Downloadtoken"));
            }

            downloadTokenDataAccess.CleanUpOldToken(token, archiveRecordId, DownloadTokenType.ArchiveRecord);

            var access = GetUserAccessFunc(userId);
            var user   = userDataAccess.GetUser(userId);

            var entityResult = elasticService.QueryForId <ElasticArchiveRecord>(archiveRecordId, access);
            var record       = entityResult.Response?.Hits?.FirstOrDefault()?.Source;

            if (record == null)
            {
                return(NotFound());
            }

            var packageId = record.PrimaryData.FirstOrDefault()?.PackageId ?? "";

            if (string.IsNullOrEmpty(packageId))
            {
                return(BadRequest("VE does not contain any primarydata and/or a valid packageid"));
            }

            if (!CheckUserHasDownloadTokensForVe(access, record))
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }

            try
            {
                if (reason != 0)
                {
                    userDataAccess.StoreDownloadReasonInHistory(record, user, access, reason);
                }

                var downloadAssetResult = await downloadClient.Request(new DownloadAssetRequest
                {
                    ArchiveRecordId   = archiveRecordId.ToString(),
                    AssetType         = AssetType.Gebrauchskopie,
                    Recipient         = userId,
                    AssetId           = record.PrimaryData.FirstOrDefault()?.PackageId,
                    RetentionCategory = await cacheHelper.GetRetentionCategory(record, access.RolePublicClient, orderDataAccess)
                });

                var stream = cacheHelper.GetStreamFromCache(downloadAssetResult.AssetDownloadLink);
                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StreamContent(stream)
                };

                result.Content.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = archiveRecordId + ".zip"
                };

                await kontrollstellenInformer.InformIfNecessary(access, new[] { new VeInfo(archiveRecordId, reason) });

                downloadLogDataAccess.LogVorgang(token, "Download");

                return(ResponseMessage(result));
            }
            catch (Exception e)
            {
                Log.Error(e, "(FileController:DownloadFile({ID}))", archiveRecordId);
                throw;
            }
        }