Skip to content

Commit

Permalink
Merge pull request #177 from Larsklopstra/main
Browse files Browse the repository at this point in the history
Give users access to a private project
  • Loading branch information
Cannonb4ll authored Oct 19, 2022
2 parents da7bd2c + 029d9ff commit ce36f1e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/Filament/Resources/ProjectResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand All @@ -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);
}
}
10 changes: 10 additions & 0 deletions app/Models/ProjectMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;

class ProjectMember extends Pivot
{
//
}
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements FilamentUser, HasAvatar, MustVerifyEmail
Expand Down Expand Up @@ -78,6 +79,11 @@ public function items()
return $this->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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('project_member', function (Blueprint $table) {
$table->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');
}
};

0 comments on commit ce36f1e

Please sign in to comment.