cfmgrv4 init...4

This commit is contained in:
최준흠 2024-10-30 11:49:31 +09:00
parent 62f6a86d53
commit 347c4a7e07
11 changed files with 52 additions and 103 deletions

View File

@ -37,19 +37,25 @@ class AuditLogController extends CloudflareController
private function init(string $action, array $fields = []): void private function init(string $action, array $fields = []): void
{ {
$this->action = $action; $this->action = $action;
$this->fields = count($fields) ? $fields : [$this->getModel()::TITLE, 'actor', 'interface', 'zone_name', 'resource_type', 'resource_name', 'status', 'updated_at', 'created_at']; $this->fields = count($fields) ? $fields : ['zone_name', $this->getModel()::TITLE, 'action_info', 'actor', 'interface', 'status', 'updated_at', 'created_at'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = ['status']; $this->filter_fields = [];
$this->field_options = $this->getFormFieldOptions($this->filter_fields); $this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = []; $this->batchjob_fields = [];
} }
//View //View
public function view(string $uid): RedirectResponse|string public function view(string $uid): RedirectResponse|string
{ {
$this->init(__FUNCTION__, [$this->getModel()::TITLE, 'action_info', 'actor', 'interface', 'zone_id', 'zone_name', 'resource_id', 'resource_type', 'resource_name', 'status', 'updated_at', 'created_at']); $this->init(__FUNCTION__, ['zone_name', $this->getModel()::TITLE, 'actor', 'action_info', 'interface', 'meta', 'resource', 'status', 'updated_at', 'created_at']);
return $this->view_procedure($uid); return $this->view_procedure($uid);
} }
// 리스트 // 리스트
protected function list_entitys_process(): array
{
//기본Soring처리
$this->getModel()->orderBy("created_at DESC,zone_name ASC");
return parent::list_entitys_process();
}
public function index(): string public function index(): string
{ {
$this->init(__FUNCTION__); $this->init(__FUNCTION__);

View File

@ -182,21 +182,8 @@ class RecordController extends CloudflareController
//리스트 //리스트
protected function list_entitys_process(): array protected function list_entitys_process(): array
{ {
$this->list_condition_process();
//기본Soring처리 //기본Soring처리
$this->getModel()->orderBy(ZoneModel::TABLE . "." . ZoneModel::TITLE . " ASC ," . $this->getModel()::TITLE . " ASC"); $this->getModel()->orderBy(ZoneModel::TABLE . "." . ZoneModel::TITLE . " ASC ," . $this->getModel()::TITLE . " ASC");
//Sorting 처리
$this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY'];
$this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY'];
if ($this->order_field !== DEFAULTS['EMPTY'] && $this->order_value !== DEFAULTS['EMPTY']) {
$this->getModel()->orderBy(sprintf(
"%s.%s %s",
$this->getModel()::TABLE,
$this->order_field,
$this->order_value
));
}
$this->getModel()->limit($this->per_page, $this->page * $this->per_page - $this->per_page);
//Join을 해서 도메인부터 Sorting하기위함 //Join을 해서 도메인부터 Sorting하기위함
$this->getModel()->join(ZoneModel::TABLE, sprintf( $this->getModel()->join(ZoneModel::TABLE, sprintf(
"%s.%s=%s.%s", "%s.%s=%s.%s",
@ -205,9 +192,7 @@ class RecordController extends CloudflareController
ZoneModel::TABLE, ZoneModel::TABLE,
ZoneModel::PK ZoneModel::PK
)); ));
$entitys = $this->getModel()->select($this->getModel()::TABLE . '.*')->findAll(); return parent::list_entitys_process();
log_message("debug", $this->getModel()->getLastQuery());
return $entitys;
} }
public function index(): string public function index(): string
{ {

View File

@ -226,22 +226,9 @@ class ZoneController extends CloudflareController
// 리스트 // 리스트
protected function list_entitys_process(): array protected function list_entitys_process(): array
{ {
$this->list_condition_process();
//기본Soring처리 //기본Soring처리
$this->getModel()->orderBy(orderBy: AccountModel::TABLE . "." . AccountModel::TITLE . " ASC ," . $this->getModel()::TITLE . " ASC"); $this->getModel()->orderBy(orderBy: AccountModel::TABLE . "." . AccountModel::TITLE . " ASC ," . $this->getModel()::TITLE . " ASC");
//Sorting 처리 //Join을 해서 Account부터 Sorting하기위함
$this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY'];
$this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY'];
if ($this->order_field !== DEFAULTS['EMPTY'] && $this->order_value !== DEFAULTS['EMPTY']) {
$this->getModel()->orderBy(sprintf(
"%s.%s %s",
$this->getModel()::TABLE,
$this->order_field,
$this->order_value
));
}
$this->getModel()->limit($this->per_page, $this->page * $this->per_page - $this->per_page);
//Join을 해서 도메인부터 Sorting하기위함
$this->getModel()->join(AccountModel::TABLE, sprintf( $this->getModel()->join(AccountModel::TABLE, sprintf(
"%s.%s=%s.%s", "%s.%s=%s.%s",
$this->getModel()::TABLE, $this->getModel()::TABLE,
@ -249,9 +236,7 @@ class ZoneController extends CloudflareController
AccountModel::TABLE, AccountModel::TABLE,
AccountModel::PK AccountModel::PK
)); ));
$entitys = $this->getModel()->select($this->getModel()::TABLE . '.*')->findAll(); return parent::list_entitys_process();
log_message("debug", $this->getModel()->getLastQuery());
return $entitys;
} }
public function index(): string public function index(): string
{ {

View File

@ -386,7 +386,12 @@ abstract class MVController extends CommonController
$this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY']; $this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY'];
$this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY']; $this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY'];
if ($this->order_field !== DEFAULTS['EMPTY'] && $this->order_value !== DEFAULTS['EMPTY']) { if ($this->order_field !== DEFAULTS['EMPTY'] && $this->order_value !== DEFAULTS['EMPTY']) {
$this->getModel()->orderBy("{$this->order_field} {$this->order_value}"); $this->getModel()->orderBy(sprintf(
"%s.%s %s",
$this->getModel()::TABLE,
$this->order_field,
$this->order_value
));
} }
$this->getModel()->limit($this->per_page, $this->page * $this->per_page - $this->per_page); $this->getModel()->limit($this->per_page, $this->page * $this->per_page - $this->per_page);
$entitys = $this->getModel()->select($this->getModel()::TABLE . '.*')->findAll(); $entitys = $this->getModel()->select($this->getModel()::TABLE . '.*')->findAll();

View File

@ -25,17 +25,16 @@ ALTER TABLE cloudflareaccount ADD UNIQUE key cloudflareaccount_ibuk_1 (id);
ALTER TABLE cloudflareaccount ADD UNIQUE key cloudflareaccount_ibuk_2 (authkey); ALTER TABLE cloudflareaccount ADD UNIQUE key cloudflareaccount_ibuk_2 (authkey);
6. auditlog용 table추가 6. auditlog용 table추가
DROP TABLE cloudflareauditlog;
CREATE TABLE cloudflareauditlog ( CREATE TABLE cloudflareauditlog (
uid varchar(255) NOT NULL COMMENT 'id', uid varchar(255) NOT NULL COMMENT 'id',
action varchar(100) NOT NULL COMMENT 'action->type', action varchar(100) NOT NULL COMMENT 'action->type',
action_info varchar(255) NULL COMMENT 'action->info', action_info varchar(255) NULL COMMENT 'action->info',
actor varchar(100) NOT NULL COMMENT 'actor->type', actor varchar(100) NOT NULL COMMENT 'actor->type',
interface varchar(100) NULL COMMENT 'interface', interface varchar(100) NULL COMMENT 'interface',
zone_id varchar(100) NOT NULL COMMENT 'newValueJson->zone_id',
zone_name varchar(100) NULL COMMENT 'newValueJson->zone_name', zone_name varchar(100) NULL COMMENT 'newValueJson->zone_name',
resource_id varchar(100) NOT NULL COMMENT 'newValueJson->id', meta text NULL COMMENT 'meta domain settings info',
resource_name varchar(100) NULL COMMENT 'newValueJson->name', resource text NULL COMMENT 'newValueJson',
resource_type varchar(50) NULL COMMENT 'newValueJson->type',
status varchar(10) NOT NULL COMMENT 'action->result', status varchar(10) NOT NULL COMMENT 'action->result',
updated_at timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT NULL,
created_at timestamp NOT NULL COMMENT 'when', created_at timestamp NOT NULL COMMENT 'when',

View File

@ -24,8 +24,8 @@ class AuditLogEntity extends CommonEntity
$this->attributes[AuditLogModel::TITLE] = $title; $this->attributes[AuditLogModel::TITLE] = $title;
} }
//Common Function //Common Function
public function getZoneID(): string public function getZoneName(): string
{ {
return $this->attributes['zone_id']; return $this->attributes['zone_name'];
} }
} }

View File

@ -45,9 +45,6 @@ class AuditLogHelper extends MVCHelper
] ]
); );
break; break;
case 'status':
$value = $viewDatas['field_options'][$field][$value];
break;
default: default:
$value = parent::getFieldView($field, $viewDatas, $extras); $value = parent::getFieldView($field, $viewDatas, $extras);
break; break;

