-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: articles crud #134
Merged
Merged
feat: articles crud #134
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
855ec08
filament resource wip
alfonsobries cdf4868
add actions to articles
alfonsobries 46e898d
add policy
alfonsobries 531c75a
article policy crud and tests
alfonsobries f4ff389
linting and fix tests
alfonsobries 55b304f
test managers scope
alfonsobries b6b121c
Update ArticleResource.php
alfonsobries 34a13b1
wip
ItsANameToo f9466d9
add permissions to forcedelete and restore article for admins
alfonsobries 69206e6
Update PermissionRepositoryTest.php
alfonsobries fac0af7
Merge branch 'feat/articles' into feat/articles-crud
ItsANameToo 760e21a
Merge branch 'feat/articles-crud' of github.com:ArdentHQ/dashbrd into…
alfonsobries 2a4e30a
Update PermissionRepositoryTest.php
alfonsobries File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums; | ||
|
||
enum ArticleCategoryEnum: string | ||
{ | ||
case News = 'news'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Enums\ArticleCategoryEnum; | ||
use App\Filament\Resources\ArticleResource\Pages\CreateArticle; | ||
use App\Filament\Resources\ArticleResource\Pages\EditArticle; | ||
use App\Filament\Resources\ArticleResource\Pages\ListArticles; | ||
use App\Filament\Resources\ArticleResource\Pages\ViewArticle; | ||
use App\Models\Article; | ||
use App\Models\User; | ||
use Filament\Forms\Components\DatePicker; | ||
use Filament\Forms\Components\Select; | ||
use Filament\Forms\Components\Textarea; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Forms\Form; | ||
use Filament\Resources\Resource; | ||
use Filament\Tables\Actions\CreateAction; | ||
use Filament\Tables\Actions\ViewAction; | ||
use Filament\Tables\Columns\TextColumn; | ||
use Filament\Tables\Table; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\SoftDeletingScope; | ||
use Illuminate\Support\Str; | ||
|
||
class ArticleResource extends Resource | ||
{ | ||
protected static ?string $model = Article::class; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-document-text'; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
TextInput::make('title')->required()->columnSpan('full'), | ||
Select::make('category') | ||
->options([ | ||
ArticleCategoryEnum::News->value => Str::title(ArticleCategoryEnum::News->value), | ||
]) | ||
->default(ArticleCategoryEnum::News->value) | ||
->required(), | ||
Textarea::make('meta_description')->nullable()->autosize()->columnSpan('full'), | ||
Textarea::make('content')->required()->autosize()->columnSpan('full'), | ||
Select::make('user_id') | ||
->relationship( | ||
name: 'user', | ||
modifyQueryUsing: fn ($query) => $query->managers()->orderBy('username')->orderBy('email') | ||
) | ||
->getOptionLabelFromRecordUsing(fn (User $user) => $user->username ?? $user->email ?? 'ID '.$user->id) | ||
->required(), | ||
DatePicker::make('published_at')->nullable(), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
TextColumn::make('title') | ||
->label('Title') | ||
->sortable() | ||
->searchable(), | ||
TextColumn::make('category') | ||
->label('Category') | ||
->sortable() | ||
->searchable(), | ||
|
||
TextColumn::make('published_at') | ||
->label('Date Published') | ||
->date() | ||
->sortable(), | ||
|
||
TextColumn::make('created_at') | ||
->label('Date Created') | ||
->dateTime() | ||
->sortable(), | ||
|
||
]) | ||
->filters([ | ||
// | ||
]) | ||
->recordUrl(fn (Article $article) => ArticleResource::getUrl('view', ['record' => $article])) | ||
->actions([ | ||
ViewAction::make(), | ||
]) | ||
->emptyStateActions([ | ||
CreateAction::make(), | ||
]); | ||
} | ||
|
||
public static function getRelations(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => ListArticles::route('/'), | ||
'create' => CreateArticle::route('/create'), | ||
'view' => ViewArticle::route('/{record}'), | ||
'edit' => EditArticle::route('/{record}/edit'), | ||
]; | ||
} | ||
|
||
/** | ||
* @return Builder<Article> | ||
*/ | ||
public static function getEloquentQuery(): Builder | ||
{ | ||
return parent::getEloquentQuery() | ||
->withoutGlobalScopes([ | ||
SoftDeletingScope::class, | ||
]); | ||
} | ||
|
||
public static function shouldSkipAuthorization(): bool | ||
{ | ||
return app()->isLocal(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/Filament/Resources/ArticleResource/Pages/CreateArticle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Resources\Pages\CreateRecord; | ||
|
||
class CreateArticle extends CreateRecord | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
} |
25 changes: 25 additions & 0 deletions
25
app/Filament/Resources/ArticleResource/Pages/EditArticle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Actions\DeleteAction; | ||
use Filament\Actions\ForceDeleteAction; | ||
use Filament\Actions\RestoreAction; | ||
use Filament\Resources\Pages\EditRecord; | ||
|
||
class EditArticle extends EditRecord | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
DeleteAction::make(), | ||
ForceDeleteAction::make(), | ||
RestoreAction::make(), | ||
]; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/Filament/Resources/ArticleResource/Pages/ListArticles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Actions\CreateAction; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListArticles extends ListRecords | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
CreateAction::make(), | ||
]; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/Filament/Resources/ArticleResource/Pages/ViewArticle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Resources\ArticleResource\Pages; | ||
|
||
use App\Filament\Resources\ArticleResource; | ||
use Filament\Actions\DeleteAction; | ||
use Filament\Actions\EditAction; | ||
use Filament\Actions\ForceDeleteAction; | ||
use Filament\Actions\RestoreAction; | ||
use Filament\Resources\Pages\ViewRecord; | ||
|
||
class ViewArticle extends ViewRecord | ||
{ | ||
protected static string $resource = ArticleResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
EditAction::make(), | ||
DeleteAction::make(), | ||
ForceDeleteAction::make(), | ||
RestoreAction::make(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Policies; | ||
|
||
use App\Models\Article; | ||
use App\Models\User; | ||
|
||
final class ArticlePolicy | ||
{ | ||
public function viewAny(User $user): bool | ||
{ | ||
return $user->hasPermissionTo('article:viewAny', 'admin'); | ||
} | ||
|
||
public function view(User $user, Article $article): bool | ||
{ | ||
// If users can view any, can view single article | ||
return $this->viewAny($user); | ||
} | ||
|
||
public function create(User $user): bool | ||
{ | ||
return $user->hasPermissionTo('article:create', 'admin'); | ||
} | ||
|
||
public function update(User $user, Article $article): bool | ||
{ | ||
if ($user->hasPermissionTo('article:updateAny', 'admin')) { | ||
return true; | ||
} | ||
|
||
// If users can create, they can update their own | ||
return $this->create($user) && ($user->is($article->user)); | ||
} | ||
|
||
public function delete(User $user, Article $article): bool | ||
{ | ||
if ($user->hasPermissionTo('article:deleteAny', 'admin')) { | ||
return true; | ||
} | ||
|
||
// If users can create, they can delete their own | ||
return $this->create($user) && ($user->is($article->user)); | ||
} | ||
|
||
public function restore(User $user, Article $article): bool | ||
{ | ||
return $user->hasPermissionTo('article:restore', 'admin'); | ||
} | ||
|
||
public function forceDelete(User $user, Article $article): bool | ||
{ | ||
return $user->hasPermissionTo('article:forceDelete', 'admin'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notice that You need to remove this to test the permissions