|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Christian Gripp <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Core23\LastFmBundle\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use Core23\LastFm\Service\AuthService; |
|
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
18
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
19
|
|
|
|
|
20
|
|
|
final class AuthController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
public const SESSION_LASTFM_NAME = '_CORE23_LASTFM_NAME'; |
|
23
|
|
|
public const SESSION_LASTFM_TOKEN = '_CORE23_LASTFM_TOKEN'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return Response |
|
27
|
|
|
*/ |
|
28
|
|
|
public function authAction(): Response |
|
29
|
|
|
{ |
|
30
|
|
|
$callbackUrl = $this->generateUrl('core23_lastfm_check', array(), UrlGeneratorInterface::ABSOLUTE_URL); |
|
31
|
|
|
|
|
32
|
|
|
return $this->redirect($this->getAuthService()->getAuthUrl($callbackUrl)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Request $request |
|
37
|
|
|
* |
|
38
|
|
|
* @return Response |
|
39
|
|
|
*/ |
|
40
|
|
|
public function checkAction(Request $request): Response |
|
41
|
|
|
{ |
|
42
|
|
|
$token = $request->query->get('token'); |
|
43
|
|
|
|
|
44
|
|
|
if (!$token) { |
|
45
|
|
|
return $this->redirectToRoute('core23_lastfm_auth'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Store session |
|
49
|
|
|
$lastFmSession = $this->getAuthService()->createSession($token); |
|
50
|
|
|
|
|
51
|
|
|
if (null === $lastFmSession) { |
|
52
|
|
|
return $this->redirectToRoute('core23_lastfm_error'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @var Session $session */ |
|
56
|
|
|
$session = $this->getSession(); |
|
57
|
|
|
$session->set(static::SESSION_LASTFM_NAME, $lastFmSession->getName()); |
|
58
|
|
|
$session->set(static::SESSION_LASTFM_TOKEN, $lastFmSession->getKey()); |
|
59
|
|
|
|
|
60
|
|
|
return $this->redirectToRoute('core23_lastfm_success'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return Response |
|
65
|
|
|
*/ |
|
66
|
|
|
public function errorAction(): Response |
|
67
|
|
|
{ |
|
68
|
|
|
if ($this->isAuthenticated()) { |
|
69
|
|
|
return $this->redirectToRoute('core23_lastfm_success'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (null !== $this->getParameter('core23.lastfm.auth_error.redirect_route')) { |
|
73
|
|
|
return $this->redirectToRoute($this->getParameter('core23.lastfm.auth_error.redirect_route'), $this->getParameter('core23.lastfm.auth_error.redirect_route_params')); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->render('Core23LastFmBundle:Auth:error.html.twig'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return Response |
|
81
|
|
|
*/ |
|
82
|
|
|
public function successAction(): Response |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->isAuthenticated()) { |
|
85
|
|
|
return $this->redirectToRoute('core23_lastfm_error'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (null !== $this->getParameter('core23.lastfm.auth_success.redirect_route')) { |
|
89
|
|
|
return $this->redirectToRoute($this->getParameter('core23.lastfm.auth_success.redirect_route'), $this->getParameter('core23.lastfm.auth_success.redirect_route_params')); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$session = $this->getSession(); |
|
93
|
|
|
|
|
94
|
|
|
return $this->render('Core23LastFmBundle:Auth:success.html.twig', array( |
|
95
|
|
|
'name' => $session->get(static::SESSION_LASTFM_NAME), |
|
96
|
|
|
)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the auth status. |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
private function isAuthenticated(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return (bool) $this->getSession()->get(static::SESSION_LASTFM_TOKEN); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return AuthService |
|
111
|
|
|
*/ |
|
112
|
|
|
private function getAuthService(): AuthService |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->get('core23.lastfm.service.auth'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return SessionInterface |
|
119
|
|
|
*/ |
|
120
|
|
|
private function getSession() : SessionInterface |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->get('session'); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|