1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Presenters; |
4
|
|
|
|
5
|
|
|
use Nette; |
6
|
|
|
use Cothema\Model\User\User; |
7
|
|
|
use Nette\Application\UI\Form; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* Sign in/out presenters. |
12
|
|
|
* |
13
|
|
|
* @author Miloš Havlíček <[email protected]> |
14
|
|
|
* @property $signInFormSucceeded |
15
|
|
|
*/ |
16
|
|
|
class SignPresenter extends BasePresenter |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
parent::__construct(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function renderIn() |
28
|
|
|
{ |
29
|
|
|
if ($this->user->isLoggedIn() && $this->user->isInRole('admin')) { |
|
|
|
|
30
|
|
|
$params = $this->request->getParameters(); |
|
|
|
|
31
|
|
|
if (!empty($params['backSignInUrl'])) { |
32
|
|
|
$this->redirectUrl($params['backSignInUrl']); |
33
|
|
|
} else { |
34
|
|
|
$this->redirect('Homepage:'); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sign-in form factory. |
41
|
|
|
* @return Nette\Application\UI\Form |
42
|
|
|
*/ |
43
|
|
|
protected function createComponentSignInForm() |
44
|
|
|
{ |
45
|
|
|
$form = new Form; |
46
|
|
|
$form->addText('username', 'Login:') |
47
|
|
|
->setRequired('Prosím, vložte svůj login.') |
|
|
|
|
48
|
|
|
->getControlPrototype() |
49
|
|
|
->class('form-control'); |
50
|
|
|
|
51
|
|
|
$form->addPassword('password', 'Heslo:') |
52
|
|
|
->setRequired('Prosím, vložte své heslo.') |
|
|
|
|
53
|
|
|
->getControlPrototype() |
54
|
|
|
->class('form-control'); |
55
|
|
|
|
56
|
|
|
$form->addHidden('backSignInUrl', $this->getParameter('backSignInUrl')); |
57
|
|
|
|
58
|
|
|
$form->addCheckbox('remember', 'Zůstat přihlášen'); |
59
|
|
|
|
60
|
|
|
$form->addSubmit('send', 'Přihlásit se') |
61
|
|
|
->getControlPrototype() |
62
|
|
|
->class('btn btn-lg btn-success'); |
63
|
|
|
|
64
|
|
|
$form->onSuccess[] = [$this, 'signInFormSucceeded']; |
65
|
|
|
return $form; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function getActiveUserByUsername($username) |
69
|
|
|
{ |
70
|
|
|
$rep = $this->em->getRepository(User::class); |
71
|
|
|
$users = $rep->findOneBy(['username' => $username, 'active' => 1]); |
72
|
|
|
|
73
|
|
|
return isset($users) ? $users : null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* @param Nette\Application\UI\Form $form |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
|
|
public function signInFormSucceeded($form) |
82
|
|
|
{ |
83
|
|
|
$values = $form->getValues(); |
84
|
|
|
|
85
|
|
|
if ($values->remember) { |
86
|
|
|
$this->user->setExpiration('14 days', false); |
|
|
|
|
87
|
|
|
} else { |
88
|
|
|
$this->user->setExpiration('20 minutes', true); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$user = $this->getActiveUserByUsername($values->username); |
93
|
|
|
if (!$user) { |
94
|
|
|
throw new \Exception('Uživatel není aktivní nebo neexistuje.'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->user->login($user->username, $values->password); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->flashMessage( |
100
|
|
|
'Byl/a jste úspěšně přihlášen/a jako "'.$user->username.'"', |
101
|
|
|
'success' |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
if (!empty($values['backSignInUrl'])) { |
105
|
|
|
$redirectToUrl = $values['backSignInUrl']; |
106
|
|
|
} else { |
107
|
|
|
$this->redirect('Homepage:'); |
108
|
|
|
} |
109
|
|
|
} catch (Nette\Security\AuthenticationException $e) { |
110
|
|
|
$form->addError($e->getMessage()); |
111
|
|
|
} catch (\Exception $e) { |
112
|
|
|
$form->addError($e->getMessage()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!empty($redirectToUrl)) { |
116
|
|
|
$this->redirectUrl($redirectToUrl); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function actionOut() |
124
|
|
|
{ |
125
|
|
|
$this->user->logout(); |
|
|
|
|
126
|
|
|
$this->flashMessage('Uživatel byl úspěšně odhlášen.', 'success'); |
127
|
|
|
$this->redirect('in'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
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.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.