Completed
Pull Request — devel (#146)
by Litera
24:35 queued 19:02
created

AuthPresenter::guardToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Presenters;
4
5
use App\Services\SkautIS\AuthService as SkautisAuthService;
6
use App\Services\SkautIS\UserService as SkautisUserService;
7
use App\Services\SkautIS\Authenticator as SkautisAuthenticator;
8
use App\Services\UserService;
9
use Skautis\Wsdl\AuthenticationException;
10
11
/**
12
 * Skautis Auth presenters.
13
 */
14
class AuthPresenter extends BasePresenter
15
{
16
17
	/**
18
	 * @var SkautisAuthService
19
	 */
20
	protected $skautisAuthService;
21
22
	/**
23
	 * @var SkautisUserService
24
	 */
25
	protected $skautisUserService;
26
27
	/**
28
	 * @var UserService
29
	 */
30
	protected $userService;
31
32
    /**
33
     * @var SkautisAuthenticator
34
     */
35
	private $skautisAuthenticator;
36
37
	/**
38
	 * AuthPresenter constructor.
39
	 * @param SkautisAuthService $skautisAuthService
40
	 * @param SkautisUserService $skautisUserService
41
	 * @param UserService        $userService
42
	 */
43
	public function __construct(
44
		SkautisAuthService $skautisAuthService,
45
		SkautisUserService $skautisUserService,
46
		UserService $userService,
47
        SkautisAuthenticator $skautisAuthenticator
48
	) {
49
		$this->setSkautisAuthService($skautisAuthService);
50
		$this->setSkautisUserService($skautisUserService);
51
		$this->setUserService($userService);
52
		$this->skautisAuthenticator = $skautisAuthenticator;
53
	}
54
55
	/**
56
	 * @return void
57
	 */
58
	protected function startup()
59
	{
60
		parent::startup();
61
	}
62
63
	/**
64
	 * @param  string $provider
65
	 * @return void
66
	 */
67
	public function actionLogin($provider)
68
	{
69
		$this->{$provider . 'Login'}();
70
	}
71
72
	/**
73
	 * @param  string $provider
74
	 * @return void
75
	 */
76
	public function actionLogout($provider)
77
	{
78
		$this->getSession('auth')->backlink = $this->getParameter('backlink') ?? null;
79
		$this->{$provider . 'Logout'}();
80
	}
81
82
	/**
83
	 * @param  string $id
84
	 * @return void
85
	 */
86
	public function actionSkautis($id)
87
	{
88
		$this->{'handleSkautis' . $id}();
89
	}
90
91
    /**
92
     * Redirects to login page
93
     *
94
     * @param   string $backlink
95
     * @return  void
96
     * @throws \Nette\Application\AbortException
97
     */
98
	protected function skautisLogin($backlink = null)
99
	{
100
		$this->redirectUrl($this->getSkautisAuthService()->getLoginUrl($backlink));
101
	}
102
103
    /**
104
     * Handle log out from Skautis
105
     * Skautis redirects to this action after log out
106
     *
107
     * @return  void
108
     * @throws \Nette\Application\AbortException
109
     */
110
	protected function skautisLogout()
111
	{
112
		$this->redirectUrl($this->getSkautisAuthService()->getLogoutUrl());
113
	}
114
115
    /**
116
     * Handle Skautis login process
117
     *
118
     * @param   string $returnUrl
119
     * @return  void
120
     * @throws \Nette\Application\AbortException
121
     * @throws \Nette\Security\AuthenticationException
122
     */
123
	protected function handleSkautisLogin($returnUrl = NULL)
124
	{
125
		try {
126
			$credentials = $this->getHttpRequest()->getPost();
127
            $this->logInfo('Auth: %s / %s / %s', [
128
                $credentials['skautIS_Token'],
129
                $credentials['skautIS_IDRole'],
130
                $credentials['skautIS_IDUnit']
131
            ]);
132
133
            $this->guardToken($credentials['skautIS_Token']);
134
135
            $this->getUser()->setExpiration('+ 29 minutes');
136
            $this->getUser()->setAuthenticator($this->skautisAuthenticator);
137
            $this->getUser()->login($credentials);
138
139
			if (isset($returnUrl)) {
140
				$this->context->application->restoreRequest($returnUrl);
0 ignored issues
show
Documentation introduced by
The property $context is declared private in Nette\Application\UI\Presenter. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
			}
142
143
			if($backlink = $this->getSession('auth')->backlink) {
144
				unset($this->getSession('auth')->backlink);
145
				$this->redirectUrl($backlink);
146
			} else {
147
				$this->redirect(':Dashboard:');
148
			}
149
		} catch (AuthenticationException $e) {
150
			$this->logWarning($e->getMessage());
151
			$this->flashFailure($e->getMessage());
152
			$this->redirect(":Auth:Login");
153
		}
154
	}
155
156
	/**
157
	 * Log out from Skautis
158
	 *
159
	 * @param   void
160
	 * @return  void
161
	 */
162
	protected function handleSkautisLogout()
163
	{
164
		$this->getUser()->logout(TRUE);
165
		//$this->getUserService()->resetLoginData();
166
		$this->flashMessage("Byl jsi úspěšně odhlášen ze SkautISu.");
167
		/*
168
		if ($this->request->post['skautIS_Logout']) {
169
			$this->presenter->flashMessage("Byl jsi úspěšně odhlášen ze SkautISu.");
170
		} else {
171
			$this->presenter->flashMessage("Odhlášení ze skautISu se nezdařilo", "danger");
172
		}
173
		*/
174
175
		if($backlink = $this->getSession('auth')->backlink) {
176
			unset($this->getSession('auth')->backlink);
177
			$this->redirectUrl($backlink);
178
		} else {
179
			$this->redirect(':Login:');
180
		}
181
	}
182
183
    /**
184
     * @param string $token
185
     * @throws \Nette\Application\AbortException
186
     */
187
    protected function guardToken(string $token = '')
188
    {
189
        if (!$token) {
190
            $this->redirect(":Login:");
191
        }
192
    }
193
194
	/**
195
	 * @return SkautisAuthService
196
	 */
197
	protected function getSkautisAuthService(): SkautisAuthService
198
	{
199
		return $this->skautisAuthService;
200
	}
201
202
	/**
203
	 * @param  SkautisAuthService $service
204
	 * @return self
205
	 */
206
	protected function setskautisAuthService(SkautisAuthService $service): self
207
	{
208
		$this->skautisAuthService = $service;
209
210
		return $this;
211
	}
212
213
	/**
214
	 * @return SkautisUserService
215
	 */
216
	protected function getSkautisUserService(): SkautisUserService
217
	{
218
		return $this->skautisUserService;
219
	}
220
221
	/**
222
	 * @param  SkautisUserService $service
223
	 * @return self
224
	 */
225
	protected function setSkautisUserService(SkautisUserService $service): self
226
	{
227
		$this->skautisUserService = $service;
228
229
		return $this;
230
	}
231
232
	/**
233
	 * @return UserService
234
	 */
235
	protected function getUserService(): UserService
236
	{
237
		return $this->userService;
238
	}
239
240
	/**
241
	 * @param  UserService $service
242
	 * @return self
243
	 */
244
	protected function setUserService(UserService $service): self
245
	{
246
		$this->userService = $service;
247
248
		return $this;
249
	}
250
251
252
253
}
254