Skip to content

Commit

Permalink
refactor(user): update naming and creation process (#107)
Browse files Browse the repository at this point in the history
Improve codebase clarity by adopting dot notation for user permissions'
naming and utilizing the models create function for permission creation.
Enhances code readability and maintainability.

Signed-off-by: Valentin Sickert <[email protected]>
  • Loading branch information
Lapotor authored Jan 9, 2024
1 parent edbdc6d commit 91d2457
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 52 deletions.
12 changes: 6 additions & 6 deletions app/Permissions/UsersPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
class UsersPermissions
{
/** Permission for listing and view all users. */
public const CAN_LIST_USERS = 'list-users';
public const CAN_LIST_USERS = 'users.list';

/** Permission for showing users itself. */
public const CAN_SHOW_USERS = 'show-users';
public const CAN_SHOW_USERS = 'users.show';

/** Permission for creating users. */
public const CAN_CREATE_USERS = 'create-users';
public const CAN_CREATE_USERS = 'users.create';

/** Permission for updating users. */
public const CAN_UPDATE_USERS = 'update-users';
public const CAN_UPDATE_USERS = 'users.update';

/** Permission for updating users itself. */
public const CAN_UPDATE_USERS_SELF = 'update-users-self';
public const CAN_UPDATE_USERS_SELF = 'users.update.self';

/** Permission for deleting users. */
public const CAN_DELETE_USERS = 'delete-users';
public const CAN_DELETE_USERS = 'users.delete';
}
77 changes: 31 additions & 46 deletions database/migrations/2023_12_13_214743_add_user_permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Carbon\Carbon;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;

return new class extends Migration
{
Expand All @@ -12,46 +13,30 @@
*/
public function up(): void
{
DB::table('permissions')->insert(
[
[
'name' => UsersPermissions::CAN_LIST_USERS,
'guard_name' => 'web',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => UsersPermissions::CAN_SHOW_USERS,
'guard_name' => 'web',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => UsersPermissions::CAN_CREATE_USERS,
'guard_name' => 'web',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => UsersPermissions::CAN_UPDATE_USERS,
'guard_name' => 'web',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => UsersPermissions::CAN_UPDATE_USERS_SELF,
'guard_name' => 'web',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => UsersPermissions::CAN_DELETE_USERS,
'guard_name' => 'web',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]
]
);
Permission::create([
'name' => 'users.list',
'guard_name' => 'web',
]);
Permission::create([
'name' => 'users.show',
'guard_name' => 'web',
]);
Permission::create([
'name' => 'users.create',
'guard_name' => 'web',
]);
Permission::create([
'name' => 'users.update',
'guard_name' => 'web',
]);
Permission::create([
'name' => 'users.update.self',
'guard_name' => 'web',
]);
Permission::create([
'name' => 'users.delete',
'guard_name' => 'web',
]);
}

/**
Expand All @@ -60,12 +45,12 @@ public function up(): void
public function down(): void
{
DB::table('permissions')->whereIn('name', [
UsersPermissions::CAN_LIST_USERS,
UsersPermissions::CAN_SHOW_USERS,
UsersPermissions::CAN_CREATE_USERS,
UsersPermissions::CAN_UPDATE_USERS,
UsersPermissions::CAN_UPDATE_USERS_SELF,
UsersPermissions::CAN_DELETE_USERS,
'users.list',
'users.show',
'users.create',
'users.update',
'users.update.self',
'users.delete',
])->delete();
}
};

0 comments on commit 91d2457

Please sign in to comment.