2015-09-10 00:30:34 +02:00
|
|
|
<?php
|
|
|
|
class UserInitShell extends AppShell {
|
2016-08-18 06:30:05 +02:00
|
|
|
public $uses = array('User', 'Role', 'Organisation', 'Server', 'ConnectionManager');
|
2015-09-10 00:30:34 +02:00
|
|
|
public function main() {
|
2016-07-22 11:21:22 +02:00
|
|
|
if (!Configure::read('Security.salt')) {
|
|
|
|
$this->loadModel('Server');
|
|
|
|
$this->Server->serverSettingsSaveValue('Security.salt', $this->User->generateRandomPassword(32));
|
|
|
|
}
|
2016-08-18 06:30:05 +02:00
|
|
|
$dataSourceConfig = ConnectionManager::getDataSource('default')->config;
|
|
|
|
$dataSource = $dataSourceConfig['datasource'];
|
2015-09-10 00:30:34 +02:00
|
|
|
$this->Role->Behaviors->unload('SysLogLogable.SysLogLogable');
|
|
|
|
$this->User->Behaviors->unload('SysLogLogable.SysLogLogable');
|
|
|
|
// populate the DB with the first role (site admin) if it's empty
|
|
|
|
if ($this->Role->find('count') == 0 ) {
|
|
|
|
$siteAdmin = array('Role' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => 'Site Admin',
|
|
|
|
'permission' => 3,
|
2015-12-30 09:54:43 +01:00
|
|
|
'perm_add' => 1,
|
|
|
|
'perm_modify' => 1,
|
|
|
|
'perm_modify_org' => 1,
|
|
|
|
'perm_publish' => 1,
|
2015-09-10 00:30:34 +02:00
|
|
|
'perm_sync' => 1,
|
|
|
|
'perm_admin' => 1,
|
|
|
|
'perm_audit' => 1,
|
|
|
|
'perm_auth' => 1,
|
|
|
|
'perm_site_admin' => 1,
|
|
|
|
'perm_regexp_access' => 1,
|
2015-12-30 09:54:43 +01:00
|
|
|
'perm_sharing_group' => 1,
|
2015-09-10 00:30:34 +02:00
|
|
|
'perm_tagger' => 1,
|
2016-07-07 15:14:07 +02:00
|
|
|
'perm_template' => 1
|
2015-09-10 00:30:34 +02:00
|
|
|
));
|
|
|
|
$this->Role->save($siteAdmin);
|
2016-08-18 06:30:05 +02:00
|
|
|
// PostgreSQL: update value of auto incremented serial primary key after setting the column by force
|
|
|
|
if ($dataSource == 'Database/Postgres') {
|
|
|
|
$sql = "SELECT setval('roles_id_seq', (SELECT MAX(id) FROM roles));";
|
|
|
|
$this->Role->query($sql);
|
|
|
|
}
|
2015-09-10 00:30:34 +02:00
|
|
|
}
|
2015-12-30 09:54:43 +01:00
|
|
|
|
|
|
|
if ($this->Organisation->find('count', array('conditions' => array('Organisation.local' => true))) == 0) {
|
2017-02-16 10:44:43 +01:00
|
|
|
$date = date('Y-m-d H:i:s');
|
2015-12-30 09:54:43 +01:00
|
|
|
$org = array('Organisation' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'name' => !empty(Configure::read('MISP.org')) ? Configure::read('MISP.org') : 'ADMIN',
|
|
|
|
'description' => 'Automatically generated admin organisation',
|
|
|
|
'type' => 'ADMIN',
|
2017-02-16 10:44:43 +01:00
|
|
|
'date_created' => $date,
|
2016-07-11 00:59:47 +02:00
|
|
|
'uuid' => CakeText::uuid(),
|
2015-12-30 09:54:43 +01:00
|
|
|
'local' => 1
|
|
|
|
));
|
|
|
|
$this->Organisation->save($org);
|
2016-08-18 06:30:05 +02:00
|
|
|
// PostgreSQL: update value of auto incremented serial primary key after setting the column by force
|
|
|
|
if ($dataSource == 'Database/Postgres') {
|
|
|
|
$sql = "SELECT setval('organisations_id_seq', (SELECT MAX(id) FROM organisations));";
|
|
|
|
$this->Organisation->query($sql);
|
|
|
|
}
|
2015-12-30 09:54:43 +01:00
|
|
|
$org_id = $this->Organisation->id;
|
|
|
|
} else {
|
|
|
|
$hostOrg = $this->Organisation->find('first', array('conditions' => array('Organisation.name' => Configure::read('MISP.org')), 'recursive' => -1));
|
|
|
|
if (!empty($hostOrg)) $org_id = $hostOrg['Organisation']['id'];
|
|
|
|
else {
|
|
|
|
$firstOrg = $this->Organisation->find('first', array('conditions' => array('Organisation.local' => true), 'order' => 'Organisation.id ASC'));
|
|
|
|
$org_id = $firstOrg['Organisation']['id'];
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-09-10 00:30:34 +02:00
|
|
|
// populate the DB with the first user if it's empty
|
|
|
|
if ($this->User->find('count') == 0 ) {
|
2018-06-20 07:32:52 +02:00
|
|
|
$auth_key = $this->User->createInitialUser($org_id);
|
|
|
|
if (!empty($auth_key)) {
|
|
|
|
echo $auth_key . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo 'Could not generate the initial user!' . PHP_EOL;
|
2016-08-18 06:30:05 +02:00
|
|
|
}
|
2015-09-10 00:30:34 +02:00
|
|
|
} else {
|
2016-06-04 01:10:45 +02:00
|
|
|
echo 'Script aborted: MISP instance already initialised.' . PHP_EOL;
|
2015-09-10 00:30:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|