cfmgrv3/app/Models/Cloudflare/API/FirewallModel.php
2023-06-20 15:56:49 +09:00

99 lines
3.6 KiB
PHP

<?php
namespace App\Models\Cloudflare\API;
use App\Entities\Cloudflare\API\FirewallEntity;
use CodeIgniter\Model;
class FirewallModel extends Model
{
const PARENT_FIELD = "zone_uid";
protected $DBGroup = "default";
protected $table = "cloudflarefirewall";
protected $primaryKey = "uid";
protected $useAutoIncrement = false;
protected $insertID = 0;
protected $returnType = "array"; //object,array,entity명::class
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = ["uid", "zone_uid", "description", "filter_id", "filter_expression", "filter_paused", "paused", "action", "updated_at", "crated_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|min_length[10]|max_length[200]",
"zone_uid" => "if_exist|min_length[10]|max_length[200]",
"description" => "if_exist|string",
"filter_id" => "if_exist|min_length[10]|max_length[200]",
"filter_expression" => "if_exist|string",
"filter_paused" => "if_exist|in_list[on,off]",
"paused" => "if_exist|in_list[on,off]",
"action" => "if_exist|string",
"updated_at" => "if_exist|valid_date",
"created_at" => "if_exist|valid_date",
];
protected $validationMessages = [];
protected $skipValidation = true;
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 getTableName()
{
return $this->table;
}
public function getEntity(string $uid): null|FirewallEntity
{
$entity = $this->asObject(FirewallEntity::class)->where("uid", $uid)->first();
if (is_null($entity)) {
throw new \Exception(__METHOD__ . "에서 {$uid} 해당 정보가 없습니다.");
}
return $entity;
}
//Index 검색용
private function getFindWordByDomain($word): array
{
//Zone의 Domain에서 검색후 해당 검색어의 Firewall정보 출력용
$query = $this->db()->table("cloudflarezone")->select("uid")->like("domain", $word, "both");
// throw new \Exception($query->getCompiledSelect());
$zone_uids = array();
foreach ($query->get()->getResult() as $row) {
array_push($zone_uids, $row->uid);
}
// throw new \Exception(var_export($zone_uids, true));
return $zone_uids;
}
public function setIndexWordFilter(string $word)
{
// $this->like("description", $word, "both"); //befor , after , both
// $this->orLike("filter_expression", $word, "both");
$this->orWhereIn("zone_uid", $this->getFindWordByDomain($word));
}
public function setIndexDateFilter($start, $end)
{
$this->where("created_at >=", $start);
$this->where("created_at <=", $end);
}
public function setIndexOrderBy($field, $order = "ASC")
{
$this->orderBy("zone_uid ASC, description ASC, {$field} {$order}");
}
}