2020-06-19 00:42:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Model\Table;
|
|
|
|
|
|
|
|
use App\Model\Table\AppTable;
|
|
|
|
use Cake\ORM\Table;
|
|
|
|
use Cake\Validation\Validator;
|
2020-06-21 21:27:11 +02:00
|
|
|
use Cake\ORM\RulesChecker;
|
2020-06-22 17:47:11 +02:00
|
|
|
use Cake\ORM\TableRegistry;
|
2020-06-19 00:42:10 +02:00
|
|
|
|
|
|
|
class UsersTable extends AppTable
|
|
|
|
{
|
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
parent::initialize($config);
|
|
|
|
$this->addBehavior('UUID');
|
|
|
|
$this->belongsTo(
|
|
|
|
'Individuals',
|
|
|
|
[
|
|
|
|
'dependent' => false,
|
|
|
|
'cascadeCallbacks' => false
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$this->belongsTo(
|
|
|
|
'Roles',
|
|
|
|
[
|
|
|
|
'dependent' => false,
|
|
|
|
'cascadeCallbacks' => false
|
|
|
|
]
|
|
|
|
);
|
2020-06-21 21:27:11 +02:00
|
|
|
$this->setDisplayField('username');
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function validationDefault(Validator $validator): Validator
|
|
|
|
{
|
|
|
|
$validator
|
2020-11-06 10:07:25 +01:00
|
|
|
->requirePresence(['password'], 'create')
|
|
|
|
->add('password', [
|
|
|
|
'password_complexity' => [
|
|
|
|
'rule' => function($value, $context) {
|
2020-11-20 11:16:57 +01:00
|
|
|
if (!preg_match('/^((?=.*\d)|(?=.*\W+))(?![\n])(?=.*[A-Z])(?=.*[a-z]).*$|.{16,}/s', $value) || strlen($value) < 12) {
|
2020-11-06 10:07:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
'message' => __('Invalid password. Passwords have to be either 16 character long or 12 character long with 3/4 special groups.')
|
|
|
|
],
|
|
|
|
'password_confirmation' => [
|
|
|
|
'rule' => function($value, $context) {
|
|
|
|
if (isset($context['data']['confirm_password'])) {
|
|
|
|
if ($context['data']['confirm_password'] !== $value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
'message' => __('Password confirmation missing or not matching the password.')
|
|
|
|
]
|
|
|
|
]);
|
2020-06-19 00:42:10 +02:00
|
|
|
return $validator;
|
|
|
|
}
|
2020-06-21 21:27:11 +02:00
|
|
|
|
|
|
|
public function buildRules(RulesChecker $rules): RulesChecker
|
|
|
|
{
|
|
|
|
return $rules;
|
|
|
|
}
|
2020-06-22 17:45:00 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
$this->Individuals = TableRegistry::get('Individuals');
|
2020-06-22 17:52:11 +02:00
|
|
|
$individual = $this->Individuals->newEntity([
|
2020-06-22 17:54:19 +02:00
|
|
|
'email' => 'admin@admin.test',
|
|
|
|
'first_name' => 'admin',
|
|
|
|
'last_name' => 'admin'
|
2020-06-22 17:45:00 +02:00
|
|
|
]);
|
|
|
|
$this->Individuals->save($individual);
|
|
|
|
$user = $this->newEntity([
|
|
|
|
'username' => 'admin',
|
|
|
|
'password' => 'Password1234',
|
2020-06-22 17:50:10 +02:00
|
|
|
'individual_id' => $individual->id,
|
|
|
|
'role_id' => $role->id
|
2020-06-22 17:45:00 +02:00
|
|
|
]);
|
|
|
|
$this->save($user);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|