Automation init...4
This commit is contained in:
parent
f68a1c639c
commit
269a1bd038
@ -121,6 +121,7 @@ define('ROLES', [
|
||||
//SESSION 관련
|
||||
define('SESSION_NAMES', [
|
||||
'RETURN_URL' => "return_url",
|
||||
'RETURN_MSG' => "return_message",
|
||||
'ISLOGIN' => "islogined",
|
||||
'AUTH' => 'auth',
|
||||
]);
|
||||
|
||||
@ -6,9 +6,9 @@ use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
use App\Traits\AuthTrait;
|
||||
use App\Controllers\CommonController;
|
||||
use App\Models\UserModel;
|
||||
use App\Traits\AuthTrait;
|
||||
|
||||
class UserController extends CommonController
|
||||
{
|
||||
@ -17,7 +17,7 @@ class UserController extends CommonController
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
parent::initController($request, $response, $logger);
|
||||
$this->session = $this->loginCheck_AuthTrait();
|
||||
$this->session = $this->session_AuthTrait();
|
||||
}
|
||||
private function getModel(): UserModel
|
||||
{
|
||||
|
||||
@ -2,47 +2,116 @@
|
||||
|
||||
namespace App\Controllers\Cloudflare;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
|
||||
use App\Entities\Cloudflare\AccountEntity;
|
||||
use App\Libraries\MySocket\Cloudflare\AccountSocket;
|
||||
use App\Libraries\MyStorage\AccountStorage;
|
||||
|
||||
use CodeIgniter\HTTP\RedirectResponse;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
use App\Models\Cloudflare\AccountModel;
|
||||
use App\Traits\AccountTrait;
|
||||
|
||||
class AccountController extends MyCloudflare
|
||||
{
|
||||
private $_email = "";
|
||||
private $_api_key = "";
|
||||
use AccountTrait;
|
||||
private $_mySocket = null;
|
||||
private $_myStorage = null;
|
||||
private $_model = null;
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
parent::initController($request, $response, $logger);
|
||||
$this->session = $this->session_AccountTrait();
|
||||
$this->class_name = 'Account';
|
||||
helper($this->class_name);
|
||||
}
|
||||
final protected function getMySocket(): AccountSocket
|
||||
{
|
||||
if ($this->_mySocket === null) {
|
||||
$this->_mySocket = new AccountSocket($this->_email, $this->_api_key);
|
||||
$this->_mySocket = new AccountSocket($auth_uid);
|
||||
}
|
||||
return $this->_mySocket;
|
||||
}
|
||||
final protected function getMyStorage(): AccountStorage
|
||||
|
||||
final protected function getModel(): AccountModel
|
||||
{
|
||||
if ($this->_myStorage === null) {
|
||||
$this->_myStorage = new AccountStorage();
|
||||
if ($this->_model === null) {
|
||||
$this->_model = new AccountModel();
|
||||
}
|
||||
return $this->_myStorage;
|
||||
return $this->_model;
|
||||
}
|
||||
|
||||
public function create(): void
|
||||
protected function create_form_process(): void
|
||||
{
|
||||
$this->_email = $this->request->getVar('email');
|
||||
$this->_api_key = $this->request->getVar('key');
|
||||
//전송
|
||||
$result = $this->getMySocket()->create_process($this->_email);
|
||||
$formDatas = [];
|
||||
$formDatas['key'] = $this->_api_key;
|
||||
$entity = $this->getMyStorage()->create_process($result, $formDatas);
|
||||
log_message("notice", __FUNCTION__ . "=> 작업을 완료하였습니다.");
|
||||
$this->fields = ['id', 'authkey'];
|
||||
$this->filter_fields = ['status'];
|
||||
parent::create_form_process();
|
||||
}
|
||||
public function create_form(): RedirectResponse|string
|
||||
{
|
||||
try {
|
||||
$this->fields = ['id', 'authkey'];
|
||||
$this->filter_fields = ['status'];
|
||||
$this->action = 'create';
|
||||
$this->getModel()->setAction($this->action);
|
||||
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
|
||||
return view("/{$this->class_name}/insert", ['attributes' => $this->getAttributes]);
|
||||
} catch (\Exception $e) {
|
||||
log_message("error", $e->getMessage());
|
||||
return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/")->with(SESSION_NAMES['RETURN_MSG'], $e->getMessage());
|
||||
}
|
||||
}
|
||||
protected function create_process($model): AccountEntity
|
||||
{
|
||||
$datas = [
|
||||
'name' => $name . "'s Account",
|
||||
'type' => 'standard',
|
||||
];
|
||||
$result = $this->getClient()->post("accounts", $datas);
|
||||
$result = json_decode($result->getBody());
|
||||
if (!$result->success) {
|
||||
throw new \Exception(var_export($result, true));
|
||||
}
|
||||
//Result 형태
|
||||
// [
|
||||
// {"id":"078e88a7735965b661715af13031ecb0",
|
||||
// "name":"Cloudwin002@idcjp.jp's Auth",
|
||||
// "type":"standard",
|
||||
// "settings":{
|
||||
// "enforce_twofactor":false,
|
||||
// "api_access_enabled":null,
|
||||
// "access_approval_expiry":null,
|
||||
// "use_account_custom_ns_by_default":false
|
||||
// },
|
||||
// "legacy_flags":{"enterprise_zone_quota":{"maximum":0,"current":0,"available":0}},
|
||||
// "created_on":"2017-06-26T05:44:49.470184Z"}
|
||||
// ]
|
||||
$formDatas[$this->getAuthModel()->PK()] = $result->id;
|
||||
$formDatas[$this->getAuthModel()->getTitleField()] = $result->name;
|
||||
$formDatas['type'] = $result->type;
|
||||
$formDatas['status'] = 'use';
|
||||
$formDatas['updated_at'] = $result->created_on;
|
||||
$formDatas['created_at'] = $result->created_on;
|
||||
return parent::create_process($model);
|
||||
}
|
||||
public function create(): RedirectResponse
|
||||
{
|
||||
$this->getModel()->transStart();
|
||||
try {
|
||||
$this->fields = ['id', 'authkey'];
|
||||
$this->filter_fields = ['status'];
|
||||
$this->action = 'create';
|
||||
$this->getModel()->setAction($this->action);
|
||||
$entity = $this->create_process($this->getModel());
|
||||
log_message("notice", __FUNCTION__ . "=>{$entity->getTitle()} 작업을 완료하였습니다.");
|
||||
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/");
|
||||
} catch (\Exception $e) {
|
||||
//Transaction Rollback
|
||||
$this->getModel()->transRollback();
|
||||
log_message("error", $e->getMessage());
|
||||
$this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
|
||||
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ class AccountController extends MyCloudflare
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
parent::initController($request, $response, $logger);
|
||||
$this->session = $this->loginCheck_AuthTrait();
|
||||
$this->session = $this->session_AuthTrait();
|
||||
}
|
||||
final protected function getMySocket(): AccountSocket
|
||||
{
|
||||
|
||||
79
app/Controllers/Cloudflare/Admin/AuthController.php
Normal file
79
app/Controllers/Cloudflare/Admin/AuthController.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Cloudflare\Admin;
|
||||
|
||||
use App\Controllers\CommonController;
|
||||
|
||||
use App\Entities\Cloudflare\AuthEntity;
|
||||
use App\Models\Cloudflare\AuthModel;
|
||||
use App\Traits\AuthTrait;
|
||||
|
||||
use CodeIgniter\HTTP\RedirectResponse;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class AuthController extends CommonController
|
||||
{
|
||||
use AuthTrait;
|
||||
private $_model = null;
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
parent::initController($request, $response, $logger);
|
||||
$this->session = $this->session_AuthTrait();
|
||||
$this->class_name = 'Auth';
|
||||
helper($this->class_name);
|
||||
}
|
||||
final protected function getModel(): AuthModel
|
||||
{
|
||||
if ($this->_model === null) {
|
||||
$this->_model = new AuthModel();
|
||||
}
|
||||
return $this->_model;
|
||||
}
|
||||
|
||||
protected function create_form_process(): void
|
||||
{
|
||||
$this->fields = ['id', 'authkey'];
|
||||
$this->filter_fields = ['status'];
|
||||
parent::create_form_process();
|
||||
}
|
||||
public function create_form(): RedirectResponse|string
|
||||
{
|
||||
try {
|
||||
$this->fields = ['id', 'authkey'];
|
||||
$this->filter_fields = ['status'];
|
||||
$this->action = 'create';
|
||||
$this->getModel()->setAction($this->action);
|
||||
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
|
||||
return view("/{$this->class_name}/insert", ['attributes' => $this->getAttributes]);
|
||||
} catch (\Exception $e) {
|
||||
log_message("error", $e->getMessage());
|
||||
return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/")->with(SESSION_NAMES['RETURN_MSG'], $e->getMessage());
|
||||
}
|
||||
}
|
||||
protected function create_process($model): AuthEntity
|
||||
{
|
||||
return parent::create_process($model);
|
||||
}
|
||||
public function create(): RedirectResponse
|
||||
{
|
||||
$this->getModel()->transStart();
|
||||
try {
|
||||
$this->fields = ['id', 'authkey'];
|
||||
$this->filter_fields = ['status'];
|
||||
$this->action = 'create';
|
||||
$this->getModel()->setAction($this->action);
|
||||
$entity = $this->create_process($this->getModel());
|
||||
log_message("notice", __FUNCTION__ . "=>{$entity->getTitle()} 작업을 완료하였습니다.");
|
||||
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/");
|
||||
} catch (\Exception $e) {
|
||||
//Transaction Rollback
|
||||
$this->getModel()->transRollback();
|
||||
log_message("error", $e->getMessage());
|
||||
$this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
|
||||
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,63 +3,59 @@
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
abstract class CommonController extends BaseController
|
||||
{
|
||||
private $_options = [];
|
||||
private $_attributes = [];
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
parent::initController($request, $response, $logger);
|
||||
}
|
||||
final public function __get($name)
|
||||
{
|
||||
if (!array_key_exists($name, $this->_options)) {
|
||||
if (!array_key_exists($name, $this->_attributes)) {
|
||||
return null;
|
||||
}
|
||||
return $this->_options[$name];
|
||||
return $this->_attributes[$name];
|
||||
}
|
||||
|
||||
final public function __set($name, $value): void
|
||||
{
|
||||
$this->_options[$name] = $value;
|
||||
$this->_attributes[$name] = $value;
|
||||
}
|
||||
|
||||
//전송된 값 검증 및 임시저장
|
||||
final public function getFormFieldDatas(array $fields, array $fieldRules, array $formDatas = []): array
|
||||
final public function getAttributes(): array
|
||||
{
|
||||
foreach ($fields as $field) {
|
||||
$formDatas[$field] = rtrim($this->request->getVar($field));
|
||||
log_message("debug", "{$field} : {$formDatas[$field]}");
|
||||
}
|
||||
//변경할 값 확인
|
||||
if (!$this->validate($fieldRules)) {
|
||||
throw new \Exception("데이터 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
|
||||
}
|
||||
return $formDatas;
|
||||
return $this->_attributes;
|
||||
}
|
||||
//Field별 Form Option용
|
||||
protected function getFormFieldOption(string $field, array $options): array
|
||||
protected function getFormFilterOption(string $filter_field, array $filter_options): array
|
||||
{
|
||||
switch ($field) {
|
||||
switch ($filter_field) {
|
||||
default:
|
||||
$temps = lang($this->_className . '.' . strtoupper($field));
|
||||
$temps = lang($this->class_name . '.' . strtoupper($filter_field));
|
||||
if (!is_array($temps)) {
|
||||
throw new \Exception(__FUNCTION__ . "에서 {$field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true));
|
||||
throw new \Exception(__FUNCTION__ . "에서 {$filter_field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true));
|
||||
}
|
||||
$options[$field] = [
|
||||
["" => lang($this->_className . '.label.' . $field) . ' 선택'],
|
||||
lang($this->_className . '.' . strtoupper($field))
|
||||
$filter_options[$filter_field] = [
|
||||
["" => lang($this->class_name . '.label.' . $filter_field) . ' 선택'],
|
||||
lang($this->class_name . '.' . strtoupper($filter_field))
|
||||
];
|
||||
break;
|
||||
}
|
||||
return $options;
|
||||
return $filter_options;
|
||||
}
|
||||
//Field별 Form Option용
|
||||
final public function getFormFieldOptions(array $fields, array $options = []): array
|
||||
final public function getFormFilterOptions(array $filter_options = []): array
|
||||
{
|
||||
foreach ($fields as $field) {
|
||||
if (is_array($field)) {
|
||||
throw new \Exception(__FUNCTION__ . "에서 field가 array 입니다.\n" . var_export($field, true));
|
||||
foreach ($this->filter_fields as $filter_field) {
|
||||
if (is_array($filter_field)) {
|
||||
throw new \Exception(__FUNCTION__ . "에서 filter_field가 array 입니다.\n" . var_export($filter_field, true));
|
||||
}
|
||||
$options = $this->getFormFieldOption($field, $options);
|
||||
$filter_options = $this->getFormFilterOption($filter_field, $filter_options);
|
||||
}
|
||||
return $options;
|
||||
return $filter_options;
|
||||
}
|
||||
protected function getFormFieldRule(string $field, array $rules): array
|
||||
{
|
||||
@ -73,11 +69,48 @@ abstract class CommonController extends BaseController
|
||||
}
|
||||
return $rules;
|
||||
}
|
||||
final public function getFormFieldRules(array $fields, array $rules = []): array
|
||||
final public function getFormFieldRules(array $rules = []): array
|
||||
{
|
||||
foreach ($fields as $field) {
|
||||
foreach ($this->fields as $field) {
|
||||
$rules = $this->getFormFieldRule($field, $rules);
|
||||
}
|
||||
return $rules;
|
||||
}
|
||||
//전송된 데이터
|
||||
protected function getFormData(string $field): void
|
||||
{
|
||||
switch ($field) {
|
||||
default:
|
||||
$this->formDatas[$field] = rtrim($this->request->getVar($field));
|
||||
break;
|
||||
}
|
||||
}
|
||||
//전송된 데이터 재정의
|
||||
protected function convertFormData(string $field): void
|
||||
{
|
||||
switch ($field) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
protected function create_form_process(): void
|
||||
{
|
||||
helper(['form']);
|
||||
}
|
||||
protected function create_process($model): mixed
|
||||
{
|
||||
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요
|
||||
$validation = \Config\Services::validation();
|
||||
$validation->setRules($model->getFieldRules($this->fields));
|
||||
if (!$validation->withRequest($this->request)->run()) {
|
||||
throw new \Exception("데이터 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
|
||||
}
|
||||
foreach ($this->fields as $field) {
|
||||
$this->getFormData($field);
|
||||
}
|
||||
foreach ($this->fields as $field) {
|
||||
$this->convertFormData($field);
|
||||
}
|
||||
return $model->create($this->formDatas);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,11 +2,11 @@
|
||||
|
||||
namespace App\Controllers\Mangboard\Admin;
|
||||
|
||||
use App\Controllers\CommonController;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
use App\Controllers\CommonController;
|
||||
use App\Models\Mangboard\UserModel;
|
||||
|
||||
class UserController extends CommonController
|
||||
|
||||
30
app/Entities/Cloudflare/AuthEntity.php
Normal file
30
app/Entities/Cloudflare/AuthEntity.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entities\Cloudflare;
|
||||
|
||||
use App\Entities\CommonEntity;
|
||||
|
||||
class AuthEntity extends CommonEntity
|
||||
{
|
||||
public function __toString()
|
||||
{
|
||||
return "{$this->getPK()}|{$this->getTitle()}|{$this->getKey()}|{$this->attributes['type']}|{$this->attributes['status']}";
|
||||
}
|
||||
public function getPK(): int
|
||||
{
|
||||
return $this->attributes['uid'];
|
||||
}
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->attributes['id'];
|
||||
}
|
||||
public function setTitle(string $title): void
|
||||
{
|
||||
$this->attributes['id'] = $title;
|
||||
}
|
||||
//Common Function
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->attributes['key'];
|
||||
}
|
||||
}
|
||||
@ -4,18 +4,18 @@ namespace App\Libraries;
|
||||
|
||||
abstract class CommonLibrary
|
||||
{
|
||||
private $_options = [];
|
||||
private $_attributes = [];
|
||||
protected function __construct() {}
|
||||
final public function __get($name)
|
||||
{
|
||||
if (!array_key_exists($name, $this->_options)) {
|
||||
if (!array_key_exists($name, $this->_attributes)) {
|
||||
return null;
|
||||
}
|
||||
return $this->_options[$name];
|
||||
return $this->_attributes[$name];
|
||||
}
|
||||
|
||||
final public function __set($name, $value): void
|
||||
{
|
||||
$this->_options[$name] = $value;
|
||||
$this->_attributes[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,9 +6,9 @@ use App\Libraries\MySocket\CloudflareSocket;
|
||||
|
||||
class AccountSocket extends CloudflareSocket
|
||||
{
|
||||
public function __construct(string $email, string $api_key)
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct($email, $api_key);
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
final public function create_process(string $name, string $type = "standard")
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Libraries\MySocket;
|
||||
|
||||
use App\Libraries\CommonLibrary;
|
||||
use App\Models\Cloudflare\AccountModel;
|
||||
use Cloudflare\API\Adapter\Guzzle;
|
||||
use Cloudflare\API\Auth\APIKey;
|
||||
|
||||
@ -11,20 +12,37 @@ class CloudflareSocket extends CommonLibrary
|
||||
private static int $_request = 0;
|
||||
private static int $_request_max = 100;
|
||||
private static int $_request_timewait = 60;
|
||||
private $_client = null;
|
||||
public function __construct(string $email, string $api_key)
|
||||
private $_accountModel = null;
|
||||
private $_clients = [];
|
||||
private $_auth_uid = "";
|
||||
public function __construct(string $auth_uid)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->_email = $email;
|
||||
$this->_api_key = $api_key;
|
||||
$this->initClients();
|
||||
$this->_auth_uid = $auth_uid;
|
||||
self::$_request_max = getenv("cfmgr.request.max");
|
||||
}
|
||||
|
||||
final protected function getAccountModel(): AccountModel
|
||||
{
|
||||
if ($this->_accountModel === null) {
|
||||
$this->_accountModel = new AccountModel();
|
||||
}
|
||||
return $this->_accountModel;
|
||||
}
|
||||
final public function initClients(): void
|
||||
{
|
||||
foreach ($this->getAccountModel()->getEntitys() as $entity) {
|
||||
$this->_clients[$entity->getKey()] = new Guzzle(
|
||||
new APIKey($entity->getTitle(), $entity->getKey())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final public function getClient(): Guzzle
|
||||
{
|
||||
if ($this->_client === null) {
|
||||
$this->_client = new Guzzle(
|
||||
$this->_client = new APIKey($this->_email, $this->_api_key)
|
||||
);
|
||||
if (!array_key_exists($this->_auth_uid, $this->_clients)) {
|
||||
throw new \Exception(+__FUNCTION__ . " => {$this->_auth_uid}에 해당하는 Adapter를 찾을수 없습니다.");
|
||||
}
|
||||
if (self::$_request >= self::$_request_max) {
|
||||
log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait));
|
||||
|
||||
@ -5,7 +5,6 @@ namespace App\Libraries\MyStorage;
|
||||
use App\Libraries\CommonLibrary;
|
||||
use App\Entities\Cloudflare\AccountEntity;
|
||||
use App\Models\Cloudflare\AccountModel;
|
||||
use App\Traits\ImageTrait;
|
||||
|
||||
class AccountStorage extends CommonLibrary
|
||||
{
|
||||
|
||||
50
app/Libraries/MyStorage/Cloudflare/AuthStorage.php
Normal file
50
app/Libraries/MyStorage/Cloudflare/AuthStorage.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries\MyStorage;
|
||||
|
||||
use App\Libraries\CommonLibrary;
|
||||
use App\Entities\Cloudflare\AuthEntity;
|
||||
use App\Models\Cloudflare\AuthModel;
|
||||
|
||||
class AuthStorage extends CommonLibrary
|
||||
{
|
||||
private $_accountModel = null;
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
final protected function getAuthModel(): AuthModel
|
||||
{
|
||||
if ($this->_accountModel === null) {
|
||||
$this->_accountModel = new AuthModel();
|
||||
}
|
||||
return $this->_accountModel;
|
||||
}
|
||||
|
||||
final public function create_process($result, array $formDatas): AuthEntity
|
||||
{
|
||||
//Result 형태
|
||||
// [
|
||||
// {"id":"078e88a7735965b661715af13031ecb0",
|
||||
// "name":"Cloudwin002@idcjp.jp's Auth",
|
||||
// "type":"standard",
|
||||
// "settings":{
|
||||
// "enforce_twofactor":false,
|
||||
// "api_access_enabled":null,
|
||||
// "access_approval_expiry":null,
|
||||
// "use_account_custom_ns_by_default":false
|
||||
// },
|
||||
// "legacy_flags":{"enterprise_zone_quota":{"maximum":0,"current":0,"available":0}},
|
||||
// "created_on":"2017-06-26T05:44:49.470184Z"}
|
||||
// ]
|
||||
$formDatas[$this->getAuthModel()->PK()] = $result->id;
|
||||
$formDatas[$this->getAuthModel()->getTitleField()] = $result->name;
|
||||
$formDatas['type'] = $result->type;
|
||||
$formDatas['status'] = 'use';
|
||||
$formDatas['updated_at'] = $result->created_on;
|
||||
$formDatas['created_at'] = $result->created_on;
|
||||
$entity = $this->getAuthModel()->create($formDatas);
|
||||
log_message("notice", __FUNCTION__ . "=> {$entity->getTitle()} 생성을 완료하였습니다.");
|
||||
return $entity;
|
||||
}
|
||||
}
|
||||
@ -12,23 +12,17 @@ class AccountModel extends Model
|
||||
protected $primaryKey = 'uid';
|
||||
protected $useAutoIncrement = false;
|
||||
protected $returnType = AccountEntity::class; //object,array,entity명::class
|
||||
protected $allowedFields = ['uid', 'email', 'key', 'oldkey', 'type', 'status', 'updated_at', 'created_at'];
|
||||
protected $allowedFields = ['uid', 'auth_uid', 'title', 'type', 'status', 'updated_at', 'created_at'];
|
||||
protected $useTimestamps = true;
|
||||
public function getTitleField(): string
|
||||
{
|
||||
return 'email';
|
||||
return 'title';
|
||||
}
|
||||
public function getFieldRule(string $field, array $rules): array
|
||||
{
|
||||
switch ($field) {
|
||||
case "email":
|
||||
$rules[$field] = "required|valid_emailvalid_email|is_unique[account.email]";
|
||||
break;
|
||||
case "key":
|
||||
$rules[$field] = "required|trim|smin_length[10]|max_length[200]";
|
||||
break;
|
||||
case "oldkey":
|
||||
$rules[$field] = "if_exist|trim|smin_length[10]|max_length[200]";
|
||||
case "auth_uid":
|
||||
$rules[$field] = $rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";;
|
||||
break;
|
||||
case "type":
|
||||
$rules[$field] = "if_exist|trim|string";
|
||||
|
||||
63
app/Models/Cloudflare/AuthModel.php
Normal file
63
app/Models/Cloudflare/AuthModel.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Cloudflare;
|
||||
|
||||
use App\Entities\Cloudflare\AuthEntity;
|
||||
use CodeIgniter\Model;
|
||||
use stdClass;
|
||||
|
||||
class AuthModel extends Model
|
||||
{
|
||||
protected $table = 'cloudflareauth';
|
||||
protected $primaryKey = 'uid';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = AuthEntity::class; //object,array,entity명::class
|
||||
protected $allowedFields = ['uid', 'id', 'authkey', 'oldkey', 'status', 'updated_at', 'created_at'];
|
||||
protected $useTimestamps = true;
|
||||
public function getTitleField(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
public function getFieldRule(string $field, array $rules): array
|
||||
{
|
||||
switch ($field) {
|
||||
case "id":
|
||||
$rules[$field] = "required|valid_emailvalid_email|is_unique[cloudflareauth.id]";
|
||||
break;
|
||||
case "authkey":
|
||||
$rules[$field] = "required|trim|smin_length[10]|max_length[200]";
|
||||
break;
|
||||
case "oldkey":
|
||||
$rules[$field] = "if_exist|trim|smin_length[10]|max_length[200]";
|
||||
break;
|
||||
default:
|
||||
$rules = parent::getFieldRule($field, $rules);
|
||||
break;
|
||||
}
|
||||
return $rules;
|
||||
}
|
||||
public function getEntityByPK(int $uid): null | AuthEntity
|
||||
{
|
||||
$this->where($this->getPKField(), $uid);
|
||||
return $this->getEntity();
|
||||
}
|
||||
public function getEntityByID(string $id): null | AuthEntity
|
||||
{
|
||||
$this->where($this->getTitleField(), $id);
|
||||
return $this->getEntity();
|
||||
}
|
||||
|
||||
//create용
|
||||
public function create(array $formDatas = []): AuthEntity
|
||||
{
|
||||
$formDatas['oldkey'] = $formDatas['authkey'];
|
||||
return $this->create_process(new AuthEntity(), $formDatas);
|
||||
}
|
||||
//modify용
|
||||
public function modify(AuthEntity $entity, array $formDatas): AuthEntity
|
||||
{
|
||||
//입력후 authkey값을 oldkey값에 넣어주기 위함
|
||||
$formDatas['oldkey'] = $entity->authkey;
|
||||
return $this->modify_process($entity, $formDatas);
|
||||
}
|
||||
}
|
||||
@ -144,17 +144,8 @@ abstract class CommonModel extends Model
|
||||
}
|
||||
|
||||
//create , modify 직전 작업용 작업
|
||||
final protected function convertEntityData(string $field, array $formDatas): string|int
|
||||
final protected function convertEntityData(string $field, array $formDatas): mixed
|
||||
{
|
||||
if ($formDatas[$field] === null) {
|
||||
throw new \Exception(
|
||||
sprintf(
|
||||
"\n-------%s FormDatas 오류--------\n%s\n-----------------------\n",
|
||||
__FUNCTION__,
|
||||
var_export($formDatas, true)
|
||||
)
|
||||
);
|
||||
}
|
||||
switch ($field) {
|
||||
case $this->getPKField():
|
||||
//$formDatas에 전달된 값이 없는경우
|
||||
@ -181,11 +172,6 @@ abstract class CommonModel extends Model
|
||||
case "content":
|
||||
$value = htmlentities($formDatas[$field], ENT_QUOTES);
|
||||
break;
|
||||
case "updated_at":
|
||||
case "created_at":
|
||||
case "deleted_at":
|
||||
$rules[$field] = "if_exist|valid_date";
|
||||
break;
|
||||
default:
|
||||
$value = $formDatas[$field];
|
||||
break;
|
||||
@ -213,7 +199,6 @@ abstract class CommonModel extends Model
|
||||
//Field에 맞는 Validation Rule 재정의
|
||||
$this->setAction(DB_ACTION['CREATE']);
|
||||
$this->setValidationRules($this->getFieldRules($this->allowedFields));
|
||||
|
||||
//저장하기 전에 데이터 값 변경이 필요한 Field
|
||||
foreach (array_keys($formDatas) as $field) {
|
||||
$entity->$field = $this->convertEntityData($field, $formDatas);
|
||||
@ -232,7 +217,6 @@ abstract class CommonModel extends Model
|
||||
//Field에 맞는 Validation Rule 재정의
|
||||
$this->setAction(DB_ACTION['MODIFY']);
|
||||
$this->setValidationRules($this->getFieldRules($this->allowedFields));
|
||||
|
||||
//저장하기 전에 데이터 값 변경이 필요한 Field
|
||||
foreach (array_keys($formDatas) as $field) {
|
||||
$entity->$field = $this->convertEntityData($field, $formDatas);
|
||||
|
||||
@ -2,18 +2,24 @@
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use CodeIgniter\Session\Session;
|
||||
|
||||
trait AuthTrait
|
||||
{
|
||||
final protected function loginCheck_AuthTrait(): array
|
||||
final protected function session_AuthTrait(): Session
|
||||
{
|
||||
//사용자 기본 Role 지정
|
||||
$session[SESSION_NAMES['ISLOGIN']] = false;
|
||||
$session['currentRoles'] = [DEFAULTS["ROLE"]];
|
||||
if (\Config\Services::session()->get(SESSION_NAMES['ISLOGIN'])) {
|
||||
$session[SESSION_NAMES['ISLOGIN']] = true;
|
||||
$session['auth'] = $this->_session->get(SESSION_NAMES['AUTH']);
|
||||
$currentRoles = explode(DEFAULTS['DELIMITER_ROLE'], $session['auth'][AUTH_FIELDS['ROLE']]);
|
||||
$session['currentRoles'] = is_array($currentRoles) ? $currentRoles : [DEFAULTS["ROLE"]];
|
||||
$session = \Config\Services::session();
|
||||
$session->set(SESSION_NAMES['ISLOGIN'], false);
|
||||
$session->set('currentRoles', [DEFAULTS["ROLE"]]);
|
||||
if ($session->get(SESSION_NAMES['ISLOGIN'])) {
|
||||
$session->set(SESSION_NAMES['ISLOGIN'], true);
|
||||
$session->set(
|
||||
'currentRoles',
|
||||
explode(
|
||||
DEFAULTS['DELIMITER_ROLE'],
|
||||
$session->get(SESSION_NAMES['AUTH'])[AUTH_FIELDS['ROLE']]
|
||||
)
|
||||
);
|
||||
}
|
||||
return $session;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user