* [MOD] Use static types.

* [MOD] CORS headers.
* [MOD] Improve Forwarded header lookup.
* [ADD] Create interface for configuration data.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2021-10-12 20:49:18 +02:00
parent 5424084a52
commit 58249da565
638 changed files with 21764 additions and 22315 deletions

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2020, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -19,17 +19,17 @@
* 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/>.
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Modules\Web\Controllers;
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use SP\Core\Acl\Acl;
use SP\Core\Acl\ActionsInterface;
use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
use SP\Core\Exceptions\ConstraintException;
@@ -48,7 +48,6 @@ use SP\Mvc\Controller\ItemTrait;
use SP\Mvc\View\Components\SelectItemAdapter;
use SP\Services\Auth\AuthException;
use SP\Services\Mail\MailService;
use SP\Services\ServiceException;
use SP\Services\User\UserService;
use SP\Services\UserGroup\UserGroupService;
use SP\Services\UserPassRecover\UserPassRecoverService;
@@ -64,29 +63,32 @@ final class UserController extends ControllerBase implements CrudControllerInter
use JsonTrait;
use ItemTrait;
/**
* @var UserService
*/
protected $userService;
protected ?UserService $userService = null;
/**
* Search action
*
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws ConstraintException
* @throws QueryException
* @throws SPException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
* @throws \SP\Core\Exceptions\ConstraintException
* @throws \SP\Core\Exceptions\QueryException
*/
public function searchAction()
public function searchAction(): bool
{
if (!$this->acl->checkUserAccess(Acl::USER_SEARCH)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_SEARCH)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->addTemplate('datagrid-table', 'grid');
$this->view->assign('index', $this->request->analyzeInt('activetab', 0));
$this->view->assign(
'index',
$this->request->analyzeInt('activetab', 0)
);
$this->view->assign('data', $this->getSearchGrid());
return $this->returnJsonResponseData(['html' => $this->render()]);
@@ -102,23 +104,33 @@ final class UserController extends ControllerBase implements CrudControllerInter
*/
protected function getSearchGrid(): DataGridInterface
{
$itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
$itemSearchData = $this->getSearchData(
$this->configData->getAccountCount(),
$this->request
);
$userGrid = $this->dic->get(UserGrid::class);
return $userGrid->updatePager($userGrid->getGrid($this->userService->search($itemSearchData)), $itemSearchData);
return $userGrid->updatePager(
$userGrid->getGrid($this->userService->search($itemSearchData)),
$itemSearchData
);
}
/**
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
*/
public function createAction()
public function createAction(): bool
{
try {
if (!$this->acl->checkUserAccess(Acl::USER_CREATE)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_CREATE)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->assign('header', __('New User'));
@@ -127,13 +139,19 @@ final class UserController extends ControllerBase implements CrudControllerInter
$this->setViewData();
$this->eventDispatcher->notifyEvent('show.user.create', new Event($this));
$this->eventDispatcher->notifyEvent(
'show.user.create',
new Event($this)
);
return $this->returnJsonResponseData(['html' => $this->render()]);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
$this->eventDispatcher->notifyEvent(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
}
@@ -147,49 +165,81 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @throws SPException
* @throws ContainerExceptionInterface
*/
protected function setViewData(?int $userId = null)
protected function setViewData(?int $userId = null): void
{
$this->view->addTemplate('user', 'itemshow');
$user = $userId ? $this->userService->getById($userId) : new UserData();
$user = $userId
? $this->userService->getById($userId)
: new UserData();
$this->view->assign('user', $user);
$this->view->assign('groups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
$this->view->assign('profiles', SelectItemAdapter::factory(UserProfileService::getItemsBasic())->getItemsFromModel());
$this->view->assign('isUseSSO', $this->configData->isAuthBasicAutoLoginEnabled());
$this->view->assign('mailEnabled', $this->configData->isMailEnabled());
$this->view->assign('nextAction', Acl::getActionRoute(Acl::ACCESS_MANAGE));
$this->view->assign(
'groups',
SelectItemAdapter::factory(UserGroupService::getItemsBasic())
->getItemsFromModel()
);
$this->view->assign(
'profiles',
SelectItemAdapter::factory(UserProfileService::getItemsBasic())
->getItemsFromModel()
);
$this->view->assign(
'isUseSSO',
$this->configData->isAuthBasicAutoLoginEnabled()
);
$this->view->assign(
'mailEnabled',
$this->configData->isMailEnabled()
);
$this->view->assign(
'nextAction',
Acl::getActionRoute(ActionsInterface::ACCESS_MANAGE)
);
if ($this->view->isView === true
|| ($this->configData->isDemoEnabled() && $user->getLogin() === 'demo')
|| ($this->configData->isDemoEnabled()
&& $user->getLogin() === 'demo')
) {
$this->view->assign('disabled', 'disabled');
$this->view->assign('readonly', 'readonly');
$this->view->assign('usage', array_map(function ($value) {
switch ($value->ref) {
case 'Account':
$value->icon = 'description';
break;
case 'UserGroup':
$value->icon = 'group';
break;
case 'PublicLink':
$value->icon = 'link';
break;
default:
$value->icon = 'info_outline';
}
$this->view->assign(
'usage',
array_map(
static function ($value) {
switch ($value->ref) {
case 'Account':
$value->icon = 'description';
break;
case 'UserGroup':
$value->icon = 'group';
break;
case 'PublicLink':
$value->icon = 'link';
break;
default:
$value->icon = 'info_outline';
}
return $value;
}, $this->userService->getUsageForUser($userId)));
return $value;
},
$this->userService->getUsageForUser($userId)
)
);
} else {
$this->view->assign('disabled', false);
$this->view->assign('readonly', false);
}
$this->view->assign('showViewCustomPass', $this->acl->checkUserAccess(Acl::CUSTOMFIELD_VIEW_PASS));
$this->view->assign('customFields', $this->getCustomFieldsForItem(Acl::USER, $userId));
$this->view->assign(
'showViewCustomPass',
$this->acl->checkUserAccess(ActionsInterface::CUSTOMFIELD_VIEW_PASS)
);
$this->view->assign(
'customFields',
$this->getCustomFieldsForItem(ActionsInterface::USER, $userId)
);
}
/**
@@ -198,14 +248,18 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @param int $id
*
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
*/
public function editAction(int $id)
public function editAction(int $id): bool
{
try {
if (!$this->acl->checkUserAccess(Acl::USER_EDIT)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_EDIT)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->assign('header', __('Edit User'));
@@ -214,13 +268,19 @@ final class UserController extends ControllerBase implements CrudControllerInter
$this->setViewData($id);
$this->eventDispatcher->notifyEvent('show.user.edit', new Event($this));
$this->eventDispatcher->notifyEvent(
'show.user.edit',
new Event($this)
);
return $this->returnJsonResponseData(['html' => $this->render()]);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
$this->eventDispatcher->notifyEvent(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
}
@@ -232,15 +292,19 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @param int $id
*
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
*/
public function editPassAction(int $id)
public function editPassAction(int $id): bool
{
try {
// Comprobar si el usuario a modificar es distinto al de la sesión
if (!$this->acl->checkUserAccess(Acl::USER_EDIT_PASS, $id)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_EDIT_PASS, $id)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->addTemplate('user_pass', 'itemshow');
@@ -249,17 +313,25 @@ final class UserController extends ControllerBase implements CrudControllerInter
$this->view->assign('isView', false);
$this->view->assign('route', 'user/saveEditPass/' . $id);
$user = $id ? $this->userService->getById($id) : new UserData();
$user = $id
? $this->userService->getById($id)
: new UserData();
$this->view->assign('user', $user);
$this->eventDispatcher->notifyEvent('show.user.editPass', new Event($this));
$this->eventDispatcher->notifyEvent(
'show.user.editPass',
new Event($this)
);
return $this->returnJsonResponseData(['html' => $this->render()]);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
$this->eventDispatcher->notifyEvent(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
}
@@ -271,47 +343,74 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @param int|null $id
*
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
*/
public function deleteAction(?int $id = null)
public function deleteAction(?int $id = null): bool
{
try {
if (!$this->acl->checkUserAccess(Acl::USER_DELETE)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_DELETE)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
if ($id === null) {
$this->userService->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
$this->userService
->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
$this->eventDispatcher->notifyEvent(
'delete.user.selection',
new Event($this, EventMessage::factory()
->addDescription(__u('Users deleted'))
->setExtra('userId', $this->getItemsIdFromRequest($this->request)))
new Event(
$this,
EventMessage::factory()
->addDescription(__u('Users deleted'))
->setExtra('userId', $this->getItemsIdFromRequest($this->request))
)
);
$this->deleteCustomFieldsForItem(Acl::USER, $id);
$this->deleteCustomFieldsForItem(
ActionsInterface::USER,
$id
);
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Users deleted'));
} else {
$this->userService->delete($id);
return $this->returnJsonResponse(
JsonResponse::JSON_SUCCESS,
__u('Users deleted')
);
}
$this->deleteCustomFieldsForItem(Acl::USER, $id);
$this->userService->delete($id);
$this->eventDispatcher->notifyEvent('delete.user',
new Event($this, EventMessage::factory()
$this->deleteCustomFieldsForItem(
ActionsInterface::USER,
$id
);
$this->eventDispatcher->notifyEvent(
'delete.user',
new Event(
$this,
EventMessage::factory()
->addDescription(__u('User deleted'))
->addDetail(__u('User'), $id)
->addExtra('userId', $id))
);
->addExtra('userId', $id)
)
);
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('User deleted'));
}
return $this->returnJsonResponse(
JsonResponse::JSON_SUCCESS,
__u('User deleted')
);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
$this->eventDispatcher->notifyEvent(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
}
@@ -319,14 +418,18 @@ final class UserController extends ControllerBase implements CrudControllerInter
/**
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
*/
public function saveCreateAction()
{
try {
if (!$this->acl->checkUserAccess(Acl::USER_CREATE)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_CREATE)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$form = new UserForm($this->dic);
@@ -336,23 +439,37 @@ final class UserController extends ControllerBase implements CrudControllerInter
$id = $this->userService->create($itemData);
$this->eventDispatcher->notifyEvent('create.user',
new Event($this, EventMessage::factory()
->addDescription(__u('User added'))
->addDetail(__u('User'), $itemData->getName()))
$this->eventDispatcher->notifyEvent(
'create.user',
new Event(
$this,
EventMessage::factory()
->addDescription(__u('User added'))
->addDetail(__u('User'), $itemData->getName())
)
);
$this->addCustomFieldsForItem(Acl::USER, $id, $this->request);
$this->addCustomFieldsForItem(
ActionsInterface::USER,
$id,
$this->request
);
$this->checkChangeUserPass($id, $itemData);
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('User added'));
return $this->returnJsonResponse(
JsonResponse::JSON_SUCCESS,
__u('User added')
);
} catch (ValidationException $e) {
return $this->returnJsonResponseException($e);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
$this->eventDispatcher->notifyEvent(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
}
@@ -362,21 +479,26 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @param int $userId
* @param UserData $userData
*
* @throws DependencyException
* @throws NotFoundException
* @throws EnvironmentIsBrokenException
* @throws ConstraintException
* @throws QueryException
* @throws ServiceException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
* @throws \PHPMailer\PHPMailer\Exception
* @throws \SP\Core\Exceptions\ConstraintException
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Services\ServiceException
*/
protected function checkChangeUserPass(int $userId, UserData $userData)
protected function checkChangeUserPass(int $userId, UserData $userData): void
{
if ($userData->isChangePass()) {
$hash = $this->dic->get(UserPassRecoverService::class)
->requestForUserId($userId);
$this->dic->get(MailService::class)
->send(__('Password Change'), $userData->getEmail(), UserPassRecoverService::getMailMessage($hash));
->send(
__('Password Change'),
$userData->getEmail(),
UserPassRecoverService::getMailMessage($hash)
);
}
}
@@ -386,41 +508,59 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @param int $id
*
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
*/
public function saveEditAction(int $id)
public function saveEditAction(int $id): bool
{
try {
if (!$this->acl->checkUserAccess(Acl::USER_EDIT)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_EDIT)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$form = new UserForm($this->dic, $id);
$form->validate(Acl::USER_EDIT);
$form->validate(ActionsInterface::USER_EDIT);
$itemData = $form->getItemData();
$this->userService->update($itemData);
$this->eventDispatcher->notifyEvent('edit.user',
new Event($this, EventMessage::factory()
->addDescription(__u('User updated'))
->addDetail(__u('User'), $itemData->getName())
->addExtra('userId', $id))
$this->eventDispatcher->notifyEvent(
'edit.user',
new Event(
$this,
EventMessage::factory()
->addDescription(__u('User updated'))
->addDetail(__u('User'), $itemData->getName())
->addExtra('userId', $id)
)
);
$this->updateCustomFieldsForItem(Acl::USER, $id, $this->request);
$this->updateCustomFieldsForItem(
ActionsInterface::USER,
$id,
$this->request
);
$this->checkChangeUserPass($id, $itemData);
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('User updated'));
return $this->returnJsonResponse(
JsonResponse::JSON_SUCCESS,
__u('User updated')
);
} catch (ValidationException $e) {
return $this->returnJsonResponseException($e);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
$this->eventDispatcher->notifyEvent(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
}
@@ -432,36 +572,50 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @param int $id
*
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
*/
public function saveEditPassAction(int $id)
public function saveEditPassAction(int $id): bool
{
try {
if (!$this->acl->checkUserAccess(Acl::USER_EDIT_PASS, $id)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_EDIT_PASS, $id)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$form = new UserForm($this->dic, $id);
$form->validate(Acl::USER_EDIT_PASS);
$form->validate(ActionsInterface::USER_EDIT_PASS);
$itemData = $form->getItemData();
$this->userService->updatePass($id, $itemData->getPass());
$this->eventDispatcher->notifyEvent('edit.user.pass',
new Event($this, EventMessage::factory()
->addDescription(__u('Password updated'))
->addDetail(__u('User'), $id))
$this->eventDispatcher->notifyEvent(
'edit.user.pass',
new Event(
$this,
EventMessage::factory()
->addDescription(__u('Password updated'))
->addDetail(__u('User'), $id)
)
);
return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Password updated'));
return $this->returnJsonResponse(
JsonResponse::JSON_SUCCESS,
__u('Password updated')
);
} catch (ValidationException $e) {
return $this->returnJsonResponseException($e);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
$this->eventDispatcher->notifyEvent(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
}
@@ -473,14 +627,18 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @param int $id
*
* @return bool
* @throws DependencyException
* @throws NotFoundException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \JsonException
*/
public function viewAction(int $id)
public function viewAction(int $id): bool
{
try {
if (!$this->acl->checkUserAccess(Acl::USER_VIEW)) {
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
if (!$this->acl->checkUserAccess(ActionsInterface::USER_VIEW)) {
return $this->returnJsonResponse(
JsonResponse::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->assign('header', __('View User'));
@@ -488,13 +646,19 @@ final class UserController extends ControllerBase implements CrudControllerInter
$this->setViewData($id);
$this->eventDispatcher->notifyEvent('show.user', new Event($this));
$this->eventDispatcher->notifyEvent(
'show.user',
new Event($this)
);
return $this->returnJsonResponseData(['html' => $this->render()]);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notifyEvent('exception', new Event($e));
$this->eventDispatcher->notifyEvent(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
}
@@ -506,7 +670,7 @@ final class UserController extends ControllerBase implements CrudControllerInter
* @throws NotFoundException
* @throws SessionTimeout
*/
protected function initialize()
protected function initialize(): void
{
$this->checkLoggedIn();