Skip to content

Commit 8df35df

Browse files
authored
Merge pull request #15 from skni-kod/feat/12-permisje
Feat/12 permisje
2 parents 7afb7ff + 9fb1042 commit 8df35df

File tree

14 files changed

+495
-9
lines changed

14 files changed

+495
-9
lines changed

app/Enums/RolesEnum.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
enum RolesEnum: string
6+
{
7+
case DRIVER = 'driver';
8+
case INSTRUCTOR = 'instructor';
9+
case OWNER = 'owner';
10+
11+
// extra helper to allow for greater customization of displayed values, without disclosing the name/value data directly
12+
public function label(): string
13+
{
14+
return match ($this) {
15+
self::DRIVER => 'L-driver',
16+
self::INSTRUCTOR => 'Instructor',
17+
self::OWNER => 'Owner',
18+
};
19+
}
20+
}

app/Http/Controllers/AuthController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function register(Request $request): JsonResponse
2424
$fields = $request->validate([
2525
'name' => 'required|string',
2626
'email' => 'required|email|unique:users,email',
27-
'password' => 'required',
27+
'password' => 'required|confirmed',
2828
]);
2929

3030
try {
@@ -54,7 +54,7 @@ public function login(LoginRequest $request): JsonResponse
5454

5555
$credentials = $request->validated();
5656

57-
$user = User::where('email', $credentials['email'])->first();
57+
$user = User::with(['roles:id,name'])->where('email', $credentials['email'])->first();
5858

5959
if (! $user || ! Hash::check($credentials['password'], $user->password)) {
6060

@@ -67,7 +67,7 @@ public function login(LoginRequest $request): JsonResponse
6767

6868
return response()->json([
6969
'success' => true,
70-
'user' => $user,
70+
'user' => new UserResource($user),
7171
'access_token' => $token,
7272
'message' => 'Login successful',
7373
], Response::HTTP_OK);

app/Models/User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
use Illuminate\Foundation\Auth\User as Authenticatable;
1010
use Illuminate\Notifications\Notifiable;
1111
use Laravel\Sanctum\HasApiTokens;
12+
use Spatie\Permission\Traits\HasRoles;
1213

1314
class User extends Authenticatable
1415
{
1516
/** @use HasFactory<UserFactory> */
16-
use HasApiTokens, HasFactory, Notifiable;
17+
use HasApiTokens, HasFactory, HasRoles, Notifiable;
1718

1819
/**
1920
* The attributes that are mass assignable.

bootstrap/app.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
use Illuminate\Foundation\Application;
44
use Illuminate\Foundation\Configuration\Exceptions;
55
use Illuminate\Foundation\Configuration\Middleware;
6+
use Spatie\Permission\Middleware\PermissionMiddleware;
7+
use Spatie\Permission\Middleware\RoleMiddleware;
8+
use Spatie\Permission\Middleware\RoleOrPermissionMiddleware;
69

710
return Application::configure(basePath: dirname(__DIR__))
811
->withRouting(
912
web: __DIR__.'/../routes/web.php',
10-
api: __DIR__.'/../routes/api.php',
13+
api: [__DIR__.'/../routes/api/api.php',
14+
__DIR__.'/../routes/api/driver.php',
15+
__DIR__.'/../routes/api/instructor.php'],
1116
commands: __DIR__.'/../routes/console.php',
1217
health: '/up',
1318
)
1419
->withMiddleware(function (Middleware $middleware) {
1520
$middleware->statefulApi();
21+
$middleware->alias([
22+
'role' => RoleMiddleware::class,
23+
'permission' => PermissionMiddleware::class,
24+
'role_or_permission' => RoleOrPermissionMiddleware::class,
25+
]);
1626
})
1727
->withExceptions(function (Exceptions $exceptions) {
1828
//

bootstrap/providers.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use Spatie\Permission\PermissionServiceProvider;
4+
35
return [
46
App\Providers\AppServiceProvider::class,
7+
PermissionServiceProvider::class,
58
];

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"laravel/sanctum": "^4.0",
1212
"laravel/socialite": "^5.16",
1313
"laravel/tinker": "^2.9",
14+
"spatie/laravel-permission": "^6.10",
1415
"spatie/laravel-query-builder": "^6.3"
1516
},
1617
"require-dev": {

composer.lock

Lines changed: 84 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/permission.php

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
3+
return [
4+
5+
'models' => [
6+
7+
/*
8+
* When using the "HasPermissions" trait from this package, we need to know which
9+
* Eloquent model should be used to retrieve your permissions. Of course, it
10+
* is often just the "Permission" model but you may use whatever you like.
11+
*
12+
* The model you want to use as a Permission model needs to implement the
13+
* `Spatie\Permission\Contracts\Permission` contract.
14+
*/
15+
16+
'permission' => Spatie\Permission\Models\Permission::class,
17+
18+
/*
19+
* When using the "HasRoles" trait from this package, we need to know which
20+
* Eloquent model should be used to retrieve your roles. Of course, it
21+
* is often just the "Role" model but you may use whatever you like.
22+
*
23+
* The model you want to use as a Role model needs to implement the
24+
* `Spatie\Permission\Contracts\Role` contract.
25+
*/
26+
27+
'role' => Spatie\Permission\Models\Role::class,
28+
29+
],
30+
31+
'table_names' => [
32+
33+
/*
34+
* When using the "HasRoles" trait from this package, we need to know which
35+
* table should be used to retrieve your roles. We have chosen a basic
36+
* default value but you may easily change it to any table you like.
37+
*/
38+
39+
'roles' => 'roles',
40+
41+
/*
42+
* When using the "HasPermissions" trait from this package, we need to know which
43+
* table should be used to retrieve your permissions. We have chosen a basic
44+
* default value but you may easily change it to any table you like.
45+
*/
46+
47+
'permissions' => 'permissions',
48+
49+
/*
50+
* When using the "HasPermissions" trait from this package, we need to know which
51+
* table should be used to retrieve your models permissions. We have chosen a
52+
* basic default value but you may easily change it to any table you like.
53+
*/
54+
55+
'model_has_permissions' => 'model_has_permissions',
56+
57+
/*
58+
* When using the "HasRoles" trait from this package, we need to know which
59+
* table should be used to retrieve your models roles. We have chosen a
60+
* basic default value but you may easily change it to any table you like.
61+
*/
62+
63+
'model_has_roles' => 'model_has_roles',
64+
65+
/*
66+
* When using the "HasRoles" trait from this package, we need to know which
67+
* table should be used to retrieve your roles permissions. We have chosen a
68+
* basic default value but you may easily change it to any table you like.
69+
*/
70+
71+
'role_has_permissions' => 'role_has_permissions',
72+
],
73+
74+
'column_names' => [
75+
/*
76+
* Change this if you want to name the related pivots other than defaults
77+
*/
78+
'role_pivot_key' => null, //default 'role_id',
79+
'permission_pivot_key' => null, //default 'permission_id',
80+
81+
/*
82+
* Change this if you want to name the related model primary key other than
83+
* `model_id`.
84+
*
85+
* For example, this would be nice if your primary keys are all UUIDs. In
86+
* that case, name this `model_uuid`.
87+
*/
88+
89+
'model_morph_key' => 'model_id',
90+
91+
/*
92+
* Change this if you want to use the teams feature and your related model's
93+
* foreign key is other than `team_id`.
94+
*/
95+
96+
'team_foreign_key' => 'team_id',
97+
],
98+
99+
/*
100+
* When set to true, the method for checking permissions will be registered on the gate.
101+
* Set this to false if you want to implement custom logic for checking permissions.
102+
*/
103+
104+
'register_permission_check_method' => true,
105+
106+
/*
107+
* When set to true, Laravel\Octane\Events\OperationTerminated event listener will be registered
108+
* this will refresh permissions on every TickTerminated, TaskTerminated and RequestTerminated
109+
* NOTE: This should not be needed in most cases, but an Octane/Vapor combination benefited from it.
110+
*/
111+
'register_octane_reset_listener' => false,
112+
113+
/*
114+
* Teams Feature.
115+
* When set to true the package implements teams using the 'team_foreign_key'.
116+
* If you want the migrations to register the 'team_foreign_key', you must
117+
* set this to true before doing the migration.
118+
* If you already did the migration then you must make a new migration to also
119+
* add 'team_foreign_key' to 'roles', 'model_has_roles', and 'model_has_permissions'
120+
* (view the latest version of this package's migration file)
121+
*/
122+
123+
'teams' => false,
124+
125+
/*
126+
* Passport Client Credentials Grant
127+
* When set to true the package will use Passports Client to check permissions
128+
*/
129+
130+
'use_passport_client_credentials' => false,
131+
132+
/*
133+
* When set to true, the required permission names are added to exception messages.
134+
* This could be considered an information leak in some contexts, so the default
135+
* setting is false here for optimum safety.
136+
*/
137+
138+
'display_permission_in_exception' => false,
139+
140+
/*
141+
* When set to true, the required role names are added to exception messages.
142+
* This could be considered an information leak in some contexts, so the default
143+
* setting is false here for optimum safety.
144+
*/
145+
146+
'display_role_in_exception' => false,
147+
148+
/*
149+
* By default wildcard permission lookups are disabled.
150+
* See documentation to understand supported syntax.
151+
*/
152+
153+
'enable_wildcard_permission' => false,
154+
155+
/*
156+
* The class to use for interpreting wildcard permissions.
157+
* If you need to modify delimiters, override the class and specify its name here.
158+
*/
159+
// 'permission.wildcard_permission' => Spatie\Permission\WildcardPermission::class,
160+
161+
/* Cache-specific settings */
162+
163+
'cache' => [
164+
165+
/*
166+
* By default all permissions are cached for 24 hours to speed up performance.
167+
* When permissions or roles are updated the cache is flushed automatically.
168+
*/
169+
170+
'expiration_time' => \DateInterval::createFromDateString('24 hours'),
171+
172+
/*
173+
* The cache key used to store all permissions.
174+
*/
175+
176+
'key' => 'spatie.permission.cache',
177+
178+
/*
179+
* You may optionally indicate a specific cache driver to use for permission and
180+
* role caching using any of the `store` drivers listed in the cache.php config
181+
* file. Using 'default' here means to use the `default` set in cache.php.
182+
*/
183+
184+
'store' => 'default',
185+
],
186+
];

0 commit comments

Comments
 (0)