From 840b0849dffddc502808c4ba52d3c918048ae0b4 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 26 Dec 2023 23:47:12 +0000 Subject: [PATCH] [user] make email-requiredness a setup option --- core/testcase.php | 2 +- ext/user/main.php | 11 +++- ext/user/theme.php | 26 +++++--- themes/rule34v2/user.theme.php | 110 ++------------------------------- 4 files changed, 35 insertions(+), 114 deletions(-) diff --git a/core/testcase.php b/core/testcase.php index db4b1787..cf19d48e 100644 --- a/core/testcase.php +++ b/core/testcase.php @@ -66,7 +66,7 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { { if (is_null(User::by_name($name))) { $userPage = new UserPage(); - $userPage->onUserCreation(new UserCreationEvent($name, $name, $name, "", false)); + $userPage->onUserCreation(new UserCreationEvent($name, $name, $name, "$name@$name.com", false)); assert(!is_null(User::by_name($name)), "Creation of user $name failed"); } } diff --git a/ext/user/main.php b/ext/user/main.php index ce2c9733..9a3884f2 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -149,6 +149,7 @@ class UserPage extends Extension $config->set_default_string("avatar_gravatar_default", ""); $config->set_default_string("avatar_gravatar_rating", "g"); $config->set_default_bool("login_tac_bbcode", true); + $config->set_default_bool("user_email_required", true); } public function onUserLogin(UserLoginEvent $event) @@ -347,6 +348,7 @@ class UserPage extends Extension $sb->start_table(); $sb->add_bool_option(UserConfig::ENABLE_API_KEYS, "Enable user API keys", true); $sb->add_bool_option("login_signup_enabled", "Allow new signups", true); + $sb->add_bool_option("user_email_required", "Require email address", true); $sb->add_longtext_option("login_tac", "Terms & Conditions", true); $sb->add_choice_option( "user_loginshowprofile", @@ -423,11 +425,12 @@ class UserPage extends Extension public function onUserCreation(UserCreationEvent $event) { + global $config, $page, $user; + $name = $event->username; //$pass = $event->password; //$email = $event->email; - global $config, $page, $user; if (!$user->can(Permissions::CREATE_USER)) { throw new UserCreationException("Account creation is currently disabled"); } @@ -452,6 +455,12 @@ class UserPage extends Extension if ($event->password != $event->password2) { throw new UserCreationException("Passwords don't match"); } + if( + $user->can(Permissions::CREATE_OTHER_USER) || + ($config->get_bool("user_email_required") && empty($event->email)) + ) { + throw new UserCreationException("Email address is required"); + } $new_user = $this->create_user($event); if ($event->login) { diff --git a/ext/user/theme.php b/ext/user/theme.php index 786dafbd..3432e2aa 100644 --- a/ext/user/theme.php +++ b/ext/user/theme.php @@ -63,14 +63,18 @@ class UserPageTheme extends Themelet public function display_signup_page(Page $page) { - global $config; + global $config, $user; $tac = $config->get_string("login_tac", ""); if ($config->get_bool("login_tac_bbcode")) { - $tfe = send_event(new TextFormattingEvent($tac)); - $tac = $tfe->formatted; + $tac = send_event(new TextFormattingEvent($tac))->formatted; } + $email_required = ( + $config->get_bool("user_email_required") && + !$user->can(Permissions::CREATE_OTHER_USER) + ); + $form = SHM_SIMPLE_FORM( "user_admin/create", TABLE( @@ -89,8 +93,8 @@ class UserPageTheme extends Themelet TD(INPUT(["type" => 'password', "name" => 'pass2', "required" => true])) ), TR( - TH(rawHTML("Email (Optional)")), - TD(INPUT(["type" => 'email', "name" => 'email'])) + TH($email_required ? "Email" : rawHTML("Email (Optional)")), + TD(INPUT(["type" => 'email', "name" => 'email', "required" => $email_required])) ), TR( TD(["colspan" => "2"], rawHTML(captcha_get_html())) @@ -135,9 +139,12 @@ class UserPageTheme extends Themelet TD(INPUT(["type" => 'password', "name" => 'pass2', "required" => true])) ), TR( - TH(rawHTML("Email (Optional)")), + TH(rawHTML("Email")), TD(INPUT(["type" => 'email', "name" => 'email'])) ), + TR( + TD(["colspan" => 2], rawHTML("(Email is optional for admin-created accounts)")), + ), ), TFOOT( TR(TD(["colspan" => "2"], INPUT(["type" => "submit", "value" => "Create Account"]))) @@ -159,6 +166,11 @@ class UserPageTheme extends Themelet } public function display_login_block(Page $page) + { + $page->add_block(new Block("Login", $this->create_login_block(), "left", 90)); + } + + public function create_login_block(): HTMLElement { global $config, $user; $form = SHM_SIMPLE_FORM( @@ -187,7 +199,7 @@ class UserPageTheme extends Themelet $html->appendChild(SMALL(A(["href" => make_link("user_admin/create")], "Create Account"))); } - $page->add_block(new Block("Login", $html, "left", 90)); + return $html; } private function _ip_list(string $name, array $ips): HTMLElement diff --git a/themes/rule34v2/user.theme.php b/themes/rule34v2/user.theme.php index 1fcee479..600d90d6 100644 --- a/themes/rule34v2/user.theme.php +++ b/themes/rule34v2/user.theme.php @@ -24,7 +24,9 @@ use function MicroHTML\OPTION; class CustomUserPageTheme extends UserPageTheme { // Override to display user block in the head and in the left column - // (with css media queries deciding which one is visible) + // (with css media queries deciding which one is visible), and also + // to switch between new-line and inline display depending on the + // number of links. public function display_user_block(Page $page, User $user, $parts) { $h_name = html_escape($user->name); @@ -48,109 +50,7 @@ class CustomUserPageTheme extends UserPageTheme // (with css media queries deciding which one is visible) public function display_login_block(Page $page) { - global $config; - $html = " -
- - - - -
Name
Password
-
- "; - if ($config->get_bool("login_signup_enabled")) { - $html .= "Create Account"; - } - $page->add_block(new Block("Login", $html, "head", 90)); - $page->add_block(new Block("Login", $html, "left", 15)); - } - - public function display_signup_page(Page $page) - { - global $config; - $tac = $config->get_string("login_tac", ""); - - if ($config->get_bool("login_tac_bbcode")) { - $tac = send_event(new TextFormattingEvent($tac))->formatted; - } - - $form = SHM_SIMPLE_FORM( - "user_admin/create", - TABLE( - ["class" => "form"], - TBODY( - TR( - TH("Name"), - TD(INPUT(["type" => 'text', "name" => 'name', "required" => true])) - ), - TR( - TH("Password"), - TD(INPUT(["type" => 'password', "name" => 'pass1', "required" => true])) - ), - TR( - TH(rawHTML("Repeat Password")), - TD(INPUT(["type" => 'password', "name" => 'pass2', "required" => true])) - ), - TR( - TH(rawHTML("Email")), - TD(INPUT(["type" => 'email', "name" => 'email', "required" => true])) - ), - TR( - TD(["colspan" => "2"], rawHTML(captcha_get_html())) - ), - ), - TFOOT( - TR(TD(["colspan" => "2"], INPUT(["type" => "submit", "value" => "Create Account"]))) - ) - ) - ); - - $html = emptyHTML( - $tac ? P(rawHTML($tac)) : null, - $form - ); - - $page->set_title("Create Account"); - $page->set_heading("Create Account"); - $page->add_block(new NavBlock()); - $page->add_block(new Block("Signup", $html)); - } - - // override to make email required - public function display_user_creator() - { - global $page; - - $form = SHM_SIMPLE_FORM( - "user_admin/create_other", - TABLE( - ["class" => "form"], - TBODY( - TR( - TH("Name"), - TD(INPUT(["type" => 'text', "name" => 'name', "required" => true])) - ), - TR( - TH("Password"), - TD(INPUT(["type" => 'password', "name" => 'pass1', "required" => true])) - ), - TR( - TH(rawHTML("Repeat Password")), - TD(INPUT(["type" => 'password', "name" => 'pass2', "required" => true])) - ), - TR( - TH(rawHTML("Email")), - TD(INPUT(["type" => 'email', "name" => 'email'])) - ), - TR( - TD(["colspan" => 2], rawHTML("(Email is optional for admin-created accounts)")), - ), - ), - TFOOT( - TR(TD(["colspan" => "2"], INPUT(["type" => "submit", "value" => "Create Account"]))) - ) - ) - ); - $page->add_block(new Block("Create User", (string)$form, "main", 75)); + $page->add_block(new Block("Login", $this->create_login_block(), "head", 90)); + $page->add_block(new Block("Login", $this->create_login_block(), "left", 15)); } }