From bfc729f5645d1ad4c2adbbe066989568914410a5 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 12 Nov 2022 09:19:25 +0800 Subject: [PATCH] feat(web): add upgrader for #18 --- db/app_uoj233.sql | 4 +- web/app/libs/uoj-utility-lib.php | 21 ------ web/app/upgrade/18_user_permissions/up.sql | 2 + .../upgrade/18_user_permissions/upgrade.php | 69 +++++++++++++++++++ 4 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 web/app/upgrade/18_user_permissions/up.sql create mode 100644 web/app/upgrade/18_user_permissions/upgrade.php diff --git a/db/app_uoj233.sql b/db/app_uoj233.sql index 558f426..0127874 100644 --- a/db/app_uoj233.sql +++ b/db/app_uoj233.sql @@ -692,7 +692,7 @@ CREATE TABLE `problems_solutions` ( `problem_id` int NOT NULL, `blog_id` int NOT NULL, PRIMARY KEY (`problem_id`, `blog_id`), - UNIQUE KEY `blog_id` (`blog_id`), + UNIQUE KEY `unique__blog_id` (`blog_id`), KEY `problem_id` (`problem_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4; /*!40101 SET character_set_client = @saved_cs_client */; @@ -987,7 +987,7 @@ UNLOCK TABLES; CREATE TABLE `user_info` ( `usergroup` char(1) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'U', `username` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL, - `usertype` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'student', + `usertype` enum('student','teacher','system','banned') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'student', `realname` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `school` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `email` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, diff --git a/web/app/libs/uoj-utility-lib.php b/web/app/libs/uoj-utility-lib.php index 2aa54c4..ad016ff 100644 --- a/web/app/libs/uoj-utility-lib.php +++ b/web/app/libs/uoj-utility-lib.php @@ -150,27 +150,6 @@ function camelize($str, $delimiters = '-_') { return $str; } -function addUserType(&$user, $type) { - $usertype = explode(',', $user['usertype']); - if (!in_array($type, $usertype)) { - $usertype[] = $type; - } - $user['usertype'] = implode(',', $usertype); - return $user; -} -function removeUserType(&$user, $type) { - $usertype = explode(',', $user['usertype']); - if (in_array($type, $usertype)) { - $usertype = array_diff($usertype, array($type)); - } - $user['usertype'] = implode(',', $usertype); - return $user; -} -function hasUserType($user, $type) { - $usertype = explode(',', $user['usertype']); - return in_array($type, $usertype); -} - function isSuperUser($user) { return $user != null && $user['usergroup'] == 'S'; } diff --git a/web/app/upgrade/18_user_permissions/up.sql b/web/app/upgrade/18_user_permissions/up.sql new file mode 100644 index 0000000..6219d6c --- /dev/null +++ b/web/app/upgrade/18_user_permissions/up.sql @@ -0,0 +1,2 @@ +ALTER TABLE `problems_solutions` ADD UNIQUE KEY `unique__blog_id` (`blog_id`); +ALTER TABLE `user_info` MODIFY `usertype` enum('student','teacher','system','banned') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'student'; diff --git a/web/app/upgrade/18_user_permissions/upgrade.php b/web/app/upgrade/18_user_permissions/upgrade.php new file mode 100644 index 0000000..b1796f9 --- /dev/null +++ b/web/app/upgrade/18_user_permissions/upgrade.php @@ -0,0 +1,69 @@ + '', + 'problems' => [ + '_placeholder' => '', + ], + 'contests' => [ + '_placeholder' => '', + ], + 'lists' => [ + '_placeholder' => '', + ], + 'groups' => [ + '_placeholder' => '', + ], + 'blogs' => [ + '_placeholder' => '', + ], + 'users' => [ + '_placeholder' => '', + ] + ]; + + if (in_array('problem_uploader', $usertype)) { + $new_permissions['problems']['create'] = true; + } + + if (in_array('problem_manager', $usertype)) { + $new_permissions['problems']['create'] = true; + $new_permissions['problems']['manage'] = true; + } + + if (in_array('contest_judger', $usertype)) { + $new_permissions['contests']['start_final_test'] = true; + } + + if (in_array('teacher', $usertype)) { + $usertype = 'teacher'; + } elseif (in_array('banned', $usertype)) { + $usertype = 'banned'; + } else { + $usertype = 'student'; + } + + $extra['permissions'] = $new_permissions; + + DB::update([ + "update user_info", + "set", [ + "usertype" => $usertype, + "extra" => json_encode($extra), + ], + "where", [ + "username" => $user['username'], + ], + ]); + } + } +};