Skip to content

Commit

Permalink
Edits migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
denismitr committed Apr 15, 2017
1 parent 92d7680 commit 066ab1e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion migrations/2017_02_05_092446_create_permissions_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -13,16 +13,15 @@ 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();

$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']);
});
}

Expand Down
55 changes: 27 additions & 28 deletions src/HasRolesAndPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ public function roles()
{
return $this->belongsToMany(Role::class, 'roles_permissions');
}


public static function fromName($name)
{
return self::updateOrCreate([
'name' => $name
]);
}
}

0 comments on commit 066ab1e

Please sign in to comment.