From 029d9ffb27c67e8706f439c556f3691e91058807 Mon Sep 17 00:00:00 2001 From: Lars Klopstra Date: Tue, 18 Oct 2022 19:44:29 +0200 Subject: [PATCH] wip --- app/Filament/Resources/ProjectResource.php | 6 ++++ app/Models/Project.php | 13 ++++++++ app/Models/ProjectMember.php | 10 ++++++ app/Models/User.php | 6 ++++ ..._18_173054_create_project_member_table.php | 33 +++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 app/Models/ProjectMember.php create mode 100644 database/migrations/2022_10_18_173054_create_project_member_table.php diff --git a/app/Filament/Resources/ProjectResource.php b/app/Filament/Resources/ProjectResource.php index 1b654931..34af4d8a 100644 --- a/app/Filament/Resources/ProjectResource.php +++ b/app/Filament/Resources/ProjectResource.php @@ -49,8 +49,14 @@ public static function form(Form $form): Form ->options(Icons::all()) ->searchable(), Forms\Components\Toggle::make('private') + ->reactive() ->default(false) ->helperText('Private projects are only visible for employees and admins'), + Forms\Components\Select::make('members') + ->multiple() + ->relationship('members', 'name') + ->visible(fn ($get) => (bool) $get('private')) + ->helperText('Allow certain users to view this project'), Forms\Components\MarkdownEditor::make('description') ->columnSpan(2) ->maxLength(65535), diff --git a/app/Models/Project.php b/app/Models/Project.php index fc652bd8..936bd46a 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -5,7 +5,9 @@ use App\Traits\Sluggable; use App\Traits\HasOgImage; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Project extends Model { @@ -30,6 +32,11 @@ public function boards() return $this->hasMany(Board::class)->orderBy('sort_order'); } + public function members(): BelongsToMany + { + return $this->belongsToMany(User::class, 'project_member')->using(ProjectMember::class); + } + public function items() { return $this->hasManyThrough(Item::class, Board::class); @@ -41,6 +48,12 @@ public function scopeVisibleForCurrentUser($query) return $query; } + if (auth()->check()) { + return $query + ->whereHas('members', fn (Builder $query) => $query->where('user_id', auth()->id())) + ->orWhere('private', false); + } + return $query->where('private', false); } } diff --git a/app/Models/ProjectMember.php b/app/Models/ProjectMember.php new file mode 100644 index 00000000..0b666292 --- /dev/null +++ b/app/Models/ProjectMember.php @@ -0,0 +1,10 @@ +hasMany(Item::class); } + public function projects(): BelongsToMany + { + return $this->belongsToMany(Project::class, 'project_member')->using(ProjectMember::class); + } + public function votes(): HasMany { return $this->hasMany(Vote::class); diff --git a/database/migrations/2022_10_18_173054_create_project_member_table.php b/database/migrations/2022_10_18_173054_create_project_member_table.php new file mode 100644 index 00000000..f42dc022 --- /dev/null +++ b/database/migrations/2022_10_18_173054_create_project_member_table.php @@ -0,0 +1,33 @@ +id(); + $table->foreignId('user_id')->constrained()->cascadeOnDelete(); + $table->foreignId('project_id')->constrained()->cascadeOnDelete(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('project_member'); + } +};