Skip to content

Commit

Permalink
Merge pull request #514 from jan-stanek/oprava-roli
Browse files Browse the repository at this point in the history
Oprava chyb
  • Loading branch information
Jan Staněk authored Feb 28, 2018
2 parents 9cec954 + 005b74a commit ce977ed
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
54 changes: 37 additions & 17 deletions app/InstallModule/presenters/InstallPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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');
Expand Down Expand Up @@ -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');
}
Expand All @@ -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);

Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion app/WebModule/presenters/WebBasePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 21 additions & 7 deletions app/model/Settings/SettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions app/services/ApplicationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})) {
Expand Down

0 comments on commit ce977ed

Please sign in to comment.