From 0c6e88cdee800e8f215485b081f484265cedab11 Mon Sep 17 00:00:00 2001 From: iglocska Date: Mon, 22 Jun 2020 17:45:00 +0200 Subject: [PATCH] new: [init] added functions to create a default user --- src/Controller/AppController.php | 3 ++- src/Model/Table/UsersTable.php | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 5cf22f3..8653818 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -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'] ]); diff --git a/src/Model/Table/UsersTable.php b/src/Model/Table/UsersTable.php index cdf6c2d..3764067 100644 --- a/src/Model/Table/UsersTable.php +++ b/src/Model/Table/UsersTable.php @@ -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; + } }