Doppelte Benutzernamen verhindern
This commit is contained in:
parent
7c8fa99fa8
commit
24fa5cebd3
3 changed files with 39 additions and 2 deletions
|
|
@ -53,7 +53,7 @@ class AuthController
|
|||
'password' => 'required|min:8',
|
||||
]);
|
||||
|
||||
$name = (string) input('name');
|
||||
$name = User::normalizeName((string) input('name'));
|
||||
|
||||
// Einfacher Spam-Schutz: mehr als drei Großbuchstaben im Namen ablehnen.
|
||||
if (preg_match_all('/[A-Z]/', $name) > 3) {
|
||||
|
|
@ -75,6 +75,12 @@ class AuthController
|
|||
redirect('/register');
|
||||
}
|
||||
|
||||
if (User::byName($name) !== null) {
|
||||
$_SESSION['_errors']['name'][] = 'Dieser Name ist bereits vergeben. Bitte einen eindeutigen Namen angeben.';
|
||||
$_SESSION['_old'] = $_POST;
|
||||
redirect('/register');
|
||||
}
|
||||
|
||||
$user = User::create([
|
||||
'name' => $name,
|
||||
'email' => (string) input('email'),
|
||||
|
|
|
|||
|
|
@ -107,6 +107,13 @@ class UserController
|
|||
'mobile' => 'required',
|
||||
]);
|
||||
|
||||
$name = User::normalizeName((string) input('name'));
|
||||
|
||||
if ($name !== User::normalizeName((string) $user->name) && User::byName($name, $userId) !== null) {
|
||||
flash_set('error', 'Dieser Name ist bereits vergeben. Bitte einen eindeutigen Namen angeben.');
|
||||
redirect('/member/' . $userId . '/edit');
|
||||
}
|
||||
|
||||
$imageError = null;
|
||||
|
||||
if (!empty($_FILES['bild']['name'])) {
|
||||
|
|
@ -123,7 +130,7 @@ class UserController
|
|||
}
|
||||
}
|
||||
|
||||
$user->name = (string) input('name');
|
||||
$user->name = $name;
|
||||
$user->email = (string) input('email');
|
||||
$user->mobile = (string) input('mobile');
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,30 @@ class User extends Model
|
|||
return $row === null ? null : static::fromRow($row);
|
||||
}
|
||||
|
||||
/** Sucht einen Benutzer anhand des Namens (optional ohne eine bestimmte ID). */
|
||||
public static function byName(string $name, ?int $excludeId = null): ?static
|
||||
{
|
||||
$name = self::normalizeName($name);
|
||||
$sql = 'SELECT * FROM `users` WHERE name = ?';
|
||||
$params = [$name];
|
||||
|
||||
if ($excludeId !== null) {
|
||||
$sql .= ' AND id <> ?';
|
||||
$params[] = $excludeId;
|
||||
}
|
||||
|
||||
$sql .= ' LIMIT 1';
|
||||
$row = db()->fetchOne($sql, $params);
|
||||
|
||||
return $row === null ? null : static::fromRow($row);
|
||||
}
|
||||
|
||||
/** Vereinheitlicht Leerzeichen im Namen. */
|
||||
public static function normalizeName(string $name): string
|
||||
{
|
||||
return trim(preg_replace('/\s+/u', ' ', $name) ?? '');
|
||||
}
|
||||
|
||||
/** Admins und Mitglieder, die als Tour Guide wählbar sind. */
|
||||
public static function selectableGuides(): array
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue