From 62ed77ab25f8f2cf5103caf572079ebc2b50f6bc Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 28 Jul 2019 15:33:01 +0200 Subject: Groups: Fix Naming and IDs --- ..._21_000001_fix_old_groups_table_id_and_name.php | 74 ++++++++++++++++++++++ includes/pages/admin_groups.php | 4 +- includes/view/User_view.php | 2 +- src/Helpers/Authenticator.php | 21 +++++- src/Helpers/AuthenticatorServiceProvider.php | 1 + .../Helpers/AuthenticatorServiceProviderTest.php | 2 + tests/Unit/Helpers/AuthenticatorTest.php | 14 +++- 7 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 db/migrations/2019_07_21_000001_fix_old_groups_table_id_and_name.php diff --git a/db/migrations/2019_07_21_000001_fix_old_groups_table_id_and_name.php b/db/migrations/2019_07_21_000001_fix_old_groups_table_id_and_name.php new file mode 100644 index 00000000..044ba132 --- /dev/null +++ b/db/migrations/2019_07_21_000001_fix_old_groups_table_id_and_name.php @@ -0,0 +1,74 @@ + 'Guest', + '2-Engel' => 'Angel', + 'Shirt-Manager' => 'Shirt Manager', + '3-Shift Coordinator' => 'Shift Coordinator', + '4-Team Coordinator' => 'Team Coordinator', + '5-Bürokrat' => 'Bureaucrat', + '6-Developer' => 'Developer', + ]; + + /** @var array */ + protected $ids = [ + -65 => -80, + -70 => -90, + ]; + + /** + * Run the migration + */ + public function up() + { + $this->migrate($this->naming, $this->ids); + } + + /** + * Reverse the migration + */ + public function down() + { + $this->migrate(array_flip($this->naming), array_flip($this->ids)); + } + + /** + * @param array $naming + * @param array $ids + */ + protected function migrate($naming, $ids) + { + if (!$this->schema->hasTable('Groups')) { + return; + } + + $connection = $this->schema->getConnection(); + foreach ($connection->table('Groups')->get() as $data) { + if (isset($naming[$data->Name])) { + $data->Name = $naming[$data->Name]; + } + + $data->oldId = $data->UID; + if (isset($ids[$data->oldId])) { + $data->UID = $ids[$data->oldId]; + } elseif (isset($ids[$data->oldId * -1])) { + $data->UID = $ids[$data->oldId * -1] * -1; + } + + $connection + ->table('Groups') + ->where('UID', $data->oldId) + ->update([ + 'UID' => $data->UID * -1, + 'Name' => $data->Name, + ]); + } + } +} diff --git a/includes/pages/admin_groups.php b/includes/pages/admin_groups.php index 6ba2ceaf..5127041d 100644 --- a/includes/pages/admin_groups.php +++ b/includes/pages/admin_groups.php @@ -57,7 +57,7 @@ function admin_groups() } else { switch ($request->input('action')) { case 'edit': - if ($request->has('id') && preg_match('/^-\d{1,11}$/', $request->input('id'))) { + if ($request->has('id') && preg_match('/^\d{1,11}$/', $request->input('id'))) { $group_id = $request->input('id'); } else { return error('Incomplete call, missing Groups ID.', true); @@ -113,7 +113,7 @@ function admin_groups() case 'save': if ( $request->has('id') - && preg_match('/^-\d{1,11}$/', $request->input('id')) + && preg_match('/^\d{1,11}$/', $request->input('id')) && $request->hasPostData('submit') ) { $group_id = $request->input('id'); diff --git a/includes/view/User_view.php b/includes/view/User_view.php index c8d60994..b8b875e4 100644 --- a/includes/view/User_view.php +++ b/includes/view/User_view.php @@ -853,7 +853,7 @@ function User_groups_render($user_groups) { $output = []; foreach ($user_groups as $group) { - $groupName = preg_replace('/(^\d+-)/', '', $group['Name']); + $groupName = $group['Name']; $output[] = __($groupName); } diff --git a/src/Helpers/Authenticator.php b/src/Helpers/Authenticator.php index db33339b..a7fbe8c7 100644 --- a/src/Helpers/Authenticator.php +++ b/src/Helpers/Authenticator.php @@ -28,6 +28,9 @@ class Authenticator /** @var int */ protected $passwordAlgorithm = PASSWORD_DEFAULT; + /** @var int */ + protected $guestRole = 10; + /** * @param ServerRequestInterface $request * @param Session $session @@ -119,7 +122,7 @@ class Authenticator } if (empty($this->permissions)) { - $this->permissions = $this->getPermissionsByGroup(-10); + $this->permissions = $this->getPermissionsByGroup($this->guestRole); } } @@ -204,6 +207,22 @@ class Authenticator $this->passwordAlgorithm = $passwordAlgorithm; } + /** + * @return int + */ + public function getGuestRole() + { + return $this->guestRole; + } + + /** + * @param int $guestRole + */ + public function setGuestRole(int $guestRole) + { + $this->guestRole = $guestRole; + } + /** * @param User $user * @return array diff --git a/src/Helpers/AuthenticatorServiceProvider.php b/src/Helpers/AuthenticatorServiceProvider.php index f06e635d..3534618d 100644 --- a/src/Helpers/AuthenticatorServiceProvider.php +++ b/src/Helpers/AuthenticatorServiceProvider.php @@ -14,6 +14,7 @@ class AuthenticatorServiceProvider extends ServiceProvider /** @var Authenticator $authenticator */ $authenticator = $this->app->make(Authenticator::class); $authenticator->setPasswordAlgorithm($config->get('password_algorithm')); + $authenticator->setGuestRole($config->get('auth_guest_role', $authenticator->getGuestRole())); $this->app->instance(Authenticator::class, $authenticator); $this->app->instance('authenticator', $authenticator); diff --git a/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php b/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php index ab9b23ec..175d720e 100644 --- a/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php +++ b/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php @@ -22,6 +22,7 @@ class AuthenticatorServiceProviderTest extends ServiceProviderTest $config = new Config(); $config->set('password_algorithm', PASSWORD_DEFAULT); + $config->set('auth_guest_role', 42); $app->instance('config', $config); $serviceProvider = new AuthenticatorServiceProvider($app); @@ -34,5 +35,6 @@ class AuthenticatorServiceProviderTest extends ServiceProviderTest /** @var Authenticator $auth */ $auth = $app->get(Authenticator::class); $this->assertEquals(PASSWORD_DEFAULT, $auth->getPasswordAlgorithm()); + $this->assertEquals(42, $auth->getGuestRole()); } } diff --git a/tests/Unit/Helpers/AuthenticatorTest.php b/tests/Unit/Helpers/AuthenticatorTest.php index 83dc72ad..d601a77f 100644 --- a/tests/Unit/Helpers/AuthenticatorTest.php +++ b/tests/Unit/Helpers/AuthenticatorTest.php @@ -132,7 +132,7 @@ class AuthenticatorTest extends ServiceProviderTest ->getMock(); $auth->expects($this->exactly(1)) ->method('getPermissionsByGroup') - ->with(-10) + ->with(10) ->willReturn([]); $auth->expects($this->exactly(1)) ->method('getPermissionsByUser') @@ -251,6 +251,18 @@ class AuthenticatorTest extends ServiceProviderTest $this->assertEquals(PASSWORD_ARGON2I, $auth->getPasswordAlgorithm()); } + /** + * @covers \Engelsystem\Helpers\Authenticator::setGuestRole + * @covers \Engelsystem\Helpers\Authenticator::getGuestRole + */ + public function testGuestRole() + { + $auth = $this->getAuthenticator(); + + $auth->setGuestRole(42); + $this->assertEquals(42, $auth->getGuestRole()); + } + /** * @return Authenticator */ -- cgit v1.2.3-54-g00ecf