Skip to content

Commit 42dd900

Browse files
author
Wazabii
committed
Structure
1 parent b4b7554 commit 42dd900

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+6075
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
._*
2+
.DS_Store
3+
.sass-cache

Auth/Middleware/LoggedIn.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace MaplePHP\Foundation\Auth\Middleware;
4+
5+
use MaplePHP\Handler\Interfaces\MiddlewareInterface;
6+
use MaplePHP\Http\Interfaces\ResponseInterface;
7+
use MaplePHP\Http\Interfaces\RequestInterface;
8+
use MaplePHP\Http\Interfaces\UrlInterface;
9+
use MaplePHP\Container\Interfaces\ContainerInterface;
10+
use MaplePHP\Foundation\Security\Token;
11+
//use MaplePHP\Foundation\Http\Provider;
12+
use MaplePHP\Foundation\Auth\Users;
13+
14+
class LoggedIn implements MiddlewareInterface
15+
{
16+
public const USER_COLUMNS = "id,firstname,lastname,email";
17+
public const LOGIN_PATH = "/profile";
18+
public const LOGOUT_PATH = "/login";
19+
20+
private $url;
21+
private $container;
22+
private $users;
23+
24+
public function __construct(
25+
ContainerInterface $container,
26+
UrlInterface $url,
27+
Users $users
28+
) {
29+
$this->container = $container;
30+
$this->url = $url;
31+
$this->users = $users;
32+
}
33+
34+
/**
35+
* Before controllers
36+
* @param ResponseInterface $response
37+
* @param RequestInterface $request
38+
* @return void
39+
*/
40+
public function before(ResponseInterface $response, RequestInterface $request)
41+
{
42+
43+
$session = $this->container->get("session");
44+
$cookies = $this->container->get("cookies");
45+
$database = $this->container->get("DB");
46+
$date = $this->container->get("date");
47+
48+
$token = new Token(Token::REMEMBER, $cookies->inst(), $database, $date);
49+
if (!$session->loggedIn() && ($userID = $token->validate()) && is_int($userID)) {
50+
// Refresh token every time session is destroyed
51+
$token->generate($userID);
52+
$session->setLogin($userID);
53+
}
54+
}
55+
56+
/**
57+
* Show if logged out, if middleware method is specified in router
58+
* @param ResponseInterface $response
59+
* @param RequestInterface $request
60+
* @return ResponseInterface
61+
*/
62+
public function publicZone(ResponseInterface $response, RequestInterface $request): ResponseInterface
63+
{
64+
if ($this->container->get("session")->loggedIn()) {
65+
// Will kill script and then redirect
66+
$response->location($this->url->getRoot(static::LOGIN_PATH));
67+
}
68+
69+
return $response;
70+
}
71+
72+
/**
73+
* Show if logged in, if middleware method is specified in router
74+
* @param ResponseInterface $response
75+
* @param RequestInterface $request
76+
* @return ResponseInterface
77+
*/
78+
public function privateZone(ResponseInterface $response, RequestInterface $request): ResponseInterface
79+
{
80+
if (!$this->container->get("session")->loggedIn()) {
81+
// Will kill script and then redirect
82+
$response->location($this->url->getRoot(static::LOGOUT_PATH));
83+
}
84+
85+
if ($obj = $this->users->getUserById($this->container->get("session")->getByKey())) {
86+
$this->container->set("user", $obj);
87+
} else {
88+
return $this->logout($response, $request);
89+
}
90+
91+
return $response;
92+
}
93+
94+
/**
95+
* Logout (This method could be duplicated to controller)
96+
* @param ResponseInterface $response
97+
* @param RequestInterface $request
98+
* @return ResponseInterface
99+
*/
100+
101+
public function logout(ResponseInterface $response, RequestInterface $request): ResponseInterface
102+
{
103+
if ($this->container->get("session")->loggedIn()) {
104+
unset($_SESSION);
105+
session_destroy();
106+
}
107+
$response->location($this->url->getRoot(static::LOGOUT_PATH));
108+
return $response;
109+
}
110+
111+
112+
/**
113+
* After controllers
114+
* @param ResponseInterface $response
115+
* @param RequestInterface $request
116+
* @return void
117+
*/
118+
public function after(ResponseInterface $response, RequestInterface $request): void
119+
{
120+
}
121+
}

