1. Go to this page and download the library: Download marcelbonnet/slim-auth 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/ */
marcelbonnet / slim-auth example snippets
class User {
protected $username;
protected $passwordHash;
/**
@OneToMany(targetEntity="Role", ...)
*/
protected $roles;
}
class Role {
/**
The role name
@var string
*/
protected $role;
/**
@ManyToOne(targetEntity="User")
*/
protected $user;
}
use marcelbonnet\Slim\Auth\ServiceProvider\SlimAuthProvider;
use Zend\Authentication\Storage\Session as SessionStorage;
use marcelbonnet\Slim\Auth\Middleware\Authorization;
use marcelbonnet\Slim\Auth\Handlers\RedirectHandler;
use marcelbonnet\Slim\Auth\Adapter\LdapRdbmsAdapter;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Slim\Http\Request as SlimHttpRequest;
use \Slim\Http\Response as SlimHttpResponse;
*******************************
*/
$acl = new Acl();
//ACLed Slim Route
$container['router'] = new \marcelbonnet\Slim\Auth\Route\AuthorizableRouter(null, $acl);
$container['acl'] = $acl;
$adapterOptions = [];
//if you want auth to be valid if some column exists with an expected value:
// $adapterOptions = [
// 'checkUserIsActivated' => 'my_column_in_user_table',
// 'userIsActivatedFlag' => true
// ];
$adapter = new marcelbonnet\Slim\Auth\Adapter\LdapRdbmsAdapter(
'/some/file.conf', //LDAP config or NULL if not using LDAP
$myEntityManager, //an Doctrine's Entity Manager instance
"\Your\Project\Dao\Role", //Role class
"role", //Role's class role attribute
"user", //Role's class user attribute (the @ManyToOne attrib)
"\Your\Project\Dao\User", //User class
"username", //User name attribute
"passwordHash", //password (as a hash) attribute
marcelbonnet\Slim\Auth\Adapter\LdapRdbmsAdapter::AUTHENTICATE_RDBMS, //auth method: LdapRdbmsAdapter::AUTHENTICATE_RDBMS | LdapRdbmsAdapter::AUTHENTICATE_LDAP
10, //a hash factor
PASSWORD_DEFAULT, //hash algorithm
$adapterOptions //if needed
);
$container["authAdapter"] = $adapter;
$slimAuthProvider = new SlimAuthProvider();
$slimAuthProvider->register($container);
$app->add(new Authorization( $container["auth"], $acl, new RedirectHandler("auth/notAuthenticated", "auth/notAuthorized") ));
# checks:
#$username=(is_array(@$c["auth"]->getStorage()->read()))? @$c["auth"]->getStorage()->read()["username"] : @$c["auth"]->getStorage()->read();
#$userRoles=(is_array(@$c["auth"]->getStorage()->read()))? @$c["auth"]->getStorage()->read()["role"] : array();
/**
Example Routes: you must set allowed Roles (as one string or as an array or string roles) for each route.
*/
$app->get('/', 'My\Controller:home' )->setName("home")->allow(Acl::MEMBER);
$app->get('/home', function (SlimHttpRequest $request, SlimHttpResponse $response, $args) use($container) {
$container->get('router')->getNamedRoute('home')->run($request, $response);
})->allow(Acl::MEMBER);
$app->get('/hello[/{name}]', 'My\Controller:sayHello')->setName('hello')->allow([Acl::GUEST, Acl::MEMBER]);
$app->get('/protected', 'My\Controller:callProtectedResource')->setName('protected')->allow(Acl::ADMIN);
$app->run();
class Acl extends SlimAuthAcl
{
const GUEST = "guest";
const ADMIN = "admin";
const MEMBER = "member";
public function __construct()
{
// APPLICATION ROLES
$this->addRole(self::GUEST);
$this->addRole(self::MEMBER, self::GUEST);
/* **************************************
* WARNING: ALLOW ALL:
* **************************************
*/
$this->addRole(self::ADMIN);
$this->allow(self::ADMIN);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.