6 Commits
1.3 ... 1.6

Author SHA1 Message Date
264e0b72ba add link action 2025-10-01 10:46:20 +02:00
48c509d1c9 fix middleware 2025-07-04 13:13:05 +02:00
b69480ccb3 add iframe action 2025-07-02 18:07:13 +02:00
f8804fc651 fix keys 2024-12-01 16:03:43 +01:00
6edb16ccbf add form json 2024-11-29 16:57:06 +01:00
52cbb95648 add form json 2024-11-29 16:12:33 +01:00
6 changed files with 77 additions and 8 deletions

View File

@@ -2,6 +2,8 @@
namespace Bluesquare\Pilot\Entity;
use Illuminate\Support\Str;
abstract class Action extends Entity
{
public function action(
@@ -30,6 +32,40 @@ abstract class Action extends Entity
];
}
public function iframe($url, $expires = 240)
{
$key = 'pilot_action_' . Str::random(10);
$token = Str::random(40);
cache()->add($key, $token, now()->addMinutes($expires));
$url = $url . (str_contains($url, '?') ? '&' : '?') . "pilot_token=$key|$token";
return [
'json' => [
'type' => 'iframe',
'url' => $url,
],
];
}
public function link($url, $expires = 5)
{
$key = 'pilot_action_' . Str::random(10);
$token = Str::random(40);
cache()->add($key, $token, now()->addMinutes($expires));
$url = $url . (str_contains($url, '?') ? '&' : '?') . "pilot_token=$key|$token";
return [
'json' => [
'type' => 'link',
'url' => $url,
],
];
}
public function error($message)
{
return [

View File

@@ -26,13 +26,13 @@ class MakeAction extends Command
*/
public function handle()
{
$name = $this->hasArgument('name')
$name = ! empty($this->argument('name'))
? $this->argument('name')
: $this->ask('What is the name of the action?', 'MakeSmoothie');
$contents = file_get_contents(__DIR__.'/../stubs/action.php');
$contents = str_replace('MakeSmoothie', $name, $contents);
$contents = str_replace('make-smoothie', Str::slug($name), $contents);
$contents = str_replace('make-smoothie', Str::snake($name, '-'), $contents);
$path = app_path('Pilot/Actions/'.$name.'.php');
if (file_exists($path)) {

View File

@@ -26,13 +26,13 @@ class MakeMetric extends Command
*/
public function handle()
{
$name = $this->hasArgument('name')
$name = ! empty($this->argument('name'))
? $this->argument('name')
: $this->ask('What is the name of the metric?', 'SmoothiesCount');
$contents = file_get_contents(__DIR__.'/../stubs/metric.php');
$contents = str_replace('SmoothiesCount', $name, $contents);
$contents = str_replace('smoothies-count', Str::slug($name), $contents);
$contents = str_replace('smoothies-count', Str::snake($name, '-'), $contents);
$path = app_path('Pilot/Metrics/'.$name.'.php');
if (file_exists($path)) {

View File

@@ -0,0 +1,28 @@
<?php
namespace Bluesquare\Pilot\Laravel\Middlewares;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class CheckPilotToken
{
public function handle(Request $request, Closure $next)
{
if (request('pilot_token')) {
list($key, $token) = explode('|', request('pilot_token'));
if (cache()->get($key) == $token) {
cache()->set($key, $token, now()->addHour());
session()->put('pilot_token', true);
}
}
if (! session('pilot_token') && ! app()->environment('local')) {
abort(403, "Pilot session expired.");
}
return $next($request);
}
}

View File

@@ -2,6 +2,7 @@
namespace Bluesquare\Pilot\Laravel;
use Bluesquare\Pilot\Laravel\Middlewares\CheckPilotToken;
use Bluesquare\Pilot\Pilot;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
@@ -36,6 +37,8 @@ class PilotServiceProvider extends ServiceProvider
return $this->handleRequest($request);
});
Route::aliasMiddleware('pilot', CheckPilotToken::class);
$this->commands([
\Bluesquare\Pilot\Laravel\Commands\MakeAction::class,
\Bluesquare\Pilot\Laravel\Commands\MakeMetric::class,
@@ -85,7 +88,7 @@ class PilotServiceProvider extends ServiceProvider
abort(403);
}
$output = $this->pilot->handle($request->all(), $request->files->all());
$output = $this->pilot->handle($request->all(), $request->allFiles());
if (isset($output['json'])) {
return response()->json($output['json']);

View File

@@ -29,7 +29,9 @@ class Pilot
{
$this->register('form', $slug, function () use ($function) {
return [
'json' => $function(),
'json' => [
'form' => $function()
],
];
});
}
@@ -51,8 +53,8 @@ class Pilot
public function handle($data = [], $files = [])
{
$type = $data['type'] ?? null;
$slug = $data['slug'] ?? null;
$type = $data['_type'] ?? null;
$slug = $data['_slug'] ?? null;
if (isset($this->registry[$type]) && isset($this->registry[$type][$slug])) {
$params = $this->resolveDependencies($slug, $this->registry[$type][$slug], $data, $files);