Skip to content

Commit

Permalink
provisioning_api: Add OpenAPI spec
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <[email protected]>
  • Loading branch information
provokateurin committed Feb 10, 2023
1 parent 7611a80 commit 2b29189
Show file tree
Hide file tree
Showing 8 changed files with 3,796 additions and 202 deletions.
61 changes: 35 additions & 26 deletions apps/provisioning_api/lib/Controller/AUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Provisioning_API\Controller;

use Exception;
use OC;
use OC\Group\Manager;
use OC\User\Backend;
use OC\User\NoUserException;
use OC_Helper;
use OC_Util;
use OCA\Provisioning_API\ResponseDefinitions;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\AppFramework\Http;
Expand All @@ -51,7 +56,11 @@
use OCP\L10N\IFactory;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
use Psr\Log\LoggerInterface;

/**
* @psalm-import-type UserDetails from ResponseDefinitions
*/
abstract class AUserData extends OCSController {
public const SCOPE_SUFFIX = 'Scope';

Expand All @@ -76,13 +85,13 @@ abstract class AUserData extends OCSController {
protected $l10nFactory;

public function __construct(string $appName,
IRequest $request,
IUserManager $userManager,
IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
IAccountManager $accountManager,
IFactory $l10nFactory) {
IRequest $request,
IUserManager $userManager,
IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
IAccountManager $accountManager,
IFactory $l10nFactory) {
parent::__construct($appName, $request);

$this->userManager = $userManager;
Expand All @@ -98,12 +107,12 @@ public function __construct(string $appName,
*
* @param string $userId
* @param bool $includeScopes
* @return array
* @return UserDetails|null
* @throws NotFoundException
* @throws OCSException
* @throws OCSNotFoundException
*/
protected function getUserData(string $userId, bool $includeScopes = false): array {
protected function getUserData(string $userId, bool $includeScopes = false): ?array {
$currentLoggedInUser = $this->userSession->getUser();
assert($currentLoggedInUser !== null, 'No user logged in');

Expand All @@ -122,7 +131,7 @@ protected function getUserData(string $userId, bool $includeScopes = false): arr
} else {
// Check they are looking up themselves
if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
return $data;
return null;
}
}

Expand Down Expand Up @@ -182,17 +191,17 @@ protected function getUserData(string $userId, bool $includeScopes = false): arr
}

foreach ([
IAccountManager::PROPERTY_PHONE,
IAccountManager::PROPERTY_ADDRESS,
IAccountManager::PROPERTY_WEBSITE,
IAccountManager::PROPERTY_TWITTER,
IAccountManager::PROPERTY_FEDIVERSE,
IAccountManager::PROPERTY_ORGANISATION,
IAccountManager::PROPERTY_ROLE,
IAccountManager::PROPERTY_HEADLINE,
IAccountManager::PROPERTY_BIOGRAPHY,
IAccountManager::PROPERTY_PROFILE_ENABLED,
] as $propertyName) {
IAccountManager::PROPERTY_PHONE,
IAccountManager::PROPERTY_ADDRESS,
IAccountManager::PROPERTY_WEBSITE,
IAccountManager::PROPERTY_TWITTER,
IAccountManager::PROPERTY_FEDIVERSE,
IAccountManager::PROPERTY_ORGANISATION,
IAccountManager::PROPERTY_ROLE,
IAccountManager::PROPERTY_HEADLINE,
IAccountManager::PROPERTY_BIOGRAPHY,
IAccountManager::PROPERTY_PROFILE_ENABLED,
] as $propertyName) {
$property = $userAccount->getProperty($propertyName);
$data[$propertyName] = $property->getValue();
if ($includeScopes) {
Expand Down Expand Up @@ -249,8 +258,8 @@ protected function getUserSubAdminGroupsData(string $userId): array {
*/
protected function fillStorageInfo(string $userId): array {
try {
\OC_Util::tearDownFS();
\OC_Util::setupFS($userId);
OC_Util::tearDownFS();
OC_Util::setupFS($userId);
$storage = OC_Helper::getStorageInfo('/', null, true, false);
$data = [
'free' => $storage['free'],
Expand All @@ -273,8 +282,8 @@ protected function fillStorageInfo(string $userId): array {
self::USER_FIELD_QUOTA => $quota !== false ? $quota : 'none',
'used' => 0
];
} catch (\Exception $e) {
\OC::$server->get(\Psr\Log\LoggerInterface::class)->error(
} catch (Exception $e) {
OC::$server->get(LoggerInterface::class)->error(
"Could not load storage info for {user}",
[
'app' => 'provisioning_api',
Expand All @@ -283,7 +292,7 @@ protected function fillStorageInfo(string $userId): array {
]
);
/* In case the Exception left things in a bad state */
\OC_Util::tearDownFS();
OC_Util::tearDownFS();
return [];
}
return $data;
Expand Down
56 changes: 30 additions & 26 deletions apps/provisioning_api/lib/Controller/AppConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* @author Joas Schilling <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -24,11 +25,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Provisioning_API\Controller;

use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\DataResponse200;
use OCP\AppFramework\Http\DataResponse403;
use OCP\AppFramework\OCSController;
use OCP\IAppConfig;
use OCP\IConfig;
Expand Down Expand Up @@ -67,13 +71,13 @@ class AppConfigController extends OCSController {
* @param IAppConfig $appConfig
*/
public function __construct(string $appName,
IRequest $request,
IConfig $config,
IAppConfig $appConfig,
IUserSession $userSession,
IL10N $l10n,
IGroupManager $groupManager,
IManager $settingManager) {
IRequest $request,
IConfig $config,
IAppConfig $appConfig,
IUserSession $userSession,
IL10N $l10n,
IGroupManager $groupManager,
IManager $settingManager) {
parent::__construct($appName, $request);
$this->config = $config;
$this->appConfig = $appConfig;
Expand All @@ -84,25 +88,25 @@ public function __construct(string $appName,
}

/**
* @return DataResponse
* @return DataResponse200<array{data: string[]}>
*/
public function getApps(): DataResponse {
return new DataResponse([
public function getApps(): DataResponse200 {
return new DataResponse200([
'data' => $this->appConfig->getApps(),
]);
}

/**
* @param string $app
* @return DataResponse
* @return DataResponse200<array{data: string[]}>|DataResponse403<array{data: array{message: string}}>
*/
public function getKeys(string $app): DataResponse {
public function getKeys(string $app): DataResponse200|DataResponse403 {
try {
$this->verifyAppId($app);
} catch (\InvalidArgumentException $e) {
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
return new DataResponse403(['data' => ['message' => $e->getMessage()]]);
}
return new DataResponse([
return new DataResponse200([
'data' => $this->config->getAppKeys($app),
]);
}
Expand All @@ -111,15 +115,15 @@ public function getKeys(string $app): DataResponse {
* @param string $app
* @param string $key
* @param string $defaultValue
* @return DataResponse
* @return DataResponse200<array{data: string}>|DataResponse403<array{data: array{message: string}}>
*/
public function getValue(string $app, string $key, string $defaultValue = ''): DataResponse {
public function getValue(string $app, string $key, string $defaultValue = ''): DataResponse200|DataResponse403 {
try {
$this->verifyAppId($app);
} catch (\InvalidArgumentException $e) {
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
return new DataResponse403(['data' => ['message' => $e->getMessage()]]);
}
return new DataResponse([
return new DataResponse200([
'data' => $this->config->getAppValue($app, $key, $defaultValue),
]);
}
Expand All @@ -131,9 +135,9 @@ public function getValue(string $app, string $key, string $defaultValue = ''): D
* @param string $app
* @param string $key
* @param string $value
* @return DataResponse
* @return DataResponse200|DataResponse403<array{data: array{message: string}}>
*/
public function setValue(string $app, string $key, string $value): DataResponse {
public function setValue(string $app, string $key, string $value): DataResponse200|DataResponse403 {
$user = $this->userSession->getUser();
if ($user === null) {
throw new \Exception("User is not logged in."); // Should not happen, since method is guarded by middleware
Expand All @@ -147,29 +151,29 @@ public function setValue(string $app, string $key, string $value): DataResponse
$this->verifyAppId($app);
$this->verifyConfigKey($app, $key, $value);
} catch (\InvalidArgumentException $e) {
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
return new DataResponse403(['data' => ['message' => $e->getMessage()]]);
}

$this->config->setAppValue($app, $key, $value);
return new DataResponse();
return new DataResponse200();
}

/**
* @PasswordConfirmationRequired
* @param string $app
* @param string $key
* @return DataResponse
* @return DataResponse200|DataResponse403<array{data: array{message: string}}>
*/
public function deleteKey(string $app, string $key): DataResponse {
public function deleteKey(string $app, string $key): DataResponse200|DataResponse403 {
try {
$this->verifyAppId($app);
$this->verifyConfigKey($app, $key, '');
} catch (\InvalidArgumentException $e) {
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
return new DataResponse403(['data' => ['message' => $e->getMessage()]]);
}

$this->config->deleteAppValue($app, $key);
return new DataResponse();
return new DataResponse200();
}

/**
Expand Down
36 changes: 22 additions & 14 deletions apps/provisioning_api/lib/Controller/AppsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @author Lukas Reschke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Tom Needham <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -26,16 +27,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Provisioning_API\Controller;

use OC_App;
use OCA\Provisioning_API\ResponseDefinitions;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\DataResponse200;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCSController;
use OCP\IRequest;

/**
* @psalm-import-type AppInfo from ResponseDefinitions
*/
class AppsController extends OCSController {
/** @var IAppManager */
private $appManager;
Expand All @@ -52,42 +59,43 @@ public function __construct(

/**
* @param string|null $filter
* @return DataResponse
* @return DataResponse200<array{apps: string[]}>
* @throws OCSException
*/
public function getApps(string $filter = null): DataResponse {
public function getApps(string $filter = null): DataResponse200 {
$apps = (new OC_App())->listAllApps();
/** @var string[] $list */
$list = [];
foreach ($apps as $app) {
$list[] = $app['id'];
}
if ($filter) {
switch ($filter) {
case 'enabled':
return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
return new DataResponse200(['apps' => OC_App::getEnabledApps()]);
break;
case 'disabled':
$enabled = OC_App::getEnabledApps();
return new DataResponse(['apps' => array_diff($list, $enabled)]);
return new DataResponse200(['apps' => array_diff($list, $enabled)]);
break;
default:
// Invalid filter variable
throw new OCSException('', 101);
}
} else {
return new DataResponse(['apps' => $list]);
return new DataResponse200(['apps' => $list]);
}
}

/**
* @param string $app
* @return DataResponse
* @return DataResponse200<AppInfo>
* @throws OCSException
*/
public function getAppInfo(string $app): DataResponse {
public function getAppInfo(string $app): DataResponse200 {
$info = $this->appManager->getAppInfo($app);
if (!is_null($info)) {
return new DataResponse($info);
return new DataResponse200($info);
}

throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
Expand All @@ -96,25 +104,25 @@ public function getAppInfo(string $app): DataResponse {
/**
* @PasswordConfirmationRequired
* @param string $app
* @return DataResponse
* @return DataResponse200
* @throws OCSException
*/
public function enable(string $app): DataResponse {
public function enable(string $app): DataResponse200 {
try {
$this->appManager->enableApp($app);
} catch (AppPathNotFoundException $e) {
throw new OCSException('The request app was not found', OCSController::RESPOND_NOT_FOUND);
}
return new DataResponse();
return new DataResponse200();
}

/**
* @PasswordConfirmationRequired
* @param string $app
* @return DataResponse
* @return DataResponse200
*/
public function disable(string $app): DataResponse {
public function disable(string $app): DataResponse200 {
$this->appManager->disableApp($app);
return new DataResponse();
return new DataResponse200();
}
}
Loading

0 comments on commit 2b29189

Please sign in to comment.