-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
BackendPassword.php
146 lines (126 loc) · 3.99 KB
/
BackendPassword.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
use Contao\CoreBundle\Exception\AccessDeniedException;
use Patchwork\Utf8;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Back end help wizard.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class BackendPassword extends Backend
{
/**
* Initialize the controller
*
* 1. Import the user
* 2. Call the parent constructor
* 3. Authenticate the user
* 4. Load the language files
* DO NOT CHANGE THIS ORDER!
*/
public function __construct()
{
$this->import(BackendUser::class, 'User');
parent::__construct();
if (!System::getContainer()->get('security.authorization_checker')->isGranted('ROLE_USER'))
{
throw new AccessDeniedException('Access denied');
}
System::loadLanguageFile('default');
System::loadLanguageFile('modules');
}
/**
* Run the controller and parse the password template
*
* @return Response
*/
public function run()
{
/** @var Request $request */
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
$objTemplate = new BackendTemplate('be_password');
if (Input::post('FORM_SUBMIT') == 'tl_password')
{
$pw = $request->request->get('password');
$cnf = $request->request->get('confirm');
// The passwords do not match
if ($pw != $cnf)
{
Message::addError($GLOBALS['TL_LANG']['ERR']['passwordMatch']);
}
// Password too short
elseif (Utf8::strlen($pw) < Config::get('minPasswordLength'))
{
Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['passwordLength'], Config::get('minPasswordLength')));
}
// Password and username are the same
elseif ($pw == $this->User->username)
{
Message::addError($GLOBALS['TL_LANG']['ERR']['passwordName']);
}
// Save the data
else
{
$encoder = System::getContainer()->get('security.encoder_factory')->getEncoder(BackendUser::class);
// Make sure the password has been changed
if ($encoder->isPasswordValid($this->User->password, $pw, null))
{
Message::addError($GLOBALS['TL_LANG']['MSC']['pw_change']);
}
else
{
$this->loadDataContainer('tl_user');
// Trigger the save_callback
if (\is_array($GLOBALS['TL_DCA']['tl_user']['fields']['password']['save_callback']))
{
$dc = new DC_Table('tl_user');
$dc->id = $this->User->id;
foreach ($GLOBALS['TL_DCA']['tl_user']['fields']['password']['save_callback'] as $callback)
{
if (\is_array($callback))
{
$this->import($callback[0]);
$pw = $this->{$callback[0]}->{$callback[1]}($pw, $dc);
}
elseif (\is_callable($callback))
{
$pw = $callback($pw, $dc);
}
}
}
$objUser = UserModel::findByPk($this->User->id);
$objUser->pwChange = '';
$objUser->password = $encoder->encodePassword($pw, null);
$objUser->save();
Message::addConfirmation($GLOBALS['TL_LANG']['MSC']['pw_changed']);
$this->redirect('contao/main.php');
}
}
$this->reload();
}
$objTemplate->theme = Backend::getTheme();
$objTemplate->messages = Message::generate();
$objTemplate->base = Environment::get('base');
$objTemplate->language = $GLOBALS['TL_LANGUAGE'];
$objTemplate->title = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['pw_new']);
$objTemplate->host = Backend::getDecodedHostname();
$objTemplate->charset = Config::get('characterSet');
$objTemplate->action = ampersand(Environment::get('request'));
$objTemplate->headline = $GLOBALS['TL_LANG']['MSC']['pw_new'];
$objTemplate->explain = $GLOBALS['TL_LANG']['MSC']['pw_change'];
$objTemplate->submitButton = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['continue']);
$objTemplate->password = $GLOBALS['TL_LANG']['MSC']['password'][0];
$objTemplate->confirm = $GLOBALS['TL_LANG']['MSC']['confirm'][0];
return $objTemplate->getResponse();
}
}
class_alias(BackendPassword::class, 'BackendPassword');