From 4d9f4694aeedbd5ae220328d35f3bee40330a542 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 28 Jul 2019 15:33:01 +0200 Subject: [PATCH] Groups: Fix naming and update IDs --- ...00000_fix_old_groups_table_id_and_name.php | 80 +++++++++++++++++++ includes/pages/admin_groups.php | 10 +-- includes/pages/admin_user.php | 26 +++--- includes/view/User_view.php | 3 +- src/Helpers/Authenticator.php | 21 ++++- src/Helpers/AuthenticatorServiceProvider.php | 1 + tests/Unit/HasDatabase.php | 1 + .../AuthenticatorServiceProviderTest.php | 2 + tests/Unit/Helpers/AuthenticatorTest.php | 14 +++- 9 files changed, 136 insertions(+), 22 deletions(-) create mode 100644 db/migrations/2022_07_21_000000_fix_old_groups_table_id_and_name.php diff --git a/db/migrations/2022_07_21_000000_fix_old_groups_table_id_and_name.php b/db/migrations/2022_07_21_000000_fix_old_groups_table_id_and_name.php new file mode 100644 index 00000000..3b54bb2f --- /dev/null +++ b/db/migrations/2022_07_21_000000_fix_old_groups_table_id_and_name.php @@ -0,0 +1,80 @@ + '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 int[] */ + protected array $ids = [ + -25 => -30, + -26 => -35, + -30 => -50, + -40 => -60, + -50 => -65, + -60 => -80, + -65 => -85, + -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 string[] $naming + * @param int[] $ids + */ + protected function migrate(array $naming, array $ids) + { + if (!$this->schema->hasTable('Groups')) { + return; + } + + $connection = $this->schema->getConnection(); + foreach ($connection->table('Groups')->orderByDesc('UID')->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 f37f5334..412266be 100644 --- a/includes/pages/admin_groups.php +++ b/includes/pages/admin_groups.php @@ -59,8 +59,8 @@ function admin_groups() } else { switch ($request->input('action')) { case 'edit': - if ($request->has('id') && preg_match('/^-\d{1,11}$/', $request->input('id'))) { - $group_id = $request->input('id'); + if ($request->has('id')) { + $group_id = (int)$request->input('id'); } else { return error('Incomplete call, missing Groups ID.', true); } @@ -103,10 +103,9 @@ function admin_groups() case 'save': if ( $request->has('id') - && preg_match('/^-\d{1,11}$/', $request->input('id')) && $request->hasPostData('submit') ) { - $group_id = $request->input('id'); + $group_id = (int)$request->input('id'); } else { return error('Incomplete call, missing Groups ID.', true); } @@ -117,7 +116,8 @@ function admin_groups() Db::delete('DELETE FROM `GroupPrivileges` WHERE `group_id`=?', [$group_id]); $privilege_names = []; foreach ($privileges as $privilege) { - if (preg_match('/^\d+$/', $privilege)) { + $privilege = (int)$privilege; + if ($privilege) { $group_privileges_source = Db::selectOne( 'SELECT `name` FROM `Privileges` WHERE `id`=? LIMIT 1', [$privilege] diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index 10de94fc..5db38912 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -127,24 +127,24 @@ function admin_user() $html .= '
'; $my_highest_group = DB::selectOne( - 'SELECT group_id FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1', + 'SELECT group_id FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` DESC LIMIT 1', [$user->id] ); if (!empty($my_highest_group)) { $my_highest_group = $my_highest_group['group_id']; } - $his_highest_group = DB::selectOne( + $angel_highest_group = DB::selectOne( 'SELECT `group_id` FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1', [$user_id] ); - if (!empty($his_highest_group)) { - $his_highest_group = $his_highest_group['group_id']; + if (!empty($angel_highest_group)) { + $angel_highest_group = $angel_highest_group['group_id']; } if ( ($user_id != $user->id || auth()->can('admin_groups')) - && ($my_highest_group <= $his_highest_group || is_null($his_highest_group)) + && ($my_highest_group >= $angel_highest_group || is_null($angel_highest_group)) ) { $html .= 'Hier kannst Du die Benutzergruppen des Engels festlegen: