Skip to content

Commit

Permalink
Version 3 prep.
Browse files Browse the repository at this point in the history
  • Loading branch information
warrickbayman committed Jan 6, 2024
1 parent c1d510c commit d0ec32a
Show file tree
Hide file tree
Showing 29 changed files with 241 additions and 325 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Deadbolt is "dead" simple. It's in the name. You define your permissions in the

Deadbolt is simple by design. If you need something more feature rich, there are plenty of other choices. If this doesn't fit the bill, then my go to package is Spatie's [laravel-permission](https://github.com/spatie/laravel-permission) package.

## Requirements

Version 3 requires PHP 8.1 or later and Laravel 10 or later. If you're still using older versions then you'll need to stick with version 2.2.

## Installation

Deadbolt can be installed via Composer:
Expand Down Expand Up @@ -118,7 +122,7 @@ $permissions = Deadbolt::describe(['articles.create', 'articles.edit']);

### Assigning permissions

Deadbolt uses the word "User" to mean any model that has permissions. Meaning any Laravel model that has a "permissions" column, but it doesn't have to your actual `User` model. It could be `Role` model, or an `Organisation` model, for example.
Deadbolt uses the word "User" to mean any model that has permissions that returns array of assigned permissions. This could be any Laravel model that has a "permissions" attribute, but it doesn't have to your actual `User` model. It could be `Role` model, or an `Organisation` model, for example.

To work with permissions on a "user" Deadbolt provides a `user()` method on the `Permissions` facade to which you need to pass your Laravel model:

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
},
"autoload-dev": {
"psr-4": {
"TPG\\Tests\\": "tests/"
"TPG\\Deadbolt\\Tests\\": "tests/",
"TPG\\Deadbolt\\Tests\\Factories\\": "tests/factories/"
}
},
"require": {
"php": "^7.3|^8.0",
"php": "^8.2",
"ext-json": "*",
"laravel/framework": ">=7.24"
"laravel/framework": "^10.0"
},
"require-dev": {
"laravel/legacy-factories": "^1.1",
"orchestra/testbench": ">=5.0",
"phpunit/phpunit": "^8.5|^9.3",
"orchestra/testbench": "^8.0",
"phpunit/phpunit": "^10.0",
"roave/security-advisories": "dev-master"
},
"minimum-stability": "dev",
Expand Down
5 changes: 2 additions & 3 deletions config/deadbolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| The name of the permissions column
|--------------------------------------------------------------------------
|
| This is the name of the column where permissions are stored. By default
| This is the name of the column where permissions are stored. By default,
| this is set to "permissions", but it can be anything you like.
|
*/
Expand All @@ -21,7 +21,7 @@
|
| Only the permissions defined in this array are permitted. This gives
| you a single source of truth and avoids errors. Permission names can
| be anything you like and you can provide a description.
| be anything you like, and you can provide a description.
|
*/

Expand All @@ -43,5 +43,4 @@
*/

