|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use MediaWiki\OAuthClient\Client; |
|
7
|
|
|
use MediaWiki\OAuthClient\ClientConfig; |
|
8
|
|
|
use MediaWiki\OAuthClient\Consumer; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
15
|
|
|
use Xtools\ProjectRepository; |
|
16
|
|
|
|
|
17
|
|
|
class DefaultController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** @var Client */ |
|
21
|
|
|
protected $oauthClient; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @Route("/", name="homepage") |
|
25
|
|
|
* @Route("/index.php", name="homepageIndexPhp") |
|
26
|
|
|
*/ |
|
27
|
|
|
public function indexAction() |
|
28
|
|
|
{ |
|
29
|
|
|
// replace this example code with whatever you need |
|
30
|
|
|
return $this->render('default/index.html.twig', [ |
|
31
|
|
|
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'), |
|
32
|
|
|
'xtPageTitle' => 'welcome', |
|
33
|
|
|
'xtPage' => "index", |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @Route("/about", name="aboutPage") |
|
39
|
|
|
*/ |
|
40
|
|
|
public function aboutAction() |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
// replace this example code with whatever you need |
|
44
|
|
|
return $this->render('default/about.html.twig', array( |
|
45
|
|
|
'xtPage' => 'index', |
|
46
|
|
|
)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @Route("/config", name="configPage") |
|
51
|
|
|
*/ |
|
52
|
|
|
public function configAction() |
|
53
|
|
|
{ |
|
54
|
|
|
|
|
55
|
|
|
if ($this->container->getParameter('kernel.environment') != "dev") { |
|
56
|
|
|
throw new NotFoundHttpException(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$params = $this->container->getParameterBag()->all(); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
foreach ($params as $key => $value) { |
|
62
|
|
|
if (strpos($key, "password") !== false) { |
|
63
|
|
|
$params[$key] = "<REDACTED>"; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// replace this example code with whatever you need |
|
68
|
|
|
return $this->render('default/config.html.twig', [ |
|
69
|
|
|
"xtTitle" => "Config", |
|
70
|
|
|
"xtPageTitle" => "Config", |
|
71
|
|
|
'xtPage' => "index", |
|
72
|
|
|
'dump' => print_r($params, true), |
|
73
|
|
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @Route("/login", name="login") |
|
78
|
|
|
*/ |
|
79
|
|
|
public function loginAction(Request $request) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
try { |
|
82
|
|
|
list( $next, $token ) = $this->getOauthClient()->initiate(); |
|
83
|
|
|
} catch (Exception $oauthException) { |
|
84
|
|
|
throw $oauthException; |
|
85
|
|
|
// @TODO Make this work. |
|
86
|
|
|
//$this->addFlash('error', $oauthException->getMessage()); |
|
|
|
|
|
|
87
|
|
|
//return $this->redirectToRoute('homepage'); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Save the request token to the session. |
|
91
|
|
|
/** @var Session $session */ |
|
92
|
|
|
$session = $this->get('session'); |
|
93
|
|
|
$session->set('oauth_request_token', $token); |
|
94
|
|
|
return new RedirectResponse($next); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @Route("/oauth_callback", name="oauth_callback") |
|
99
|
|
|
*/ |
|
100
|
|
|
public function oauthCallbackAction(Request $request) |
|
101
|
|
|
{ |
|
102
|
|
|
// Give up if the required GET params don't exist. |
|
103
|
|
|
if (!$request->get('oauth_verifier')) { |
|
104
|
|
|
return $this->createNotFoundException(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** @var Session $session */ |
|
108
|
|
|
$session = $this->get('session'); |
|
109
|
|
|
|
|
110
|
|
|
// Complete authentication. |
|
111
|
|
|
$client = $this->getOauthClient(); |
|
112
|
|
|
$token = $session->get('oauth_request_token'); |
|
113
|
|
|
$verifier = $request->get('oauth_verifier'); |
|
114
|
|
|
$accessToken = $client->complete($token, $verifier); |
|
115
|
|
|
|
|
116
|
|
|
// Store access token, and remove request token. |
|
117
|
|
|
$session->set('oauth_access_token', $accessToken); |
|
118
|
|
|
$session->remove('oauth_request_token'); |
|
119
|
|
|
|
|
120
|
|
|
// Store user identity. |
|
121
|
|
|
$ident = $client->identify($accessToken); |
|
122
|
|
|
$session->set('logged_in_user', $ident); |
|
123
|
|
|
|
|
124
|
|
|
// Send back to homepage. |
|
125
|
|
|
return $this->redirectToRoute('homepage'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get an OAuth client, configured to the default project. |
|
130
|
|
|
* (This shouldn't really be in this class, but oh well.) |
|
131
|
|
|
* @return Client |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function getOauthClient() |
|
134
|
|
|
{ |
|
135
|
|
|
if ($this->oauthClient instanceof Client) { |
|
136
|
|
|
return $this->oauthClient; |
|
137
|
|
|
} |
|
138
|
|
|
$defaultProject = ProjectRepository::getDefaultProject($this->container); |
|
|
|
|
|
|
139
|
|
|
$endpoint = $defaultProject->getUrl(false) |
|
140
|
|
|
. $defaultProject->getScriptPath() |
|
141
|
|
|
. '/index.php?title=Special:OAuth'; |
|
142
|
|
|
$conf = new ClientConfig($endpoint); |
|
143
|
|
|
$consumerKey = $this->getParameter('oauth_key'); |
|
144
|
|
|
$consumerSecret = $this->getParameter('oauth_secret'); |
|
145
|
|
|
$conf->setConsumer(new Consumer($consumerKey, $consumerSecret)); |
|
146
|
|
|
$this->oauthClient = new Client($conf); |
|
147
|
|
|
// Callback URL is hardcoded in the consumer registration. |
|
148
|
|
|
$this->oauthClient->setCallback('oob'); |
|
149
|
|
|
return $this->oauthClient; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @Route("/logout", name="logout") |
|
154
|
|
|
*/ |
|
155
|
|
|
public function logoutAction() |
|
156
|
|
|
{ |
|
157
|
|
|
$this->get('session')->invalidate(); |
|
158
|
|
|
return $this->redirectToRoute('homepage'); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.