From 3a7e52fac1c26eaa3c267343c1909a05df56b108 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 17 Mar 2022 21:52:10 +0800 Subject: [PATCH] feat(web): add problem permissions --- db/app_uoj233.sql | 2 ++ web/app/controllers/problem_set.php | 6 +++--- web/app/controllers/super_manage.php | 12 ++++++++++++ web/app/libs/uoj-query-lib.php | 17 ++++++++++------- web/app/libs/uoj-utility-lib.php | 7 +++++++ 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/db/app_uoj233.sql b/db/app_uoj233.sql index 482b34f..5e53a20 100644 --- a/db/app_uoj233.sql +++ b/db/app_uoj233.sql @@ -450,6 +450,7 @@ UNLOCK TABLES; CREATE TABLE `problems` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` text NOT NULL, + `uploader` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL, `is_hidden` tinyint(1) NOT NULL DEFAULT '0', `submission_requirement` text, `hackable` tinyint(1) NOT NULL DEFAULT '0', @@ -618,6 +619,7 @@ CREATE TABLE `user_info` ( `usergroup` char(1) NOT NULL DEFAULT 'U', `username` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL, `realname` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `usertype` varchar(50) NOT NULL DEFAULT 'student', `email` varchar(50) NOT NULL, `password` char(32) NOT NULL, `svn_password` char(10) NOT NULL, diff --git a/web/app/controllers/problem_set.php b/web/app/controllers/problem_set.php index f74875b..a301f56 100644 --- a/web/app/controllers/problem_set.php +++ b/web/app/controllers/problem_set.php @@ -7,10 +7,10 @@ becomeMsgPage(UOJLocale::get('need login')); } - if (isSuperUser($myUser)) { + if (isSuperUser($myUser) || isProblemManager($myUser) || isProblemUploader($myUser)) { $new_problem_form = new UOJForm('new_problem'); $new_problem_form->handle = function() { - DB::query("insert into problems (title, is_hidden, submission_requirement) values ('New Problem', 1, '{}')"); + DB::query("insert into problems (title, uploader, is_hidden, submission_requirement) values ('New Problem', '{$myUser['username']}', 1, '{}')"); $id = DB::insert_id(); DB::query("insert into problems_contents (id, statement, statement_md) values ($id, '', '')"); dataNewProblem($id); @@ -189,7 +189,7 @@ $('#input-show_submit_mode').click(function() { echo ''; echo ''; - if (isSuperUser($myUser)) { + if (isSuperUser($myUser) || isProblemManager($myUser) || isProblemUploader($myUser)) { $new_problem_form->printHTML(); } diff --git a/web/app/controllers/super_manage.php b/web/app/controllers/super_manage.php index 9805922..6a508f8 100644 --- a/web/app/controllers/super_manage.php +++ b/web/app/controllers/super_manage.php @@ -122,6 +122,8 @@ $options = array( 'banneduser' => '设为封禁用户', 'normaluser' => '设为普通用户', + 'problem_uploader' => '设为题目上传者', + 'problem_manager' => '设为题目管理员', 'superuser' => '设为超级用户' ); $user_form->addSelect('op_type', $options, '操作类型', ''); @@ -132,9 +134,19 @@ switch ($_POST['op_type']) { case 'banneduser': DB::update("update user_info set usergroup = 'B' where username = '{$username}'"); + DB::update("update user_info set usertype = 'banned' where username = '{$username}'"); break; case 'normaluser': DB::update("update user_info set usergroup = 'U' where username = '{$username}'"); + DB::update("update user_info set usertype = 'student' where username = '{$username}'"); + break; + case 'problem_uploader': + DB::update("update user_info set usergroup = 'U' where username = '{$username}'"); + DB::update("update user_info set usertype = 'problem_uploader' where username = '{$username}'"); + break; + case 'problem_manager': + DB::update("update user_info set usergroup = 'U' where username = '{$username}'"); + DB::update("update user_info set usertype = 'problem_manager' where username = '{$username}'"); break; case 'superuser': DB::update("update user_info set usergroup = 'S' where username = '{$username}'"); diff --git a/web/app/libs/uoj-query-lib.php b/web/app/libs/uoj-query-lib.php index 4907ba1..ddc8462 100644 --- a/web/app/libs/uoj-query-lib.php +++ b/web/app/libs/uoj-query-lib.php @@ -4,20 +4,23 @@ function hasProblemPermission($user, $problem) { if ($user == null) { return false; } - if (isSuperUser($user)) { + if (isSuperUser($user) || isProblemManager($user)) { + return true; + } + if ($problem['uploader'] == $user['username']) { return true; } return DB::selectFirst("select * from problems_permissions where username = '{$user['username']}' and problem_id = {$problem['id']}") != null; } -function hasViewPermission($str,$user,$problem,$submission) { - if ($str=='ALL') { +function hasViewPermission($str, $user, $problem, $submission) { + if ($str == 'ALL') { return true; } - if ($str=='ALL_AFTER_AC') { + if ($str == 'ALL_AFTER_AC') { return hasAC($user,$problem); } - if ($str=='SELF') { - return $submission['submitter']==$user['username']; + if ($str == 'SELF') { + return $submission['submitter'] == $user['username']; } return false; } @@ -178,4 +181,4 @@ function deleteBlog($id) { DB::delete("delete from blogs_comments where blog_id = $id"); DB::delete("delete from important_blogs where blog_id = $id"); DB::delete("delete from blogs_tags where blog_id = $id"); -} \ No newline at end of file +} diff --git a/web/app/libs/uoj-utility-lib.php b/web/app/libs/uoj-utility-lib.php index 675d310..6fecced 100644 --- a/web/app/libs/uoj-utility-lib.php +++ b/web/app/libs/uoj-utility-lib.php @@ -129,6 +129,13 @@ function blog_name_decode($name) { return $name; } +function isProblemUploader($user) { + return $user != null && $user['usertype'] == 'problem_uploader'; +} +function isProblemManager($user) { + return $user != null && $user['usertype'] == 'problem_manager'; +} + function isSuperUser($user) { return $user != null && $user['usergroup'] == 'S'; }