diff --git a/README.md b/README.md index 6e3a03c..d7304fb 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Then run `php artisan migrate` and the following _5_ tables will be created: * roles * roles_permissions * users_permissions -* users_roles +* role_user Creating the __CRUD__ and populating thoses tables is up to you. diff --git a/migrations/2017_02_05_092446_create_permissions_table.php b/migrations/2017_02_05_092446_create_permissions_table.php index 340d216..d0b8503 100644 --- a/migrations/2017_02_05_092446_create_permissions_table.php +++ b/migrations/2017_02_05_092446_create_permissions_table.php @@ -15,7 +15,7 @@ public function up() { Schema::create('permissions', function (Blueprint $table) { $table->increments('id'); - $table->string('name'); + $table->string('name')->unique(); $table->timestamps(); }); } diff --git a/migrations/2017_02_05_093559_create_role_user_table.php b/migrations/2017_02_05_093559_create_users_roles_table.php similarity index 79% rename from migrations/2017_02_05_093559_create_role_user_table.php rename to migrations/2017_02_05_093559_create_users_roles_table.php index f860732..25d3d6d 100644 --- a/migrations/2017_02_05_093559_create_role_user_table.php +++ b/migrations/2017_02_05_093559_create_users_roles_table.php @@ -4,7 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateRoleUserTable extends Migration +class CreateUsersRolesTable extends Migration { /** * Run the migrations. @@ -13,8 +13,7 @@ class CreateRoleUserTable extends Migration */ public function up() { - Schema::create('role_user', function (Blueprint $table) { - $table->increments('id'); + Schema::create('users_roles', function (Blueprint $table) { $table->integer('user_id')->unsigned()->index(); $table->integer('role_id')->unsigned()->index(); $table->timestamps(); @@ -22,7 +21,7 @@ public function up() $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); - $table->unique(['user_id', 'role_id']); + $table->primary(['user_id', 'role_id']); }); } diff --git a/src/HasRolesAndPermissions.php b/src/HasRolesAndPermissions.php index 3ee895b..3ac5557 100644 --- a/src/HasRolesAndPermissions.php +++ b/src/HasRolesAndPermissions.php @@ -14,16 +14,8 @@ trait HasRolesAndPermissions * @param Denismitr\Permissions\Models\Permission|string $permission * @return bool */ - public function hasPermissionTo($permission) + public function hasPermissionTo(string $permission) { - if (is_string($permission)) { - $permission = Permission::where('name', $name); - } - - if (! $permission) { - return false; - } - return $this->hasPermissionThroughRole($permission) || $this->hasPermission($permission); } @@ -34,20 +26,18 @@ public function hasPermissionTo($permission) */ public function givePermissionTo(...$permissions) { - $permissions = $this->getAllPermissions(array_flatten($permissions)); - - if ($permissions === null) { - return $this; - } - foreach ($permissions as $key => $permission) { if ($this->hasPermissionTo($permission)) { unset($permissions[$key]); + } else { + $permissions[$key] = Permission::fromName($permission); } } $this->permissions()->saveMany($permissions); + $this->load('permissions'); + return $this; } @@ -58,11 +48,12 @@ public function givePermissionTo(...$permissions) */ public function grantAllPermissions() { - $permission = new Permission; - $permission->name = 'all'; + $permission = Permission::fromName('all'); $this->permissions()->saveMany([$permission]); + $this->load('permissions'); + return $this; } @@ -119,32 +110,40 @@ public function hasRole(...$roles) return false; } - + /** + * Get roles of the user + * + * @return Illuminate\Database\Eloquent\Relations\BelongsToMany + */ public function roles() { - return $this->belongsToMany(Role::class); + return $this->belongsToMany(Role::class, 'users_roles'); } - + /** + * Get permissions for user + * + * @return Illuminate\Database\Eloquent\Relations\BelongsToMany + */ public function permissions() { return $this->belongsToMany(Permission::class, 'users_permissions'); } - protected function hasPermissionThroughRole(Permission $permission) + protected function hasPermissionThroughRole(string $permission) { - foreach ($permission->roles as $role) { - if ($this->roles->contains($role)) { - return true; - } - } + // foreach ($permission->roles as $role) { + // if ($this->roles->contains($role)) { + // return true; + // } + // } return false; } - protected function hasPermission(Permission $permission) + protected function hasPermission(string $permission) { - return (bool) $this->permissions->where('name', $permission->name)->count(); + return (bool) $this->permissions->where('name', $permission)->count(); } protected function getAllPermissions(array $permissions) diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 71fb22e..e77d7a9 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -12,4 +12,12 @@ public function roles() { return $this->belongsToMany(Role::class, 'roles_permissions'); } + + + public static function fromName($name) + { + return self::updateOrCreate([ + 'name' => $name + ]); + } }