From 239f4fbcb22144f1b89110d6ec12e5879fd1f4cc Mon Sep 17 00:00:00 2001 From: Norby Baruani Date: Sun, 19 May 2024 21:51:15 +0200 Subject: [PATCH 1/2] drop php7 support and refactor migration file --- .github/workflows/run-tests.yml | 14 +----- .gitignore | 1 + README.md | 40 ++++++----------- composer.json | 14 ++++-- ...es.php => create_authorization_tables.php} | 12 ++--- phpunit.xml | 45 ++++++++++--------- src/Authorization.php | 19 -------- src/AuthorizationServiceProvider.php | 16 +------ tests/Stubs/User.php | 4 +- tests/TestCase.php | 26 +++++++---- 10 files changed, 73 insertions(+), 118 deletions(-) rename database/migrations/{2014_10_12_200000_create_authorization_tables.php => create_authorization_tables.php} (91%) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 122ec08..ee47327 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -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.* @@ -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: diff --git a/.gitignore b/.gitignore index 8bce370..e7c157b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor +/*.cache composer.phar composer.lock Thumbs.db diff --git a/README.md b/README.md index 8b14e6e..bb97627 100644 --- a/README.md +++ b/README.md @@ -31,14 +31,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: @@ -60,27 +67,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. diff --git a/composer.json b/composer.json index 44ec318..ae0534e 100644 --- a/composer.json +++ b/composer.json @@ -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, @@ -36,5 +36,11 @@ "DirectoryTree\\Authorization\\AuthorizationServiceProvider" ] } + }, + "config": { + "sort-packages": true + }, + "scripts": { + "test": "vendor/bin/phpunit" } } diff --git a/database/migrations/2014_10_12_200000_create_authorization_tables.php b/database/migrations/create_authorization_tables.php similarity index 91% rename from database/migrations/2014_10_12_200000_create_authorization_tables.php rename to database/migrations/create_authorization_tables.php index e40ca62..5006923 100644 --- a/database/migrations/2014_10_12_200000_create_authorization_tables.php +++ b/database/migrations/create_authorization_tables.php @@ -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'); @@ -51,10 +49,8 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::dropIfExists('role_user'); Schema::dropIfExists('permission_role'); @@ -62,4 +58,4 @@ public function down() Schema::dropIfExists('permissions'); Schema::dropIfExists('roles'); } -} +}; diff --git a/phpunit.xml b/phpunit.xml index 182bc41..22001a3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,24 +1,25 @@ - - - - ./tests/ - - - - - - - - - + + + + ./tests/ + + + + + + + + + diff --git a/src/Authorization.php b/src/Authorization.php index 4a78f40..72bb233 100644 --- a/src/Authorization.php +++ b/src/Authorization.php @@ -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. * @@ -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; - } } diff --git a/src/AuthorizationServiceProvider.php b/src/AuthorizationServiceProvider.php index e40e0ef..e0ee699 100644 --- a/src/AuthorizationServiceProvider.php +++ b/src/AuthorizationServiceProvider.php @@ -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'); } @@ -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'); - } - } } diff --git a/tests/Stubs/User.php b/tests/Stubs/User.php index 1554dcb..91292d9 100644 --- a/tests/Stubs/User.php +++ b/tests/Stubs/User.php @@ -9,5 +9,7 @@ class User extends BaseUser { use Authorizable; - protected $fillable = ['name']; + protected $fillable = [ + 'name', 'email', 'password', + ]; } diff --git a/tests/TestCase.php b/tests/TestCase.php index 0cd4017..ed0a7cf 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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'); } /** @@ -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); } From 92c070075754ec679eb5c7eff187e63e75063786 Mon Sep 17 00:00:00 2001 From: Norby Baruani Date: Sun, 19 May 2024 22:08:33 +0200 Subject: [PATCH 2/2] upate readme --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index bb97627..5ab98f7 100644 --- a/README.md +++ b/README.md @@ -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) @@ -38,7 +37,7 @@ composer require directorytree/authorization You should publish the migration file as the package needs to store roles and permissions: ```bash -php artisan vendor:publish --tag=authorization-migrations" +php artisan vendor:publish --tag="authorization-migrations" ``` Run migrations