Commit 83834bbc authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Merge branch '13-make-database-seeders' into 'master'

Resolve "Make database seeders"

Closes #13

See merge request kretschmar/doorcode!11
parents ad50fc98 b64bac64
Pipeline #2238 passed with stages
in 1 minute and 53 seconds
......@@ -9,8 +9,9 @@ class DatabaseSeeder extends Seeder
*
* @return void
*/
public function run()
public function run(): void
{
// $this->call(UsersTableSeeder::class);
$this->call(GroupsSeeder::class);
$this->call(UsersSeeder::class);
}
}
<?php
use Illuminate\Database\Seeder;
use Source\Gateways\Groups\LocalGroupsRepository;
use Source\Gateways\Groups\DatabaseGroupsRepository;
class GroupsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @param \Source\Gateways\Groups\DatabaseGroupsRepository $groups
* @return void
*/
public function run(DatabaseGroupsRepository $groups)
{
if ($groups->all()) {
throw new RuntimeException('Cannot seed database as it is not empty.');
}
$groups->create(LocalGroupsRepository::getAdminGroup());
$groups->create(LocalGroupsRepository::getManageUsersGroup());
$groups->create(LocalGroupsRepository::getManageDoorsGroup());
$groups->create(LocalGroupsRepository::getManageTokenGroup());
$groups->create(LocalGroupsRepository::getTokenCreateGroup());
$groups->create(LocalGroupsRepository::getCodeQueryGroup());
$groups->create(LocalGroupsRepository::getManageGroupsGroup());
}
}
<?php
use Source\Entities\User;
use Illuminate\Database\Seeder;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\GroupUser\DatabaseGroupUserRepository;
class UsersSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @param \Source\Gateways\Users\DatabaseUsersRepository $users
* @param \Source\Gateways\GroupUser\DatabaseGroupUserRepository $repository
* @return void
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function run(DatabaseUsersRepository $users, DatabaseGroupUserRepository $repository)
{
if ($users->all()) {
throw new RuntimeException('Cannot seed database as it not empty!');
}
$user = $users->create(new User(
0,
'Admin',
'Admin',
'Admin User',
null,
'admin@admin.user',
'Default Admin Password',
null
));
if (!$user) {
throw new RuntimeException('Could not create the admin user');
}
$repository->addUserToGroup($user->getId(), 1);
}
}
......@@ -85,8 +85,12 @@ class DatabaseUsersRepository implements UsersRepository
return $dbUser;
}
protected function secureDoorcode(string $doorcode): string
protected function secureDoorcode(?string $doorcode): ?string
{
if (!$doorcode) {
return null;
}
// As of PHP 7 the salt parameter to password_hash has been depreciated.
// As we need to be able to search for users by doorcode they either all need to have the same salt
// (in this case, the app key) or no salt (I'd prefer a salt). This way the doorcode can be hashed and
......@@ -119,7 +123,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function delete(string $userId): bool
{
\App\User::destroy($userId);
return \App\User::destroy($userId);
}
/**
......@@ -153,9 +157,9 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function findByDoorcode(string $doorcode): ?User
{
$doorcode = $this->secureDoorcode($doorcode);
$doorcodeSearch = $this->secureDoorcode($doorcode);
$user = \App\User::where('doorcode', $doorcode)->first();
$user = \App\User::where('doorcode', $doorcodeSearch)->first();
if (!$user) {
return null;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment