|
| 1 | +<?php namespace App\Http\Controllers; |
| 2 | +/** |
| 3 | + * Copyright 2026 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use App\Models\Exceptions\AuthzException; |
| 16 | +use App\Models\Foundation\Main\IGroup; |
| 17 | +use App\Models\Foundation\Summit\Locations\SummitVenue; |
| 18 | +use App\Services\Apis\IDropboxMaterializerApi; |
| 19 | +use models\oauth2\IResourceServerContext; |
| 20 | +use models\summit\ISummitRepository; |
| 21 | +use models\summit\Summit; |
| 22 | +use models\exceptions\ValidationException; |
| 23 | +use models\exceptions\EntityNotFoundException; |
| 24 | + |
| 25 | +/** |
| 26 | + * Class OAuth2SummitDropboxSyncApiController |
| 27 | + * @package App\Http\Controllers |
| 28 | + */ |
| 29 | +final class OAuth2SummitDropboxSyncApiController extends OAuth2ProtectedController |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var IDropboxMaterializerApi |
| 33 | + */ |
| 34 | + private $materializer_api; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param ISummitRepository $summit_repository |
| 38 | + * @param IDropboxMaterializerApi $materializer_api |
| 39 | + * @param IResourceServerContext $resource_server_context |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + ISummitRepository $summit_repository, |
| 43 | + IDropboxMaterializerApi $materializer_api, |
| 44 | + IResourceServerContext $resource_server_context |
| 45 | + ) |
| 46 | + { |
| 47 | + parent::__construct($resource_server_context); |
| 48 | + $this->repository = $summit_repository; |
| 49 | + $this->materializer_api = $materializer_api; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param Summit $summit |
| 54 | + * @return void |
| 55 | + * @throws \Exception |
| 56 | + */ |
| 57 | + private function checkAdminPermission(Summit $summit): void |
| 58 | + { |
| 59 | + $current_member = $this->resource_server_context->getCurrentUser(); |
| 60 | + if (!is_null($current_member) && !$current_member->isAdmin() && !$current_member->hasPermissionForOnGroup($summit, IGroup::SummitAdministrators)) |
| 61 | + throw new AuthzException( |
| 62 | + sprintf("Member %s has not permission for this Summit", $current_member->getId()) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param int $summit_id |
| 68 | + * @return Summit |
| 69 | + * @throws EntityNotFoundException |
| 70 | + */ |
| 71 | + private function findSummit(int $summit_id): Summit |
| 72 | + { |
| 73 | + $summit = $this->repository->getById($summit_id); |
| 74 | + if (is_null($summit) || !$summit instanceof Summit) |
| 75 | + throw new EntityNotFoundException(sprintf("Summit %s not found", $summit_id)); |
| 76 | + return $summit; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param Summit $summit |
| 81 | + * @throws ValidationException |
| 82 | + */ |
| 83 | + private function requireSyncEnabled(Summit $summit): void |
| 84 | + { |
| 85 | + if (!$summit->isDropboxSyncEnabled()) |
| 86 | + throw new ValidationException("Dropbox sync is not enabled for this summit."); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * POST /api/v1/summits/{id}/dropbox-sync/materialize |
| 91 | + */ |
| 92 | + public function materialize($summit_id) |
| 93 | + { |
| 94 | + return $this->processRequest(function () use ($summit_id) { |
| 95 | + $summit = $this->findSummit(intval($summit_id)); |
| 96 | + $this->checkAdminPermission($summit); |
| 97 | + $this->requireSyncEnabled($summit); |
| 98 | + |
| 99 | + $result = $this->materializer_api->materialize($summit->getId()); |
| 100 | + return $this->ok($result); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * POST /api/v1/summits/{id}/dropbox-sync/materialize/{location_id}/{room_id} |
| 106 | + */ |
| 107 | + public function materializeRoom($summit_id, $location_id, $room_id) |
| 108 | + { |
| 109 | + return $this->processRequest(function () use ($summit_id, $location_id, $room_id) { |
| 110 | + $summit = $this->findSummit(intval($summit_id)); |
| 111 | + $this->checkAdminPermission($summit); |
| 112 | + $this->requireSyncEnabled($summit); |
| 113 | + |
| 114 | + $venue = $summit->getLocation(intval($location_id)); |
| 115 | + if (is_null($venue) || !$venue instanceof SummitVenue) |
| 116 | + throw new EntityNotFoundException(sprintf("Venue %s not found", $location_id)); |
| 117 | + |
| 118 | + $room = $venue->getRoom(intval($room_id)); |
| 119 | + if (is_null($room)) |
| 120 | + throw new EntityNotFoundException(sprintf("Room %s not found", $room_id)); |
| 121 | + |
| 122 | + $result = $this->materializer_api->materializeRoom( |
| 123 | + $summit->getId(), |
| 124 | + $venue->getName(), |
| 125 | + $room->getName() |
| 126 | + ); |
| 127 | + return $this->ok($result); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * POST /api/v1/summits/{id}/dropbox-sync/backfill |
| 133 | + */ |
| 134 | + public function backfill($summit_id) |
| 135 | + { |
| 136 | + return $this->processRequest(function () use ($summit_id) { |
| 137 | + $summit = $this->findSummit(intval($summit_id)); |
| 138 | + $this->checkAdminPermission($summit); |
| 139 | + $this->requireSyncEnabled($summit); |
| 140 | + |
| 141 | + $result = $this->materializer_api->backfill($summit->getId()); |
| 142 | + return $this->ok($result); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * POST /api/v1/summits/{id}/dropbox-sync/rebuild |
| 148 | + */ |
| 149 | + public function rebuild($summit_id) |
| 150 | + { |
| 151 | + return $this->processRequest(function () use ($summit_id) { |
| 152 | + $summit = $this->findSummit(intval($summit_id)); |
| 153 | + $this->checkAdminPermission($summit); |
| 154 | + |
| 155 | + $result = $this->materializer_api->rebuild($summit->getId()); |
| 156 | + return $this->ok($result); |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * GET /api/v1/summits/{id}/dropbox-sync/preflight |
| 162 | + */ |
| 163 | + public function preflight($summit_id) |
| 164 | + { |
| 165 | + return $this->processRequest(function () use ($summit_id) { |
| 166 | + $summit = $this->findSummit(intval($summit_id)); |
| 167 | + $this->checkAdminPermission($summit); |
| 168 | + |
| 169 | + $result = $this->materializer_api->preflight($summit->getId()); |
| 170 | + return $this->ok($result); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * GET /api/v1/summits/{id}/dropbox-sync/status |
| 176 | + */ |
| 177 | + public function status($summit_id) |
| 178 | + { |
| 179 | + return $this->processRequest(function () use ($summit_id) { |
| 180 | + $summit = $this->findSummit(intval($summit_id)); |
| 181 | + $this->checkAdminPermission($summit); |
| 182 | + |
| 183 | + $result = $this->materializer_api->status($summit->getId()); |
| 184 | + return $this->ok($result); |
| 185 | + }); |
| 186 | + } |
| 187 | +} |
0 commit comments