Skip to content

Commit 99df880

Browse files
committed
wip: add policies
1 parent 6306562 commit 99df880

11 files changed

Lines changed: 378 additions & 26 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"javaabu/helpers": "^1.61",
2222
"javaabu/translatable": "^1.1",
2323
"javaabu/menu-builder": "^1.6",
24-
"kalnoy/nestedset": "^6.0"
24+
"kalnoy/nestedset": "^6.0",
25+
"javaabu/auth": "^1.16"
2526
},
2627
"require-dev": {
2728
"laravel/pint": "^1.14",

config/config.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,30 @@
1010
|
1111
*/
1212

13+
/**
14+
* The model classes that are used in this application. You can extend the
15+
* classes and override from here
16+
*/
17+
'models' => [
18+
'admin' => \App\Models\User::class,
19+
'user' => \App\Models\User::class,
20+
'post' => \Javaabu\Cms\Models\Post::class,
21+
'post_type' => \Javaabu\Cms\Models\PostType::class,
22+
'category' => \Javaabu\Cms\Models\Category::class,
23+
'category_type' => \Javaabu\Cms\Models\CategoryType::class,
24+
],
25+
26+
/**
27+
* This config section defines the policies that are used in the CMS package.
28+
* Not all applications will be having the same policies, so you can define the
29+
* policies that you want to use in the application for CMS models here.
30+
*/
31+
'policies' => [
32+
'post' => \Javaabu\Cms\Policies\PostPolicy::class,
33+
'post_type' => \Javaabu\Cms\Policies\PostTypePolicy::class,
34+
'category' => \Javaabu\Cms\Policies\CategoryPolicy::class,
35+
'category_type' => \Javaabu\Cms\Policies\CategoryTypePolicy::class,
36+
],
37+
1338
'should_translate' => false,
1439
];

src/Cms.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public function registerRoutes()
5656
}
5757
}
5858

59+
public function registerAdminRoutes()
60+
{
61+
if (config('cms.should_translate')) {
62+
$this->registerTranslatableAdminRoutes();
63+
} else {
64+
$this->registerNormalAdminRoutes();
65+
}
66+
}
67+
5968
public function registerNormalRoutes(): void
6069
{
6170
$root_slugs = app(RootSlugsRegistrar::class)->getSlugs();
@@ -76,7 +85,7 @@ public function registerNormalRoutes(): void
7685
}
7786
}
7887