View File

@ -7,17 +7,11 @@ return [
'action_info' => "ActInfo", 'action_info' => "ActInfo",
'actor' => "작업자", 'actor' => "작업자",
'interface' => "UI형식", 'interface' => "UI형식",
'zone_id' => "도메인ID",
'zone_name' => "도메인", 'zone_name' => "도메인",
'resource_id' => "자원ID", 'meta' => "meta정보",
'resource_name' => "자원명", 'resource' => "자원정보",
'resource_type' => "자원형식",
'status' => "상태", 'status' => "상태",
'updated_at' => "수정일", 'updated_at' => "수정일",
'created_at' => "작성일", 'created_at' => "작성일",
], ],
"STATUS" => [
"true" => "완료",
"false" => "실패",
],
]; ];

View File

@ -15,7 +15,7 @@ class AuditLogModel extends CommonModel
protected $primaryKey = self::PK; protected $primaryKey = self::PK;
protected $useAutoIncrement = false; protected $useAutoIncrement = false;
protected $returnType = AuditLogEntity::class; //object,array,entity명::class protected $returnType = AuditLogEntity::class; //object,array,entity명::class
protected $allowedFields = [self::PK, self::TITLE, 'action_info', 'actor', 'interface', 'zone_id', 'zone_name', 'resource_id', 'resource_name', 'resource_type', 'status', 'updated_at', 'created_at']; protected $allowedFields = [self::PK, self::TITLE, 'action_info', 'actor', 'interface', 'zone_name', 'meta', 'resource', 'status', 'updated_at', 'created_at'];
protected $useTimestamps = true; protected $useTimestamps = true;
public function __construct() public function __construct()
@ -35,19 +35,17 @@ class AuditLogModel extends CommonModel
break; break;
case self::TITLE: case self::TITLE:
case 'actor': case 'actor':
case 'zone_id': case 'zone_name':
case 'resource_id': case "status":
$rule = "required|trim|string"; $rule = "required|trim|string";
break; break;
case 'interface': case 'interface':
case 'action_info': case 'action_info':
case 'zone_name': $rule = "if_exist|string";
case 'resource_name':
case 'resource_type':
$rule = "if_exist|trim|string";
break; break;
case "status": case 'meta':
$rule = "if_exist|in_list[true,false]"; case 'resource':
$rule = "if_exist";
break; break;
default: default:
$rule = parent::getFieldRule($action, $field); $rule = parent::getFieldRule($action, $field);
@ -55,16 +53,6 @@ class AuditLogModel extends CommonModel
} }
return $rule; return $rule;
} }
public function getFormFieldInputOption(string $field, array $options = []): array
{
switch ($field) {
default:
$this->orderBy(self::TITLE, 'asc');
$options = parent::getFormFieldInputOption($field, $options);
break;
}
return $options;
}
public function getEntityByPK(string $uid): null|AuditLogEntity public function getEntityByPK(string $uid): null|AuditLogEntity
{ {
$this->where(self::PK, $uid); $this->where(self::PK, $uid);
@ -90,6 +78,7 @@ class AuditLogModel extends CommonModel
{ {
parent::setList_WordFilter($word, $field); parent::setList_WordFilter($word, $field);
$this->orLike(self::TABLE . '.zone_name', $word, 'both'); $this->orLike(self::TABLE . '.zone_name', $word, 'both');
$this->orLike(self::TABLE . '.resource_name', $word, 'both'); $this->orLike(self::TABLE . '.meta', $word, 'both');
$this->orLike(self::TABLE . '.resource', $word, 'both');
} }
} }

