cfmgrv3/app/Models/Cloudflare/API/AuthModel.php
2024-05-15 11:06:17 +09:00

87 lines
2.8 KiB
PHP

<?php
namespace App\Models\Cloudflare\API;
use App\Entities\Cloudflare\API\AuthEntity;
use CodeIgniter\Model;
class AuthModel extends Model
{
protected $DBGroup = 'default';
protected $table = 'cloudflareauth';
protected $primaryKey = 'uid';
protected $useAutoIncrement = true;
protected $insertID = 0;
protected $returnType = 'array'; //object,array,entity명::class
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = ['id', 'authkey', 'oldkey', 'status', 'updated_at', 'created_at'];
// Dates
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [
'uid' => 'if_exist|numeric',
'id' => 'if_exist|valid_email',
'authkey' => 'if_exist|min_length[10]|max_length[200]',
'oldkey' => 'if_exist|min_length[10]|max_length[200]',
'status' => 'if_exist|in_list[use,unuse]',
'updated_at' => 'if_exist|valid_date',
'created_at' => 'if_exist|valid_date',
];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
public function getEntity(string $uid): AuthEntity
{
$entity = $this->asObject(AuthEntity::class)->where('uid', $uid)->first();
if (is_null($entity)) {
throw new \Exception(__METHOD__ . "에서 {$uid} 해당 정보가 없습니다.");
}
return $entity;
}
public function getEntitys(array $wheres)
{
return $this->asObject(AuthEntity::class)->where($wheres)->findAll();
}
//Index 검색용
public function setIndexWordFilter(string $word)
{
$this->like('id', $word, 'both'); //befor , after , both
}
public function setIndexDateFilter($start, $end)
{
$this->where('created_at >=', $start);
$this->where('created_at <=', $end);
}
public function setIndexOrderBy($field, $order = 'ASC')
{
$this->orderBy("id ASC, {$field} {$order}");
}
//도메인이 이미 존재하는지 체크
public function isUniqueApikey(string $uid): bool
{
$this->where('uid', $uid);
return is_null($this->find()) ? true : false;
}
}