mirror of
https://github.com/renbaoshuo/S2OJ.git
synced 2024-11-22 18:48:41 +00:00
feat(web): add upgrader for #18
This commit is contained in:
parent
1c94b98efc
commit
bfc729f564
@ -692,7 +692,7 @@ CREATE TABLE `problems_solutions` (
|
|||||||
`problem_id` int NOT NULL,
|
`problem_id` int NOT NULL,
|
||||||
`blog_id` int NOT NULL,
|
`blog_id` int NOT NULL,
|
||||||
PRIMARY KEY (`problem_id`, `blog_id`),
|
PRIMARY KEY (`problem_id`, `blog_id`),
|
||||||
UNIQUE KEY `blog_id` (`blog_id`),
|
UNIQUE KEY `unique__blog_id` (`blog_id`),
|
||||||
KEY `problem_id` (`problem_id`)
|
KEY `problem_id` (`problem_id`)
|
||||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
|
||||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
@ -987,7 +987,7 @@ UNLOCK TABLES;
|
|||||||
CREATE TABLE `user_info` (
|
CREATE TABLE `user_info` (
|
||||||
`usergroup` char(1) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'U',
|
`usergroup` char(1) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'U',
|
||||||
`username` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
|
`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 '',
|
`realname` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
|
||||||
`school` varchar(50) 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,
|
`email` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
|
||||||
|
@ -150,27 +150,6 @@ function camelize($str, $delimiters = '-_') {
|
|||||||
return $str;
|
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) {
|
function isSuperUser($user) {
|
||||||
return $user != null && $user['usergroup'] == 'S';
|
return $user != null && $user['usergroup'] == 'S';
|
||||||
}
|
}
|
||||||
|
2
web/app/upgrade/18_user_permissions/up.sql
Normal file
2
web/app/upgrade/18_user_permissions/up.sql
Normal 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';
|
69
web/app/upgrade/18_user_permissions/upgrade.php
Normal file
69
web/app/upgrade/18_user_permissions/upgrade.php
Normal 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'],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user