From 8618e5e89602c817f5ee557b8c41c0de470df7ae Mon Sep 17 00:00:00 2001 From: DariusIII Date: Wed, 27 Jun 2018 10:02:44 +0200 Subject: [PATCH] Merge roles and user_roles tables for new permission system --- Changelog | 1 + config/permission.php | 88 +++++++++++++++++++ .../fixtures/{user_roles.php => roles.php} | 5 -- .../2018_01_16_195548_create_users_table.php | 3 +- ...01_17_150719_create_permission_tables.php} | 24 +++++ ...8_01_17_150719_create_user_roles_table.php | 45 ---------- 6 files changed, 114 insertions(+), 52 deletions(-) create mode 100644 config/permission.php rename database/fixtures/{user_roles.php => roles.php} (93%) rename database/migrations/{2018_06_26_000322_create_permission_tables.php => 2018_01_17_150719_create_permission_tables.php} (71%) delete mode 100644 database/migrations/2018_01_17_150719_create_user_roles_table.php diff --git a/Changelog b/Changelog index d5dfdb3f8..f3e79c268 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-06-27 DariusIII + * Chg: Merge roles and user_roles tables for new permission system * Chg: Update Movie processing handling 2018-06-26 DariusIII * Chg: Add HasRole trait to User model diff --git a/config/permission.php b/config/permission.php new file mode 100644 index 000000000..4750e5f8e --- /dev/null +++ b/config/permission.php @@ -0,0 +1,88 @@ + [ + + /* + * When using the "HasRoles" trait from this package, we need to know which + * Eloquent model should be used to retrieve your permissions. Of course, it + * is often just the "Permission" model but you may use whatever you like. + * + * The model you want to use as a Permission model needs to implement the + * `Spatie\Permission\Contracts\Permission` contract. + */ + + 'permission' => Spatie\Permission\Models\Permission::class, + + /* + * When using the "HasRoles" trait from this package, we need to know which + * Eloquent model should be used to retrieve your roles. Of course, it + * is often just the "Role" model but you may use whatever you like. + * + * The model you want to use as a Role model needs to implement the + * `Spatie\Permission\Contracts\Role` contract. + */ + + 'role' => Spatie\Permission\Models\Role::class, + + ], + + 'table_names' => [ + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your roles. We have chosen a basic + * default value but you may easily change it to any table you like. + */ + + 'roles' => 'roles', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your permissions. We have chosen a basic + * default value but you may easily change it to any table you like. + */ + + 'permissions' => 'permissions', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your models permissions. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'model_has_permissions' => 'model_has_permissions', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your models roles. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'model_has_roles' => 'model_has_roles', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your roles permissions. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'role_has_permissions' => 'role_has_permissions', + ], + + /* + * By default all permissions will be cached for 24 hours unless a permission or + * role is updated. Then the cache will be flushed immediately. + */ + + 'cache_expiration_time' => 60 * 24, + + /* + * When set to true, the required permission/role names are added to the exception + * message. This could be considered an information leak in some contexts, so + * the default setting is false here for optimum safety. + */ + + 'display_permission_in_exception' => false, +]; diff --git a/database/fixtures/user_roles.php b/database/fixtures/roles.php similarity index 93% rename from database/fixtures/user_roles.php rename to database/fixtures/roles.php index 3385b09fa..f8d195b16 100644 --- a/database/fixtures/user_roles.php +++ b/database/fixtures/roles.php @@ -2,7 +2,6 @@ return [ 0 => [ - 'id' => 1, 'name' => 'User', 'apirequests' => 10, 'downloadrequests' => 10, @@ -14,7 +13,6 @@ return [ 'addyears' => 0, ], 1 => [ - 'id' => 2, 'name' => 'Admin', 'apirequests' => 1000, 'downloadrequests' => 1000, @@ -26,7 +24,6 @@ return [ 'addyears' => 0, ], 2 => [ - 'id' => 3, 'name' => 'Disabled', 'apirequests' => 0, 'downloadrequests' => 0, @@ -38,7 +35,6 @@ return [ 'addyears' => 0, ], 3 => [ - 'id' => 4, 'name' => 'Moderator', 'apirequests' => 1000, 'downloadrequests' => 1000, @@ -50,7 +46,6 @@ return [ 'addyears' => 0, ], 4 => [ - 'id' => 5, 'name' => 'Friend', 'apirequests' => 100, 'downloadrequests' => 100, diff --git a/database/migrations/2018_01_16_195548_create_users_table.php b/database/migrations/2018_01_16_195548_create_users_table.php index 359c21abd..ea877c8ef 100644 --- a/database/migrations/2018_01_16_195548_create_users_table.php +++ b/database/migrations/2018_01_16_195548_create_users_table.php @@ -23,11 +23,9 @@ class CreateUsersTable extends Migration { $table->string('lastname')->nullable(); $table->string('email'); $table->string('password'); - $table->integer('user_roles_id')->default(1)->index('ix_user_roles')->comment('FK to user_roles.id'); $table->string('host', 40)->nullable(); $table->integer('grabs')->default(0); $table->string('api_token', 64); - $table->timestamps(); $table->string('resetguid', 50)->nullable(); $table->dateTime('lastlogin')->nullable(); $table->dateTime('apiaccess')->nullable(); @@ -59,6 +57,7 @@ class CreateUsersTable extends Migration { $table->boolean('verified')->default(false); $table->string('verification_token')->nullable(); $table->rememberToken(); + $table->timestamps(); }); } diff --git a/database/migrations/2018_06_26_000322_create_permission_tables.php b/database/migrations/2018_01_17_150719_create_permission_tables.php similarity index 71% rename from database/migrations/2018_06_26_000322_create_permission_tables.php rename to database/migrations/2018_01_17_150719_create_permission_tables.php index 122491496..ee23018b7 100644 --- a/database/migrations/2018_06_26_000322_create_permission_tables.php +++ b/database/migrations/2018_01_17_150719_create_permission_tables.php @@ -16,6 +16,9 @@ class CreatePermissionTables extends Migration $tableNames = config('permission.table_names'); Schema::create($tableNames['permissions'], function (Blueprint $table) { + $table->engine = 'InnoDB'; + $table->charset = 'utf8'; + $table->collation = 'utf8_unicode_ci'; $table->increments('id'); $table->string('name'); $table->string('guard_name'); @@ -23,13 +26,28 @@ class CreatePermissionTables extends Migration }); Schema::create($tableNames['roles'], function (Blueprint $table) { + $table->engine = 'InnoDB'; + $table->charset = 'utf8'; + $table->collation = 'utf8_unicode_ci'; $table->increments('id'); $table->string('name'); $table->string('guard_name'); + $table->integer('apirequests')->unsigned(); + $table->integer('rate_limit')->default(60); + $table->integer('downloadrequests')->unsigned(); + $table->integer('defaultinvites')->unsigned(); + $table->boolean('isdefault')->default(0); + $table->boolean('canpreview')->default(0); + $table->boolean('hideads')->default(0); + $table->integer('donation')->default(0); + $table->integer('addyears')->default(0); $table->timestamps(); }); Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames) { + $table->engine = 'InnoDB'; + $table->charset = 'utf8'; + $table->collation = 'utf8_unicode_ci'; $table->unsignedInteger('permission_id'); $table->morphs('model'); @@ -42,6 +60,9 @@ class CreatePermissionTables extends Migration }); Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames) { + $table->engine = 'InnoDB'; + $table->charset = 'utf8'; + $table->collation = 'utf8_unicode_ci'; $table->unsignedInteger('role_id'); $table->morphs('model'); @@ -54,6 +75,9 @@ class CreatePermissionTables extends Migration }); Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { + $table->engine = 'InnoDB'; + $table->charset = 'utf8'; + $table->collation = 'utf8_unicode_ci'; $table->unsignedInteger('permission_id'); $table->unsignedInteger('role_id'); diff --git a/database/migrations/2018_01_17_150719_create_user_roles_table.php b/database/migrations/2018_01_17_150719_create_user_roles_table.php deleted file mode 100644 index 0bbb08a9b..000000000 --- a/database/migrations/2018_01_17_150719_create_user_roles_table.php +++ /dev/null @@ -1,45 +0,0 @@ -engine = 'InnoDB'; - $table->charset = 'utf8'; - $table->collation = 'utf8_unicode_ci'; - $table->integer('id', true); - $table->string('name', 32); - $table->integer('apirequests')->unsigned(); - $table->integer('rate_limit')->default(60); - $table->integer('downloadrequests')->unsigned(); - $table->integer('defaultinvites')->unsigned(); - $table->boolean('isdefault')->default(0); - $table->boolean('canpreview')->default(0); - $table->boolean('hideads')->default(0); - $table->integer('donation')->default(0); - $table->integer('addyears')->default(0); - }); - } - - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::drop('user_roles'); - } - -}