Skip to content

Commit 957a8cd

Browse files
Given NcubeGiven Ncube
authored andcommitted
feat: added support for laravel 10, the ability to generate policy for user model
1 parent 9f0afef commit 957a8cd

File tree

4 files changed

+146
-35
lines changed

4 files changed

+146
-35
lines changed

composer.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20-
"illuminate/contracts": "^9.0",
21-
"spatie/laravel-package-tools": "^1.12.1",
22-
"spatie/laravel-permission": "^5.5.5",
23-
"symfony/filesystem": "^6.1.3"
20+
"illuminate/contracts": "^9.0|^10.0",
21+
"spatie/laravel-package-tools": "^1.14.1",
22+
"spatie/laravel-permission": "^5.9.1",
23+
"symfony/filesystem": "^6.2.7"
2424
},
2525
"require-dev": {
26-
"laravel/pint": "^1.1.1",
27-
"nunomaduro/collision": "^6.2.1",
28-
"nunomaduro/larastan": "^2.1.12",
29-
"orchestra/testbench": "^7.7",
30-
"pestphp/pest": "^1.21.3",
31-
"pestphp/pest-plugin-laravel": "^1.2",
32-
"phpstan/extension-installer": "^1.1",
33-
"phpstan/phpstan-deprecation-rules": "^1.0",
34-
"phpstan/phpstan-phpunit": "^1.1.1",
35-
"phpunit/phpunit": "^9.5.23",
26+
"laravel/pint": "^1.6.0",
27+
"nunomaduro/collision": "^6.4.0",
28+
"nunomaduro/larastan": "^2.5.1",
29+
"orchestra/testbench": "^7.22.1",
30+
"pestphp/pest": "^1.22.5",
31+
"pestphp/pest-plugin-laravel": "^1.4",
32+
"phpstan/extension-installer": "^1.2",
33+
"phpstan/phpstan-deprecation-rules": "^1.1.2",
34+
"phpstan/phpstan-phpunit": "^1.3.10",
35+
"phpunit/phpunit": "^9.6.4",
3636
"roave/security-advisories": "dev-latest"
3737
},
3838
"autoload": {

src/Commands/LaravelAuthorizerCommand.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function handle(): int
6666
public function generateAllPolicies(): void
6767
{
6868
$this->getModels()->each(
69-
fn (string $model) => $this->generatePolicy($model, $model)
69+
fn(string $model) => $this->generatePolicy($model, $model)
7070
);
7171
}
7272

@@ -80,7 +80,7 @@ public function generatePlainPolicy(string $name): void
8080
{
8181
if (
8282
file_exists($this->getPolicyPath($name)) &&
83-
! $this->option('force')
83+
!$this->option('force')
8484
) {
8585
$this->error(sprintf('Policy "%s" already exists!', $name));
8686

@@ -92,8 +92,8 @@ public function generatePlainPolicy(string $name): void
9292
'namespace' => $this->getNamespace(),
9393
'class' => $this->getClassName($name),
9494
])->reduce(
95-
fn ($carry, $value, $key) => Str::replace(
96-
'{{ '.$key.' }}',
95+
fn($carry, $value, $key) => Str::replace(
96+
'{{ ' . $key . ' }}',
9797
$value,
9898
$carry
9999
)
@@ -113,7 +113,7 @@ private function generatePolicy(string $name, string $model): void
113113
{
114114
if (
115115
file_exists($this->getPolicyPath($name)) &&
116-
! $this->option('force')
116+
!$this->option('force')
117117
) {
118118
$this->error(sprintf('Policy "%s" already exists!', $name));
119119

@@ -123,7 +123,8 @@ private function generatePolicy(string $name, string $model): void
123123
$compiled = collect([
124124
'name' => $name,
125125
'model' => $model,
126-
'modelVariable' => strtolower($model) ===
126+
'modelVariable' =>
127+
strtolower($model) ===
127128
strtolower(
128129
Str::afterLast($this->getNamespacedUserModel(), '\\')
129130
)
@@ -137,12 +138,19 @@ private function generatePolicy(string $name, string $model): void
137138
'namespacedUserModel' => $this->getNamespacedUserModel(),
138139
'user' => Str::afterLast($this->getNamespacedUserModel(), '\\'),
139140
])->reduce(
140-
static fn ($old, $value, $key) => Str::replace(
141-
'{{ '.$key.' }}',
141+
static fn($old, $value, $key) => Str::replace(
142+
'{{ ' . $key . ' }}',
142143
$value,
143144
$old
144145
),
145-
file_get_contents($this->getStub())
146+
file_get_contents(
147+
strtolower($model) ===
148+
strtolower(
149+
Str::afterLast($this->getNamespacedUserModel(), '\\')
150+
)
151+
? $this->getStub()
152+
: $this->getUserPolicyPolicyStub()
153+
)
146154
);
147155

148156
(new Filesystem())->dumpFile($this->getPolicyPath($name), $compiled);
@@ -156,7 +164,7 @@ private function generatePolicy(string $name, string $model): void
156164
*/
157165
public function getPolicyPath(string $name): string
158166
{
159-
return app_path('Policies/'.$this->getClassName($name).'.php');
167+
return app_path('Policies/' . $this->getClassName($name) . '.php');
160168
}
161169

162170
/**
@@ -166,7 +174,7 @@ public function getPolicyPath(string $name): string
166174
*/
167175
public function getNamespace(): string
168176
{
169-
return app()->getNamespace().'Policies';
177+
return app()->getNamespace() . 'Policies';
170178
}
171179

172180
/**
@@ -181,7 +189,7 @@ public function getClassName(string $name): string
181189
return Str::studly($name);
182190
}
183191

184-
return Str::studly($name).'Policy';
192+
return Str::studly($name) . 'Policy';
185193
}
186194

187195
/**
@@ -192,7 +200,7 @@ public function getClassName(string $name): string
192200
*/
193201
public function getNamespacedModel(string $model): string
194202
{
195-
return app()->getNamespace().'Models\\'.Str::studly($model);
203+
return app()->getNamespace() . 'Models\\' . Str::studly($model);
196204
}
197205

198206
/**
@@ -212,6 +220,16 @@ public function getNamespacedUserModel(): string
212220
*/
213221
public function getStub(): string
214222
{
215-
return __DIR__.'/stubs/policy.stub';
223+
return __DIR__ . '/stubs/policy.stub';
224+
}
225+
226+
/**
227+
* Get the path to the user policy stub
228+
*
229+
* @return string
230+
*/
231+
public function getUserPolicyPolicyStub(): string
232+
{
233+
return __DIR__ . '/stubs/policy.user.stub';
216234
}
217235
}

src/Commands/stubs/policy.stub

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class {{ class }}
1616
* @param \{{ namespacedUserModel }} $user
1717
* @return \Illuminate\Auth\Access\Response|bool
1818
*/
19-
public function viewAny({{ user }} $user)
19+
public function viewAny({{ user }} $user): bool
2020
{
2121
return $user->can('view all {{ modelPluralLowerCase }}');
2222
}
@@ -28,7 +28,7 @@ class {{ class }}
2828
* @param \{{ namespacedModel }} ${{ modelVariable }}
2929
* @return \Illuminate\Auth\Access\Response|bool
3030
*/
31-
public function view({{ user }} $user, {{ model }} ${{ modelVariable }})
31+
public function view({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
3232
{
3333
return $user->can('view {{ modelSingularLowerCase }}', ${{ modelVariable }});
3434
}
@@ -39,7 +39,7 @@ class {{ class }}
3939
* @param \{{ namespacedUserModel }} $user
4040
* @return \Illuminate\Auth\Access\Response|bool
4141
*/
42-
public function create({{ user }} $user)
42+
public function create({{ user }} $user): bool
4343
{
4444
return $user->can('create {{ modelSingularLowerCase }}');
4545
}
@@ -51,7 +51,7 @@ class {{ class }}
5151
* @param \{{ namespacedModel }} ${{ modelVariable }}
5252
* @return \Illuminate\Auth\Access\Response|bool
5353
*/
54-
public function update({{ user }} $user, {{ model }} ${{ modelVariable }})
54+
public function update({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
5555
{
5656
return $user->can('update {{ modelSingularLowerCase }}', ${{ modelVariable }});
5757
}
@@ -63,7 +63,7 @@ class {{ class }}
6363
* @param \{{ namespacedModel }} ${{ modelVariable }}
6464
* @return \Illuminate\Auth\Access\Response|bool
6565
*/
66-
public function delete({{ user }} $user, {{ model }} ${{ modelVariable }})
66+
public function delete({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
6767
{
6868
return $user->can('delete {{ modelSingularLowerCase }}', ${{ modelVariable }});
6969
}
@@ -75,7 +75,7 @@ class {{ class }}
7575
* @param \{{ namespacedModel }} ${{ modelVariable }}
7676
* @return \Illuminate\Auth\Access\Response|bool
7777
*/
78-
public function restore({{ user }} $user, {{ model }} ${{ modelVariable }})
78+
public function restore({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
7979
{
8080
return $user->can('restore {{ modelSingularLowerCase }}', ${{ modelVariable }});
8181
}
@@ -87,7 +87,7 @@ class {{ class }}
8787
* @param \{{ namespacedModel }} ${{ modelVariable }}
8888
* @return \Illuminate\Auth\Access\Response|bool
8989
*/
90-
public function forceDelete({{ user }} $user, {{ model }} ${{ modelVariable }})
90+
public function forceDelete({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
9191
{
9292
return $user->can('force delete {{ modelSingularLowerCase }}', ${{ modelVariable }});
9393
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Auth\Access\HandlesAuthorization;
6+
use {{ namespacedUserModel }};
7+
8+
class {{ class }}
9+
{
10+
use HandlesAuthorization;
11+
12+
/**
13+
* Determine whether the user can view any models.
14+
*
15+
* @param \{{ namespacedUserModel }} $user
16+
* @return \Illuminate\Auth\Access\Response|bool
17+
*/
18+
public function viewAny({{ user }} $user): bool
19+
{
20+
return $user->can('view all {{ modelPluralLowerCase }}');
21+
}
22+
23+
/**
24+
* Determine whether the user can view the model.
25+
*
26+
* @param \{{ namespacedUserModel }} $user
27+
* @param \{{ namespacedUserModel }} ${{ modelVariable }}
28+
* @return \Illuminate\Auth\Access\Response|bool
29+
*/
30+
public function view({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
31+
{
32+
return $user->can('view {{ modelSingularLowerCase }}', ${{ modelVariable }});
33+
}
34+
35+
/**
36+
* Determine whether the user can create models.
37+
*
38+
* @param \{{ namespacedUserModel }} $user
39+
* @return \Illuminate\Auth\Access\Response|bool
40+
*/
41+
public function create({{ user }} $user): bool
42+
{
43+
return $user->can('create {{ modelSingularLowerCase }}');
44+
}
45+
46+
/**
47+
* Determine whether the user can update the model.
48+
*
49+
* @param \{{ namespacedUserModel }} $user
50+
* @param \{{ namespacedUserModel }} ${{ modelVariable }}
51+
* @return \Illuminate\Auth\Access\Response|bool
52+
*/
53+
public function update({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
54+
{
55+
return $user->can('update {{ modelSingularLowerCase }}', ${{ modelVariable }});
56+
}
57+
58+
/**
59+
* Determine whether the user can delete the model.
60+
*
61+
* @param \{{ namespacedUserModel }} $user
62+
* @param \{{ namespacedUserModel }} ${{ modelVariable }}
63+
* @return \Illuminate\Auth\Access\Response|bool
64+
*/
65+
public function delete({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
66+
{
67+
return $user->can('delete {{ modelSingularLowerCase }}', ${{ modelVariable }});
68+
}
69+
70+
/**
71+
* Determine whether the user can restore the model.
72+
*
73+
* @param \{{ namespacedUserModel }} $user
74+
* @param \{{ namespacedUserModel }} ${{ modelVariable }}
75+
* @return \Illuminate\Auth\Access\Response|bool
76+
*/
77+
public function restore({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
78+
{
79+
return $user->can('restore {{ modelSingularLowerCase }}', ${{ modelVariable }});
80+
}
81+
82+
/**
83+
* Determine whether the user can permanently delete the model.
84+
*
85+
* @param \{{ namespacedUserModel }} $user
86+
* @param \{{ namespacedUserModel }} ${{ modelVariable }}
87+
* @return \Illuminate\Auth\Access\Response|bool
88+
*/
89+
public function forceDelete({{ user }} $user, {{ model }} ${{ modelVariable }}): bool
90+
{
91+
return $user->can('force delete {{ modelSingularLowerCase }}', ${{ modelVariable }});
92+
}
93+
}

0 commit comments

Comments
 (0)