Skip to content

Commit 3943f08

Browse files
Merge pull request nextcloud#53212 from nextcloud/feat/core/install-without-admin-user
feat(core): Add option to disable creating an admin user when installing
2 parents ebdb12f + d11d5b7 commit 3943f08

7 files changed

Lines changed: 56 additions & 50 deletions

File tree

core/Command/Maintenance/Install.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected function configure(): void {
4444
->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'Login to connect to the database')
4545
->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
4646
->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null)
47+
->addOption('disable-admin-user', null, InputOption::VALUE_NONE, 'Disable the creation of an admin user')
4748
->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'Login of the admin account', 'admin')
4849
->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
4950
->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'E-Mail of the admin account')
@@ -120,6 +121,7 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
120121
if ($input->hasParameterOption('--database-pass')) {
121122
$dbPass = (string)$input->getOption('database-pass');
122123
}
124+
$disableAdminUser = (bool)$input->getOption('disable-admin-user');
123125
$adminLogin = $input->getOption('admin-user');
124126
$adminPassword = $input->getOption('admin-pass');
125127
$adminEmail = $input->getOption('admin-email');
@@ -142,7 +144,7 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
142144
}
143145
}
144146

145-
if (is_null($adminPassword)) {
147+
if (!$disableAdminUser && $adminPassword === null) {
146148
/** @var QuestionHelper $helper */
147149
$helper = $this->getHelper('question');
148150
$question = new Question('What is the password you like to use for the admin account <' . $adminLogin . '>?');
@@ -151,7 +153,7 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
151153
$adminPassword = $helper->ask($input, $output, $question);
152154
}
153155

154-
if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
156+
if (!$disableAdminUser && $adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
155157
throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
156158
}
157159

@@ -161,6 +163,7 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
161163
'dbpass' => $dbPass,
162164
'dbname' => $dbName,
163165
'dbhost' => $dbHost,
166+
'admindisable' => $disableAdminUser,
164167
'adminlogin' => $adminLogin,
165168
'adminpass' => $adminPassword,
166169
'adminemail' => $adminEmail,

lib/private/Setup.php

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,15 @@ public function install(array $options, ?IOutput $output = null): array {
304304
$error = [];
305305
$dbType = $options['dbtype'];
306306

307-
if (empty($options['adminlogin'])) {
308-
$error[] = $l->t('Set an admin Login.');
309-
}
310-
if (empty($options['adminpass'])) {
311-
$error[] = $l->t('Set an admin password.');
307+
$disableAdminUser = (bool)($options['admindisable'] ?? false);
308+
309+
if (!$disableAdminUser) {
310+
if (empty($options['adminlogin'])) {
311+
$error[] = $l->t('Set an admin Login.');
312+
}
313+
if (empty($options['adminpass'])) {
314+
$error[] = $l->t('Set an admin password.');
315+
}
312316
}
313317
if (empty($options['directory'])) {
314318
$options['directory'] = \OC::$SERVERROOT . '/data';
@@ -318,8 +322,6 @@ public function install(array $options, ?IOutput $output = null): array {
318322
$dbType = 'sqlite';
319323
}
320324

321-
$username = htmlspecialchars_decode($options['adminlogin']);
322-
$password = htmlspecialchars_decode($options['adminpass']);
323325
$dataDir = htmlspecialchars_decode($options['directory']);
324326

325327
$class = self::$dbSetupClasses[$dbType];
@@ -375,7 +377,7 @@ public function install(array $options, ?IOutput $output = null): array {
375377
$this->outputDebug($output, 'Configuring database');
376378
$dbSetup->initialize($options);
377379
try {
378-
$dbSetup->setupDatabase($username);
380+
$dbSetup->setupDatabase();
379381
} catch (\OC\DatabaseSetupException $e) {
380382
$error[] = [
381383
'error' => $e->getMessage(),
@@ -405,19 +407,22 @@ public function install(array $options, ?IOutput $output = null): array {
405407
return $error;
406408
}
407409

408-
$this->outputDebug($output, 'Create admin account');
409-
410-
// create the admin account and group
411410
$user = null;
412-
try {
413-
$user = Server::get(IUserManager::class)->createUser($username, $password);
414-
if (!$user) {
415-
$error[] = "Account <$username> could not be created.";
411+
if (!$disableAdminUser) {
412+
$username = htmlspecialchars_decode($options['adminlogin']);
413+
$password = htmlspecialchars_decode($options['adminpass']);
414+
$this->outputDebug($output, 'Create admin account');
415+
416+
try {
417+
$user = Server::get(IUserManager::class)->createUser($username, $password);
418+
if (!$user) {
419+
$error[] = "Account <$username> could not be created.";
420+
return $error;
421+
}
422+
} catch (Exception $exception) {
423+
$error[] = $exception->getMessage();
416424
return $error;
417425
}
418-
} catch (Exception $exception) {
419-
$error[] = $exception->getMessage();
420-
return $error;
421426
}
422427

423428
$config = Server::get(IConfig::class);
@@ -432,7 +437,7 @@ public function install(array $options, ?IOutput $output = null): array {
432437
}
433438

434439
$group = Server::get(IGroupManager::class)->createGroup('admin');
435-
if ($group instanceof IGroup) {
440+
if ($user !== null && $group instanceof IGroup) {
436441
$group->addUser($user);
437442
}
438443

@@ -464,26 +469,28 @@ public function install(array $options, ?IOutput $output = null): array {
464469
$bootstrapCoordinator = Server::get(\OC\AppFramework\Bootstrap\Coordinator::class);
465470
$bootstrapCoordinator->runInitialRegistration();
466471

467-
// Create a session token for the newly created user
468-
// The token provider requires a working db, so it's not injected on setup
469-
/** @var \OC\User\Session $userSession */
470-
$userSession = Server::get(IUserSession::class);
471-
$provider = Server::get(PublicKeyTokenProvider::class);
472-
$userSession->setTokenProvider($provider);
473-
$userSession->login($username, $password);
474-
$user = $userSession->getUser();
475-
if (!$user) {
476-
$error[] = 'No account found in session.';
477-
return $error;
478-
}
479-
$userSession->createSessionToken($request, $user->getUID(), $username, $password);
472+
if (!$disableAdminUser) {
473+
// Create a session token for the newly created user
474+
// The token provider requires a working db, so it's not injected on setup
475+
/** @var \OC\User\Session $userSession */
476+
$userSession = Server::get(IUserSession::class);
477+
$provider = Server::get(PublicKeyTokenProvider::class);
478+
$userSession->setTokenProvider($provider);
479+
$userSession->login($username, $password);
480+
$user = $userSession->getUser();
481+
if (!$user) {
482+
$error[] = 'No account found in session.';
483+
return $error;
484+
}
485+
$userSession->createSessionToken($request, $user->getUID(), $username, $password);
480486

481-
$session = $userSession->getSession();
482-
$session->set('last-password-confirm', Server::get(ITimeFactory::class)->getTime());
487+
$session = $userSession->getSession();
488+
$session->set('last-password-confirm', Server::get(ITimeFactory::class)->getTime());
483489

484-
// Set email for admin
485-
if (!empty($options['adminemail'])) {
486-
$user->setSystemEMailAddress($options['adminemail']);
490+
// Set email for admin
491+
if (!empty($options['adminemail'])) {
492+
$user->setSystemEMailAddress($options['adminemail']);
493+
}
487494
}
488495

489496
return $error;

lib/private/Setup/AbstractDatabase.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ protected function connect(array $configOverwrite = []): Connection {
127127
return $connection;
128128
}
129129

130-
/**
131-
* @param string $username
132-
*/
133-
abstract public function setupDatabase($username);
130+
abstract public function setupDatabase();
134131

135132
public function runMigrations(?IOutput $output = null) {
136133
if (!is_dir(\OC::$SERVERROOT . '/core/Migrations')) {

lib/private/Setup/MySQL.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class MySQL extends AbstractDatabase {
1717
public $dbprettyname = 'MySQL/MariaDB';
1818

19-
public function setupDatabase($username) {
19+
public function setupDatabase() {
2020
//check if the database user has admin right
2121
$connection = $this->connect(['dbname' => null]);
2222

@@ -28,7 +28,7 @@ public function setupDatabase($username) {
2828
}
2929

3030
if ($this->tryCreateDbUser) {
31-
$this->createSpecificUser($username, new ConnectionAdapter($connection));
31+
$this->createSpecificUser('oc_admin', new ConnectionAdapter($connection));
3232
}
3333

3434
$this->config->setValues([

lib/private/Setup/OCI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function validate($config) {
4040
return $errors;
4141
}
4242

43-
public function setupDatabase($username) {
43+
public function setupDatabase() {
4444
try {
4545
$this->connect();
4646
} catch (\Exception $e) {

lib/private/Setup/PostgreSQL.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ class PostgreSQL extends AbstractDatabase {
1616
public $dbprettyname = 'PostgreSQL';
1717

1818
/**
19-
* @param string $username
2019
* @throws \OC\DatabaseSetupException
2120
*/
22-
public function setupDatabase($username) {
21+
public function setupDatabase() {
2322
try {
2423
$connection = $this->connect([
2524
'dbname' => 'postgres'
@@ -46,7 +45,7 @@ public function setupDatabase($username) {
4645
//use the admin login data for the new database user
4746

4847
//add prefix to the postgresql user name to prevent collisions
49-
$this->dbUser = 'oc_' . strtolower($username);
48+
$this->dbUser = 'oc_admin';
5049
//create a new password so we don't need to store the admin config in the config file
5150
$this->dbPassword = \OC::$server->get(ISecureRandom::class)->generate(30, ISecureRandom::CHAR_ALPHANUMERIC);
5251

lib/private/Setup/Sqlite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function initialize($config) {
4545
}
4646
}
4747

48-
public function setupDatabase($username) {
48+
public function setupDatabase() {
4949
$datadir = $this->config->getValue(
5050
'datadirectory',
5151
\OC::$SERVERROOT . '/data'

0 commit comments

Comments
 (0)