Accounts: the website username follows the Discord username (Discord-only logins)
Asked for: "der benutzername muss auch automatisch angepasst werden". - discord_apply_member() (session check every 5 minutes, "Jetzt mit Discord abgleichen", Discord login) now also calls discord_sync_username(): accounts without password login get the username from the current Discord name, cleaned like at account creation (discord_username_base()) and unique (discord_unique_username() skips the own account, a taken name gets _2 and stays stable). Accounts with password login keep their login name. - Members who renamed before are caught up at their next check, since their username no longer fits the Discord name. Profile links use the account id and stay valid. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9d5daf8451
commit
25394fdfae
1 changed files with 39 additions and 10 deletions
|
|
@ -71,10 +71,12 @@ function discord_apply_member(int $userId, array $member): void
|
|||
db()->prepare('UPDATE users SET display_name = ? WHERE id = ?')->execute([mb_substr($discordName, 0, 100), $userId]);
|
||||
}
|
||||
// Discord-Benutzername ("Verknuepft als …" in "Mein Profil") - bisher nur beim Anmelden ueber Discord gesetzt,
|
||||
// nach einer Umbenennung bei Discord stand sonst bis zum naechsten Login noch der alte da
|
||||
// nach einer Umbenennung bei Discord stand sonst bis zum naechsten Login noch der alte da. Der Website-
|
||||
// Benutzername folgt ihm (ausser bei Passwort-Login).
|
||||
$discordUsername = trim((string) ($member['username'] ?? ''));
|
||||
if ($discordUsername !== '') {
|
||||
db()->prepare('UPDATE users SET discord_username = ? WHERE id = ?')->execute([mb_substr($discordUsername, 0, 100), $userId]);
|
||||
discord_sync_username($userId, $discordUsername);
|
||||
}
|
||||
// Beitritt zum Discord-Server merken ("Dabei seit" in Mitgliederliste und Profil)
|
||||
$joined = !empty($member['joined_at']) ? strtotime($member['joined_at']) : false;
|
||||
|
|
@ -266,18 +268,13 @@ function discord_sync_avatar(int $userId, array $member): void
|
|||
/**
|
||||
* Macht aus dem Discord-Namen einen freien Benutzernamen fuer die Website.
|
||||
*/
|
||||
function discord_unique_username(string $discordName): string
|
||||
function discord_unique_username(string $discordName, ?int $exceptUserId = null): string
|
||||
{
|
||||
$base = strtolower(preg_replace('/[^A-Za-z0-9._-]/', '', $discordName) ?? '');
|
||||
if ($base === '') {
|
||||
$base = 'discord';
|
||||
}
|
||||
$base = substr($base, 0, 40);
|
||||
|
||||
$stmt = db()->prepare('SELECT 1 FROM users WHERE username = ?');
|
||||
$base = discord_username_base($discordName);
|
||||
$stmt = db()->prepare('SELECT 1 FROM users WHERE username = ? AND id <> ?');
|
||||
$candidate = $base;
|
||||
for ($i = 2; ; $i++) {
|
||||
$stmt->execute([$candidate]);
|
||||
$stmt->execute([$candidate, $exceptUserId ?? 0]);
|
||||
if (!$stmt->fetchColumn()) {
|
||||
return $candidate;
|
||||
}
|
||||
|
|
@ -285,6 +282,38 @@ function discord_unique_username(string $discordName): string
|
|||
}
|
||||
}
|
||||
|
||||
/** Website-Benutzername aus einem Discord-Namen: klein, nur a-z, 0-9, Punkt, Unter- und Bindestrich, hoechstens 40 Zeichen. */
|
||||
function discord_username_base(string $discordName): string
|
||||
{
|
||||
$base = strtolower(preg_replace('/[^A-Za-z0-9._-]/', '', $discordName) ?? '');
|
||||
return substr($base !== '' ? $base : 'discord', 0, 40);
|
||||
}
|
||||
|
||||
/**
|
||||
* Der Website-Benutzername folgt dem Discord-Namen - bei Konten, die sich nur ueber Discord anmelden. Wer sich per
|
||||
* Passwort anmeldet, behaelt seinen Anmeldenamen. Passt er schon (auch mit Zahl dahinter, weil der Name vergeben war),
|
||||
* bleibt er, wie er ist.
|
||||
*/
|
||||
function discord_sync_username(int $userId, string $discordName): void
|
||||
{
|
||||
$stmt = db()->prepare('SELECT username, password_login FROM users WHERE id = ?');
|
||||
$stmt->execute([$userId]);
|
||||
$row = $stmt->fetch();
|
||||
if (!$row || (int) $row['password_login'] === 1) {
|
||||
return;
|
||||
}
|
||||
$base = discord_username_base($discordName);
|
||||
$current = (string) $row['username'];
|
||||
if ($current === $base || preg_match('/^' . preg_quote($base, '/') . '_\d+$/', $current) === 1) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
db()->prepare('UPDATE users SET username = ? WHERE id = ?')->execute([discord_unique_username($discordName, $userId), $userId]);
|
||||
} catch (PDOException $e) {
|
||||
// Gleichzeitig vergeben (eindeutiger Schluessel) - beim naechsten Abgleich noch einmal
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Findet das Konto zu einem Discord-Account oder legt ein neues an. Neue Konten sind noch
|
||||
* nicht freigeschaltet (status = pending) und koennen sich nur ueber Discord anmelden.
|
||||
|
|
|
|||
Loading…
Reference in a new issue