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 .= '