79-
public function registerAdminRoutes(): void
88+
public function registerNormalAdminRoutes(): void
8089
{
8190
/**
8291
* Categories
@@ -136,36 +145,41 @@ public function registerTranslatableAdminRoutes()
136145
Route::group([
137146
'prefix' => '{language}',
138147
], function () {
139-
$this->registerAdminRoutes();
148+
$this->registerNormalAdminRoutes();
140149
});
141150
}
142151

143-
public function addToSidebar(array $menus = [])
152+
public function adminMenuItems(array $menus = [])
144153
{
145154
$all_post_types = PostType::all();
146155

147156
foreach ($all_post_types as $post_type) {
148157
$name = Str::title($post_type->name);
149158
$children = [
150159
MenuItem::make($name)
160+
->controller(PostsController::class)
151161
->can('view_' . $post_type->permission_slug)
152162
->active(optional(request()->route('post_type'))->slug == $post_type->slug)
153-
->url(config('cms.should_translate')
154-
? translate_route('admin.posts.index', $post_type->slug)
155-
: route('admin.posts.index', $post_type->slug)
156-
)
163+
// ->url(config('cms.should_translate')
164+
// ? translate_route('admin.posts.index', $post_type->slug)
165+
// : route('admin.posts.index', $post_type->slug)
166+
// )
167+
->url(translate_route('admin.posts.index', $post_type->slug))
157168
->icon('zmdi-' . $post_type->icon)
158169
->count(Post::query()->userVisibleForPostType($post_type)->postType($post_type->slug)->pending()),
159170
];
160171

161172
if ($post_type->hasFeature(PostTypeFeatures::CATEGORIES)) {
162173
$children[] = MenuItem::make(_d(':name Categories', ['name' => Str::singular($name)]))
174+
->controller(CategoriesController::class)
163175
->can('view_' . Str::singular($post_type->permission_slug) . '_categories')
164-
->url(config('cms.should_translate')
165-
? translate_route('admin.categories.index', Str::singular($post_type->slug) . '-categories')
166-
: route('admin.categories.index', Str::singular($post_type->slug) . '-categories')
167-
)
168-
->active(optional(request()->route('category_type'))->slug == Str::singular($post_type->slug) . '-categories');
176+
// ->active(optional(request()->route('category_type'))->slug == Str::singular($post_type->slug) . '-categories')
177+
// ->url(config('cms.should_translate')
178+
// ? translate_route('admin.categories.index', Str::singular($post_type->slug) . '-categories')
179+
// : route('admin.categories.index', Str::singular($post_type->slug) . '-categories')
180+
// )
181+
->url(translate_route('admin.categories.index', Str::singular($post_type->slug) . '-categories'))
182+
;
169183

170184
$menus[] =
171185
MenuItem::make($name)

src/CmsServiceProvider.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Javaabu\Cms;
44

55
use Carbon\Carbon;
6-
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Gate;
77
use Illuminate\Support\Str;
88
use Javaabu\Cms\Models\Post;
99
use Javaabu\Cms\Models\PostType;
@@ -15,9 +15,9 @@ class CmsServiceProvider extends ServiceProvider
1515
{
1616
protected array $migrations = [
1717
'create_category_types_table',
18-
'create_categories_table',
1918
'create_post_types_table',
2019
'create_posts_table',
20+
'create_categories_table',
2121
];
2222

2323
/**
@@ -26,6 +26,8 @@ class CmsServiceProvider extends ServiceProvider
2626
public function boot()
2727
{
2828
$this->offerPublishing();
29+
30+
$this->registerPolicies();
2931
}
3032

3133
/**
@@ -74,6 +76,15 @@ public function offerPublishing(): void
7476
}
7577
}
7678

79+
public function registerPolicies()
80+
{
81+
$policies = $this->getPolicies();
82+
83+
foreach ($policies as $key => $value) {
84+
Gate::policy($key, $value);
85+
}
86+
}
87+
7788
public function registerSingletons(): void
7889
{
7990
$this->app->singleton(Cms::class, function () {
@@ -171,4 +182,15 @@ protected function generateMigrationName(string $migrationFileName, Carbon $now)
171182

172183
return database_path($migrationsPath . $timestamp . '_' . $migrationFileName);
173184
}
185+
186+
private function getPolicies(): array
187+
{
188+
$policies = [];
189+
190+
foreach (config('cms.policies') as $model_name => $policy) {
191+
$policies[config("cms.models.{$model_name}")] = $policy;
192+
}
193+
194+
return $policies;
195+
}
174196
}

src/Http/Controllers/Admin/PostsController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ public function index($locale, PostType $type, Request $request, bool $trashed =
8989
$posts->onlyTrashed();
9090
}
9191

92-
$posts->with('department');
93-
9492
// if ($request->download) {
9593
// return (new PostsExport($posts))->download('posts.xlsx');
9694
// }

src/Http/Requests/PostRequest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Validation\Rules\Unique;
1010
use Javaabu\Cms\Enums\PageStyles;
11+
use Javaabu\Cms\Models\PostType;
1112
use Javaabu\Helpers\Enums\PublishStatuses;
1213
use Illuminate\Foundation\Http\FormRequest;
1314
use Javaabu\Helpers\Media\AllowedMimeTypes;
14-
use Javaabu\Translatable\Facades\Languages;
1515

1616
class PostRequest extends FormRequest
1717
{
@@ -69,10 +69,6 @@ public function rules(): array
6969
'recently_updated' => 'boolean',
7070
];
7171

72-
if (config('cms.should_translate')) {
73-
$rules['lang'] = 'in:' . implode(',', Languages::all()->pluck('code')->toArray());
74-
}
75-
7672
$rules['title'] = 'string|max:500';
7773
$rules['slug'] = ['string', 'max:255'];
7874

@@ -131,12 +127,10 @@ public function rules(): array
131127
'exists:menus,id',
132128
];
133129

134-
if ($model) {
135-
//
136-
} else {
130+
if (! $model) {
137131
$rules['title'] .= '|required';
138132
$rules['slug'][] = 'required';
139-
$rules['lang'] .= '|required';
133+
if (config('cms.should_translate')) $rules['lang'] = 'required';
140134
}
141135

142136
return $rules;

src/Models/TranslatablePost.php

Whitespace-only changes.

src/Policies/CategoryPolicy.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Javaabu\Cms\Policies;
4+
5+
use Javaabu\Auth\User;
6+
use Javaabu\Cms\Models\Category;
7+
use Javaabu\Cms\Models\CategoryType;
8+
9+
class CategoryPolicy
10+
{
11+
12+
/**
13+
* Determine whether the user can see view any categories
14+
*/
15+
public function viewAny(User $user, CategoryType $category_type)
16+
{
17+
return $user->can('viewAny', $category_type);
18+
}
19+
20+
/**
21+
* Determine whether the user can view the category.
22+
*/
23+
public function view(User $user, Category $category)
24+
{
25+
return $user->can('view', $category->type);
26+
}
27+
28+
/**
29+
* Determine whether the user can create category.
30+
*/
31+
public function create(User $user, CategoryType $category_type)
32+
{
33+
return $user->can('create', $category_type);
34+
}
35+
36+
/**
37+
* Determine whether the user can delete the category.
38+
*/
39+
public function delete(User $user, Category $category)
40+
{
41+
return $user->can('delete', $category->type);
42+
}
43+
44+
/**
45+
* Determine whether the user can update the category.
46+
*/
47+
public function update(User $user, Category $category)
48+
{
49+
return $user->can('update', $category->type);
50+
}
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Javaabu\Cms\Policies;
4+
5+
use Javaabu\Auth\User;
6+
use Javaabu\Cms\Models\CategoryType;
7+
8+
class CategoryTypePolicy
9+
{
10+
/**
11+
* Determine whether the user can see view any category types
12+
*/
13+
public function viewAny(User $user, CategoryType $category_type): bool
14+
{
15+
return $user->can('view_' . $category_type->permission_slug);
16+
}
17+
18+
/**
19+
* Determine whether the user can view the category type.
20+
*/
21+
public function view(User $user, CategoryType $category_type): bool
22+
{
23+
return $user->can('view_' . $category_type->permission_slug);
24+
}
25+
26+
/**
27+
* Determine whether the user can create category type.
28+
*/
29+
public function create(User $user, CategoryType $category_type): bool
30+
{
31+
return $user->can('edit_' . $category_type->permission_slug);
32+
}
33+
34+
/**
35+
* Determine whether the user can delete the category type.
36+
*/
37+
public function delete(User $user, CategoryType $category_type): bool
38+
{
39+
return $user->can('delete_' . $category_type->permission_slug);
40+
}
41+
42+
/**
43+
* Determine whether the user can update the category type.
44+
*/
45+
public function update(User $user, CategoryType $category_type): bool
46+
{
47+
return $user->can('edit_' . $category_type->permission_slug);
48+
}
49+
}

0 commit comments

Comments
 (0)