diff --git a/app/InstallModule/presenters/InstallPresenter.php b/app/InstallModule/presenters/InstallPresenter.php index 38d325ab2..08bee1e79 100644 --- a/app/InstallModule/presenters/InstallPresenter.php +++ b/app/InstallModule/presenters/InstallPresenter.php @@ -3,10 +3,17 @@ namespace App\InstallModule\Presenters; use App\Model\ACL\Role; +use App\Model\ACL\RoleRepository; use App\Model\Settings\Settings; use App\Model\Settings\SettingsException; +use App\Model\Settings\SettingsRepository; +use App\Model\Structure\SubeventRepository; +use App\Model\User\UserRepository; +use App\Services\ApplicationService; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\DBAL\Exception\TableNotFoundException; use Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand; +use Kdyby\Console\Application; use Kdyby\Console\StringOutput; use Skautis\Config; use Skautis\Skautis; @@ -26,29 +33,41 @@ class InstallPresenter extends InstallBasePresenter { /** - * @var \Kdyby\Console\Application + * @var Application * @inject */ public $application; /** - * @var \App\Model\Settings\SettingsRepository + * @var SettingsRepository * @inject */ public $settingsRepository; /** - * @var \App\Model\ACL\RoleRepository + * @var RoleRepository * @inject */ public $roleRepository; /** - * @var \App\Model\User\UserRepository + * @var UserRepository * @inject */ public $userRepository; + /** + * @var SubeventRepository + * @inject + */ + public $subeventRepository; + + /** + * @var ApplicationService + * @inject + */ + public $applicationService; + /** * Zobrazení první stránky průvodce. @@ -61,7 +80,7 @@ public function renderDefault() } try { - if (filter_var($this->settingsRepository->getValue(Settings::ADMIN_CREATED), FILTER_VALIDATE_BOOLEAN)) { + if ($this->settingsRepository->getBoolValue(Settings::ADMIN_CREATED)) { $this->redirect('installed'); } $this->flashMessage('install.schema.schema_already_created', 'info'); @@ -95,13 +114,13 @@ public function handleImportSchema() /** * Zobrazení stránky pro vytvoření administrátora. - * @throws SettingsException * @throws \Nette\Application\AbortException + * @throws \Throwable */ public function renderAdmin() { try { - if (filter_var($this->settingsRepository->getValue(Settings::ADMIN_CREATED), FILTER_VALIDATE_BOOLEAN)) { + if ($this->settingsRepository->getBoolValue(Settings::ADMIN_CREATED)) { $this->flashMessage('install.admin.admin_already_created', 'info'); $this->redirect('finish'); } @@ -112,17 +131,18 @@ public function renderAdmin() } if ($this->user->isLoggedIn()) { - $user = $this->userRepository->findById($this->user->id); - - $nonregisteredRole = $this->roleRepository->findBySystemName(Role::NONREGISTERED); - $user->removeRole($nonregisteredRole); + $this->userRepository->getEntityManager()->transactional(function ($em) { + $user = $this->userRepository->findById($this->user->id); + $this->userRepository->save($user); - $adminRole = $this->roleRepository->findBySystemName(Role::ADMIN); - $user->addRole($adminRole); + $adminRole = $this->roleRepository->findBySystemName(Role::ADMIN); + $implicitSubevent = $this->subeventRepository->findImplicit(); - $this->settingsRepository->setValue(Settings::ADMIN_CREATED, TRUE); + $this->applicationService->register($user, new ArrayCollection([$adminRole]), + new ArrayCollection([$implicitSubevent]), $user, TRUE); - $this->userRepository->save($user); + $this->settingsRepository->setValue(Settings::ADMIN_CREATED, TRUE); + }); $this->user->logout(TRUE); @@ -150,7 +170,7 @@ public function handleCreateAdmin() public function renderFinish() { try { - if (!filter_var($this->settingsRepository->getValue(Settings::ADMIN_CREATED), FILTER_VALIDATE_BOOLEAN)) + if (!$this->settingsRepository->getBoolValue(Settings::ADMIN_CREATED)) $this->redirect('default'); } catch (TableNotFoundException $ex) { $this->redirect('default'); @@ -166,7 +186,7 @@ public function renderFinish() public function renderInstalled() { try { - if (!filter_var($this->settingsRepository->getValue(Settings::ADMIN_CREATED), FILTER_VALIDATE_BOOLEAN)) + if (!$this->settingsRepository->getBoolValue(Settings::ADMIN_CREATED)) $this->redirect('default'); } catch (TableNotFoundException $ex) { $this->redirect('default'); diff --git a/app/WebModule/presenters/WebBasePresenter.php b/app/WebModule/presenters/WebBasePresenter.php index 2cad76c05..027310eb5 100644 --- a/app/WebModule/presenters/WebBasePresenter.php +++ b/app/WebModule/presenters/WebBasePresenter.php @@ -164,7 +164,7 @@ public function actionExitRoleTest() private function checkInstallation() { try { - if (!filter_var($this->settingsRepository->getValue(Settings::ADMIN_CREATED), FILTER_VALIDATE_BOOLEAN)) + if (!$this->settingsRepository->getBoolValue(Settings::ADMIN_CREATED)) $this->redirect(':Install:Install:default'); else $this->databaseService->update(); diff --git a/app/model/Settings/SettingsRepository.php b/app/model/Settings/SettingsRepository.php index efb77449c..b56a41287 100644 --- a/app/model/Settings/SettingsRepository.php +++ b/app/model/Settings/SettingsRepository.php @@ -39,7 +39,7 @@ public function __construct(EntityManager $em, Mapping\ClassMetadata $class, ISt * @return mixed * @throws SettingsException */ - public function getValue($item) + public function getValue($item): ?string { $value = $this->cache->load($item); if ($value !== NULL) @@ -61,7 +61,7 @@ public function getValue($item) * @param $value * @throws SettingsException */ - public function setValue($item, $value) + public function setValue($item, $value): void { $settings = $this->findOneBy(['item' => $item]); if ($settings === NULL) @@ -73,13 +73,27 @@ public function setValue($item, $value) $this->cache->save($item, $value); } + /** + * Vrátí hodnotu položky typu bool. + * @param $item + * @return bool|null + * @throws SettingsException + */ + public function getBoolValue($item): ?bool + { + $value = $this->getValue($item); + if ($value === NULL) + return NULL; + return filter_var($value, FILTER_VALIDATE_BOOLEAN); + } + /** * Vrátí hodnotu položky typu datum a čas. * @param $item * @return \DateTime|null * @throws SettingsException */ - public function getDateTimeValue($item) + public function getDateTimeValue($item): ?\DateTime { $value = $this->getValue($item); if ($value === NULL) @@ -107,7 +121,7 @@ public function getDateTimeValueText($item): ?string * @param \DateTime|null $value * @throws SettingsException */ - public function setDateTimeValue($item, $value) + public function setDateTimeValue($item, $value): void { if ($value === NULL) $this->setValue($item, NULL); @@ -118,10 +132,10 @@ public function setDateTimeValue($item, $value) /** * Vrátí hodnotu položky typu datum. * @param $item - * @return \DateTime + * @return null|\DateTime * @throws SettingsException */ - public function getDateValue($item) + public function getDateValue($item): ?\DateTime { $value = $this->getValue($item); if ($value === NULL) @@ -149,7 +163,7 @@ public function getDateValueText($item): ?string * @param \DateTime|null $value * @throws SettingsException */ - public function setDateValue($item, $value) + public function setDateValue($item, $value): void { if ($value === NULL) $this->setValue($item, NULL); diff --git a/app/services/ApplicationService.php b/app/services/ApplicationService.php index e693b7d1b..664eb094e 100644 --- a/app/services/ApplicationService.php +++ b/app/services/ApplicationService.php @@ -188,13 +188,13 @@ public function updateRoles(User $user, Collection $roles, ?User $createdBy, boo } $this->applicationRepository->getEntityManager()->transactional(function ($em) use ($user, $roles, $createdBy, $approve, $oldRoles) { - $user->setRoles($roles); - $this->userRepository->save($user); - if ($oldRoles->contains($this->roleRepository->findBySystemName(Role::NONREGISTERED))) { $this->createRolesApplication($user, $roles, $createdBy, $approve); $this->createSubeventsApplication($user, new ArrayCollection([$this->subeventRepository->findImplicit()]), $createdBy); } else { + $user->setRoles($roles); + $this->userRepository->save($user); + if ($roles->forAll(function (int $key, Role $role) { return $role->isApprovedAfterRegistration(); })) {