generated from jric11/baseProject
ChatGPT's update to harden software and run db updates.
This commit is contained in:
+22
-2
@@ -4,6 +4,7 @@
|
||||
class AuthService extends Base {
|
||||
|
||||
public const IDLE_TIMEOUT = 1800;
|
||||
public const ABSOLUTE_TIMEOUT = 28800;
|
||||
|
||||
public function findUser(string $identifier): ?array {
|
||||
$stmt = $this->connect()->prepare('SELECT * FROM view_users WHERE user_email = :email OR user_name = :name LIMIT 1');
|
||||
@@ -50,6 +51,8 @@ class AuthService extends Base {
|
||||
$stmt->execute([':hash' => password_hash($password, PASSWORD_DEFAULT), ':id' => $user['user_serial'], ':old_hash' => $hash]);
|
||||
}
|
||||
session_regenerate_id(true);
|
||||
unset($_SESSION['mfa_user'],$_SESSION['recent_auth'],$_SESSION['mfa_pending'],$_SESSION['mfa_replacement']);
|
||||
$_SESSION['authenticated_at']=time();
|
||||
$_SESSION[self::USER_AUTHENTICATED] = true;
|
||||
$_SESSION[self::USER_SERIAL] = (int) $user['user_serial'];
|
||||
$_SESSION[self::USER_NAME] = $user['user_name'];
|
||||
@@ -68,7 +71,7 @@ class AuthService extends Base {
|
||||
public function currentUser(): ?array {
|
||||
if (empty($_SESSION[self::USER_AUTHENTICATED]))
|
||||
return null;
|
||||
if (time() - ($_SESSION['last_activity'] ?? 0) > self::IDLE_TIMEOUT)
|
||||
if (time() - ($_SESSION['last_activity'] ?? 0) > self::IDLE_TIMEOUT || time() - ($_SESSION['authenticated_at'] ?? 0) >= self::ABSOLUTE_TIMEOUT)
|
||||
return null;
|
||||
$stmt = $this->connect()->prepare('SELECT * FROM view_users WHERE user_serial = :id');
|
||||
$stmt->execute([':id' => $_SESSION[self::USER_SERIAL] ?? 0]);
|
||||
@@ -104,8 +107,25 @@ class AuthService extends Base {
|
||||
if ($stmt->fetch())
|
||||
throw new InvalidArgumentException('Unable to create an account with these details. Try signing in or resetting your password.');
|
||||
$stmt = $db->prepare("INSERT INTO users (user_name, user_first_name, user_last_name, user_email, user_password_hash, user_role, user_status, user_creator) VALUES (:name, :first, :last, :email, :hash, 'User', 'active', 'Self registration')");
|
||||
$kind=$input['registration_kind']??'account';
|
||||
if(!in_array($kind,['account','new_club','invited'],true))throw new InvalidArgumentException('Choose a valid signup option.');
|
||||
$clubName=trim($input['club_name']??'');
|
||||
if($kind==='new_club' && ($clubName===''||strlen($clubName)>128))throw new InvalidArgumentException('Enter your new club name (up to 128 bytes).');
|
||||
try {
|
||||
$stmt->execute([':name' => $name, ':first' => $first, ':last' => $last, ':email' => $email, ':hash' => password_hash($input['user_password'], PASSWORD_DEFAULT)]);
|
||||
DatabaseTransaction::run($db,function(PDO $db)use($stmt,$name,$first,$last,$email,$input,$kind,$clubName){
|
||||
if($kind==='invited'){
|
||||
$invites=$db->prepare("SELECT i.club_serial,i.invited_by,i.membership_role FROM club_invitations i JOIN clubs c ON c.club_serial=i.club_serial WHERE i.invite_email=? AND i.invitation_status='pending' AND i.expires_at>UTC_TIMESTAMP() AND c.club_status='active'");$invites->execute([$email]);$valid=false;$access=new TenantAccess();
|
||||
foreach($invites->fetchAll() as $invite)if($access->canManage((int)$invite['club_serial'],$db,(int)$invite['invited_by'])&&($invite['membership_role']==='member'||$access->isSuper($db,(int)$invite['invited_by']))){$valid=true;break;}
|
||||
if(!$valid)throw new InvalidArgumentException('Ask your club administrator for a current invitation to this email address.');
|
||||
}
|
||||
$stmt->execute([':name'=>$name,':first'=>$first,':last'=>$last,':email'=>$email,':hash'=>password_hash($input['user_password'],PASSWORD_DEFAULT)]);
|
||||
$user=(int)$db->lastInsertId();
|
||||
if($kind==='new_club'){
|
||||
$club=$db->prepare("INSERT INTO clubs(club_name,club_status) VALUES(?,'active')");$club->execute([$clubName]);$clubId=(int)$db->lastInsertId();
|
||||
$membership=$db->prepare("INSERT INTO club_memberships(club_serial,user_serial,membership_role,membership_status,joined_on) VALUES(?,?,'administrator','active',?)");$membership->execute([$clubId,$user,date('Y-m-d')]);
|
||||
Audit::required($db,$user,'registered new club '.json_encode(['club'=>$clubId,'name'=>$clubName]));
|
||||
}
|
||||
});
|
||||
} catch (PDOException $e) {
|
||||
if ($e->getCode() === '23000')
|
||||
throw new InvalidArgumentException('Unable to create an account with these details. Try signing in or resetting your password.');
|
||||
|
||||
Reference in New Issue
Block a user