Auth/Middleware/SessionStart.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace MaplePHP\Foundation\Auth\Middleware;
4+
5+
use MaplePHP\Handler\Interfaces\MiddlewareInterface;
6+
use MaplePHP\Http\Interfaces\ResponseInterface;
7+
use MaplePHP\Http\Interfaces\RequestInterface;
8+
use MaplePHP\Container\Interfaces\ContainerInterface;
9+
use MaplePHP\Foundation\Security\Session;
10+
11+
class SessionStart implements MiddlewareInterface
12+
{
13+
//private $url;
14+
private $container;
15+
//private $json;
16+
17+
public function __construct(RequestInterface $request, ContainerInterface $container)
18+
{
19+
$this->container = $container;
20+
// Config and prepare session
21+
}
22+
23+
/**
24+
* Start prepared session Before controllers method view but after controllers construct
25+
* @param ResponseInterface $response
26+
* @param RequestInterface $request
27+
* @return void
28+
*/
29+
public function before(ResponseInterface $response, RequestInterface $request)
30+
{
31+
$session = new Session(
32+
"maple",
33+
(int)getenv("SESSION_TIME"),
34+
"/",
35+
$request->getUri()->getHost(),
36+
((int)getenv("SESSION_SSL") === 1),
37+
true
38+
);
39+
$this->container->set("session", $session);
40+
$this->container->get("session")->start();
41+
}
42+
43+
/**
44+
* After controllers
45+
* @param ResponseInterface $response
46+
* @param RequestInterface $request
47+
* @return void
48+
*/
49+
public function after(ResponseInterface $response, RequestInterface $request)
50+
{
51+
}
52+
}

