2017 © Pedro Peláez
 

library slimauth

Slim Framework 3 Auth middleware

image

slimauth/slimauth

Slim Framework 3 Auth middleware

  • Thursday, March 10, 2016
  • by ushiboy
  • Repository
  • 1 Watchers
  • 0 Stars
  • 43 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

Auth Middleware for Slim

Build Status, (*1)

Unofficial auth middleware for Slim Framework., (*2)

apply authentication and authorization settings to each route., (*3)

Usage

Registration of middleware

<?php
session_start();

$app = new \Slim\App([
    'auth' => function($c) {
        return new SlimAuth\Auth(function($id) {
            return User::findOne($id);  // null => 403 response.
        });
    }
]);

Route settings

use secure method., (*4)

<?php
$auth = $app->getContainer()->get('auth');

$app->get('/private', function ($request, $response) {
    $response->getBody()->write('OK');
    return $response;
})->add($auth->secure());

Session authentication

use permit method., (*5)

<?php
$app->post('/login', function ($request, $response) {
    $parsedBody = $request->getParsedBody();
    $user = User::findBy($parsedBody['user_cd']);
    if ($user && $user->authenticate($parsedBody['password'])) {
        $this->get('auth')->permit($user->id);
    }
    return $response->withRedirect('/', 301);
});

Dispose session authentication

use clear method., (*6)

<?php
$app->get('/logout', function ($request, $response) {
    $this->get('auth')->clear();
    return $response->withRedirect('/', 301);
});

Advanced Usage

ACL authorization

use checkAcl option., (*7)

<?php
$app = new \Slim\App([
    'auth' => function($c) {
        return new SlimAuth\Auth(function($id) {
            return User::findOne($id);
        }, [
            'checkAcl' => function($currentUser, $acl) {
                return $currentUser->allowAccess($acl);
            }
        ]);
    }
]);

use secure method with acl list., (*8)

<?php
$auth = $app->getContainer()->get('auth');

$app->get('/admin', function ($request, $response) {
    $response->getBody()->write('OK');
    return $response;
})->add($auth->secure(['admin', 'superuser']));

Extra failure

use failure option., (*9)

<?php
$app = new \Slim\App([
    'auth' => function($c) {
        return new SlimAuth\Auth(function($id) {
            return User::findOne($id);
        }, [
            'failure' => function($request, $response) {
                return $response->withRedirect('/', 301);
            }
        ]);
    }
]);

Example

Start the sample with the following command., (*10)

$ php -S localhost:8080 -t example

The Versions

10/03 2016

dev-master

9999999-dev

Slim Framework 3 Auth middleware

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

10/03 2016

0.1.0

0.1.0.0

Slim Framework 3 Auth middleware

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires