-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBeUserCommand.php
More file actions
285 lines (256 loc) · 10.5 KB
/
BeUserCommand.php
File metadata and controls
285 lines (256 loc) · 10.5 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
namespace CliTools\Console\Command\TYPO3;
/*
* CliTools Command
* Copyright (C) 2016 WebDevOps.io
* Copyright (C) 2015 Markus Blaschke <markus@familie-blaschke.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use CliTools\Database\DatabaseConnection;
use CliTools\Utility\Typo3Utility;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class BeUserCommand extends \CliTools\Console\Command\Mysql\AbstractCommand
{
/**
* Configure command
*/
protected function configure()
{
parent::configure();
$this->setName('typo3:beuser')
->setDescription('Add backend admin user to database')
->addArgument(
'database',
InputArgument::OPTIONAL,
'Database name'
)
->addArgument(
'typo3-user',
InputArgument::OPTIONAL,
'Username'
)
->addArgument(
'typo3-password',
InputArgument::OPTIONAL,
'Password'
)
->addArgument(
'hash',
InputArgument::OPTIONAL,
'Choose the hashing algorithm for saving the password: md5, md5_salted, bcrypt, argon2i, argon2id'
);
}
/**
* Execute command
*
* @param InputInterface $input Input instance
* @param OutputInterface $output Output instance
*
* @return int|null|void
*/
public function execute(InputInterface $input, OutputInterface $output)
{
// ##################
// Init
// ##################
$dbName = $input->getArgument('database');
$username = $input->getArgument('typo3-user');
$password = $input->getArgument('typo3-password');
$output->writeln('<h2>Injecting TYPO3 backend user</h2>');
// Set default user if not specified
if (empty($username)) {
$username = 'dev';
}
// check username
if (!preg_match('/^[-_a-zA-Z0-9\.]+$/', $username)) {
$output->writeln('<p-error>Invalid username</p-error>');
return 1;
}
$output->writeln('<p>Using user: "' . htmlspecialchars($username) . '"</p>');
// Set default password if not specified
if (empty($password)) {
$password = 'dev';
}
$output->writeln('<p>Using pass: "' . htmlspecialchars($password) . '"</p>');
// ##################
// Password hashing
// ##################
$hash = $input->getArgument('hash');
list($password, $hash) = Typo3Utility::generatePassword($password, $hash);
$this->output->writeln('<p>Generating password with "' . htmlspecialchars($hash) . '" hashing algorithm</p>');
// ##############
// Loop through databases
// ##############
if (empty($dbName)) {
// ##############
// All databases
// ##############
$databaseList = $this->mysqlDatabaseList();
$dbFound = false;
foreach ($databaseList as $dbName) {
// Check if database is TYPO3 instance
$query = 'SELECT COUNT(*) as count
FROM information_schema.tables
WHERE table_schema = ' . $this->mysqlQuote($dbName) . '
AND table_name = \'be_users\'';
$isTypo3Database = $this->execSqlCommand($query);
$isTypo3Database = reset($isTypo3Database);
if ($isTypo3Database) {
$this->setTypo3UserForDatabase($dbName, $username, $password);
$dbFound = true;
}
}
if (!$dbFound) {
$output->writeln('<p-error>No valid TYPO3 database found</p-error>');
}
} else {
// ##############
// One databases
// ##############
$this->setTypo3UserForDatabase($dbName, $username, $password);
}
return 0;
}
/**
* Set TYPO3 user for database
*
* @param string $database Database
* @param string $username Username
* @param string $password Password (salted/hashed)
*/
protected function setTypo3UserForDatabase($database, $username, $password)
{
// ##################
// Update/insert user
// ##################
// Default UserTS
$tsConfig = array(
// Backend stuff
'options.clearCache.system = 1',
'options.clearCache.all = 1',
'options.enableShowPalettes = 1',
'options.pageTree.showPageIdWithTitle = 1',
'options.pageTree.showPathAboveMounts = 1',
'options.pageTree.showDomainNameWithTitle = 1',
// adm panel
'admPanel.enable.edit = 1',
'admPanel.module.edit.forceDisplayFieldIcons = 1',
'admPanel.hide = 0',
// Setup defaults
'setup.default.thumbnailsByDefault = 1',
'setup.default.enableFlashUploader = 0',
'setup.default.recursiveDelete = 1',
'setup.default.showHiddenFilesAndFolders = 1',
'setup.default.resizeTextareas_Flexible = 1',
'setup.default.copyLevels = 99',
'setup.default.rteResize = 99',
// Web list
'setup.default.moduleData.web_list.bigControlPanel = 1',
'setup.default.moduleData.web_list.clipBoard = 1',
'setup.default.moduleData.web_list.localization = 1',
'setup.default.moduleData.web_list.showPalettes = 1',
// File list
'setup.default.moduleData.file_list.bigControlPanel = 1',
'setup.default.moduleData.file_list.clipBoard = 1',
'setup.default.moduleData.file_list.localization = 1',
'setup.default.moduleData.file_list.showPalettes = 1',
);
$tsConfig = implode("\n", $tsConfig);
// Default uc
$uc = array(
'thumbnailsByDefault' => 1,
'recursiveDelete' => 1,
'showHiddenFilesAndFolders' => 1,
'edit_RTE' => 1,
'resizeTextareas' => 1,
'resizeTextareas_Flexible' => 1,
'copyLevels' => 99,
'rteResize' => 99,
'moduleData' => array(
'web_layout' => array(
// not "quick edit" but "columns" should be the
// default submodule within the page module
'function' => '1',
),
'web_list' => array(
// check the important boxes right away
'bigControlPanel' => '1',
'clipBoard' => '1',
'localization' => '1',
'showPalettes' => '1',
),
'web_ts' => array(
// not "constant editor" but "object browser"
'function' => 'TYPO3\CMS\Tstemplate\Controller\TypoScriptTemplateObjectBrowserModuleFunctionController',
// better defaults for immediate debugging the actual typoscript
'ts_browser_type' => 'setup',
'ts_browser_const' => 'subst',
'ts_browser_fixedLgd' => '0',
'ts_browser_showComments' => '1',
),
'file_list' => array(
'bigControlPanel' => '1',
'clipBoard' => '1',
'localization' => '1',
'showPalettes' => '1',
),
),
);
$uc = serialize($uc);
try {
// Get uid from current dev user (if already existing)
$query = 'SELECT uid
FROM ' . DatabaseConnection::sanitizeSqlDatabase($database) . '.be_users
WHERE username = ' . $this->mysqlQuote($username) . '
AND deleted = 0';
$beUserId = $this->execSqlCommand($query);
$beUserId = reset($beUserId);
// Insert or update user in TYPO3 database
$query = 'INSERT INTO ' . DatabaseConnection::sanitizeSqlDatabase($database) . '.be_users
(uid, tstamp, crdate, realName, username, password, TSconfig, uc, admin, disable, starttime, endtime)
VALUES(
' . $this->mysqlQuote($beUserId) . ',
UNIX_TIMESTAMP(),
UNIX_TIMESTAMP(),
' . $this->mysqlQuote('DEVELOPMENT') . ',
' . $this->mysqlQuote($username) . ',
' . $this->mysqlQuote($password) . ',
' . $this->mysqlQuote($tsConfig) . ',
' . $this->mysqlQuote($uc) . ',
1,
0,
0,
0
) ON DUPLICATE KEY UPDATE
realName = VALUES(realName),
password = VALUES(password),
TSconfig = VALUES(TSconfig),
disable = VALUES(disable),
starttime = VALUES(starttime),
endtime = VALUES(endtime)';
$this->execSqlCommand($query);
if ($beUserId) {
$this->output->writeln('<p>User successfully updated to "' . $database . '"</p>');
} else {
$this->output->writeln('<p>User successfully added to "' . $database . '"</p>');
}
} catch (\Exception $e) {
$this->output->writeln('<p-error>User adding failed</p-error>');
}
}
}