View File

@ -4,14 +4,14 @@ namespace App\Services\Cloudflare;
use App\Entities\Cloudflare\AccountEntity; use App\Entities\Cloudflare\AccountEntity;
use App\Entities\Cloudflare\AuditLogEntity; use App\Entities\Cloudflare\AuditLogEntity;
use App\Models\Cloudflare\AccountModel; use App\Models\Cloudflare\ZoneModel;
use App\Models\Cloudflare\AuditLogModel; use App\Models\Cloudflare\AuditLogModel;
use stdClass; use stdClass;
class AuditLogService extends CloudflareService class AuditLogService extends CloudflareService
{ {
private ?AuditLogModel $_model = null; private ?AuditLogModel $_model = null;
private ?AccountModel $_accountModel = null; private ?ZoneModel $_zoneModel = null;
public function __construct() public function __construct()
{ {
$this->class_name = "AuditLog"; $this->class_name = "AuditLog";
@ -25,12 +25,12 @@ class AuditLogService extends CloudflareService
} }
return $this->_model; return $this->_model;
} }
protected function getAccountModel(): AccountModel protected function getZoneModel(): ZoneModel
{ {
if ($this->_accountModel === null) { if ($this->_zoneModel === null) {
$this->_accountModel = new AccountModel(); $this->_zoneModel = new ZoneModel();
} }
return $this->_accountModel; return $this->_zoneModel;
} }
protected function getArrayByResult(\stdClass $result, array $formDatas = []): array protected function getArrayByResult(\stdClass $result, array $formDatas = []): array
@ -40,11 +40,9 @@ class AuditLogService extends CloudflareService
$formDatas['action_info'] = isset($result->action->info) ? $result->action->info : ""; $formDatas['action_info'] = isset($result->action->info) ? $result->action->info : "";
$formDatas['actor'] = $result->actor->type; $formDatas['actor'] = $result->actor->type;
$formDatas['interface'] = isset($result->interface) ? $result->interface : ""; $formDatas['interface'] = isset($result->interface) ? $result->interface : "";
$formDatas['zone_id'] = $result->newValueJson->zone_id; $formDatas['zone_name'] = $result->metadata->zone_name;
$formDatas['zone_name'] = isset($result->newValueJson->zone_name) ? $result->newValueJson->zone_name : ""; $formDatas['meta'] = isset($result->metadata) ? var_export($result->metadata, true) : "";
$formDatas['resource_id'] = $result->newValueJson->id; $formDatas['resource'] = isset($result->newValueJson) ? var_export($result->newValueJson, true) : "";
$formDatas['resource_name'] = isset($result->newValueJson->name) ? $result->newValueJson->name : "";
$formDatas['resource_type'] = $result->newValueJson->type ? $result->newValueJson->type : "";
$formDatas['status'] = $result->action->result ? "true" : "false"; $formDatas['status'] = $result->action->result ? "true" : "false";
$formDatas['updated_at'] = date("Y-m-d H:i:s"); $formDatas['updated_at'] = date("Y-m-d H:i:s");
$formDatas['created_at'] = $result->when; $formDatas['created_at'] = $result->when;
@ -54,8 +52,8 @@ class AuditLogService extends CloudflareService
private function auditlog_process(AuditLogEntity $entity): void private function auditlog_process(AuditLogEntity $entity): void
{ {
//해당 Zone을 Sync작업한다 //해당 Zone을 Sync작업한다
$zone_service = new ZoneService(); $this->getZoneModel()->where('domain', $entity->getZoneName());
$zone_entity = $zone_service->getEntityByPK($entity->getZoneID()); $zone_entity = $this->getZoneModel()->getEntity();
if ($zone_entity !== null) { if ($zone_entity !== null) {
// $zone_entity = $zone_service->sync($entity, $zone_entity); // $zone_entity = $zone_service->sync($entity, $zone_entity);
// //해당 Zone의 Record reload작업한다 // //해당 Zone의 Record reload작업한다
@ -80,19 +78,15 @@ class AuditLogService extends CloudflareService
try { try {
// 오늘 날짜의 ISO 8601 형식 문자열 생성 // 오늘 날짜의 ISO 8601 형식 문자열 생성
$today = date('Y-m-d') . 'T00:00:00'; $today = date('Y-m-d') . 'T00:00:00';
$endpoint = sprintf("accounts/%s/audit_logs?since=%s", $account_entity->getPK(), $today); $response = $this->getMySocket()->get("accounts/{$account_entity->getPK()}/audit_logs", ['since' => $today]);
log_message("debug", "Auditlog Endpoint: " . $endpoint);
$response = $this->getMySocket()->get($endpoint);
$body = json_decode($response->getBody()); $body = json_decode($response->getBody());
foreach ($body->result as $result) { foreach ($body->result as $result) {
if (isset($result->action->result) && $result->action->result && isset($result->newValueJson->zone_id) && isset($result->newValueJson->zone_name)) { if (isset($result->action->result) && $result->action->result && isset($result->metadata->zone_name)) {
// log_message("debug", var_export($result->newValueJson, true)); $formDatas = $this->getArrayByResult($result);
$entity = $this->getModel()->getEntityByPK($result->id); $entity = $this->getModel()->modify(new AuditlogEntity(), $formDatas);
if ($entity === null) { $this->auditlog_process($entity);
$formDatas = $this->getArrayByResult($result); } else {
$entity = $this->getModel()->modify(new AuditlogEntity(), $formDatas); log_message("debug", var_export($result, true));
$this->auditlog_process($entity);
}
} }
} }
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -46,11 +46,6 @@ class ZoneService extends CloudflareService
} }
return $this->_model; return $this->_model;
} }
public function getEntityByPK(string $uid): null|ZoneEntity
{
return $this->getModel()->getEntityByPK($uid);
}
protected function getArrayByResult(\stdClass $result, array $formDatas = []): array protected function getArrayByResult(\stdClass $result, array $formDatas = []): array
{ {
// log_message("debug", var_export($result, true)); // log_message("debug", var_export($result, true));