* [MOD] Include custom fields in API response using adapters

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2020-03-07 12:01:44 +01:00
parent 3bcf2e0b4d
commit 978eca80d2
12 changed files with 357 additions and 39 deletions

View File

@@ -43,6 +43,7 @@ use SP\Services\Account\AccountRequest;
use SP\Services\Account\AccountSearchFilter;
use SP\Services\Account\AccountService;
use SP\Services\Api\ApiResponse;
use SP\Util\Util;
/**
* Class AccountController
@@ -72,7 +73,7 @@ final class AccountController extends ControllerBase
$this->setupApi(ActionsInterface::ACCOUNT_VIEW);
$id = $this->apiService->getParamInt('id', true);
$customFields = boolval($this->apiService->getParamString('customFields'));
$customFields = Util::boolval($this->apiService->getParamString('customFields'));
if ($customFields) {
$this->apiService->requireMasterPass();
@@ -99,13 +100,14 @@ final class AccountController extends ControllerBase
$adapter = new AccountAdapter($this->configData);
$out = $this->fractal
->createData(new Item($accountResponse, $adapter));
if ($customFields) {
$adapter->withCustomFields($this->getCustomFieldsForItem(ActionsInterface::ACCOUNT, $id));
$this->fractal->parseIncludes(['customFields']);
}
$item = new Item($accountResponse, $adapter);
$this->returnResponse(ApiResponse::makeSuccess($this->fractal->createData($item)->toArray(), $id));
$this->returnResponse(ApiResponse::makeSuccess($out->toArray(), $id));
} catch (Exception $e) {
$this->returnResponseException($e);

View File

@@ -27,6 +27,8 @@ namespace SP\Modules\Api\Controllers;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use League\Fractal\Resource\Item;
use SP\Adapters\CategoryAdapter;
use SP\Core\Acl\ActionsInterface;
use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
@@ -34,8 +36,10 @@ use SP\Core\Exceptions\InvalidClassException;
use SP\DataModel\CategoryData;
use SP\DataModel\ItemSearchData;
use SP\Modules\Api\Controllers\Help\CategoryHelp;
use SP\Mvc\Controller\ItemTrait;
use SP\Services\Api\ApiResponse;
use SP\Services\Category\CategoryService;
use SP\Util\Util;
/**
@@ -45,6 +49,8 @@ use SP\Services\Category\CategoryService;
*/
final class CategoryController extends ControllerBase
{
use ItemTrait;
/**
* @var CategoryService
*/
@@ -59,6 +65,10 @@ final class CategoryController extends ControllerBase
$this->setupApi(ActionsInterface::CATEGORY_VIEW);
$id = $this->apiService->getParamInt('id', true);
$customFields = Util::boolval($this->apiService->getParamString('customFields'));
$adapter = new CategoryAdapter($this->configData);
$categoryData = $this->categoryService->getById($id);
$this->eventDispatcher->notifyEvent('show.category',
@@ -68,7 +78,16 @@ final class CategoryController extends ControllerBase
->addDetail('ID', $id))
);
$this->returnResponse(ApiResponse::makeSuccess($categoryData, $id));
$out = $this->fractal
->createData(
new Item($categoryData, new CategoryAdapter($this->configData)));
if ($customFields) {
$this->apiService->requireMasterPass();
$this->fractal->parseIncludes(['customFields']);
}
$this->returnResponse(ApiResponse::makeSuccess($out->toArray(), $id));
} catch (Exception $e) {
processException($e);

View File

@@ -27,6 +27,8 @@ namespace SP\Modules\Api\Controllers;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use League\Fractal\Resource\Item;
use SP\Adapters\ClientAdapter;
use SP\Core\Acl\ActionsInterface;
use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
@@ -34,8 +36,10 @@ use SP\Core\Exceptions\InvalidClassException;
use SP\DataModel\ClientData;
use SP\DataModel\ItemSearchData;
use SP\Modules\Api\Controllers\Help\ClientHelp;
use SP\Mvc\Controller\ItemTrait;
use SP\Services\Api\ApiResponse;
use SP\Services\Client\ClientService;
use SP\Util\Util;
/**
* Class ClientController
@@ -44,6 +48,8 @@ use SP\Services\Client\ClientService;
*/
final class ClientController extends ControllerBase
{
use ItemTrait;
/**
* @var ClientService
*/
@@ -58,6 +64,9 @@ final class ClientController extends ControllerBase
$this->setupApi(ActionsInterface::CLIENT_VIEW);
$id = $this->apiService->getParamInt('id', true);
$customFields = Util::boolval($this->apiService->getParamString('customFields'));
$clientData = $this->clientService->getById($id);
$this->eventDispatcher->notifyEvent('show.client', new Event($this));
@@ -69,7 +78,20 @@ final class ClientController extends ControllerBase
->addDetail('ID', $id))
);
$this->returnResponse(ApiResponse::makeSuccess($clientData, $id));
if ($customFields) {
$this->apiService->requireMasterPass();
}
$out = $this->fractal
->createData(
new Item($clientData, new ClientAdapter($this->configData)));
if ($customFields) {
$this->apiService->requireMasterPass();
$this->fractal->parseIncludes(['customFields']);
}
$this->returnResponse(ApiResponse::makeSuccess($out->toArray(), $id));
} catch (Exception $e) {
processException($e);

View File

@@ -40,7 +40,9 @@ final class CategoryHelp implements HelpInterface
{
return
[
self::getItem('id', __('Category Id'), true)
self::getItem('id', __('Category Id'), true),
self::getItem('customFields', __('Get custom fields for the category within the response')),
self::getItem('tokenPass', __('Token\'s password when custom fields are retrieved'), true)
];
}

View File

@@ -40,7 +40,9 @@ final class ClientHelp implements HelpInterface
{
return
[
self::getItem('id', __('Client Id'), true)
self::getItem('id', __('Client Id'), true),
self::getItem('customFields', __('Get custom fields for the client within the response')),
self::getItem('tokenPass', __('Token\'s password when custom fields are retrieved'), true)
];
}

View File

@@ -24,13 +24,12 @@
namespace SP\Adapters;
use League\Fractal\TransformerAbstract;
use SP\Config\ConfigData;
use League\Fractal\Resource\Collection;
use SP\Core\Acl\ActionsInterface;
use SP\Core\Exceptions\SPException;
use SP\DataModel\Dto\AccountDetailsResponse;
use SP\DataModel\ItemData;
use SP\Mvc\Controller\ItemTrait;
use SP\Mvc\View\Components\SelectItemAdapter;
use SP\Services\CustomField\CustomFieldItem;
use SP\Util\Link;
/**
@@ -38,33 +37,26 @@ use SP\Util\Link;
*
* @package SP\Adapters
*/
final class AccountAdapter extends TransformerAbstract
final class AccountAdapter extends AdapterBase
{
/**
* @var CustomFieldItem[]
*/
private $customFields;
/**
* @var ConfigData
*/
private $configData;
use ItemTrait;
protected $availableIncludes = [
'customFields'
];
/**
* AccountAdapter constructor.
* @param AccountDetailsResponse $data
*
* @param ConfigData $configData
* @return Collection
* @throws SPException
*/
public function __construct(ConfigData $configData)
public function includeCustomFields(AccountDetailsResponse $data)
{
$this->configData = $configData;
}
/**
* @param ItemData[] $items
*/
public function withCustomFields(array $items)
{
$this->customFields = $items;
return $this->collection(
$this->getCustomFieldsForItem(ActionsInterface::ACCOUNT, $data->getId()),
new CustomFieldAdapter($this->configData)
);
}
/**
@@ -72,7 +64,7 @@ final class AccountAdapter extends TransformerAbstract
*
* @return array
*/
public function transform(AccountDetailsResponse $data)
public function transform(AccountDetailsResponse $data): array
{
$account = $data->getAccountVData();
@@ -109,7 +101,7 @@ final class AccountAdapter extends TransformerAbstract
'tags' => SelectItemAdapter::factory($data->getTags())->getItemsFromModel(),
'users' => SelectItemAdapter::factory($data->getUsers())->getItemsFromModel(),
'userGroups' => SelectItemAdapter::factory($data->getUserGroups())->getItemsFromModel(),
'customFields' => $this->customFields,
'customFields' => null,
'links' => [
[
'rel' => 'self',

View File

@@ -0,0 +1,52 @@
<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2020, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Adapters;
use League\Fractal\TransformerAbstract;
use SP\Config\ConfigData;
/**
* Class AdapterBase
*
* @package SP\Adapters
*/
abstract class AdapterBase extends TransformerAbstract
{
/**
* @var ConfigData
*/
protected $configData;
/**
* AccountAdapter constructor.
*
* @param ConfigData $configData
*/
public function __construct(ConfigData $configData)
{
$this->configData = $configData;
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2020, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Adapters;
use League\Fractal\Resource\Collection;
use SP\Core\Acl\ActionsInterface;
use SP\Core\Exceptions\SPException;
use SP\DataModel\CategoryData;
use SP\Mvc\Controller\ItemTrait;
use SP\Util\Link;
/**
* Class CategoryAdapter
*
* @package SP\Adapters
*/
final class CategoryAdapter extends AdapterBase
{
use ItemTrait;
protected $availableIncludes = [
'customFields'
];
/**
* @param CategoryData $data
*
* @return Collection
* @throws SPException
*/
public function includeCustomFields(CategoryData $data)
{
return $this->collection(
$this->getCustomFieldsForItem(ActionsInterface::CATEGORY, $data->id),
new CustomFieldAdapter($this->configData)
);
}
/**
* @param CategoryData $data
*
* @return array
*/
public function transform(CategoryData $data): array
{
return [
'id' => (int)$data->getId(),
'name' => $data->getName(),
'description' => $data->getDescription(),
'customFields' => null,
'links' => [
[
'rel' => 'self',
'uri' => Link::getDeepLink($data->getId(),
ActionsInterface::CATEGORY_VIEW,
$this->configData,
true)
]
],
];
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2020, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Adapters;
use League\Fractal\Resource\Collection;
use SP\Core\Acl\ActionsInterface;
use SP\Core\Exceptions\SPException;
use SP\DataModel\ClientData;
use SP\Mvc\Controller\ItemTrait;
use SP\Util\Link;
/**
* Class ClientAdapter
*
* @package SP\Adapters
*/
final class ClientAdapter extends AdapterBase
{
use ItemTrait;
protected $availableIncludes = [
'customFields'
];
/**
* @param ClientData $data
*
* @return Collection
* @throws SPException
*/
public function includeCustomFields(ClientData $data)
{
return $this->collection(
$this->getCustomFieldsForItem(ActionsInterface::CLIENT, $data->id),
new CustomFieldAdapter($this->configData)
);
}
/**
* @param ClientData $data
*
* @return array
*/
public function transform(ClientData $data): array
{
return [
'id' => (int)$data->getId(),
'name' => $data->getName(),
'description' => $data->getDescription(),
'global' => $data->isGlobal,
'customFields' => null,
'links' => [
[
'rel' => 'self',
'uri' => Link::getDeepLink($data->getId(),
ActionsInterface::CLIENT_VIEW,
$this->configData,
true)
]
],
];
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2020, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Adapters;
use SP\Services\CustomField\CustomFieldItem;
/**
* Class CustomFieldAdapter
*
* @package SP\Adapters
*/
final class CustomFieldAdapter extends AdapterBase
{
/**
* @param CustomFieldItem $data
*
* @return array
*/
public function transform(CustomFieldItem $data): array
{
return [
'type' => $data->typeName,
'typeText' => $data->typeText,
'definitionId' => $data->definitionId,
'definitionName' => $data->definitionName,
'help' => $data->help,
'value' => $data->value,
'encrypted' => $data->isEncrypted,
'required' => $data->required
];
}
}

View File

@@ -2,8 +2,8 @@
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2020, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
@@ -57,6 +57,7 @@ trait ItemTrait
* @throws ConstraintException
* @throws QueryException
* @throws ServiceException
* @throws SPException
*/
protected function getCustomFieldsForItem($moduleId, $itemId)
{
@@ -90,7 +91,7 @@ trait ItemTrait
$customFields[] = $customField;
} catch (CryptoException $e) {
logger($e->getMessage());
throw new SPException(__u('Internal error'), SPException::ERROR);
}
}

View File

@@ -63,6 +63,8 @@ final class AuthTokenService extends Service
const CAN_USE_SECURE_TOKEN_ACTIONS = [
ActionsInterface::ACCOUNT_VIEW,
ActionsInterface::CATEGORY_VIEW,
ActionsInterface::CLIENT_VIEW,
];
/**