diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index a98a10f0..d4e82fc6 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -9,6 +9,7 @@ use b8\Config; use b8\Store\Factory; use PHPCensor\Model\ProjectGroup; use PHPCensor\Store\UserStore; +use PHPCensor\Store\ProjectGroupStore; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; @@ -474,7 +475,12 @@ class InstallCommand extends Command { try { /** @var UserStore $userStore */ - $userStore = Factory::getStore('User'); + $userStore = Factory::getStore('User'); + $adminUser = $userStore->getByEmail($admin['email']); + if ($adminUser) { + throw new \RuntimeException('Admin account already exists!'); + } + $userService = new UserService($userStore); $userService->createUser($admin['name'], $admin['email'], 'internal', json_encode(['type' => 'internal']), $admin['password'], true); @@ -491,6 +497,13 @@ class InstallCommand extends Command protected function createDefaultGroup($output) { try { + /** @var ProjectGroupStore $projectGroupStore */ + $projectGroupStore = Factory::getStore('ProjectGroup'); + $projectGroup = $projectGroupStore->getByTitle('Projects'); + if ($projectGroup) { + throw new \RuntimeException('Default project group already exists!'); + } + $group = new ProjectGroup(); $group->setTitle('Projects'); diff --git a/src/PHPCensor/Store/Base/ProjectGroupStoreBase.php b/src/PHPCensor/Store/Base/ProjectGroupStoreBase.php index c4c2b393..a8aed1a8 100644 --- a/src/PHPCensor/Store/Base/ProjectGroupStoreBase.php +++ b/src/PHPCensor/Store/Base/ProjectGroupStoreBase.php @@ -30,7 +30,13 @@ class ProjectGroupStoreBase extends Store /** * Get a single ProjectGroup by Id. - * @return null|ProjectGroup + * + * @param integer $value + * @param string $useConnection + * + * @return ProjectGroup|null + * + * @throws HttpException */ public function getById($value, $useConnection = 'read') { @@ -39,7 +45,8 @@ class ProjectGroupStoreBase extends Store } $query = 'SELECT * FROM {{project_group}} WHERE {{id}} = :id LIMIT 1'; - $stmt = Database::getConnection($useConnection)->prepareCommon($query); + $stmt = Database::getConnection($useConnection)->prepareCommon($query); + $stmt->bindValue(':id', $value); if ($stmt->execute()) { @@ -50,4 +57,34 @@ class ProjectGroupStoreBase extends Store return null; } + + /** + * Get a single ProjectGroup by title. + * + * @param integer $value + * @param string $useConnection + * + * @return ProjectGroup|null + * + * @throws HttpException + */ + public function getByTitle($value, $useConnection = 'read') + { + if (is_null($value)) { + throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); + } + + $query = 'SELECT * FROM {{project_group}} WHERE {{title}} = :title LIMIT 1'; + $stmt = Database::getConnection($useConnection)->prepareCommon($query); + + $stmt->bindValue(':title', $value); + + if ($stmt->execute()) { + if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) { + return new ProjectGroup($data); + } + } + + return null; + } } diff --git a/src/PHPCensor/Store/Base/UserStoreBase.php b/src/PHPCensor/Store/Base/UserStoreBase.php index edbbb507..e8ab3f35 100644 --- a/src/PHPCensor/Store/Base/UserStoreBase.php +++ b/src/PHPCensor/Store/Base/UserStoreBase.php @@ -68,7 +68,8 @@ class UserStoreBase extends Store } $query = 'SELECT * FROM {{user}} WHERE {{email}} = :email LIMIT 1'; - $stmt = Database::getConnection()->prepareCommon($query); + $stmt = Database::getConnection()->prepareCommon($query); + $stmt->bindValue(':email', $value); if ($stmt->execute()) {