Skip to content

Commit

Permalink
[user] UserAccountsConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Dec 16, 2024
1 parent dbb674d commit 69cdba6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ext/user/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Shimmie2;

class UserPageConfig extends ConfigGroup
class UserAccountsConfig extends ConfigGroup
{
public const ANON_ID = "anon_id";
public const SIGNUP_ENABLED = "login_signup_enabled";
Expand Down
33 changes: 17 additions & 16 deletions ext/user/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function login(string $username, string $password): LoginResult
);
} catch (UserNotFound $ex) {
return new LoginResult(
User::by_id($config->get_int(UserPageConfig::ANON_ID, 0)),
User::by_id($config->get_int(UserAccountsConfig::ANON_ID, 0)),
null,
"No user found"
);
Expand All @@ -119,7 +119,7 @@ public static function create_user(string $username, string $password1, string $
);
} catch (UserCreationException $ex) {
return new LoginResult(
User::by_id($config->get_int(UserPageConfig::ANON_ID, 0)),
User::by_id($config->get_int(UserAccountsConfig::ANON_ID, 0)),
null,
$ex->getMessage()
);
Expand All @@ -135,14 +135,15 @@ class UserPage extends Extension
public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_bool(UserPageConfig::SIGNUP_ENABLED, true);
$config->set_default_int(UserPageConfig::LOGIN_MEMORY, 365);
$config->set_default_bool(UserAccountsConfig::SIGNUP_ENABLED, true);
$config->set_default_int(UserAccountsConfig::LOGIN_MEMORY, 365);
$config->set_default_bool(UserAccountsConfig::LOGIN_TAC_BBCODE, true);
$config->set_default_bool(UserAccountsConfig::USER_EMAIL_REQUIRED, false);

$config->set_default_string(AvatarConfig::HOST, "none");
$config->set_default_int(AvatarConfig::GRAVATAR_SIZE, 80);
$config->set_default_string(AvatarConfig::GRAVATAR_DEFAULT, "");
$config->set_default_string(AvatarConfig::GRAVATAR_RATING, "g");
$config->set_default_bool(UserPageConfig::LOGIN_TAC_BBCODE, true);
$config->set_default_bool(UserPageConfig::USER_EMAIL_REQUIRED, false);
}

