Skip to content

Commit 9c11af7

Browse files
authored
Merge pull request #6 from pdsinterop/feature/user-tests
Add unit tests for the 'User' class. Introduces the Session class for basic session handling. Move www/profile/index.php to www/user/profile.php to let the filenames make more sense
2 parents e40fe84 + 5a9623d commit 9c11af7

File tree

9 files changed

+402
-25
lines changed

9 files changed

+402
-25
lines changed

TODO

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@
6565
- [v] IpAttempts
6666
- [v] Util
6767
- [v] PasswordValidator
68+
- [v] User
6869
- [ ] Mailer
6970
- [ ] MailTemplateGenerator
7071
- [ ] MailTemplates
7172
- [ ] Server
72-
- [ ] SolidNotifications
73-
- [ ] SolidPubSub
7473
- [ ] StorageServer
75-
- [ ] User
74+
- [-] Session
75+
- [-] SolidNotifications
76+
- [-] SolidPubSub
7677
- [-] Middleware
7778
- [-] Db

docker/solid.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<VirtualHost *:443>
2121
ServerName identity.solid.local
2222
ServerAlias *.solid.local
23-
DocumentRoot /opt/solid/www/profile
23+
DocumentRoot /opt/solid/www/user
2424

2525
SSLEngine on
2626
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
@@ -39,7 +39,7 @@
3939
RewriteCond %{HTTP_HOST} ^id-([a-zA-Z0-9-]+)\.solid\.local$ [NC]
4040
# Example rewrite rule based on the first part of the hostname
4141
# This will redirect to /subdomain-content/first_part_of_hostname
42-
RewriteRule ^(.+)$ index.php [QSA,L]
42+
RewriteRule ^(.+)$ profile.php [QSA,L]
4343

4444
# Extract the first part of the subdomain (before the first dot)
4545
RewriteCond %{HTTP_HOST} ^storage-([a-zA-Z0-9-]+)\.solid\.local$ [NC]

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'] = $username;
11+
}
12+
13+
public static function getLoggedInUser() {
14+
session_start();
15+
if (!isset($_SESSION['username'])) {
16+
return false;
17+
}
18+
return $_SESSION['username'];
19+
}
20+
}

lib/User.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static function getStorage($userId) {
178178
public static function setStorage($userId, $storageUrl) {
179179
Db::connect();
180180
$query = Db::$pdo->prepare(
181-
'INSERT OR REPLACE INTO storage VALUES(:userId, :storageUrl)'
181+
'INSERT OR REPLACE INTO userStorage VALUES(:userId, :storageUrl)'
182182
);
183183
$query->execute([
184184
':userId' => $userId,
@@ -187,6 +187,9 @@ public static function setStorage($userId, $storageUrl) {
187187
}
188188

189189
public static function getUser($email) {
190+
if (!isset($email)) {
191+
return false;
192+
}
190193
Db::connect();
191194
$query = Db::$pdo->prepare(
192195
'SELECT user_id, data FROM users WHERE email=:email'
@@ -247,24 +250,12 @@ public static function checkPassword($email, $password) {
247250
$result = $query->fetchAll();
248251
if (sizeof($result) === 1) {
249252
if (password_verify($password, $result[0]['password'])) {
250-
session_start([
251-
'cookie_lifetime' => 24*60*60 // 1 day
252-
]);
253-
$_SESSION['username'] = $email;
254253
return true;
255254
}
256255
}
257256
return false;
258257
}
259258

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-
268259
public static function userIdExists($userId) {
269260
Db::connect();
270261
$query = Db::$pdo->prepare(

0 commit comments

Comments
 (0)