Browse Source

Refactor user role relation

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
93e3e47c88
  1. 15
      app/Entities/Users/User.php
  2. 4
      tests/Traits/DatabaseMigrateSeeds.php

15
app/Entities/Users/User.php

@ -43,9 +43,8 @@ class User extends Authenticatable
*/
public function assignRole($role)
{
return $this->roles()->save(
Role::whereName($role)->firstOrFail()
);
$roleId = Role::whereName($role)->firstOrFail()->id;
return $this->roles()->attach($roleId);
}
/**
@ -56,9 +55,8 @@ class User extends Authenticatable
*/
public function removeRole($role)
{
return $this->roles()->detach(
Role::whereName($role)->firstOrFail()
);
$roleId = Role::whereName($role)->firstOrFail()->id;
return $this->roles()->detach($roleId);
}
/**
@ -78,8 +76,9 @@ class User extends Authenticatable
public function hasRoles(array $roleNameArray)
{
return $this->roles->contains(function($role, $key) use ($roleNameArray) {
return in_array($role->name, $roleNameArray);
return $this->roles->pluck('name')
->contains(function($role, $key) use ($roleNameArray) {
return in_array($role, $roleNameArray);
});
}

4
tests/Traits/DatabaseMigrateSeeds.php

@ -17,9 +17,5 @@ trait DatabaseMigrateSeeds
$this->artisan('db:seed');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
}
Loading…
Cancel
Save