diff --git a/app/Bots/Mattermost.php b/app/Bots/Mattermost.php deleted file mode 100644 index 891f5e4..0000000 --- a/app/Bots/Mattermost.php +++ /dev/null @@ -1,75 +0,0 @@ -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', - ] - ], - ]; - } -} diff --git a/app/Livewire/BotsList.php b/app/Livewire/BotsList.php index ce87fa2..c1c331c 100644 --- a/app/Livewire/BotsList.php +++ b/app/Livewire/BotsList.php @@ -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) diff --git a/app/Livewire/CreateEditBot.php b/app/Livewire/CreateEditBot.php index ef7c416..078c218 100644 --- a/app/Livewire/CreateEditBot.php +++ b/app/Livewire/CreateEditBot.php @@ -141,6 +141,7 @@ class CreateEditBot extends Component 'enabled' => $this->enabled, 'schedule' => $this->schedule, 'config' => $this->config, + 'user_id' => auth()->user()->id, ]); } diff --git a/app/Models/Bot.php b/app/Models/Bot.php index c1829b2..1af2e7d 100644 --- a/app/Models/Bot.php +++ b/app/Models/Bot.php @@ -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); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 3cb5ccb..e35f49d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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); + } } diff --git a/app/Policies/BotPolicy.php b/app/Policies/BotPolicy.php new file mode 100644 index 0000000..f5ebf79 --- /dev/null +++ b/app/Policies/BotPolicy.php @@ -0,0 +1,23 @@ +id === $bot->user_id; + } +} diff --git a/database/migrations/2025_08_31_121431_add_user_id_field_to_bots_table.php b/database/migrations/2025_08_31_121431_add_user_id_field_to_bots_table.php new file mode 100644 index 0000000..fcd5bbb --- /dev/null +++ b/database/migrations/2025_08_31_121431_add_user_id_field_to_bots_table.php @@ -0,0 +1,28 @@ +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'); + }); + } +}; diff --git a/routes/web.php b/routes/web.php index 2522cde..936cbc6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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');