Skip to content

Commit 369bec0

Browse files
author
Given Ncube
committed
feat: added the ability to override existing policies
1 parent 50bf0e9 commit 369bec0

File tree

3 files changed

+118
-21
lines changed

3 files changed

+118
-21
lines changed

src/Commands/LaravelAuthorizerCommand.php

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,24 @@
1313

1414
class LaravelAuthorizerCommand extends Command
1515
{
16-
public $signature = 'authorizer:generate {name? : The name of the policy to generate} {--m|model= : The model to use for the policy}';
17-
16+
/**
17+
* The name and signature of the console command.
18+
*
19+
* @var string
20+
*/
21+
public $signature = 'authorizer:generate {name? : The name of the policy to generate} {--m|model= : The model to use for the policy} {--f|force : Overwrite existing files}';
22+
23+
/**
24+
* The console command description.
25+
*
26+
* @var string
27+
*/
1828
public $description = 'Generate authorizations for your application';
1929

30+
/**
31+
* Run the command.
32+
* @return int
33+
*/
2034
public function handle(): int
2135
{
2236
$this->comment('Generating policies...');
@@ -45,6 +59,11 @@ public function handle(): int
4559
return self::SUCCESS;
4660
}
4761

62+
/**
63+
* Generate policies for all models.
64+
*
65+
* @return void
66+
*/
4867
public function generateAllPolicies(): void
4968
{
5069
$models = collect(File::allFiles(app_path()))
@@ -70,16 +89,26 @@ public function generateAllPolicies(): void
7089
->map(fn($model) => Str::afterLast($model, '\\'));
7190

7291
$models->each(function (string $model) {
73-
if (file_exists($this->getPolicyPath($model))) {
74-
return;
75-
}
76-
7792
$this->generatePolicy($model, $model);
7893
});
7994
}
8095

96+
/**
97+
* Generate a plain policy without a model.
98+
*
99+
* @param string $name
100+
* @return void
101+
*/
81102
public function generatePlainPolicy(string $name): void
82103
{
104+
if (
105+
file_exists($this->getPolicyPath($name)) &&
106+
!$this->option('force')
107+
) {
108+
$this->error(sprintf('Policy "%s" already exists!', $name));
109+
return;
110+
}
111+
83112
$compiled = collect([
84113
'namespacedUserModel' => $this->getNamespacedUserModel(),
85114
'namespace' => $this->getNamespace(),
@@ -95,14 +124,20 @@ public function generatePlainPolicy(string $name): void
95124
(new Filesystem())->dumpFile($this->getPolicyPath($name), $compiled);
96125
}
97126

98-
public function getPolicyPath(string $name): string
99-
{
100-
return app_path('Policies/' . $this->getClassName($name) . '.php');
101-
}
102-
127+
/**
128+
* Generate a policy for a given model.
129+
*
130+
* @param string $name
131+
* @param string $model
132+
* @return void
133+
*/
103134
private function generatePolicy(string $name, string $model): void
104135
{
105-
if (file_exists($this->getPolicyPath($name))) {
136+
if (
137+
file_exists($this->getPolicyPath($name)) &&
138+
!$this->option('force')
139+
) {
140+
$this->error(sprintf('Policy "%s" already exists!', $name));
106141
return;
107142
}
108143

@@ -135,11 +170,32 @@ private function generatePolicy(string $name, string $model): void
135170
(new Filesystem())->dumpFile($this->getPolicyPath($name), $compiled);
136171
}
137172

173+
/**
174+
* Get the path to the policy.
175+
*
176+
* @return string
177+
*/
178+
public function getPolicyPath(string $name): string
179+
{
180+
return app_path('Policies/' . $this->getClassName($name) . '.php');
181+
}
182+
183+
/**
184+
* Get the policies namespace.
185+
*
186+
* @return string
187+
*/
138188
public function getNamespace(): string
139189
{
140190
return app()->getNamespace() . 'Policies';
141191
}
142192

193+
/**
194+
* Get the class name for the policy.
195+
*
196+
* @param string $name The name of the policy
197+
* @return string
198+
*/
143199
public function getClassName(string $name): string
144200
{
145201
if (Str::endsWith(Str::lower($name), 'policy')) {
@@ -149,16 +205,32 @@ public function getClassName(string $name): string
149205
return Str::studly($name) . 'Policy';
150206
}
151207

208+
/**
209+
* Get the namespace for the model.
210+
*
211+
* @param string $model The name of the model
212+
* @return string
213+
*/
152214
public function getNamespacedModel(string $model): string
153215
{
154216
return app()->getNamespace() . 'Models\\' . Str::studly($model);
155217
}
156218

219+
/**
220+
* Get the namespace for the User model.
221+
*
222+
* @return string
223+
*/
157224
public function getNamespacedUserModel(): string
158225
{
159226
return config('auth.providers.users.model');
160227
}
161228

229+
/**
230+
* Get the path to the stub.
231+
*
232+
* @return string
233+
*/
162234
public function getStub(): string
163235
{
164236
return __DIR__ . '/stubs/policy.stub';

src/LaravelAuthorizerServiceProvider.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace FlixtechsLabs\LaravelAuthorizer;
44

5-
<<<<<<< HEAD
65
use Illuminate\Foundation\Console\AboutCommand;
7-
=======
86
use FlixtechsLabs\LaravelAuthorizer\Commands\LaravelAuthorizerCommand;
9-
>>>>>>> 75fcbe42c60682fff2220c3af0c2a1ce820a8c88
107
use Spatie\LaravelPackageTools\Package;
118
use Spatie\LaravelPackageTools\PackageServiceProvider;
129

@@ -31,10 +28,13 @@ public function boot()
3128
{
3229
parent::boot();
3330

34-
AboutCommand::add('Laravel Authorizer', fn() => [
35-
'version' => '0.0.1',
36-
'author' => 'Flixtechs Labs',
37-
'license' => 'MIT',
38-
]);
31+
AboutCommand::add(
32+
'Laravel Authorizer',
33+
fn() => [
34+
'version' => '0.0.1',
35+
'author' => 'Flixtechs Labs',
36+
'license' => 'MIT',
37+
]
38+
);
3939
}
4040
}

tests/LaravelAuthorizerTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use FlixtechsLabs\LaravelAuthorizer\Commands\LaravelAuthorizerCommand;
4-
use Illuminate\Support\Facades\Artisan;
54
use function PHPUnit\Framework\assertFileExists;
5+
use function PHPUnit\Framework\assertStringContainsString;
66

77
it('can run the command successfully', function () {
88
$this->artisan(LaravelAuthorizerCommand::class, [
@@ -76,3 +76,28 @@ function () {
7676
'--model' => 'User',
7777
])->assertSuccessful();
7878
});
79+
80+
it('can force create policy even if it exists', function () {
81+
$this->artisan(LaravelAuthorizerCommand::class, [
82+
'name' => 'User',
83+
'--model' => 'User',
84+
])->assertSuccessful();
85+
86+
assertStringContainsString(
87+
'create user',
88+
file_get_contents(base_path('app/Policies/UserPolicy.php'))
89+
);
90+
91+
$this->artisan(LaravelAuthorizerCommand::class, [
92+
'name' => 'User',
93+
'--model' => 'Post',
94+
'--force' => true,
95+
])->assertSuccessful();
96+
97+
assertFileExists(app_path('Policies/UserPolicy.php'));
98+
99+
assertStringContainsString(
100+
'create post',
101+
file_get_contents(app_path('Policies/UserPolicy.php'))
102+
);
103+
});

0 commit comments

Comments
 (0)