PHP code example of kojit2009 / laravel-roles-permissions
1. Go to this page and download the library: Download kojit2009/laravel-roles-permissions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kojit2009 / laravel-roles-permissions example snippets
use Illuminate\Foundation\Auth\User as Authenticatable;
use Centeron\Permissions\Traits\HasAuthItems;
class User extends Authenticatable
{
use HasAuthItems;
// ...
}
use Centeron\Permissions\Models\AuthItem;
$role = AuthItem::createRole(['name' => 'admin']);
$role = AuthItem::create(['name' => 'admin', 'type' => AuthItem::TYPE_ROLE]); // alternative to the previous code
$permission = AuthItem::createPermission(['name' => 'admin']);
$permission = AuthItem::create(['name' => 'admin', 'type' => AuthItem::TYPE_PERMISSION]); // alternative to the previous code
use Centeron\Permissions\Models\AuthItem;
AuthItem::where('name', 'admin')->delete();
$user->hasAnyAuthItems('View post', 'Edit post'); // true, if it has any of a given list
$user->hasAllAuthItems('View post', 'Edit post'); // true, if it has all of a given list
$user->canAnyAuthItems(['View post', 'Edit post'], [1]); // true, possible edit or view a post with ID = 1
$user->hasAllAuthItems(['View post', 'Edit post'], [1]); // true, possible edit and view post with ID = 1
if (Gate::denies('Edit post', $post)) {}
if (Gate::allows('Delete comment', [$post, $comment])) {}
if (Gate::check('admin')) {}
@authHasAny('Edit post', 'View post')
// Showing info if a user can edit or view post
@authEnd
@authHasAll('Edit post', 'View post')
// Showing info if a user can edit and view post
@authElse
// Otherwise
@authEnd
@authCanAny(['Edit post', 'View post'], [1])
// Showing info if a user can edit or view post with ID=1
@authEnd
@authCanAll(['Edit post', 'View post'], [1])
// Showing info if a user can edit and view post with ID=1
@authElse
// Otherwise
@authEnd
php artisan centeron:auth-item-create // Create a new auth item (role or description)
php artisan centeron:auth-items-remove // Remove auth items (roles and permissions)
php artisan centeron:auth-item-inherit // Add child items to chosen auth item
php artisan centeron:auth-item-disinherit // Remove childs from an auth item
php artisan centeron:auth-items-attach // Attach auth items to the model
php artisan centeron:auth-items-detach // Deattach auth items from the model
namespace Centeron\Permissions\Rules;
use Centeron\Permissions\Contracts\Rule;
class OnWeekdDays implements Rule
{
public function handle($authItem, $model, $params = []): bool
{
return date('N') <= 5;
}
}
namespace Centeron\Permissions\Rules;
use Centeron\Permissions\Contracts\Rule;
class OnWeekdDays implements Rule
{
public function handle($authItem, $model, $params = []): bool
{
$entityId = $params[0] ?? null;
return $model->id === $entityId;
}
}
namespace Centeron\Permissions\Rules;
use Centeron\Permissions\Contracts\Rule;
class OnWeekdDays implements Rule
{
public function handle($authItem, $model, $params = []): bool
{
$myCategories = unserialize($authItem['data']);
return array_intersect($params, $myCategories) ? true : false;
}
}
$user->canAnyAuthItems(['View post', 'Edit post'], [1]); // true, possible edit or view a post with ID = 1
$user->hasAllAuthItems(['View post', 'Edit post'], [1]); // true, possible edit and view a post with ID = 1