Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
laravel: [ 11.*, 10.*, 9.*, 8.*, 7.*, 6.*, 5.* ]
laravel: [ 11.*, 10.*, 9.*, 8.* ]
dependency-version: [ prefer-stable ]
include:
- laravel: 11.*
Expand All @@ -32,18 +32,6 @@ jobs:
testbench: 6.*
php: 8.1

- laravel: 7.*
testbench: 5.*
php: 8.0

- laravel: 6.*
testbench: 4.*
php: 8.0

- laravel: 5.*
testbench: 3.*
php: 7.4

name: ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
/*.cache
composer.phar
composer.lock
Thumbs.db
Expand Down
41 changes: 13 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ An easy, native role / permission management system for Laravel.
## Index

- [Installation](#installation)
- [Migration Customization](#migration-customization)
- [Model Customization](#model-customization)
- [Usage](#usage)
- [Checking Permissions & Roles](#checking-permissions--roles)
Expand All @@ -31,14 +30,21 @@ An easy, native role / permission management system for Laravel.

To get started, install Authorization via the Composer package manager:

composer require directorytree/authorization
```bash
composer require directorytree/authorization
```

You should publish the migration file as the package needs to store roles and permissions:

The Authorization service provider registers its own database migration directory
with the framework, so you should migrate your database after installing the
package. The Authorization migrations will create the tables your
application needs to store roles and permissions:
```bash
php artisan vendor:publish --tag="authorization-migrations"
```

php artisan migrate
Run migrations

```bash
php artisan migrate
```

Now insert the `DirectoryTree\Authorization\Traits\Authorizable` onto your `App\Models\User` model:

Expand All @@ -60,27 +66,6 @@ class User extends Authenticatable

You can now perform user authorization.

### Migration Customization

If you would not like to use Authorization's default migrations, you should call the
`Authorization::ignoreMigrations` method in the `register` method of your
`AppServiceProvider`. You may export the default migrations using
`php artisan vendor:publish --tag=authorization-migrations`.

```php
use DirectoryTree\Authorization\Authorization;

/**
* Register any application services.
*
* @return void
*/
public function register()
{
Authorization::ignoreMigrations();
}
```

### Model Customization

By default, the `App\Models\User` class is registered as the authorizable user model.
Expand Down
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.4",
"illuminate/database": "^5.5|^6.20|^7.0|^8.0|^9.0|^10.0|^11.0"
"php": "^8.0",
"illuminate/database": "^8.0|^9.0|^10.0|^11.0"
},
"require-dev": {
"orchestra/testbench": "^3.7|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0",
"phpunit/phpunit": "^7.0|^8.0|^9.0|^10.0"
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0",
"phpunit/phpunit": "^9.0|^10.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -36,5 +36,11 @@
"DirectoryTree\\Authorization\\AuthorizationServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAuthorizationTables extends Migration
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
Expand Down Expand Up @@ -51,15 +49,13 @@ public function up()

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('role_user');
Schema::dropIfExists('permission_role');
Schema::dropIfExists('permission_user');
Schema::dropIfExists('permissions');
Schema::dropIfExists('roles');
}
}
};
45 changes: 23 additions & 22 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
19 changes: 0 additions & 19 deletions src/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@

class Authorization
{
/**
* Indicates if Authorization migrations will be run.
*
* @var bool
*/
public static $runsMigrations = true;

/**
* Indicates if Authorization will register permissions into the gate.
*
Expand Down Expand Up @@ -211,16 +204,4 @@ public static function disablePermissionCache()

return new static;
}

/**
* Configure Authorization to not register its migrations.
*
* @return static
*/
public static function ignoreMigrations()
{
static::$runsMigrations = false;

return new static;
}
}
16 changes: 1 addition & 15 deletions src/AuthorizationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ class AuthorizationServiceProvider extends ServiceProvider
public function boot()
{
if ($this->app->runningInConsole()) {
$this->registerMigrations();

$this->publishes([
__DIR__.'/../database/migrations' => database_path('migrations'),
__DIR__.'/../database/migrations/create_authorization_tables.php' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_authorization_tables.php'),
], 'authorization-migrations');
}

Expand All @@ -26,16 +24,4 @@ public function boot()
app(PermissionRegistrar::class)->register();
}
}

/**
* Register Authorization migration files.
*
* @return void
*/
protected function registerMigrations()
{
if (Authorization::$runsMigrations) {
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}
}
4 changes: 3 additions & 1 deletion tests/Stubs/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ class User extends BaseUser
{
use Authorizable;

protected $fillable = ['name'];
protected $fillable = [
'name', 'email', 'password',
];
}
26 changes: 17 additions & 9 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@

namespace DirectoryTree\Authorization\Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;
use Illuminate\Foundation\Testing\WithFaker;
use DirectoryTree\Authorization\Authorization;
use DirectoryTree\Authorization\AuthorizationServiceProvider;
use DirectoryTree\Authorization\Tests\Stubs\User;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use DirectoryTree\Authorization\AuthorizationServiceProvider;

class TestCase extends BaseTestCase
abstract class TestCase extends BaseTestCase
{
use RefreshDatabase;
use WithFaker;

public function setUp(): void
{
parent::setUp();
}

// Create the users table for testing.
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
/**
* {@inheritdoc}
*/
protected function defineDatabaseMigrations()
{
$this->loadMigrationsFrom(base_path('migrations'));
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}

/**
Expand All @@ -44,6 +50,8 @@ protected function getPackageProviders($app)
*/
protected function createUser($attributes = [])
{
$attributes['email'] = $this->faker->unique()->safeEmail;
$attributes['password'] = Hash::make(Str::random(10));
return User::create($attributes);
}

Expand Down