Auth/Users.php

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
3+
namespace MaplePHP\Foundation\Auth;
4+
5+
use MaplePHP\Foundation\Security\Hash;
6+
use MaplePHP\Validate\Inp;
7+
use MaplePHP\Foundation\Migrate\Tables\Users as UserDBStructure;
8+
use MaplePHP\Foundation\Migrate\Tables\Login as LoginDBStructure;
9+
use MaplePHP\Foundation\Migrate\Tables\Organizations as OrgDBStructure;
10+
use MaplePHP\Foundation\Http\Provider;
11+
12+
class Users
13+
{
14+
private $database;
15+
16+
public function __construct(Provider $provider)
17+
{
18+
$this->database = $provider->DB();
19+
}
20+
21+
/**
22+
* Makes select/insert/update of email consistent
23+
* @param string $email
24+
* @return string
25+
*/
26+
public static function formatEmail(string $email): string
27+
{
28+
return strtolower(trim($email));
29+
}
30+
31+
/**
32+
* Get user by email
33+
* @param string $email
34+
* @param string $col
35+
* @return bool|object
36+
*/
37+
public function getUserByEmail(string $email, string $col = "id,firstname,lastname,email"): bool|object
38+
{
39+
$select = $this->selectUser($col);
40+
$select->where("email", static::formatEmail($email));
41+
return $select->get();
42+
}
43+
44+
/**
45+
* Get user by ID
46+
* @param int $identifier
47+
* @param string $col
48+
* @return bool|object
49+
*/
50+
public function getUserById(int $identifier, string $col = "id,firstname,lastname,email,oid,org_name"): bool|object
51+
{
52+
$select = $this->selectUser($col);
53+
$select->joinLeft(new OrgDBStructure());
54+
$select->whereId($identifier);
55+
return $select->get();
56+
}
57+
58+
/**
59+
* Validate user login
60+
* @param string $email
61+
* @param string $password
62+
* @return bool|int (false or user id)
63+
*/
64+
public function validateUserLogin(string $email, string $password): bool|int
65+
{
66+
$hash = new Hash($password);
67+
$obj = $this->getUserByEmail($email, "id,email,hashed_password");
68+
if (is_object($obj) && $hash->verify($obj->hashed_password)) {
69+
return (int)$obj->id;
70+
}
71+
return false;
72+
}
73+
74+
/**
75+
* Get all users in org
76+
* @param int $oid [description]
77+
* @param string $col [description]
78+
* @return array
79+
*/
80+
public function getAllUsersInOrg(int $oid, string $col = "id,firstname,lastname,email,oid,org_name"): array
81+
{
82+
$select = $this->selectUser($col);
83+
$select->join(new OrgDBStructure());
84+
$select->whereOrg_id($oid);
85+
return $select->fetch();
86+
}
87+
88+
/**
89+
* Get org
90+
* @param int $oid
91+
* @param string $col
92+
* @return bool|object
93+
*/
94+
public function getOrgById(int $oid, string $col = "org_id,org_name"): bool|object
95+
{
96+
$select = $this->database::select($col, new OrgDBStructure());
97+
$select->whereOrg_id($oid);
98+
return $select->get();
99+
}
100+
101+
/**
102+
* Get all orgs
103+
* @param string $col
104+
* @return array
105+
*/
106+
public function getAllOrgs(string $col = "org_id,org_name", ?callable $callback = null): array
107+
{
108+
$select = $this->database::select($col, new OrgDBStructure());
109+
return $select->fetch($callback);
110+
}
111+
112+
113+
/**
114+
* Change password (will auto hash!) (can be called like a static method (shortcut))
115+
* @param string $password Password (not hashed!)
116+
* @return string
117+
*/
118+
protected function hashPassword(string $password): string
119+
{
120+
$hash = new Hash($password);
121+
return $hash->passwordHash();
122+
}
123+
124+
/**
125+
* Insert user (can be called like a static method (shortcut))
126+
* @param array $set
127+
* @return mixed
128+
*/
129+
public function insertUser(array $userSet): mixed
130+
{
131+
$insert = $this->database::insert(new UserDBStructure())->set($userSet);
132+
$insert->execute();
133+
return $insert;
134+
}
135+
136+
/**
137+
* Insert login (can be called like a static method (shortcut))
138+
* @param int $userID
139+
* @param string $email
140+
* @param string $password
141+
* @param int $status
142+
* @return mixed
143+
*/
144+
public function insertLogin(int $userID, string $email, string $password, int $status = 1): mixed
145+
{
146+
$email = static::formatEmail($email);
147+
if (!Inp::value($email)->email()) {
148+
throw new \Exception("The email is not a valid email address.", 1);
149+
}
150+
151+
$insert = $this->database::insert(new LoginDBStructure())
152+
->set([
153+
"user_id" => $userID, "email" => $email,
154+
"status" => $status, "hashed_password" => $this->hashPassword($password)
155+
]);
156+
157+
$insert->execute();
158+
return $insert;
159+
}
160+
161+
/**
162+
* Insert user (can be called like a static method (shortcut))
163+
* @param array $set
164+
* @return mixed
165+
*/
166+
public function insertOrg(array $set): mixed
167+
{
168+
$insert = $this->database::insert(new OrgDBStructure())->set($set);
169+
$insert->execute();
170+
return $insert;
171+
}
172+
173+
/**
174+
* Change password (will auto hash!) can be called like a static method (shortcut))
175+
* @param string $password Password Input a unhashed password, the method will hash it
176+
* @return mixed
177+
*/
178+
public function updatePassword(int $userID, string $password): mixed
179+
{
180+
$update = $this->database::update(new LoginDBStructure());
181+
$update->set("hashed_password", $this->hashPassword($password));
182+
$update->where("user_id", $userID);
183+
$update->limit(1);
184+
return $update->execute();
185+
}
186+
187+
/**
188+
* Select user with login join
189+
* @param string $col
190+
* @return mixed
191+
*/
192+
protected function selectUser(string $col): mixed
193+
{
194+
$select = $this->database::select($col, new UserDBStructure());
195+
$select->join(new LoginDBStructure());
196+
return $select;
197+
}
198+
}

0 commit comments

Comments
 (0)