PHP code example of germania-kg / authorization

1. Go to this page and download the library: Download germania-kg/authorization 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/ */

    

germania-kg / authorization example snippets



use Germania\Authorization\Authorization;

// Define tasks and allowed roles
$acl = array(
	'/foo' => [ "coworkers", "superuser"],
	'/bar' => [ "superuser", "registered"]
);

// Wether to permit undefined tasks
$default_permission = true;

// Create instance, optional with PSR-3 Logger
$authorization = new Authorization( $acl, $default_permission );
$authorization = new Authorization( $acl, $default_permission, $logger );


$user_roles = [ "coworkers", "somegroup" ];

// Result is TRUE
$allowed = $authorization->authorize("/foo", $user_roles);
$allowed = $authorization("/foo", $user_roles);

// Result is FALSE
$allowed = $authorization->authorize("/bar", $user_roles);
$allowed = $authorization("/bar", $user_roles);

// Should be TRUE due to default permission above
$allowed = $authorization->authorize("/somethingelse", $user_roles);
$allowed = $authorization("/somethingelse", $user_roles);


$silent_log = new Psr\Log\NullLogger;

$authorization->authorize("/foo", $user_roles, $silent_log);
$authorization("/foo", $user_roles, $silent_log);


use Germania\Authorization\TaskNotFoundException;
use Psr\Container\NotFoundExceptionInterface;

// Assuming example from above:
// TRUE
$has = $authorization->has( "/foo" );

// array( "coworkers", "superuser"] )
try {
	$roles = $authorization->get( "/foo" );
	
	// will throw TaskNotFoundException
	$roles = $authorization->get( "/something-else" );
}
catch (NotFoundExceptionInterface $e) {
	if ($e instanceOf NotFoundException) {
		echo "Interop Container: NotFoundException";
	}
}

// Your Callable passed into constructor
$authorize = $this->authorizer;

if (!$authorize( $url )):
	$response = $response->withStatus( 401 );
endif;

$response = $next($request, $response);
return $response;


use Germania\Authorization\RequestUriAuthorizationMiddleware;

// Have your Authorization callable at hand
$auth = new Authorization( ... );

// Optionally with PSR-3 Logger
$middleware = new RequestUriAuthorizationMiddleware( $auth )
$middleware = new RequestUriAuthorizationMiddleware( $auth, $logger )


use Germania\Authorization\RouteNameAuthorizationMiddleware;

// Have your Authorization callable at hand
$auth = new Authorization( ... );

// Optionally with PSR-3 Logger
$middleware = new RouteNameAuthorizationMiddleware( $auth );
$middleware = new RouteNameAuthorizationMiddleware( $auth, $logger );

// Setup Slim App:
$app = new \Slim\App( [
	'settings' => [
		// Set this to true to get access to route within middleware
		'determineRouteBeforeAppMiddleware' => true
	]
]);

// Add Middleware
$app->add( $middleware );


use Germania\Authorization\AuthorizationMiddleware;

// Have your Authorization callable at hand
$auth = new Authorization( ... );

// Setup Callable for URLs (or, permissions, you name it)
$url_getter = function( $request ) {
	return (string) $request->getUri();
};

// Optionally with PSR-3 Logger
$middleware = new AuthorizationMiddleware( $auth, $url_getter );
$middleware = new AuthorizationMiddleware( $auth, $url_getter, $logger );