You've already forked pilot-sdk
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 31dd5ec559 | |||
| 264e0b72ba | |||
| 48c509d1c9 | |||
| b69480ccb3 | |||
| f8804fc651 | |||
| 6edb16ccbf | |||
| 52cbb95648 |
@@ -2,15 +2,17 @@
|
|||||||
|
|
||||||
namespace Bluesquare\Pilot\Entity;
|
namespace Bluesquare\Pilot\Entity;
|
||||||
|
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
abstract class Action extends Entity
|
abstract class Action extends Entity
|
||||||
{
|
{
|
||||||
public function action(
|
public function action(
|
||||||
$type = 'info',
|
string $type = 'info',
|
||||||
$title = null,
|
?string $title = null,
|
||||||
$message = null,
|
?string $message = null,
|
||||||
$button = null,
|
?string $button = null,
|
||||||
$url = null,
|
?string $url = null,
|
||||||
$files = [],
|
array $files = [],
|
||||||
) {
|
) {
|
||||||
return [
|
return [
|
||||||
'json' => [
|
'json' => [
|
||||||
@@ -21,16 +23,50 @@ abstract class Action extends Entity
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function file(
|
public function file(
|
||||||
$contents,
|
string $contents,
|
||||||
$name = '',
|
string $name = '',
|
||||||
$type = ''
|
string $type = ''
|
||||||
) {
|
) {
|
||||||
return [
|
return [
|
||||||
'file' => compact('contents', 'name', 'type'),
|
'file' => compact('contents', 'name', 'type'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function error($message)
|
public function iframe(string $url, int $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(string $url, int $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(string $message)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'json' => [
|
'json' => [
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ abstract class Entity
|
|||||||
{
|
{
|
||||||
protected $slug = null;
|
protected $slug = null;
|
||||||
|
|
||||||
public function __construct($slug = null)
|
public function __construct(?string $slug = null)
|
||||||
{
|
{
|
||||||
if (! is_null($slug)) {
|
if (! is_null($slug)) {
|
||||||
$this->slug = $slug;
|
$this->slug = $slug;
|
||||||
|
|||||||
@@ -62,12 +62,12 @@ abstract class Metric extends Widget
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filter($label, $value, callable $callback)
|
public function filter(string $label, $value, callable $callback)
|
||||||
{
|
{
|
||||||
return ['label' => $label, 'value' => $value, 'callback' => $callback];
|
return ['label' => $label, 'value' => $value, 'callback' => $callback];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filters($filters)
|
public function filters(array $filters)
|
||||||
{
|
{
|
||||||
return $this->resolveFilter($filters);
|
return $this->resolveFilter($filters);
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ abstract class Metric extends Widget
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function trend($values)
|
public function trend(array $values)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'type' => 'trend',
|
'type' => 'trend',
|
||||||
@@ -89,7 +89,7 @@ abstract class Metric extends Widget
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function partition($values)
|
public function partition(array $values)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'type' => 'partition',
|
'type' => 'partition',
|
||||||
@@ -106,7 +106,7 @@ abstract class Metric extends Widget
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function monitor($available)
|
public function monitor(bool $available)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'type' => 'monitor',
|
'type' => 'monitor',
|
||||||
|
|||||||
@@ -26,13 +26,13 @@ class MakeAction extends Command
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$name = $this->hasArgument('name')
|
$name = ! empty($this->argument('name'))
|
||||||
? $this->argument('name')
|
? $this->argument('name')
|
||||||
: $this->ask('What is the name of the action?', 'MakeSmoothie');
|
: $this->ask('What is the name of the action?', 'MakeSmoothie');
|
||||||
|
|
||||||
$contents = file_get_contents(__DIR__.'/../stubs/action.php');
|
$contents = file_get_contents(__DIR__.'/../stubs/action.php');
|
||||||
$contents = str_replace('MakeSmoothie', $name, $contents);
|
$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');
|
$path = app_path('Pilot/Actions/'.$name.'.php');
|
||||||
|
|
||||||
if (file_exists($path)) {
|
if (file_exists($path)) {
|
||||||
|
|||||||
@@ -26,13 +26,13 @@ class MakeMetric extends Command
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$name = $this->hasArgument('name')
|
$name = ! empty($this->argument('name'))
|
||||||
? $this->argument('name')
|
? $this->argument('name')
|
||||||
: $this->ask('What is the name of the metric?', 'SmoothiesCount');
|
: $this->ask('What is the name of the metric?', 'SmoothiesCount');
|
||||||
|
|
||||||
$contents = file_get_contents(__DIR__.'/../stubs/metric.php');
|
$contents = file_get_contents(__DIR__.'/../stubs/metric.php');
|
||||||
$contents = str_replace('SmoothiesCount', $name, $contents);
|
$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');
|
$path = app_path('Pilot/Metrics/'.$name.'.php');
|
||||||
|
|
||||||
if (file_exists($path)) {
|
if (file_exists($path)) {
|
||||||
|
|||||||
28
src/Laravel/Middlewares/CheckPilotToken.php
Normal file
28
src/Laravel/Middlewares/CheckPilotToken.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Bluesquare\Pilot\Laravel;
|
namespace Bluesquare\Pilot\Laravel;
|
||||||
|
|
||||||
|
use Bluesquare\Pilot\Laravel\Middlewares\CheckPilotToken;
|
||||||
use Bluesquare\Pilot\Pilot;
|
use Bluesquare\Pilot\Pilot;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
@@ -36,6 +37,8 @@ class PilotServiceProvider extends ServiceProvider
|
|||||||
return $this->handleRequest($request);
|
return $this->handleRequest($request);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::aliasMiddleware('pilot', CheckPilotToken::class);
|
||||||
|
|
||||||
$this->commands([
|
$this->commands([
|
||||||
\Bluesquare\Pilot\Laravel\Commands\MakeAction::class,
|
\Bluesquare\Pilot\Laravel\Commands\MakeAction::class,
|
||||||
\Bluesquare\Pilot\Laravel\Commands\MakeMetric::class,
|
\Bluesquare\Pilot\Laravel\Commands\MakeMetric::class,
|
||||||
@@ -85,7 +88,7 @@ class PilotServiceProvider extends ServiceProvider
|
|||||||
abort(403);
|
abort(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = $this->pilot->handle($request->all(), $request->files->all());
|
$output = $this->pilot->handle($request->all(), $request->allFiles());
|
||||||
|
|
||||||
if (isset($output['json'])) {
|
if (isset($output['json'])) {
|
||||||
return response()->json($output['json']);
|
return response()->json($output['json']);
|
||||||
|
|||||||
@@ -18,41 +18,43 @@ class Pilot
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function authorize($token, $user_token)
|
public function authorize(string $token, string $user_token)
|
||||||
{
|
{
|
||||||
if ($token !== $user_token) {
|
if ($token !== $user_token) {
|
||||||
throw new \Exception('Unauthorized Pilot request');
|
throw new \Exception('Unauthorized Pilot request');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function form($slug, callable $function)
|
public function form(string $slug, callable $function)
|
||||||
{
|
{
|
||||||
$this->register('form', $slug, function () use ($function) {
|
$this->register('form', $slug, function () use ($function) {
|
||||||
return [
|
return [
|
||||||
'json' => $function(),
|
'json' => [
|
||||||
|
'form' => $function()
|
||||||
|
],
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function action($slug, callable $function)
|
public function action(string $slug, callable $function)
|
||||||
{
|
{
|
||||||
$this->register('action', $slug, $function);
|
$this->register('action', $slug, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function metric($slug, callable $function)
|
public function metric(string $slug, callable $function)
|
||||||
{
|
{
|
||||||
$this->register('metric', $slug, $function);
|
$this->register('metric', $slug, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function register($type, $slug, callable $function)
|
protected function register(string $type, string $slug, callable $function)
|
||||||
{
|
{
|
||||||
$this->registry[$type][$slug] = $function;
|
$this->registry[$type][$slug] = $function;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle($data = [], $files = [])
|
public function handle($data = [], $files = [])
|
||||||
{
|
{
|
||||||
$type = $data['type'] ?? null;
|
$type = $data['_type'] ?? null;
|
||||||
$slug = $data['slug'] ?? null;
|
$slug = $data['_slug'] ?? null;
|
||||||
|
|
||||||
if (isset($this->registry[$type]) && isset($this->registry[$type][$slug])) {
|
if (isset($this->registry[$type]) && isset($this->registry[$type][$slug])) {
|
||||||
$params = $this->resolveDependencies($slug, $this->registry[$type][$slug], $data, $files);
|
$params = $this->resolveDependencies($slug, $this->registry[$type][$slug], $data, $files);
|
||||||
@@ -63,7 +65,7 @@ class Pilot
|
|||||||
return Response::error("Unknown {$type} {$slug}");
|
return Response::error("Unknown {$type} {$slug}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resolveDependencies($slug, $function, $data, $files)
|
public function resolveDependencies(string $slug, $function, $data, $files)
|
||||||
{
|
{
|
||||||
$entries = isset($data['entries']) && is_array($data['entries']) ? $data['entries'] : [];
|
$entries = isset($data['entries']) && is_array($data['entries']) ? $data['entries'] : [];
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ namespace Bluesquare\Pilot;
|
|||||||
class Response
|
class Response
|
||||||
{
|
{
|
||||||
public static function action(
|
public static function action(
|
||||||
$type = 'info',
|
string $type = 'info',
|
||||||
$title = null,
|
?string $title = null,
|
||||||
$message = null,
|
?string $message = null,
|
||||||
$button = null,
|
?string $button = null,
|
||||||
$url = null,
|
?string $url = null,
|
||||||
$files = [],
|
array $files = [],
|
||||||
) {
|
) {
|
||||||
return [
|
return [
|
||||||
'json' => [
|
'json' => [
|
||||||
@@ -20,7 +20,7 @@ class Response
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function error($message)
|
public static function error(string $message)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'json' => [
|
'json' => [
|
||||||
@@ -31,9 +31,9 @@ class Response
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function file(
|
public static function file(
|
||||||
$contents,
|
string $contents,
|
||||||
$name = '',
|
string $name = '',
|
||||||
$type = ''
|
string $type = ''
|
||||||
) {
|
) {
|
||||||
return [
|
return [
|
||||||
'file' => compact('contents', 'name', 'type'),
|
'file' => compact('contents', 'name', 'type'),
|
||||||
|
|||||||
Reference in New Issue
Block a user