feat(web): add upgrader for #18

This commit is contained in:
Baoshuo Ren 2022-11-12 09:19:25 +08:00
parent 1c94b98efc
commit bfc729f564
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
4 changed files with 73 additions and 23 deletions

View File

@ -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,

View File

@ -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';
}

View File

@ -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';

View File

@ -0,0 +1,69 @@
<?php
return function ($type) {
if ($type === 'up') {
DB::init();
$users = DB::selectAll("select * from user_info");
foreach ($users as $user) {
$usertype = explode(',', $user['usertype']);
$extra = UOJUser::getExtra($user);
$new_permissions = [
'_placeholder' => '',
'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'],
],
]);
}
}
};