Skip to content

Commit

Permalink
Add level blade extension
Browse files Browse the repository at this point in the history
  • Loading branch information
romanbican committed Jun 25, 2015
1 parent f8fe6b7 commit 6f740ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ if ($user->allowed('edit.articles', $article, false)) { // now owner check is di

### Blade Extensions

There are three Blade extensions. Basically, it is replacement for classic if statements.
There are four Blade extensions. Basically, it is replacement for classic if statements.

```php
@role('admin') // @if(Auth::check() && Auth::user()->is('admin'))
Expand All @@ -290,6 +290,10 @@ There are three Blade extensions. Basically, it is replacement for classic if st
// user can edit articles
@endpermission

@level(2) // @if(Auth::check() && Auth::user()->level() >= 2)
// user has level 2 or higher
@endlevel

@allowed('edit', $article) // @if(Auth::check() && Auth::user()->allowed('edit', $article))
// show edit button
@endallowed
Expand Down
10 changes: 10 additions & 0 deletions src/Bican/Roles/RolesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ protected function registerBladeExtensions()
return "<?php endif; ?>";
});

$blade->directive('level', function ($expression) {
$level = trim($expression, '()');

return "<?php if (Auth::check() && Auth::user()->level() >= {$level}): ?>";
});

$blade->directive('endlevel', function () {
return "<?php endif; ?>";
});

$blade->directive('allowed', function ($expression) {
return "<?php if (Auth::check() && Auth::user()->allowed{$expression}): ?>";
});
Expand Down

0 comments on commit 6f740ed

Please sign in to comment.