Laravel 4 permissions
so this is basically the sql model I got so far:
This is how it would look in my user controller (stackoverflow formatting):
if (Auth::user()->is('admin'))
{
if (Auth::user()->can('delete'))
{
echo 'hurra!';
}
}
The permission keys will probably look like this:
admin.delete maybe something like user.can.buy
And now I want to know if this is good as I do it in methods:
public function is($roleName)
{
$role = $this->roles;
if ($role->name == $roleName)
{
return true;
}
return false;
}
I think this one is good
BUT
public function can($permissionKey)
{
$permissions = $this->roles->permissions()
->where('key', $permissionKey)
->count();
if ($permissions > 0)
{
return true;
}
return false;
}
looks awkward to me. Am I choosing the right data with eloquent orm?
Thanks in advance!
source to share
Well, I installed the profiler and checked all the SQL queries that were called:
select * from users
where id
= '1' limit 1
select * from roles
where id
= '1' limit 1
select count (*) as a collection from the permissions
inner join permission_role
on permissions
. id
= permission_role
. permission_id
where permission_role
. role_id
= '1' and key
= 'view.admin'
it looks good to me, so I think it was, okay.
source to share