|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* LoginDialog.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license http://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
|
8
|
|
|
* @package iPublikuj:Flickr! |
|
9
|
|
|
* @subpackage UI |
|
10
|
|
|
* @since 5.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 17.02.15 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace IPub\Flickr\UI; |
|
16
|
|
|
|
|
17
|
|
|
use Nette; |
|
18
|
|
|
use Nette\Application; |
|
19
|
|
|
use Nette\Http; |
|
20
|
|
|
|
|
21
|
|
|
use IPub; |
|
22
|
|
|
use IPub\Flickr; |
|
23
|
|
|
use IPub\Flickr\Exceptions; |
|
24
|
|
|
|
|
25
|
|
|
use IPub\OAuth; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Component that you can connect to presenter |
|
29
|
|
|
* and use as public mediator for Flickr OAuth redirects communication |
|
30
|
|
|
* |
|
31
|
|
|
* @package iPublikuj:Flickr! |
|
32
|
|
|
* @subpackage UI |
|
33
|
|
|
* |
|
34
|
|
|
* @method onResponse(LoginDialog $dialog) |
|
35
|
|
|
*/ |
|
36
|
|
|
class LoginDialog extends Application\UI\Control |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var array of function(LoginDialog $dialog) |
|
40
|
|
|
*/ |
|
41
|
|
|
public $onResponse = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Flickr\Client |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $client; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var Flickr\Configuration |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $config; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var Flickr\SessionStorage |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $session; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Flickr\Client $flickr |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(Flickr\Client $flickr) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->client = $flickr; |
|
64
|
|
|
$this->config = $flickr->getConfig(); |
|
65
|
|
|
$this->session = $flickr->getSession(); |
|
66
|
|
|
|
|
67
|
|
|
parent::__construct(); |
|
68
|
|
|
|
|
69
|
|
|
$this->monitor('Nette\Application\IPresenter'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return Flickr\Client |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getClient() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->client; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Nette\ComponentModel\Container $obj |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function attached($obj) |
|
84
|
|
|
{ |
|
85
|
|
|
parent::attached($obj); |
|
86
|
|
|
|
|
87
|
|
|
if ($obj instanceof Nette\Application\IPresenter) { |
|
88
|
|
|
$this->client->getConsumer()->setCallbackUrl(new Http\UrlScript($this->link('//response!'))); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function handleCallback() |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Checks, if there is a user in storage and if not, it redirects to login dialog. |
|
99
|
|
|
* If the user is already in session storage, it will behave, as if were redirected from flickr right now, |
|
100
|
|
|
* this means, it will directly call onResponse event. |
|
101
|
|
|
* |
|
102
|
|
|
* @throws Nette\Application\AbortException |
|
103
|
|
|
*/ |
|
104
|
|
|
public function handleOpen() |
|
105
|
|
|
{ |
|
106
|
|
|
if (!$this->client->getUser()) { // no user |
|
107
|
|
|
$this->open(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->onResponse($this); |
|
111
|
|
|
$this->presenter->redirect('this'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @throws Nette\Application\AbortException |
|
116
|
|
|
* @throws OAuth\Exceptions\RequestFailedException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function open() |
|
119
|
|
|
{ |
|
120
|
|
|
if ($this->client->obtainRequestToken()) { |
|
121
|
|
|
$this->presenter->redirectUrl($this->getUrl()); |
|
122
|
|
|
|
|
123
|
|
|
} else { |
|
124
|
|
|
throw new OAuth\Exceptions\RequestFailedException(sprintf('User could not be authenticated to "%s".', 'flickr')); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getQueryParams() |
|
132
|
|
|
{ |
|
133
|
|
|
$params = [ |
|
134
|
|
|
'oauth_token' => $this->session->request_token, |
|
135
|
|
|
'perms' => $this->config->permission |
|
136
|
|
|
]; |
|
137
|
|
|
|
|
138
|
|
|
return $params; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getUrl() |
|
145
|
|
|
{ |
|
146
|
|
|
return (string) $this->config->createUrl('oauth', 'authorize', $this->getQueryParams()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function handleResponse() |
|
150
|
|
|
{ |
|
151
|
|
|
$this->client->getUser(); // check the received parameters and save user |
|
152
|
|
|
$this->onResponse($this); |
|
153
|
|
|
$this->presenter->redirect('this', ['oauth_token' => NULL, 'oauth_verifier' => NULL]); |
|
154
|
|
|
} |
|
155
|
|
|
} |