new: [init] added functions to create a default user
parent
81f2f82a64
commit
0c6e88cdee
|
@ -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']
|
||||
]);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue