Skip to content

Commit 4ee9439

Browse files
committed
move session handling out of the user class
1 parent 1c2fc38 commit 4ee9439

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

lib/Session.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
class Session {
5+
private $cookieLifetime = 24*60*60;
6+
public static function start($username) {
7+
session_start([
8+
'cookie_lifetime' => 24*60*60 // 1 day
9+
]);
10+
$_SESSION['username'] = $email;
11+
}
12+
13+
public static function getLoggedInUser() {
14+
session_start();
15+
if (!isset($_SESSION['username'])) {
16+
return false;
17+
}
18+
return self::getUser($_SESSION['username']);
19+
}
20+
}

lib/User.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,24 +247,12 @@ public static function checkPassword($email, $password) {
247247
$result = $query->fetchAll();
248248
if (sizeof($result) === 1) {
249249
if (password_verify($password, $result[0]['password'])) {
250-
session_start([
251-
'cookie_lifetime' => 24*60*60 // 1 day
252-
]);
253-
$_SESSION['username'] = $email;
254250
return true;
255251
}
256252
}
257253
return false;
258254
}
259255

260-
public static function getLoggedInUser() {
261-
session_start();
262-
if (!isset($_SESSION['username'])) {
263-
return false;
264-
}
265-
return self::getUser($_SESSION['username']);
266-
}
267-
268256
public static function userIdExists($userId) {
269257
Db::connect();
270258
$query = Db::$pdo->prepare(

www/idp/index.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Pdsinterop\PhpSolid\Server;
1111
use Pdsinterop\PhpSolid\ClientRegistration;
1212
use Pdsinterop\PhpSolid\User;
13+
use Pdsinterop\PhpSolid\Session;
1314
use Pdsinterop\PhpSolid\Mailer;
1415
use Pdsinterop\PhpSolid\IpAttempts;
1516
use Pdsinterop\PhpSolid\JtiStore;
@@ -36,7 +37,7 @@
3637
break;
3738
case "/authorize":
3839
case "/authorize/":
39-
$user = User::getLoggedInUser();
40+
$user = Session::getLoggedInUser();
4041
if (!$user) {
4142
header("Location: /login/?redirect_uri=" . urlencode($_SERVER['REQUEST_URI']));
4243
exit();
@@ -124,7 +125,7 @@
124125
break;
125126
case "/dashboard":
126127
case "/dashboard/":
127-
$user = User::getLoggedInUser();
128+
$user = Session::getLoggedInUser();
128129
if (!$user) {
129130
header("Location: /login/");
130131
exit();
@@ -133,7 +134,7 @@
133134
break;
134135
case "/logout":
135136
case "/logout/":
136-
$user = User::getLoggedInUser();
137+
$user = Session::getLoggedInUser();
137138
if ($user) {
138139
session_destroy();
139140
}
@@ -161,7 +162,7 @@
161162
break;
162163
case "/sharing":
163164
case "/sharing/":
164-
$user = User::getLoggedInUser();
165+
$user = Session::getLoggedInUser();
165166
if (!$user) {
166167
header("Location: /login/");
167168
exit();
@@ -311,6 +312,7 @@
311312
exit();
312313
}
313314
if (User::checkPassword($_POST['username'], $_POST['password'])) {
315+
Session::start($_POST['username']);
314316
if (!isset($_POST['redirect_uri']) || $_POST['redirect_uri'] === '') {
315317
header("Location: /dashboard/");
316318
exit();
@@ -366,7 +368,7 @@
366368
break;
367369
case "/api/sharing":
368370
case "/api/sharing/":
369-
$user = User::getLoggedInUser();
371+
$user = Session::getLoggedInUser();
370372
if (!$user) {
371373
header("HTTP/1.1 400 Bad request");
372374
} else {

0 commit comments

Comments
 (0)