Home > Backend Development > PHP Tutorial > Removing the Pain of User Authorization with Sentinel

Removing the Pain of User Authorization with Sentinel

William Shakespeare
Release: 2025-02-16 10:48:13
Original
615 people have browsed it

This article demonstrates building a simple demo application using Slim micro-framework and Cartalyst's Sentinel package for user authorization. Sentinel streamlines user management, including roles, permissions, and authentication, in multi-user applications.

Removing the Pain of User Authorization with Sentinel

Key Features & Benefits:

  • Simplified user authorization: Sentinel offers a user-friendly API for managing users, groups, and permissions.
  • Framework Integration: Works well with Slim and Laravel, providing tools for role creation, authentication, and permission control.
  • Easy Role Implementation: Creating and managing roles with detailed permission settings is straightforward.
  • Enhanced Security: Includes user activation and password reminder systems for improved security via email verification and password resets.
  • Practical Example: The tutorial provides a hands-on demonstration of Sentinel's capabilities in a sample application.

Setting up the Environment:

This tutorial utilizes Slim, Vagrant, and Composer. The complete demo code is available on Github. Begin by installing required packages:

composer require slim/slim:~2.0 twig/twig:~1.* cartalyst/sentinel:2.0.* illuminate/database illuminate/events symfony/http-foundation ircmaxell/password-compat
Copy after login

Database Setup:

For database interaction, create the necessary tables. Laravel users can use migrations:

php artisan vendor:publish --provider="Cartalyst\Sentinel\Laravel\SentinelServiceProvider"
php artisan migrate
Copy after login

Otherwise, manually execute vendor/cartalyst/sentinel/schema/mysql.sql after adding your database connection details at the top of the file.

Application Bootstrap (public/index.php):

<?php
require_once __DIR__.'/../vendor/autoload.php';

$app = new \Slim\Slim();
//register bindings

include_once __DIR__.'/../app/bootstrap/container.php';

include_once __DIR__.'/../app/routes.php';

$app->run();
Copy after login

Container Bindings (app/bootstrap/container.php):

Configure container bindings for Twig and Eloquent:

$app->container->twigLoader = new Twig_Loader_Filesystem(__DIR__.'/../views');
$app->container->twig = new Twig_Environment($app->container->twigLoader, array(
    'cache' => false,
));

$capsule = new \Illuminate\Database\Capsule\Manager();
$capsule->addConnection([
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'capsule',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
]);
$capsule->bootEloquent();

$app->container->sentinel = (new \Cartalyst\Sentinel\Native\Facades\Sentinel())->getSentinel();
Copy after login

Creating Roles:

Define roles and permissions (this code is temporary, run once to populate the database):

$app->container->sentinel->getRoleRepository()->createModel()->create(array(
    'name'          => 'Admin',
    'slug'          => 'admin',
    'permissions'   => array(
        'user.create' => true,
        'user.update' => true,
        'user.delete' => true
    ),
));

$app->container->sentinel->getRoleRepository()->createModel()->create(array(
    'name'          => 'User',
    'slug'          => 'user',
    'permissions'   => array(
        'user.update' => true
    ),
));
Copy after login

The remainder of the article details creating signup and login pages, handling user activation, implementing permission checks (using hasAccess()), and logging out users. The code examples cover routing, user creation, role assignment, activation email sending, and permission-based access control. The article concludes with a FAQ section addressing common Sentinel usage questions.

The above is the detailed content of Removing the Pain of User Authorization with Sentinel. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template