Skip to content

Commit

Permalink
Merge pull request #988 from SlovakNationalGallery/MG-53-frontends
Browse files Browse the repository at this point in the history
[collection] add frontend scope
  • Loading branch information
rastislav-chynoransky authored Apr 12, 2024
2 parents eefc263 + 5dfda7f commit 819c12a
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 35 deletions.
1 change: 1 addition & 0 deletions app/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function getArtworksDirAttribute()

protected $casts = [
'published_at' => 'datetime',
'frontends' => 'array',
];

public function items()
Expand Down
19 changes: 16 additions & 3 deletions app/Http/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ public function store()
}

$collection->featured = Request::boolean('featured');
$collection->published_at = Request::input('published_at');

if (Gate::allows('administer')) {
$collection->frontends = Request::input('frontends');
} else {
$collection->frontends = [\auth()->user()->frontend];
}
if (Gate::allows('publish')) {
$collection->published_at = Request::input('published_at');
}
if (Request::has('title_color')) {
$collection->title_color = Request::input('title_color');
}
Expand Down Expand Up @@ -160,7 +166,14 @@ public function update($id)
}
}

$collection->published_at = Request::input('published_at', null);

if (Gate::allows('administer')) {
$collection->frontends = Request::input('frontends');
}

if (Gate::allows('publish', $collection)) {
$collection->published_at = Request::input('published_at');
}

if (Request::has('user_id') && Gate::allows('administer')) {
$collection->user_id = Request::input('user_id');
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Middleware/ApplyFrontendScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Middleware;

use App\Collection;
use App\Facades\Frontend;
use App\Item;
use Illuminate\Http\Request;
Expand All @@ -11,7 +12,8 @@ class ApplyFrontendScope
{
public function handle(Request $request, \Closure $next): Response
{
Item::addGlobalScope('frontend', fn ($query) => $query->whereJsonContains('frontends', Frontend::get()));
Collection::addGlobalScope('frontend', fn ($query) => $query->whereJsonContains('collections.frontends', Frontend::get()));
Item::addGlobalScope('frontend', fn ($query) => $query->whereJsonContains('items.frontends', Frontend::get()));

return $next($request);
}
Expand Down
10 changes: 5 additions & 5 deletions app/Policies/ImportPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ImportPolicy

public function viewAny(User $user)
{
return $user->role === 'importer';
return $user->can_import;
}

public function viewAll()
Expand All @@ -22,7 +22,7 @@ public function viewAll()

public function view(User $user, Import $import)
{
return $user->id === $import->user_id && $user->role === 'importer';
return $user->id === $import->user_id && $user->can_import;
}

public function create()
Expand All @@ -32,12 +32,12 @@ public function create()

public function update(User $user, Import $import)
{
return $user->id === $import->user_id && $user->role === 'importer';
return $user->id === $import->user_id && $user->can_import;
}

public function updateFile(User $user, Import $import)
{
return $user->id === $import->user_id && $user->role === 'importer';
return $user->id === $import->user_id && $user->can_import;
}

public function updateMetadata()
Expand All @@ -52,6 +52,6 @@ public function delete()

public function launch(User $user, Import $import)
{
return $user->id === $import->user_id && $user->role === 'importer';
return $user->id === $import->user_id && $user->can_import;
}
}
12 changes: 8 additions & 4 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ class AuthServiceProvider extends ServiceProvider
public function boot()
{
Gate::define('administer', function ($user) {
return $user->role === 'admin';
return $user->can_administer;
});

Gate::define('edit', function ($user) {
return $user->role === 'editor';
return $user->can_edit;
});

Gate::define('publish', function ($user) {
return $user->can_publish;
});

Gate::define('import', function ($user) {
return $user->role === 'importer';
return $user->can_import;
});

Gate::before(function ($user) {
if ($user->role === 'admin') {
if ($user->can_administer) {
return true;
}
});
Expand Down
45 changes: 36 additions & 9 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

namespace App;

use App\Enums\FrontendEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Validation\Rule;

class User extends Authenticatable
{
use HasFactory;
public static $roles = ['admin', 'editor', 'importer'];

protected $fillable = ['name', 'email', 'role', 'username'];
protected $fillable = [
'name',
'email',
'username',
'frontend',
'can_administer',
'can_edit',
'can_publish',
'can_import',
];

/**
* The attributes that should be hidden for arrays.
Expand All @@ -19,10 +29,27 @@ class User extends Authenticatable
*/
protected $hidden = ['password', 'remember_token'];

public static $rules = [
'name' => 'required',
'role' => 'in:admin,editor,importer|required',
'email' => 'email|required',
'username' => 'required',
];
public static $rules;

public static function boot()
{
parent::boot();
self::$rules = [
'name' => 'required',
'email' => 'email|required',
'username' => 'required',
'can_administer' => 'boolean|required',
'can_edit' => 'boolean|required',
'can_publish' => 'boolean|required',
'can_import' => 'boolean|required',
'frontend' => [Rule::enum(FrontendEnum::class), 'required'],
];
}

public function getPermissionsAttribute(): Collection
{
return collect(['can_administer', 'can_edit', 'can_publish', 'can_import'])->filter(
fn($permission) => $this->$permission
);
}
}
1 change: 0 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function definition()
'username' => fake()->userName(),
'name' => fake()->name(),
'email' => fake()->safeEmail(),
'role' => 'editor',
'password' => bcrypt(Str::random(10)),
'remember_token' => Str::random(10),
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement("ALTER TABLE collections ADD COLUMN frontends JSON DEFAULT (JSON_ARRAY('webumenia'))");
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('collections', function (Blueprint $table) {
$table->dropColumn('frontends');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('frontend')->default('webumenia');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('frontend');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('can_administer')->default(false);
$table->boolean('can_edit')->default(false);
$table->boolean('can_publish')->default(false);
$table->boolean('can_import')->default(false);
});

DB::table('users')
->eachById(function ($user) {
DB::table('users')
->where('id', $user->id)
->update([
'can_administer' => $user->role === 'admin',
'can_edit' => $user->role === 'editor',
'can_import' => $user->role === 'importer',
]);
});

Schema::table('users', function (Blueprint $table) {
$table->dropColumn('role');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('role')->nullable();
});

Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['can_administer', 'can_edit', 'can_publish', 'can_import']);
});
}
};
8 changes: 8 additions & 0 deletions lang/sk/permissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'can_administer' => 'Administrácia',
'can_edit' => 'Editovanie',
'can_publish' => 'Publikovanie',
'can_import' => 'Import',
];
15 changes: 14 additions & 1 deletion resources/views/collections/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@
<!-- translatable -->
<div class="col-md-12">

@can('admin')
<div class="form-group">
{{ Form::label('frontends[]', 'Publikovať na') }}
<div>
@foreach(\App\Enums\FrontendEnum::cases() as $frontend)
<label class="checkbox-inline">
{{ Form::checkbox('frontends[]', $frontend->value, old('frontends[]'), ['class' => 'form-control']) }} {{ $frontend }}
</label>
@endforeach
</div>
</div>
@endcan

<!-- Nav tabs -->
<ul class="nav nav-tabs top-space" role="tablist">
@foreach (\Config::get('translatable.locales') as $i=>$locale)
Expand Down Expand Up @@ -102,7 +115,7 @@
@endif
</div>
</div>
@can('administer')
@can('publish')
<div class="col-md-6">
<div class="form-group checkbox">
{!! Form::label('published_at', 'Publikovať') !!}
Expand Down
Loading

0 comments on commit 819c12a

Please sign in to comment.