Skip to content

Commit

Permalink
Merge pull request #132 from cyberionsys/v4.0
Browse files Browse the repository at this point in the history
V4.0
  • Loading branch information
mostafamaklad authored Feb 22, 2022
2 parents 8046ea7 + 1a91f1c commit bc1cf2c
Show file tree
Hide file tree
Showing 29 changed files with 182 additions and 153 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All Notable changes to `laravel-permission-mongodb` will be documented in this file.

## 4.0.0 - 2022-02-21

### Added
- Support of Laravel 9.x
- Added some return toward PHP 8 transitioning to require return types
- Use of DatabaseMigration and Seeder in tests
- Fix some tests (api guard is no more in auth.php by default)


## 3.1.0 - 2020-10-04

### Added
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,24 @@ $user->can('edit articles');
6.x | 2.x or 3.x
7.x | 3.x
8.x | 3.1.x
9.x | 4.x

### Laravel

You can install the package via composer:

For laravel 9.x use

``` bash
composer require mostafamaklad/laravel-permission-mongodb
```

For laravel 8.x and older use

``` bash
composer require mostafamaklad/laravel-permission-mongodb:"^3.1"
```

You can publish [the migration](database/migrations/create_permission_collections.php.stub) with:

```bash
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
}
],
"require": {
"php": ">=7.2",
"illuminate/auth": "^6.0|^7.0|^8.0",
"illuminate/container": "^6.0|^7.0|^8.0",
"illuminate/contracts": "^6.0|^7.0|^8.0",
"jenssegers/mongodb": "^3.0"
"php": "^8.0.2",
"illuminate/auth": "^9.0",
"illuminate/container": "^9.0",
"illuminate/contracts": "^9.0",
"jenssegers/mongodb": "^3.9"
},
"require-dev": {
"monolog/monolog": "^1.23|^2.0",
"orchestra/testbench": "^3.2|^4.0|^5.0",
"phpunit/phpunit": "^5.7|^6.0|^7.0|^8.0",
"squizlabs/php_codesniffer": "^3.1"
"monolog/monolog": "^2.3",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 4 additions & 4 deletions database/migrations/create_permission_collections.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Jenssegers\Mongodb\Schema\Blueprint;

class CreatePermissionCollections extends Migration
return new class extends Migration
{
/**
* Run the migrations.
Expand Down Expand Up @@ -33,7 +33,7 @@ class CreatePermissionCollections extends Migration
{
$collectionNames = config('permission.collection_names');

Schema::drop($collectionNames['roles']);
Schema::drop($collectionNames['permissions']);
Schema::dropIfExists($collectionNames['roles']);
Schema::dropIfExists($collectionNames['permissions']);
}
}
};
35 changes: 18 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="League Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<testsuites>
<testsuite name="League Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</phpunit>
6 changes: 3 additions & 3 deletions src/Commands/CreatePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
*/
class CreatePermission extends Command
{
protected $signature = 'permission:create-permission
{name : The name of the permission}
protected $signature = 'permission:create-permission
{name : The name of the permission}
{guard? : The name of the guard}';

protected $description = 'Create a permission';

public function handle()
public function handle(): void
{
$permissionClass = \app(\config('permission.models.permission'));

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/CreateRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CreateRole extends Command

protected $description = 'Create a role';

public function handle()
public function handle(): void
{
$roleClass = \app(\config('permission.models.role'));

Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/RoleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function permissions(): BelongsToMany;
*
* @throws RoleDoesNotExist
*/
public static function findByName(string $name, $guardName): RoleInterface;
public static function findByName(string $name, ?string $guardName): RoleInterface;

/**
* Determine if the user may perform the given permission.
Expand All @@ -36,5 +36,5 @@ public static function findByName(string $name, $guardName): RoleInterface;
*
* @return bool
*/
public function hasPermissionTo($permission): bool;
public function hasPermissionTo(string|PermissionInterface $permission): bool;
}
10 changes: 5 additions & 5 deletions src/Directives/PermissionDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class PermissionDirectives
{
private $bladeCompiler;
private BladeCompiler $bladeCompiler;

public function __construct(BladeCompiler $bladeCompiler)
{
Expand All @@ -20,7 +20,7 @@ public function __construct(BladeCompiler $bladeCompiler)
/**
* Declare role directive
*/
public function roleDirective()
public function roleDirective(): void
{
$this->bladeCompiler->directive('role', function ($arguments) {
list($role, $guard) = $this->extractRoleGuard($arguments);
Expand All @@ -36,7 +36,7 @@ public function roleDirective()
/**
* Declare hasrole directive
*/
public function hasroleDirective()
public function hasroleDirective(): void
{
$this->bladeCompiler->directive('hasrole', function ($arguments) {
list($role, $guard) = $this->extractRoleGuard($arguments);
Expand All @@ -51,7 +51,7 @@ public function hasroleDirective()
/**
* Declare hasanyrole directive
*/
public function hasanyroleDirective()
public function hasanyroleDirective(): void
{
$this->bladeCompiler->directive('hasanyrole', function ($arguments) {
list($roles, $guard) = $this->extractRoleGuard($arguments);
Expand All @@ -66,7 +66,7 @@ public function hasanyroleDirective()
/**
* Declare hasallroles directive
*/
public function hasallrolesDirective()
public function hasallrolesDirective(): void
{
$this->bladeCompiler->directive('hasallroles', function ($arguments) {
list($roles, $guard) = $this->extractRoleGuard($arguments);
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/MakladException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MakladException extends InvalidArgumentException
/**
* MakladException constructor.
*
* @param string $message
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Exceptions/UnauthorizedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
*/
class UnauthorizedException extends HttpException
{
private $requiredRoles = [];
private $requiredPermissions = [];
private array $requiredRoles = [];
private array $requiredPermissions = [];

/**
* UnauthorizedException constructor.
*
* @param $statusCode
* @param string $message
* @param string|null $message
* @param array $requiredRoles
* @param array $requiredPermissions
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/UnauthorizedPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UnauthorizedPermission extends UnauthorizedException
* UnauthorizedPermission constructor.
*
* @param $statusCode
* @param string $message
* @param string|null $message
* @param array $requiredPermissions
*/
public function __construct($statusCode, string $message = null, array $requiredPermissions = [])
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/UnauthorizedRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UnauthorizedRole extends UnauthorizedException
* UnauthorizedPermission constructor.
*
* @param $statusCode
* @param string $message
* @param string|null $message
* @param array $requiredRoles
*/
public function __construct($statusCode, string $message = null, array $requiredRoles = [])
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Helpers
*
* @return string|null
*/
public function getModelForGuard(string $guard)
public function getModelForGuard(string $guard): ?string
{
return \collect(\config('auth.guards'))
->map(function ($guard) {
Expand Down
10 changes: 6 additions & 4 deletions src/Middlewares/PermissionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Maklad\Permission\Middlewares;

use Closure;
use Illuminate\Http\Request;
use Maklad\Permission\Exceptions\UnauthorizedException;
use Maklad\Permission\Exceptions\UnauthorizedPermission;
use Maklad\Permission\Exceptions\UserNotLoggedIn;
use Maklad\Permission\Helpers;
Expand All @@ -14,14 +16,14 @@
class PermissionMiddleware
{
/**
* @param $request
* @param Request $request
* @param Closure $next
* @param $permission
* @param array|string $permission
*
* @return mixed
* @throws \Maklad\Permission\Exceptions\UnauthorizedException
* @throws UnauthorizedException
*/
public function handle($request, Closure $next, $permission)
public function handle(Request $request, Closure $next, array|string $permission): mixed
{
if (app('auth')->guest()) {
$helpers = new Helpers();
Expand Down
7 changes: 4 additions & 3 deletions src/Middlewares/RoleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Maklad\Permission\Middlewares;

use Closure;
use Illuminate\Http\Request;
use Maklad\Permission\Exceptions\UnauthorizedRole;
use Maklad\Permission\Exceptions\UserNotLoggedIn;
use Maklad\Permission\Helpers;
Expand All @@ -14,14 +15,14 @@
class RoleMiddleware
{
/**
* @param $request
* @param Request $request
* @param Closure $next
* @param $role
* @param array|string $role
*
* @return mixed
* @throws \Maklad\Permission\Exceptions\UnauthorizedException
*/
public function handle($request, Closure $next, $role)
public function handle(Request $request, Closure $next, array|string $role): mixed
{
if (app('auth')->guest()) {
$helpers = new Helpers();
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Permission extends Model implements PermissionInterface
use RefreshesPermissionCache;

public $guarded = ['id'];
protected $helpers;
protected Helpers $helpers;

/**
* Permission constructor.
Expand All @@ -49,7 +49,7 @@ public function __construct(array $attributes = [])
*
* @param array $attributes
*
* @return $this|\Illuminate\Database\Eloquent\Model
* @return $this|mixed
* @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists
* @throws \ReflectionException
*/
Expand All @@ -74,7 +74,7 @@ public static function create(array $attributes = [])
* Find or create permission by its name (and optionally guardName).
*
* @param string $name
* @param string $guardName
* @param string|null $guardName
*
* @return PermissionInterface
* @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists
Expand Down Expand Up @@ -117,7 +117,7 @@ public function users(): BelongsToMany
* Find a permission by its name (and optionally guardName).
*
* @param string $name
* @param string $guardName
* @param string|null $guardName
*
* @return PermissionInterface
* @throws PermissionDoesNotExist
Expand Down
Loading

0 comments on commit bc1cf2c

Please sign in to comment.