[user] make email-requiredness a setup option

This commit is contained in:
Shish 2023-12-26 23:47:12 +00:00
parent f902fba814
commit 840b0849df
4 changed files with 35 additions and 114 deletions

View file

@ -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");
}
}

View file

@ -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) {

View file

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

View file

@ -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 = "
<form action='".make_link("user_admin/login")."' method='POST'>
<table class='form' style='width: 100%;'>
<tr><th>Name</th><td><input type='text' name='user'></td></tr>
<tr><th>Password</th><td><input type='password' name='pass'></td></tr>
<tr><td colspan='2'><input type='submit' name='gobu' value='Log In'></td></tr>
</table>
</form>
";
if ($config->get_bool("login_signup_enabled")) {
$html .= "<small><a href='".make_link("user_admin/create")."'>Create Account</a></small>";
}
$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&nbsp;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&nbsp;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));
}
}