Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Nahum authored and Julien Nahum committed Oct 6, 2021
2 parents 5a274e9 + 39b6c16 commit a22a15d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
25 changes: 16 additions & 9 deletions src/Http/Controllers/CustomCodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function executeCustomCode(Request $request, Tinker $tinker)
{
$validated = $request->validate([
'code' => 'required',
'chart_type' => ['required', Rule::in(self::CHART_TYPES)]
'chart_type' => ['required', Rule::in(self::CHART_TYPES)],
]);

$result = $tinker->injectDates(now()->subMonth(), now())
Expand All @@ -35,8 +35,10 @@ public function executeCustomCode(Request $request, Tinker $tinker)
return $this->success([
'output' => $result,
'code_executed' => $codeExecuted,
'valid_output' => $codeExecuted ? $this->isValidOutput($request->chart_type,
$tinker->getCustomCodeResult()) : false
'valid_output' => $codeExecuted ? $this->isValidOutput(
$request->chart_type,
$tinker->getCustomCodeResult()
) : false,
]);
}

Expand Down Expand Up @@ -65,8 +67,10 @@ public function widgetData(Request $request, Tinker $tinker)
return $this->error([
'output' => $result,
'code_executed' => $codeExecuted,
'valid_output' => $codeExecuted ? $this->isValidOutput($request->chart_type,
$tinker->getCustomCodeResult()) : false
'valid_output' => $codeExecuted ? $this->isValidOutput(
$request->chart_type,
$tinker->getCustomCodeResult()
) : false,
]);
}
}
Expand All @@ -79,32 +83,35 @@ private function isValidOutput(string $chartType, $data)
case 'line_chart':
return $this->validateLineChartData($data);
}

return false;
}

private function validateBarChartData($data)
{
if (!is_array($data)) {
if (! is_array($data)) {
return false;
}
foreach ($data as $key => $value) {
if (!is_string($key) || !is_numeric($value)) {
if (! is_string($key) || ! is_numeric($value)) {
return false;
}
}

return true;
}

private function validateLineChartData($data)
{
if (!is_array($data)) {
if (! is_array($data)) {
return false;
}
foreach ($data as $key => $value) {
if (!is_string($key) || !is_numeric($value)) {
if (! is_string($key) || ! is_numeric($value)) {
return false;
}
}

return true;
}
}
7 changes: 3 additions & 4 deletions src/Http/Middleware/CustomCodeEnabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace Jhumanj\LaravelModelStats\Http\Middleware;

use Jhumanj\LaravelModelStats\LaravelModelStats;

class CustomCodeEnabled
{
/**
Expand All @@ -16,11 +14,12 @@ class CustomCodeEnabled
*/
public function handle($request, $next)
{
if (!config('model-stats.allow_custom_code')) {
if (! config('model-stats.allow_custom_code')) {
return response([
'message' => 'Custom code not enabled.',
],403);
], 403);
}

return $next($request);
}
}
25 changes: 15 additions & 10 deletions src/Services/Tinker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Application;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Laravel\Tinker\ClassAliasAutoloader;
use Psy\Configuration;
use Psy\ExecutionLoopClosure;
use Psy\Shell;
use Symfony\Component\Console\Output\BufferedOutput;
use Illuminate\Support\Facades\Config;

/**
* Taken from https://github.com/spatie/laravel-web-tinker/blob/master/src/Tinker.php
Expand Down Expand Up @@ -52,7 +52,7 @@ public function execute(string $phpCode): string
$resultVars = $this->shell->getScopeVariables();

// Detect db write exception
if (!$this->lastExecSuccess() && isset($resultVars['_e'])) {
if (! $this->lastExecSuccess() && isset($resultVars['_e'])) {
$lastException = $resultVars['_e'];
if (get_class($lastException) === 'Illuminate\Database\QueryException') {
if (Str::of($lastException->getMessage())->contains(self::FAKE_WRITE_HOST)) {
Expand All @@ -72,18 +72,20 @@ public function execute(string $phpCode): string
/**
* Get the content of result variable
*/
public function getCustomCodeResult() {
if (!$this->lastExecSuccess()) {
public function getCustomCodeResult()
{
if (! $this->lastExecSuccess()) {
return null;
}

try {
$result = $this->shell->getScopeVariable('result');
} catch (\Exception $exception) {
ray($exception);

return null;
}
if ($result && !empty($result) ){
if ($result && ! empty($result)) {
return $result;
}

Expand All @@ -93,26 +95,29 @@ public function getCustomCodeResult() {
/**
* Check if last execution worked without exceptions
*/
public function lastExecSuccess() {
public function lastExecSuccess()
{
return $this->shell->getLastExecSuccess();
}

/**
* Prevents unwanted database modifications by enabling creating and using a readonly connection.
*/
public function readonly() {
public function readonly()
{
$defaultConnection = config('database.default');
$databaseConnection = Config::get('database.connections.'.$defaultConnection);
$host = $databaseConnection['host'];
unset($databaseConnection['host']);
$databaseConnection['read'] = [
'host' => $host
'host' => $host,
];
$databaseConnection['write'] = [
'host' => self::FAKE_WRITE_HOST
'host' => self::FAKE_WRITE_HOST,
];
Config::set('database.connections.readonly',$databaseConnection);
Config::set('database.connections.readonly', $databaseConnection);
DB::setDefaultConnection('readonly');

return $this;
}

Expand Down

0 comments on commit a22a15d

Please sign in to comment.