From dbbaa12ac52a52a032cc80570f504f4806a5309b Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 12 Nov 2022 14:29:11 +0800 Subject: [PATCH] feat(web): tmp user --- web/app/controllers/login.php | 7 +- web/app/controllers/super_manage.php | 147 ++++++++++++++++++++++++- web/app/controllers/user_info_edit.php | 33 ++++++ web/app/locale/user/en.php | 1 + web/app/locale/user/zh-cn.php | 1 + web/app/models/UOJUser.php | 62 +++++++++++ web/app/views/user-info.php | 4 + 7 files changed, 253 insertions(+), 2 deletions(-) diff --git a/web/app/controllers/login.php b/web/app/controllers/login.php index 01a9e07..5620cdb 100644 --- a/web/app/controllers/login.php +++ b/web/app/controllers/login.php @@ -145,11 +145,16 @@ if (isset($_POST['login'])) { $('#div-captcha > .form-floating, #input-captcha').addClass('is-invalid'); $('#help-captcha').html('验证码错误。'); refreshCaptcha(); - } else if (msg == 'banned') { + } else if (msg == 'account:banned') { $('#div-username').addClass('has-validation'); $('#div-username > .form-floating, #input-username').addClass('is-invalid'); $('#help-username').html('该用户已被封停,请联系管理员。'); refreshCaptcha(); + } else if (msg == 'account:expired') { + $('#div-username').addClass('has-validation'); + $('#div-username > .form-floating, #input-username').addClass('is-invalid'); + $('#help-username').html('该用户已过期,请联系管理员。'); + refreshCaptcha(); } else if (msg == 'expired') { $('#div-username').addClass('has-validation'); $('#div-username > .form-floating, #input-username').addClass('is-invalid'); diff --git a/web/app/controllers/super_manage.php b/web/app/controllers/super_manage.php index 1f1999e..1e03420 100644 --- a/web/app/controllers/super_manage.php +++ b/web/app/controllers/super_manage.php @@ -439,6 +439,130 @@ if ($cur_tab == 'index') { EOD); $register_form->runAtServer(); + $register_tmp_user_form = new UOJBs4Form('register_tmp_user'); + $register_tmp_user_form->addVInput( + 'new_tmp_username', + 'text', + '用户名', + '', + function ($username, &$vdata) { + if (!validateUsername($username)) { + return '用户名不合法'; + } + + if (UOJUser::query($username)) { + return '该用户已存在'; + } + + $vdata['username'] = $username; + + return ''; + }, + null + ); + $register_tmp_user_form->addVInput( + 'new_tmp_password', + 'password', + '密码', + '', + function ($password, &$vdata) { + $vdata['password'] = $password; + + return ''; + }, + 'validatePassword' + ); + $register_tmp_user_form->addVInput( + 'new_tmp_email', + 'text', + '电子邮件(选填)', + '', + function ($email, &$vdata) { + if ($email && !validateEmail($email)) { + return '邮件地址不合法'; + } + + $vdata['email'] = $email; + + return ''; + }, + null + ); + $register_tmp_user_form->addVInput( + 'new_tmp_realname', + 'text', + '真实姓名(选填)', + '', + function ($realname, &$vdata) { + $vdata['realname'] = $realname; + + return ''; + }, + null + ); + $register_tmp_user_form->addVInput( + 'new_tmp_school', + 'text', + '学校名称(选填)', + '', + function ($school, &$vdata) { + $vdata['school'] = $school; + + return ''; + }, + null + ); + $register_tmp_user_form->addVInput( + 'new_tmp_expiration_time', + 'text', + '过期时间', + (new DateTime())->add(new DateInterval('P7D'))->format('Y-m-d H:i:s'), + function ($str, &$vdata) { + try { + $vdata['expiration_time'] = new DateTime($str); + } catch (Exception $e) { + return '无效时间格式'; + } + + return ''; + }, + null + ); + $register_tmp_user_form->handle = function (&$vdata) { + $user = [ + 'username' => $vdata['username'], + 'realname' => $vdata['realname'], + 'school' => $vdata['school'], + 'email' => $vdata['email'], + 'expiration_time' => $vdata['expiration_time']->format('Y-m-d H:i:s'), + 'password' => hash_hmac('md5', $vdata['password'], getPasswordClientSalt()), + ]; + + UOJUser::registerTmpAccount($user, ['check_email' => false]); + + dieWithJsonData(['status' => 'success', 'message' => '']); + }; + $register_tmp_user_form->setAjaxSubmit(<<runAtServer(); + $change_password_form = new UOJBs4Form('change_password'); $change_password_form->addVInput( 'p_username', @@ -1151,6 +1275,9 @@ EOD); + @@ -1253,7 +1380,25 @@ EOD);
  • 用户名推荐格式为年级 + 姓名全拼,如 2022 级的张三同学可以设置为 2022zhangsan。对于外校学生,推荐格式为学校名称缩写 + 姓名拼音首字母,如山大附中的赵锦熙同学可以设置为 sdfzzjx)。
  • 请提醒用户及时修改初始密码,以免账号被盗导致教学资源流出。请勿设置过于简单的初始密码。
  • 我们推荐在创建账号时输入号主的电子邮件地址以便后期发生忘记密码等情况时进行验证。
  • -
  • 创建账号后可以在「修改个人信息」页面中的「特权」选项卡为用户分配权限。特别地,如果该用户是外校学生,那么您可能需要将其设置为「仅比赛参加者」以禁止其查看已参与比赛以外的题目。
  • +
  • 创建账号后可以在「修改个人信息」页面中的「特权」选项卡为用户分配权限。特别地,如果该用户是外校学生,那么您可能需要禁用其 所有权限,并为其手动报名比赛。
  • +
  • 对于外校学生,更推荐分发 临时账号
  • + + + + +
    + +
    +
    + printHTML() ?> +
    +
    +
    注意事项
    +
      +
    • 用户名推荐格式为年级 + 姓名全拼,如 2022 级的张三同学可以设置为 2022zhangsan。对于外校学生,推荐格式为学校名称缩写 + 姓名拼音首字母,如山大附中的赵锦熙同学可以设置为 sdfzzjx)。
    • +
    • 请提醒用户及时修改初始密码,以免账号被盗导致教学资源流出。请勿设置过于简单的初始密码。
    • +
    • 我们推荐在创建账号时输入号主的电子邮件地址以便后期发生忘记密码等情况时进行验证。
    • +
    • 临时账号不具有任何权限,只能查看、参加已经用户报名了的比赛。创建账号后可以在「修改个人信息」页面中的「特权」选项卡为用户分配权限。特别地,如果该用户是外校学生,那么您可能需要禁用其 所有权限,并为其手动报名比赛。
    diff --git a/web/app/controllers/user_info_edit.php b/web/app/controllers/user_info_edit.php index 1d470ba..185b4da 100644 --- a/web/app/controllers/user_info_edit.php +++ b/web/app/controllers/user_info_edit.php @@ -70,6 +70,35 @@ EOD);
    EOD); } + if (isTmpUser($user)) { + if (isSuperUser(Auth::user())) { + $update_profile_form->addVInput( + 'expiration_time', + 'text', + UOJLocale::get('user::expiration time'), + $user['expiration_time'], + function ($str, &$vdata) { + try { + $vdata['expiration_time'] = new DateTime($str); + } catch (Exception $e) { + return '无效时间格式'; + } + + return ''; + }, + null + ); + } else { + $expiration_time = UOJLocale::get('user::expiration time'); + $update_profile_form->appendHTML(<< + + +
    只有管理员才能修改用户的账号过期时间。
    + + EOD); + } + } $update_profile_form->addVCheckboxes('avatar_source', [ 'gravatar' => 'Gravatar', 'qq' => 'QQ', @@ -215,6 +244,10 @@ EOD); if (isSuperUser(Auth::user())) { $data['realname'] = $vdata['realname']; $data['school'] = $vdata['school']; + + if (isTmpUser($user)) { + $data['expiration_time'] = $vdata['expiration_time']->format(UOJTime::FORMAT); + } } DB::update([ diff --git a/web/app/locale/user/en.php b/web/app/locale/user/en.php index c98fe3c..a3d2e7e 100644 --- a/web/app/locale/user/en.php +++ b/web/app/locale/user/en.php @@ -20,4 +20,5 @@ return [ 'last active at' => 'Last active at', 'online' => 'Online', 'offline' => 'Offline', + 'expiration time' => 'Expiration time', ]; diff --git a/web/app/locale/user/zh-cn.php b/web/app/locale/user/zh-cn.php index ba2d97c..27093f2 100644 --- a/web/app/locale/user/zh-cn.php +++ b/web/app/locale/user/zh-cn.php @@ -20,4 +20,5 @@ return [ 'online' => '在线', 'offline' => '离线', 'last active at' => '最后活动于', + 'expiration time' => '过期时间', ]; diff --git a/web/app/models/UOJUser.php b/web/app/models/UOJUser.php index e4652e7..7226ff8 100644 --- a/web/app/models/UOJUser.php +++ b/web/app/models/UOJUser.php @@ -76,6 +76,68 @@ class UOJUser { return $user; } + public static function registerTmpAccount($user, $cfg = []) { + UOJUser::checkBasicInfo($user, $cfg); + + $password = getPasswordToStore($user['password'], $user['username']); + $extra = [ + 'permissions' => [ + 'problems' => [ + 'view' => false, + 'download_testdata' => false, + 'create' => false, + 'manage' => false, + ], + 'contests' => [ + 'view' => false, + 'register' => false, + 'create' => false, + 'start_final_test' => false, + 'manage' => false, + ], + 'lists' => [ + 'view' => false, + 'create' => false, + 'manage' => false, + ], + 'groups' => [ + 'view' => false, + 'create' => false, + 'manage' => false, + ], + 'blogs' => [ + 'view' => false, + 'create' => false, + 'manage' => false, + ], + 'users' => [ + 'view' => false, + 'upload_image' => false, + ], + ], + ]; + + $info = [ + 'username' => $user['username'], + 'usergroup' => 'T', + 'email' => $user['email'], + 'school' => $user['school'] ?: '', + 'password' => $password, + 'svn_password' => uojRandString(20), + 'register_time' => DB::now(), + 'expiration_time' => $user['expiration_time'], + 'extra' => json_encode($extra), + ]; + + DB::insert([ + "insert into user_info", + DB::bracketed_fields(array_keys($info)), + "values", DB::tuple($info) + ]); + + return $user; + } + public static function registerTmpACMTeamAccount($team, $cfg = []) { UOJUser::checkBasicInfo($team, $cfg); diff --git a/web/app/views/user-info.php b/web/app/views/user-info.php index 96449f9..36623be 100644 --- a/web/app/views/user-info.php +++ b/web/app/views/user-info.php @@ -292,6 +292,10 @@
    last_visit_time

    +
  • +
    expiration_time
    +

    +