public function onUserLogin(UserLoginEvent $event): void
Expand Down Expand Up @@ -174,15 +175,15 @@ public function onPageRequest(PageRequestEvent $event): void
}
if ($event->page_matches("user_admin/create", method: "GET", permission: Permissions::CREATE_USER)) {
global $config, $page, $user;
if (!$config->get_bool(UserPageConfig::SIGNUP_ENABLED)) {
if (!$config->get_bool(UserAccountsConfig::SIGNUP_ENABLED)) {
$this->theme->display_signups_disabled($page);
return;
}
$this->theme->display_signup_page($page);
}
if ($event->page_matches("user_admin/create", method: "POST", authed: false, permission: Permissions::CREATE_USER)) {
global $config, $page, $user;
if (!$config->get_bool(UserPageConfig::SIGNUP_ENABLED)) {
if (!$config->get_bool(UserAccountsConfig::SIGNUP_ENABLED)) {
$this->theme->display_signups_disabled($page);
return;
}
Expand Down Expand Up @@ -312,7 +313,7 @@ public function onPageRequest(PageRequestEvent $event): void

if ($event->page_matches("user/{name}")) {
$display_user = User::by_name($event->get_arg('name'));
if ($display_user->id == $config->get_int(UserPageConfig::ANON_ID)) {
if ($display_user->id == $config->get_int(UserAccountsConfig::ANON_ID)) {
throw new UserNotFound("No such user");
}
$e = send_event(new UserPageBuildingEvent($display_user));
Expand Down Expand Up @@ -414,8 +415,8 @@ public function onSetupBuilding(SetupBuildingEvent $event): void
$sb = $event->panel->create_new_block("User Options");
$sb->start_table();
$sb->add_bool_option(UserConfig::ENABLE_API_KEYS, "Enable user API keys", true);
$sb->add_bool_option(UserPageConfig::SIGNUP_ENABLED, "Allow new signups", true);
$sb->add_bool_option(UserPageConfig::USER_EMAIL_REQUIRED, "Require email address", true);
$sb->add_bool_option(UserAccountsConfig::SIGNUP_ENABLED, "Allow new signups", true);
$sb->add_bool_option(UserAccountsConfig::USER_EMAIL_REQUIRED, "Require email address", true);
$sb->add_longtext_option("login_tac", "Terms & Conditions", true);
$sb->add_choice_option(
"user_loginshowprofile",
Expand Down Expand Up @@ -502,7 +503,7 @@ public function onUserCreation(UserCreationEvent $event): void
if (!$user->can(Permissions::CREATE_USER)) {
throw new UserCreationException("Account creation is currently disabled");
}
if (!$config->get_bool(UserPageConfig::SIGNUP_ENABLED) && !$user->can(Permissions::CREATE_OTHER_USER)) {
if (!$config->get_bool(UserAccountsConfig::SIGNUP_ENABLED) && !$user->can(Permissions::CREATE_OTHER_USER)) {
throw new UserCreationException("Account creation is currently disabled");
}
if (strlen($name) < 1) {
Expand Down Expand Up @@ -530,7 +531,7 @@ public function onUserCreation(UserCreationEvent $event): void
// Users who can create other users (ie, admins) are exempt
// from the email requirement
!$user->can(Permissions::CREATE_OTHER_USER) &&
($config->get_bool(UserPageConfig::USER_EMAIL_REQUIRED) && empty($event->email))
($config->get_bool(UserAccountsConfig::USER_EMAIL_REQUIRED) && empty($event->email))
) {
throw new UserCreationException("Email address is required");
}
Expand Down Expand Up @@ -630,11 +631,11 @@ private function page_login(string $name, string $pass): void
private function page_logout(): void
{
global $page, $config;
$page->add_cookie("session", "", time() + 60 * 60 * 24 * $config->get_int(UserPageConfig::LOGIN_MEMORY), "/");
$page->add_cookie("session", "", time() + 60 * 60 * 24 * $config->get_int(UserAccountsConfig::LOGIN_MEMORY), "/");
if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_bool(SpeedHaxConfig::PURGE_COOKIE)) {
# to keep as few versions of content as possible,
# make cookies all-or-nothing
$page->add_cookie("user", "", time() + 60 * 60 * 24 * $config->get_int(UserPageConfig::LOGIN_MEMORY), "/");
$page->add_cookie("user", "", time() + 60 * 60 * 24 * $config->get_int(UserAccountsConfig::LOGIN_MEMORY), "/");
}
log_info("user", "Logged out");
$page->set_mode(PageMode::REDIRECT);
Expand Down Expand Up @@ -760,7 +761,7 @@ private function delete_user(Page $page, int $uid, bool $with_images = false, bo
} else {
$database->execute(
"UPDATE images SET owner_id = :new_owner_id WHERE owner_id = :old_owner_id",
["new_owner_id" => $config->get_int(UserPageConfig::ANON_ID), "old_owner_id" => $uid]
["new_owner_id" => $config->get_int(UserAccountsConfig::ANON_ID), "old_owner_id" => $uid]
);
}

Expand Down
8 changes: 4 additions & 4 deletions ext/user/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public function display_user_block(Page $page, User $user, array $parts): void
public function display_signup_page(Page $page): void
{
global $config, $user;
$tac = $config->get_string(UserPageConfig::LOGIN_TAC, "");
$tac = $config->get_string(UserAccountsConfig::LOGIN_TAC, "");

if ($config->get_bool(UserPageConfig::LOGIN_TAC_BBCODE)) {
if ($config->get_bool(UserAccountsConfig::LOGIN_TAC_BBCODE)) {
$tac = format_text($tac);
}

$email_required = (
$config->get_bool(UserPageConfig::USER_EMAIL_REQUIRED) &&
$config->get_bool(UserAccountsConfig::USER_EMAIL_REQUIRED) &&
!$user->can(Permissions::CREATE_OTHER_USER)
);

Expand Down Expand Up @@ -190,7 +190,7 @@ public function create_login_block(): HTMLElement

$html = emptyHTML();
$html->appendChild($form);
if ($config->get_bool(UserPageConfig::SIGNUP_ENABLED) && $user->can(Permissions::CREATE_USER)) {
if ($config->get_bool(UserAccountsConfig::SIGNUP_ENABLED) && $user->can(Permissions::CREATE_USER)) {
$html->appendChild(SMALL(A(["href" => make_link("user_admin/create")], "Create Account")));
}

Expand Down

0 comments on commit 69cdba6

Please sign in to comment.