Skip to content

Commit

Permalink
Merge pull request #12 from daycry/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
daycry authored Aug 30, 2022
2 parents 64e2ecc + 1903581 commit 6f3db7e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 81 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@
}
},
"scripts": {
"cs-fixer": [ "vendor/bin/php-cs-fixer fix src", "vendor/bin/php-cs-fixer fix tests" ],
"test": [
"Composer\\Config::disableProcessTimeout",
"vendor/bin/phpunit"
],
"cs-fixer": "vendor/bin/php-cs-fixer fix src",
"cs-fixer-tests": "vendor/bin/php-cs-fixer fix tests"
]
}
}
2 changes: 1 addition & 1 deletion src/Commands/DiscoverClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private function _checkClassNamespace(\Daycry\RestServer\Entities\ApiEntity $api
// @codeCoverageIgnoreEnd

if ($namespace->controller == $class) {
$namespace->fill(array( 'checked_at' => (new DateTime('now'))->format('Y-m-d H:i:s'), 'methods' => $methods) );
$namespace->fill(array( 'checked_at' => (new DateTime('now'))->format('Y-m-d H:i:s'), 'methods' => $methods));
$namespaceModel->save($namespace);
$found = true;
break;
Expand Down
118 changes: 50 additions & 68 deletions src/Commands/KeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,96 +27,85 @@ public function run(array $params)
{
$this->parser = \Config\Services::parser();
$this->config = config('RestServer');
$this->db = db_connect( $this->config->restDatabaseGroup);
$this->db = db_connect($this->config->restDatabaseGroup);

try
{
if( $this->config->restEnableKeys != true )
{
try {
if ($this->config->restEnableKeys != true) {
$this->_printError(lang('RestCommand.apiKeyDisabled'));
exit;
}

$this->_key = $params[ 'key' ] ?? CLI::getOption( 'key' );
$this->_key = $params[ 'key' ] ?? CLI::getOption('key');

if( empty( $this->_key ) )
{
$this->_key = CLI::prompt( lang( 'RestCommand.insertApiKey' ), null, 'required|max_length['.$this->config->restKeyLength.']' );
if (empty($this->_key)) {
$this->_key = CLI::prompt(lang('RestCommand.insertApiKey'), null, 'required|max_length['.$this->config->restKeyLength.']');
CLI::newLine();
}

$keyModel = new \Daycry\RestServer\Models\KeyModel();
$row = $keyModel->where($this->config->restKeyColumn, $this->_key)->first();

if(!$row)
{
if (!$row) {
throw UnauthorizedException::forInvalidApiKey($this->_key);
}

$this->_getKeyInfo($row);
$this->_getKeyUser($row);
$this->_getKeyAccess($row);
$this->_getKeyLocks($row);

}catch( \Exception $ex )
{
} catch (\Exception $ex) {
CLI::newLine();
CLI::error( '**** ' . $ex->getMessage() . ' ****' );
CLI::error('**** ' . $ex->getMessage() . ' ****');
CLI::newLine();
}
}

private function _getKeyInfo( \Daycry\RestServer\Entities\KeyEntity $row )
private function _getKeyInfo(\Daycry\RestServer\Entities\KeyEntity $row)
{
$this->_printHeader( lang( 'RestCommand.apiKeyInfo' ) );
$this->_printHeader(lang('RestCommand.apiKeyInfo'));

$row = [
[$row->id, $row->{$this->config->restKeyColumn}, $row->level, $row->ignore_limits, $row->is_private_key, $row->ip_addresses, $row->created_at, $row->updated_at, $row->deleted_at]
];

$this->_printTable( $this->_getTableColumns($this->config->restKeysTable), $row);
$this->_printTable($this->_getTableColumns($this->config->restKeysTable), $row);
}

private function _getKeyUser( \Daycry\RestServer\Entities\KeyEntity $row )
private function _getKeyUser(\Daycry\RestServer\Entities\KeyEntity $row)
{
$this->_printHeader( lang( 'RestCommand.apiKeyUser' ) );
$this->_printHeader(lang('RestCommand.apiKeyUser'));

$users = $row->{$this->config->restUsersTable};
$body = $columns = [];

if( !$users )
{
$this->_printError(lang( 'RestCommand.apiKeyNoUsers' ));
}else{
foreach( $users as $user )
{
if (!$users) {
$this->_printError(lang('RestCommand.apiKeyNoUsers'));
} else {
foreach ($users as $user) {
array_push($body, array( $user->id, $user->{$this->config->userNameColumn}, $user->created_at, $user->updated_at, $user->deleted_at ));
}

$columns = array( 'id', $this->config->userNameColumn, 'created_at', 'updated_at', 'deleted_at' );

$this->_printTable( $columns, $body);
$this->_printTable($columns, $body);
}
}

private function _getKeyAccess( \Daycry\RestServer\Entities\KeyEntity $row )
private function _getKeyAccess(\Daycry\RestServer\Entities\KeyEntity $row)
{
$this->_printHeader( lang( 'RestCommand.apiKeyAccess' ) );
$this->_printHeader(lang('RestCommand.apiKeyAccess'));

if( $this->config->restEnableAccess != true)
{
if ($this->config->restEnableAccess != true) {
$this->_printError(lang('RestCommand.apiKeyAccessDisabled'));
}

$body = [];
$accesses = $row->{$this->config->restAccessTable};

if( !$accesses )
{
$this->_printError(lang( 'RestCommand.apiKeyNoAccesses' ));
}else{
foreach( $row->{$this->config->restAccessTable} as $access)
{
if (!$accesses) {
$this->_printError(lang('RestCommand.apiKeyNoAccesses'));
} else {
foreach ($row->{$this->config->restAccessTable} as $access) {
// @codeCoverageIgnoreStart
if (!$access instanceof AccessEntity) {
$access = new AccessEntity((array)$access);
Expand All @@ -133,94 +122,87 @@ private function _getKeyAccess( \Daycry\RestServer\Entities\KeyEntity $row )
return $element !== "key_id";
});

$this->_printTable( $columns, $body);
$this->_printTable($columns, $body);
}
}

private function _getKeyLocks( \Daycry\RestServer\Entities\KeyEntity $row )
private function _getKeyLocks(\Daycry\RestServer\Entities\KeyEntity $row)
{
$this->_printHeader( lang( 'RestCommand.apiKeyLocks' ) );
$this->_printHeader(lang('RestCommand.apiKeyLocks'));

if( $this->config->restEnableLimits != true || $this->config->restEnableOverridePetition != true)
{
if ($this->config->restEnableLimits != true || $this->config->restEnableOverridePetition != true) {
$this->_printError($this->parser->setData(array( 'table' => $this->config->configRestPetitionsTable ))->renderString(lang('RestCommand.apiKeyLimitsDisabled')));
}else{
} else {
$limits = $row->{$this->config->restLimitsTable};
$body = [];

if( !$limits )
{
$this->_printError(lang( 'RestCommand.apiKeyNoLocks' ));
}else{
foreach( $limits as $limit )
{
if (!$limits) {
$this->_printError(lang('RestCommand.apiKeyNoLocks'));
} else {
foreach ($limits as $limit) {
// @codeCoverageIgnoreStart
if (!$limit instanceof LimitEntity) {
$limit = new LimitEntity((array)$limit);
}
// @codeCoverageIgnoreEnd

$r = $limit->{$this->config->configRestPetitionsTable};
if($r)
{
if ($r) {
// @codeCoverageIgnoreStart
if (!$r instanceof PetitionEntity) {
$r = new PetitionEntity((array)$r);
}
// @codeCoverageIgnoreEnd

$timeLimit = ( isset( $r->time ) ) ? $r->time : 3600;
if( $limit->count >= $r->limit && $limit->hour_started > (time() - $timeLimit) )
{
$timeLimit = (isset($r->time)) ? $r->time : 3600;
if ($limit->count >= $r->limit && $limit->hour_started > (time() - $timeLimit)) {
$timeLeft = $this->_getTimeLeft($limit->hour_started - (time() - $timeLimit));

$limit->hour_started = date('Y-m-d H:i:s', $limit->hour_started);
array_push( $body, array( $timeLeft, $limit->id, $r->{$this->config->restNamespaceTable}->controller, $limit->uri, $limit->count, $limit->hour_started, $limit->created_at, $limit->updated_at, $limit->deleted_at ) );
array_push($body, array( $timeLeft, $limit->id, $r->{$this->config->restNamespaceTable}->controller, $limit->uri, $limit->count, $limit->hour_started, $limit->created_at, $limit->updated_at, $limit->deleted_at ));
}
}
}

if( !$body )
{
$this->_printError(lang( 'RestCommand.apiKeyNoLocks' ));
}else{
if (!$body) {
$this->_printError(lang('RestCommand.apiKeyNoLocks'));
} else {
$columns = $this->_getTableColumns($this->config->restLimitsTable);
$columns = \array_filter($columns, static function ($element) {
return $element !== "key_id";
});

array_unshift($columns, 'Time Left');
$this->_printTable( $columns, $body);
$this->_printTable($columns, $body);
}
}
}
}

private function _getTimeLeft( int $timeLeft)
private function _getTimeLeft(int $timeLeft)
{
$init = new DateTime('now');
$end = new DateTime();
$end->setTimestamp( time() + $timeLeft );
$end->setTimestamp(time() + $timeLeft);

$interval = $init->diff($end);

return $interval->format('%H:%I:%S');
}

private function _getTableColumns(string $table) :array
private function _getTableColumns(string $table): array
{
return $this->db->getFieldNames($table);
}

private function _printTable( $header, $rows)
private function _printTable($header, $rows)
{
$tbody = [];
$thead = $header;
foreach( $rows as $row)
{
foreach ($rows as $row) {
array_push($tbody, $row);
}

CLI::table($tbody, $thead);
}

Expand All @@ -232,7 +214,7 @@ private function _printHeader(string $header)
private function _printError(string $error)
{
CLI::newLine();
CLI::error( '**** ' . $error . ' ****' );
CLI::error('**** ' . $error . ' ****');
CLI::newLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\BaseCommand;

class RestServerPublish extends BaseCommand
class Publish extends BaseCommand
{
protected $group = 'Rest Server';
protected $name = 'restserver:publish';
Expand Down Expand Up @@ -53,12 +53,6 @@ protected function publishConfig()
$content = str_replace('namespace Daycry\RestServer\Config', 'namespace Config', $content);
$content = str_replace('extends BaseConfig', 'extends \Daycry\RestServer\Config\RestServer', $content);
$this->writeFile('Config/RestServer.php', $content);

$path = $this->sourcePath . '/Config/JWT.php';
$content = file_get_contents($path);
$content = str_replace('namespace Daycry\RestServer\Config', 'namespace Config', $content);
$content = str_replace('extends BaseConfig', 'extends \Daycry\RestServer\Config\JWT', $content);
$this->writeFile('Config/JWT.php', $content);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function up()
]);

$this->db->query('ALTER TABLE `'.$this->config->restLimitsTable.'` ADD CONSTRAINT `'.$this->config->restLimitsTable.'_request_id_foreign` FOREIGN KEY (`request_id`) REFERENCES `'.$this->config->configRestPetitionsTable.'` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE;');

$cache = \Config\Services::cache();
$cache->delete('database-schema-' . config('RestServer')->restDatabaseGroup);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Language/en/RestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
'apiKeyNoAccesses' => 'There are no accesses',
'apiKeyDisabled' => 'Api Key control disabled, try to enable \'restEnableKeys\' in Restserver config',
'apiKeyAccessDisabled' => 'Api Key access disabled, try to enable \'restEnableAccess\' in Restserver config'
];
];

0 comments on commit 6f3db7e

Please sign in to comment.