-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
528 additions
and
608 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use BikeShare\App\Security\TokenProvider; | ||
use BikeShare\App\Security\UserProvider; | ||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; | ||
use Symfony\Config\SecurityConfig; | ||
|
||
return function (SecurityConfig $security) { | ||
$security | ||
->passwordHasher(PasswordAuthenticatedUserInterface::class) | ||
->algorithm('sha512') | ||
->encodeAsBase64(false) | ||
->iterations(1); | ||
|
||
$security | ||
->provider('app_user_provider') | ||
->id(UserProvider::class); | ||
|
||
$security | ||
->firewall('dev') | ||
->pattern('^/(_(profiler|wdt)|css|images|js)/') | ||
->security(false); | ||
|
||
$mainFirewall = $security->firewall('main'); | ||
$mainFirewall->anonymous(); | ||
$mainFirewall | ||
->formLogin() | ||
->loginPath('login') | ||
->checkPath('login'); | ||
$mainFirewall | ||
->logout() | ||
->path('logout') | ||
->target('/'); | ||
$mainFirewall | ||
->rememberMe() | ||
->secret('%kernel.secret%') | ||
->lifetime(604800) // 1 week in seconds | ||
->tokenProvider( | ||
[ | ||
'service' => TokenProvider::class, | ||
] | ||
); | ||
|
||
$security | ||
->accessControl() | ||
->path('^/login$') | ||
->roles(['IS_AUTHENTICATED_ANONYMOUSLY']); | ||
$security | ||
->accessControl() | ||
->path('^/(sms/)?receive.php$') | ||
->roles(['IS_AUTHENTICATED_ANONYMOUSLY']); | ||
$security | ||
->accessControl() | ||
->path('^/register.php$') | ||
->roles(['IS_AUTHENTICATED_ANONYMOUSLY']); | ||
$security | ||
->accessControl() | ||
->path('^/agree.php$') | ||
->roles(['IS_AUTHENTICATED_ANONYMOUSLY']); | ||
|
||
$security | ||
->accessControl() | ||
->path('^/') | ||
->roles(['ROLE_USER']); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace BikeShare\App\Entity; | ||
|
||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class User implements UserInterface, PasswordAuthenticatedUserInterface | ||
{ | ||
private int $userId; | ||
private string $number; | ||
private string $password; | ||
private string $city; | ||
private string $userName; | ||
private int $privileges; | ||
|
||
public function __construct( | ||
int $userId, | ||
string $number, | ||
string $password, | ||
string $city, | ||
string $userName, | ||
int $privileges | ||
) { | ||
$this->userId = $userId; | ||
$this->number = $number; | ||
$this->password = $password; | ||
$this->city = $city; | ||
$this->userName = $userName; | ||
$this->privileges = $privileges; | ||
} | ||
|
||
public function getCity(): string | ||
{ | ||
return $this->city; | ||
} | ||
|
||
public function getNumber(): string | ||
{ | ||
return $this->number; | ||
} | ||
|
||
public function getPrivileges(): int | ||
{ | ||
return $this->privileges; | ||
} | ||
|
||
public function getUserId(): int | ||
{ | ||
return $this->userId; | ||
} | ||
|
||
public function getUsername(): string | ||
{ | ||
return $this->userName; | ||
} | ||
|
||
public function getPassword(): string | ||
{ | ||
return $this->password; | ||
} | ||
|
||
public function getUserIdentifier(): string | ||
{ | ||
return $this->number; | ||
} | ||
|
||
public function getRoles(): array | ||
{ | ||
return ['ROLE_USER']; | ||
} | ||
|
||
public function getSalt(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function eraseCredentials() | ||
{ | ||
// If you store any temporary, sensitive data on the user, clear it here | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace BikeShare\App\Security; | ||
|
||
use BikeShare\Db\DbInterface; | ||
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken; | ||
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentTokenInterface; | ||
use Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface; | ||
use Symfony\Component\Security\Core\Exception\TokenNotFoundException; | ||
|
||
class TokenProvider implements TokenProviderInterface | ||
{ | ||
private $tokens = []; | ||
private DbInterface $db; | ||
|
||
public function __construct( | ||
DbInterface $db | ||
) { | ||
$this->db = $db; | ||
} | ||
|
||
public function loadTokenBySeries(string $series) | ||
{ | ||
if (!isset($this->tokens[$series])) { | ||
$result = $this->db->query( | ||
"SELECT * FROM remember_me_tokens WHERE series='$series'" | ||
); | ||
if (!$result || $result->rowCount() == 0) { | ||
throw new TokenNotFoundException('No token found.'); | ||
} | ||
|
||
$row = $result->fetchAssoc(); | ||
|
||
$this->tokens[$series] = new PersistentToken( | ||
$row['class'], | ||
$row['username'], | ||
$row['series'], | ||
$row['value'], | ||
new \DateTime($row['lastUsed']) | ||
); | ||
} | ||
|
||
return $this->tokens[$series]; | ||
|
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function updateToken(string $series, string $tokenValue, \DateTime $lastUsed) | ||
{ | ||
$currentToken = $this->loadTokenBySeries($series); | ||
|
||
$token = new PersistentToken( | ||
$currentToken->getClass(), | ||
method_exists($currentToken, 'getUserIdentifier') ? $currentToken->getUserIdentifier() : $currentToken->getUsername(), | ||
$series, | ||
$tokenValue, | ||
$lastUsed | ||
); | ||
$this->tokens[$series] = $token; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteTokenBySeries(string $series) | ||
{ | ||
$this->db->query( | ||
"DELETE FROM remember_me_tokens WHERE series='$series'" | ||
); | ||
|
||
unset($this->tokens[$series]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createNewToken(PersistentTokenInterface $token) | ||
{ | ||
$this->db->query( | ||
"INSERT INTO remember_me_tokens (class, username, series, value, lastUsed) | ||
VALUES ('{$token->getClass()}', '{$token->getUsername()}', '{$token->getSeries()}', '{$token->getTokenValue()}', '{$token->getLastUsed()->format('Y-m-d H:i:s')}')" | ||
); | ||
|
||
$this->tokens[$token->getSeries()] = $token; | ||
} | ||
} |
Oops, something went wrong.