Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Login | |
100.00% |
34 / 34 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
login | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
8 | |||
logout | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Miniframe\SocialLogin\Controller; |
4 | |
5 | use Miniframe\Core\AbstractController; |
6 | use Miniframe\Core\Config; |
7 | use Miniframe\Core\Request; |
8 | use Miniframe\Core\Response; |
9 | use Miniframe\Response\PhpResponse; |
10 | use Miniframe\Response\RedirectResponse; |
11 | use Miniframe\SocialLogin\Middleware\SocialLogin; |
12 | use Miniframe\SocialLogin\Provider\ProviderInterface; |
13 | use Miniframe\SocialLogin\Provider\Email; |
14 | |
15 | class Login extends AbstractSocialLoginController |
16 | { |
17 | /** |
18 | * Reference to a provider |
19 | * |
20 | * @var ProviderInterface|null |
21 | */ |
22 | protected $provider; |
23 | |
24 | /** |
25 | * List of known providers. |
26 | * |
27 | * @var array |
28 | */ |
29 | protected $providers; |
30 | |
31 | /** |
32 | * Initializes the Login Controller |
33 | * |
34 | * @param Request $request Reference to the Request object. |
35 | * @param Config $config Reference to the Config object. |
36 | * @param SocialLogin $socialLogin Reference to the SocialLogin middleware. |
37 | * @param string $socialLoginPrefixUrl Base URL for the Social Login pages. |
38 | * @param array $providers List of known providers. |
39 | * @param ProviderInterface|null $provider Provider class, if known. |
40 | */ |
41 | public function __construct( |
42 | Request $request, |
43 | Config $config, |
44 | SocialLogin $socialLogin, |
45 | string $socialLoginPrefixUrl, |
46 | array $providers, |
47 | ?ProviderInterface $provider = null |
48 | ) { |
49 | parent::__construct($request, $config, $socialLogin, $socialLoginPrefixUrl); |
50 | $this->providers = $providers; |
51 | $this->provider = $provider; |
52 | } |
53 | |
54 | /** |
55 | * Returns the login page |
56 | * |
57 | * @return Response |
58 | */ |
59 | public function login(): Response |
60 | { |
61 | $state = SocialLogin::parseState($this->request->getRequest('state')); |
62 | |
63 | // When a provider is defined, log in with that provider |
64 | if ($this->provider) { |
65 | $this->socialLogin->login($this->provider->authenticate()); |
66 | return new RedirectResponse($state['redirectUrl'] ?? $this->config->get('framework', 'base_href')); |
67 | } |
68 | |
69 | // Is there just one provider that's not Email, and autologin is enabled? Redirect directly |
70 | if ( |
71 | count($this->providers) === 1 |
72 | && !isset($this->providers['email']) |
73 | && $this->config->has('sociallogin', 'autologin') |
74 | && $this->config->get('sociallogin', 'autologin') == true |
75 | ) { |
76 | $providerName = array_keys($this->providers)[0]; |
77 | $redirectUrl = $this->socialLoginPrefixUrl |
78 | . 'login/' . $providerName |
79 | . '?state=' . rawurlencode($this->request->getRequest('state')) |
80 | ; |
81 | return new RedirectResponse($redirectUrl); |
82 | } |
83 | |
84 | // Get provider themes |
85 | $providerImages = array(); |
86 | $providerColors = array(); |
87 | foreach ($this->providers as $provider => $class) { |
88 | $providerImages[$provider] = $class::getLogoSource(); |
89 | $providerColors[$provider] = $class::getThemeColor(); |
90 | } |
91 | |
92 | // Define the public href for the "Close" button |
93 | $publicHref = null; |
94 | if ($this->config->has('sociallogin', 'public_href')) { |
95 | $publicHref = $this->config->get('sociallogin', 'public_href'); |
96 | } |
97 | |
98 | // Do we have an error? |
99 | $error = $state['error'] ?? null; |
100 | $state['error'] = null; |
101 | |
102 | return new PhpResponse(__DIR__ . '/../../templates/login.html.php', [ |
103 | 'socialLoginPrefixUrl' => $this->socialLoginPrefixUrl, |
104 | 'providers' => array_keys(array_diff($this->providers, [Email::class])), |
105 | 'providerImages' => $providerImages, |
106 | 'providerColors' => $providerColors, |
107 | 'publicHref' => $publicHref, |
108 | 'emailEnabled' => in_array(Email::class, $this->providers), |
109 | 'state' => SocialLogin::generateState($state), |
110 | 'error' => $error, |
111 | 'darkmode' => $this->darkmode, |
112 | ]); |
113 | } |
114 | |
115 | /** |
116 | * Logs out the current user and redirects to the previous page. |
117 | * |
118 | * @return Response |
119 | */ |
120 | public function logout(): Response |
121 | { |
122 | $this->socialLogin->logout(); |
123 | |
124 | $state = SocialLogin::parseState($this->request->getRequest('state')); |
125 | return new RedirectResponse($state['redirectUrl'] ?? $this->config->get('framework', 'base_href')); |
126 | } |
127 | } |