Browse Source

fix guest login

master
o.moresis 2 years ago
parent
commit
6600f542e2
  1. 17
      src/Controllers/AuthController.php
  2. 19
      src/Services/PgSql.php

17
src/Controllers/AuthController.php

@ -24,12 +24,12 @@ class AuthController {
// Handle guest login // Handle guest login
if (!isset($_POST['password'])) { if (!isset($_POST['password'])) {
$_SESSION['user_id'] = guidv4($email);
$_SESSION['user_groups'] = [1]; $_SESSION['user_groups'] = [1];
$_SESSION['user_name'] = "Guest"; $_SESSION['user_name'] = "Guest";
$_SESSION['user_id'] = self::insertGuestUser($email);
/* header('HTTP/1.1 302 Found'); */ /* header('HTTP/1.1 302 Found'); */
header('Location: /home'); header('Location: /');
/* return TRUE; */ return TRUE;
} else { // handle user login } else { // handle user login
$email = $_POST['username']; $email = $_POST['username'];
$password = $_POST['password']; $password = $_POST['password'];
@ -85,6 +85,17 @@ class AuthController {
exit(); exit();
/* header('Refresh: 2; URL = index.php'); */ /* header('Refresh: 2; URL = index.php'); */
} }
private function insertGuestUser($email) {
$uid = guidv4($email);
$sql = "INSERT INTO webapp.users (email, password, origin, geo_id)
VALUES ('${email}', '', 'GUEST', '${uid}')
ON CONFLICT (email) DO UPDATE
SET geo_id = EXCLUDED.geo_id,
last_login = now()";
return json_decode(PgSql::insert($sql, 'geo_id'))->geo_id;
}
} }

19
src/Services/PgSql.php

@ -101,6 +101,25 @@ class PgSql
return json_encode($response); return json_encode($response);
} }
// For UPSERT
// Returns one or more columns of the successful insert
public static function upsert($sql, $conf, $exc, ...$ret)
{
$excluded_columns = implode(',', $exc);
if ($ret != 'NULL') {
$sql = rtrim($sql, ';');
$sql .= "\nON CONFLICT (${conf}) DO UPDATE
SET ";
$sql .= ' RETURNING ';
$sql .= implode(',', $ret);
}
echo $sql;
$result = pg_query(self::$db, $sql);
if (pg_last_error()) exit(pg_last_error());
$response = pg_fetch_object($result);
return json_encode($response);
}
// For UPDATE, DELETE and CREATE TABLE // For UPDATE, DELETE and CREATE TABLE
// Returns number of affected rows // Returns number of affected rows
public static function exec($sql) public static function exec($sql)

Loading…
Cancel
Save