summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-07-28 15:33:01 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2019-07-29 00:58:02 +0200
commit62ed77ab25f8f2cf5103caf572079ebc2b50f6bc (patch)
tree8bd2e4b7b32a3389ca4e2e4ccbaee1168a2fed67
parentbb45d460987032bedca6870df4386cfc4c01800f (diff)
Groups: Fix Naming and IDs
-rw-r--r--db/migrations/2019_07_21_000001_fix_old_groups_table_id_and_name.php74
-rw-r--r--includes/pages/admin_groups.php4
-rw-r--r--includes/view/User_view.php2
-rw-r--r--src/Helpers/Authenticator.php21
-rw-r--r--src/Helpers/AuthenticatorServiceProvider.php1
-rw-r--r--tests/Unit/Helpers/AuthenticatorServiceProviderTest.php2
-rw-r--r--tests/Unit/Helpers/AuthenticatorTest.php14
7 files changed, 113 insertions, 5 deletions
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 @@
+<?php
+
+namespace Engelsystem\Migrations;
+
+use Engelsystem\Database\Migration\Migration;
+
+class FixOldGroupsTableIdAndName extends Migration
+{
+ /** @var array */
+ protected $naming = [
+ '1-Gast' => '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);
}
}
@@ -205,6 +208,22 @@ class Authenticator
}
/**
+ * @return int
+ */
+ public function getGuestRole()
+ {
+ return $this->guestRole;
+ }
+
+ /**
+ * @param int $guestRole
+ */
+ public function setGuestRole(int $guestRole)
+ {
+ $this->guestRole = $guestRole;
+ }
+
+ /**
* @param User $user
* @return array
* @codeCoverageIgnore
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')
@@ -252,6 +252,18 @@ class AuthenticatorTest extends ServiceProviderTest
}
/**
+ * @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
*/
protected function getAuthenticator()