'driver' => TPG\Deadbolt\Drivers\ArrayDriver::class,

];
8 changes: 4 additions & 4 deletions database/add_deadbolt_permissions_column.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class addDeadboltPermissionsColumn extends Migration
return new class extends Migration
{
protected $table = 'users';
protected string $table = 'users';

public function up(): void
{
Schema::table($this->table, function (Blueprint $table) {
Schema::table($this->table, static function (Blueprint $table) {
$table->json('permissions')->after('id')->nullable();
});
}
Expand All @@ -21,4 +21,4 @@ public function down(): void
{
Schema::dropColumns($this->table, ['permissions']);
}
}
};
24 changes: 7 additions & 17 deletions src/Contracts/DeadboltServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,36 @@
interface DeadboltServiceInterface
{
/**
* @param array $config
* @param array $config
*/
public function __construct(array $config);

/**
* A user.
*
* @param Model $model
* @return User
* Specify the user to manipulate permissions for.
*/
public function user(Model $model): User;

/**
* A collection of users.
*
* @param ...$users
* @return UserCollection
* A collection of users to manipulate permissions for.
* *
* @param array<Model> $users
*/
public function users(...$users): UserCollection;

/**
* Set permissions the driver.
*
* @param DriverInterface $driver
* @return DeadboltServiceInterface
* Set the driver used to access permissions.
*/
public function driver(DriverInterface $driver): DeadboltServiceInterface;

/**
* Get an array of permissions.
*
* @return array
*/
public function all(): array;

/**
* Get the permission descriptions.
*
* @param ...$permissions
* @return array
* @param array<string> $permissions
*/
public function describe(...$permissions): array;
}
31 changes: 12 additions & 19 deletions src/Contracts/UserCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,62 @@ public function __construct(array $users, array $permissions, array $config);
/**
* Give the specified permissions to the user collection.
*
* @param ...$names
* @return UserCollectionInterface
* @param array<string> $names
*/
public function give(...$names): UserCollectionInterface;

/**
* Give all permissions to the user collection.
*
* @return UserCollectionInterface
*/
public function super(): UserCollectionInterface;

/**
* Revoke the specified permissions from the user collection.
*
* @param ...$names
* @return UserCollectionInterface
* @param array<string> $names
*/
public function revoke(...$names): UserCollectionInterface;

/**
* Revoke all permissions from the user collection.
*
* @return UserCollectionInterface
*/
public function revokeAll(): UserCollectionInterface;

/**
* Sync the specified permissions with the user collection.
*
* @param ...$names
* @return UserCollectionInterface
* @param array<string> $names
*/
public function sync(...$names): UserCollectionInterface;

/**
* Save the user collection.
*
* @return UserCollectionInterface
*/
public function save(): UserCollectionInterface;

/**
* Check if all the users have all the specified permissions.
*
* @param ...$permissions
* @return bool
* @param array<string> $permissions
*/
public function allHave(...$permissions): bool;

/**
* Check if all the users have at least one of the specified permissions.
*
* @param ...$permissions
* @return bool
* @param array<string> $permissions
*/
public function anyHave(...$permissions): bool;

/**
* Check if all of the users have none of the specified permissions.
* Check if all the users have the specified permissions.
*/
public function has(string $permission): bool;

/**
* Check if all the users have none of the specified permissions.
*
* @param ...$permissions
* @return bool
* @param array<string> $permissions
*/
public function noneHave(...$permissions): bool;
}
41 changes: 13 additions & 28 deletions src/Contracts/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,99 +16,84 @@ public function __construct(Model $user, array $permissions, array $config);
/**
* Give the specified permissions.
*
* @param mixed ...$names
* @param array<string>|string ...$names
* @return UserInterface
*
* @throws JsonException|NoSuchPermissionException
*/
public function give(...$names): UserInterface;

/**
* Make a super user.
*
* @return UserInterface
* Make a superuser.
*
* @throws JsonException|NoSuchPermissionException
*/
public function super(): UserInterface;

/**
* Check if the user is super.
*/
public function isSuper(): bool;

/**
* Revoke the specified permissions.
*
* @param mixed ...$names
* @return UserInterface
* @param array<string>|string $names
*
* @throws JsonException
*/
public function revoke(...$names): UserInterface;

/**
* Revoke all permissions.
*
* @return UserInterface
*/
public function revokeAll(): UserInterface;

/**
* Sync permissions with the names provided.
*
* @param mixed ...$names
* @return UserInterface
* @param array<string>|string ...$names
*/
public function sync(...$names): UserInterface;

/**
* Save the current permission set.
*
* @return UserInterface
*/
public function save(): UserInterface;

/**
* Check if the specified permission is assigned.
*
* @param string $permission
* @return bool
*
* Check if the specified permission is assigned.*
* @throws JsonException
*/
public function has(string $permission): bool;

/**
* Check if all the specified permissions are assigned.
*
* @param mixed ...$permissions
* @return bool
*
* @param array<string>|string $permissions
* @throws JsonException
*/
public function hasAll(...$permissions): bool;

/**
* Check if any of the specified permissions are assigned.
*
* @param mixed ...$permissions
* @return bool
*
* @param array<string>|string $permissions
* @throws JsonException
*/
public function hasAny(...$permissions): bool;

/**
* Check if none of the specified permissions are assigned.
*
* @param mixed ...$permissions
* @return bool
*
* @param array<string>|string ...$permissions
* @throws JsonException
*/
public function hasNone(...$permissions): bool;

/**
* Get an array of permissions assigned to the user.
*
* @return array
*
* @throws JsonException
*/
public function all(): array;
Expand Down
Loading

0 comments on commit d0ec32a

Please sign in to comment.