|
18 | 18 |
|
19 | 19 | namespace Rhubarb\Scaffolds\Authentication; |
20 | 20 |
|
21 | | -use Rhubarb\Crown\LoginProviders\LoginProvider; |
22 | 21 | use Rhubarb\Crown\LoginProviders\UrlHandlers\ValidateLoginUrlHandler; |
23 | 22 | use Rhubarb\Crown\Module; |
24 | | -use Rhubarb\Crown\UrlHandlers\ClassMappedUrlHandler; |
25 | 23 | use Rhubarb\Leaf\UrlHandlers\LeafCollectionUrlHandler; |
| 24 | +use Rhubarb\Scaffolds\Authentication\Settings\ProtectedUrl; |
| 25 | +use Rhubarb\Scaffolds\Authentication\UrlHandlers\CallableUrlHandler; |
26 | 26 | use Rhubarb\Stem\Schema\SolutionSchema; |
27 | 27 | use Rhubarb\Stem\StemModule; |
28 | 28 |
|
29 | 29 | class AuthenticationModule extends Module |
30 | 30 | { |
31 | | - protected $urlToProtect; |
32 | | - protected $loginUrl; |
33 | | - |
34 | 31 | /** |
35 | 32 | * Creates an instance of the Authentication module. |
36 | 33 | * |
37 | 34 | * @param null $loginProviderClassName |
38 | 35 | * @param string $urlToProtect Optional. The URL stub to protect by requiring a login. Defaults to |
39 | 36 | * the entire URL tree. |
40 | 37 | * @param string $loginUrl The URL to redirect the user to for logging in |
41 | | - * @param string $identityColumnName The name of the column in the user table storing the login identity. |
| 38 | + * @internal param string $identityColumnName The name of the column in the user table storing the login identity. |
42 | 39 | */ |
43 | | - public function __construct($loginProviderClassName = null, $urlToProtect = "/", $loginUrl = "/login/") |
| 40 | + public function __construct($loginProviderClassName = null, $urlToProtect = '/', $loginUrl = '/login/') |
44 | 41 | { |
45 | 42 | parent::__construct(); |
46 | 43 |
|
47 | | - $this->urlToProtect = $urlToProtect; |
48 | | - $this->loginUrl = $loginUrl; |
49 | | - |
50 | | - if ($loginProviderClassName != null) { |
51 | | - LoginProvider::setProviderClassName($loginProviderClassName); |
| 44 | + if ($loginProviderClassName !== null) { |
| 45 | + $this->registerProtectedUrl(new ProtectedUrl( |
| 46 | + $urlToProtect, |
| 47 | + $loginProviderClassName, |
| 48 | + $loginUrl |
| 49 | + )); |
52 | 50 | } |
53 | 51 | } |
54 | 52 |
|
| 53 | + public function registerProtectedUrl(ProtectedUrl $urlToProtect) |
| 54 | + { |
| 55 | + $this->protectedUrls[] = $urlToProtect; |
| 56 | + } |
| 57 | + |
| 58 | + /** @var ProtectedUrl[] */ |
| 59 | + private $protectedUrls = []; |
| 60 | + |
55 | 61 | public function initialise() |
56 | 62 | { |
57 | | - SolutionSchema::registerSchema("Authentication", __NAMESPACE__ . '\DatabaseSchema'); |
| 63 | + SolutionSchema::registerSchema('Authentication', DatabaseSchema::class); |
58 | 64 | } |
59 | 65 |
|
60 | 66 | protected function registerUrlHandlers() |
61 | 67 | { |
62 | | - $reset = new LeafCollectionUrlHandler( |
63 | | - __NAMESPACE__ . '\Leaves\ResetPassword', |
64 | | - __NAMESPACE__ . '\Leaves\ConfirmResetPassword'); |
65 | | - |
66 | | - $login = new ClassMappedUrlHandler(__NAMESPACE__ . '\Leaves\Login', [ |
67 | | - "reset/" => $reset |
68 | | - ]); |
69 | | - |
70 | | - $login->setName("login"); |
71 | | - |
72 | | - $validateLoginUrlHandler = new ValidateLoginUrlHandler(LoginProvider::getProvider(), $this->loginUrl); |
73 | | - |
74 | | - $this->addUrlHandlers( |
75 | | - [ |
76 | | - $this->loginUrl => $login, |
77 | | - $this->urlToProtect => $validateLoginUrlHandler |
| 68 | + foreach ($this->protectedUrls as $url) { |
| 69 | + |
| 70 | + $provider = $url->loginProviderClassName; |
| 71 | + |
| 72 | + $this->addUrlHandlers([ |
| 73 | + $url->loginUrl => $login = new CallableUrlHandler(function () use ($url) { |
| 74 | + $className = $url->loginLeafClassName; |
| 75 | + return new $className($url->loginProviderClassName); |
| 76 | + }, [ |
| 77 | + $url->resetChildUrl => $reset = new LeafCollectionUrlHandler( |
| 78 | + $url->resetPasswordLeafClassName, |
| 79 | + $url->confirmResetPasswordLeafClassName |
| 80 | + ), |
| 81 | + $url->logoutChildUrl => $logout = new CallableUrlHandler(function () use ($url) { |
| 82 | + $className = $url->logoutLeafClassName; |
| 83 | + return new $className($url->loginProviderClassName); |
| 84 | + }), |
| 85 | + ]), |
| 86 | + $url->urlToProtect => $protected = |
| 87 | + new ValidateLoginUrlHandler($provider::singleton(), $url->loginUrl), |
78 | 88 | ]); |
79 | 89 |
|
80 | | - $logout = new ClassMappedUrlHandler(__NAMESPACE__ . '\Leaves\Logout'); |
| 90 | + // Make sure that the login url handlers are given greater precedence than those of the application. |
| 91 | + $login->setPriority(10); |
| 92 | + $login->setName('login'); |
81 | 93 |
|
82 | | - $logout->setName("logout"); |
| 94 | + $logout->setPriority(10); |
| 95 | + $logout->setName('logout'); |
83 | 96 |
|
84 | | - $this->addUrlHandlers( |
85 | | - [ |
86 | | - "/logout/" => $logout |
87 | | - ]); |
| 97 | + $reset->setPriority(10); |
| 98 | + $reset->setName('reset'); |
88 | 99 |
|
89 | | - // Make sure that the login url handlers are given greater precedence than those of the application. |
90 | | - $login->setPriority(10); |
91 | | - //$reset->setPriority(10); |
92 | | - $validateLoginUrlHandler->setPriority(10); |
| 100 | + $protected->setPriority(10); |
| 101 | + } |
93 | 102 | } |
94 | 103 |
|
95 | 104 | /** |
|
0 commit comments