Add user based bots and policy
All checks were successful
Deploy App / deploy (push) Successful in 9s
All checks were successful
Deploy App / deploy (push) Successful in 9s
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bots;
|
||||
|
||||
use App\Bots\BotContract;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Mattermost implements BotContract
|
||||
{
|
||||
protected Client $client;
|
||||
|
||||
public function __construct(private ?array $config = [])
|
||||
{
|
||||
$this->client = new Client([
|
||||
'base_uri' => rtrim(config('scheduler.mattermost.server_url'), '/') . '/api/v4/',
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . config('scheduler.mattermost.access_token'),
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
try {
|
||||
$this->client->request(
|
||||
$this->config['method'] ?? 'POST',
|
||||
$this->config['endpoint'],
|
||||
[
|
||||
'json' => json_decode($this->config['body'] ?? '', true),
|
||||
]
|
||||
);
|
||||
} catch (GuzzleException $e) {
|
||||
Log::error("Call to Mattermost failed. " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function configSchema(): array
|
||||
{
|
||||
return [
|
||||
'endpoint' => [
|
||||
'type' => 'string',
|
||||
'label' => 'API Endpoint',
|
||||
'rules' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
]
|
||||
],
|
||||
'method' => [
|
||||
'type' => 'string',
|
||||
'label' => 'HTTP Method',
|
||||
'default' => 'POST',
|
||||
'rules' => [
|
||||
'required',
|
||||
'in:GET,POST,PUT,DELETE,PATCH',
|
||||
]
|
||||
],
|
||||
'body' => [
|
||||
'type' => 'json',
|
||||
'label' => 'Request Body (JSON)',
|
||||
'rules' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'json',
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\Bot;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Jantinnerezo\LivewireAlert\Facades\LivewireAlert;
|
||||
use Livewire\Component;
|
||||
|
||||
@@ -12,7 +13,7 @@ class BotsList extends Component
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->bots = \App\Models\Bot::all();
|
||||
$this->bots = Auth::user()->bots;
|
||||
}
|
||||
|
||||
public function toggleBot($botId)
|
||||
|
||||
@@ -141,6 +141,7 @@ class CreateEditBot extends Component
|
||||
'enabled' => $this->enabled,
|
||||
'schedule' => $this->schedule,
|
||||
'config' => $this->config,
|
||||
'user_id' => auth()->user()->id,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Cron\CronExpression;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Lorisleiva\CronTranslator\CronTranslator;
|
||||
|
||||
class Bot extends Model
|
||||
@@ -14,7 +15,8 @@ class Bot extends Model
|
||||
'class',
|
||||
'config',
|
||||
'schedule',
|
||||
'enabled'
|
||||
'enabled',
|
||||
'user_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@@ -44,4 +46,9 @@ class Bot extends Model
|
||||
get: fn() => class_basename($this->class)
|
||||
);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -58,4 +59,9 @@ class User extends Authenticatable
|
||||
->map(fn ($word) => Str::substr($word, 0, 1))
|
||||
->implode('');
|
||||
}
|
||||
|
||||
public function bots(): HasMany
|
||||
{
|
||||
return $this->hasMany(Bot::class);
|
||||
}
|
||||
}
|
||||
|
||||
23
app/Policies/BotPolicy.php
Normal file
23
app/Policies/BotPolicy.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Bot;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class BotPolicy
|
||||
{
|
||||
/**
|
||||
* Create a new policy instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
return Auth::check();
|
||||
}
|
||||
|
||||
public function view(User $user, Bot $bot): bool
|
||||
{
|
||||
return $user->id === $bot->user_id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('bots', function (Blueprint $table) {
|
||||
$table->foreignId('user_id')->references('id')->on('users')->after('id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('bots', function (Blueprint $table) {
|
||||
$table->dropColumn('user_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -20,7 +20,7 @@ Route::middleware(['auth'])->group(function () {
|
||||
Route::get('bots/create', CreateEditBot::class)
|
||||
->name('bots.create');
|
||||
Route::get('bots/edit/{bot}', CreateEditBot::class)
|
||||
->name('bots.edit');
|
||||
->name('bots.edit')->middleware('can:view,bot');
|
||||
|
||||
Route::redirect('settings', 'settings/profile');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user