cerebrate/src/Model/Table/AuthKeysTable.php

49 lines
1.2 KiB
PHP
Raw Normal View History

2020-06-21 21:31:30 +02:00
<?php
namespace App\Model\Table;
use App\Model\Table\AppTable;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Event\EventInterface;
use Cake\Utility\Security;
use ArrayObject;
class AuthKeysTable extends AppTable
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->addBehavior('UUID');
$this->belongsTo(
'Users'
);
$this->setDisplayField('authkey');
}
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options)
{
$data['created'] = time();
if (empty($data['valid_until'])) {
$data['valid_until'] = 0;
}
if (empty($data['authkey'])) {
$data['authkey'] = $this->generateAuthKey();
}
}
public function generateAuthKey()
{
return Security::randomString(40);
}
public function validationDefault(Validator $validator): Validator
{
$validator
->notEmptyString('authkey')
->notEmptyString('user_id')
->requirePresence(['authkey', 'user_id'], 'create');
return $validator;
}
}