new: [init] added functions to create a default user

remotes/origin/main
iglocska 2020-06-22 17:45:00 +02:00
parent 81f2f82a64
commit 0c6e88cdee
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
2 changed files with 29 additions and 1 deletions

View File

@ -84,10 +84,11 @@ class AppController extends Controller
public function beforeFilter(EventInterface $event)
{
$this->loadModel('Users');
$this->Users->checkForNewInstance();
$this->authApiUser();
$this->ACL->setPublicInterfaces();
if (!empty($this->request->getAttribute('identity'))) {
$this->loadModel('Users');
$user = $this->Users->get($this->request->getAttribute('identity')->getIdentifier(), [
'contain' => ['Roles', 'Individuals' => 'Organisations']
]);

View File

@ -41,4 +41,31 @@ class UsersTable extends AppTable
{
return $rules;
}
public function checkForNewInstance(): bool
{
if (empty($this->find()->first())) {
$this->Roles = TableRegistry::get('Roles');
$role = $this->Roles->newEntity([
'name' => 'admin',
'perm_admin' => 1
]);
$this->Roles->save($role);
$roleId = $this->Roles->id;
$this->Individuals = TableRegistry::get('Individuals');
$individual = $this->Individual->newEntity([
'email' => 'admin@admin.test'
]);
$this->Individuals->save($individual);
$individualId = $this->Individuals->id;
$user = $this->newEntity([
'username' => 'admin',
'password' => 'Password1234',
'individual_id' => $individualId,
'role_id' => $roleId
]);
$this->save($user);
}
return true;
}
}