Skip to content

Instantly share code, notes, and snippets.

@infacq
Last active January 3, 2016 20:19
Show Gist options
  • Select an option

  • Save infacq/8514213 to your computer and use it in GitHub Desktop.

Select an option

Save infacq/8514213 to your computer and use it in GitHub Desktop.
/**
* Authorise function, used as Slim Route Middlewear (http://www.slimframework.com/documentation/stable#routing-middleware)
*/
function authorize() {
return function () use ( $role ) {
// Get the Slim framework object
$app = Slim::getInstance();
// First, check to see if the user is logged in at all
if(!empty($_SESSION['user'])) {
if($_SESSION['user']['token'] == $_SERVER['HTTP_X_CSRF_TOKEN']) {
//User is logged in and has the correct permissions... Nice!
return true;
} else {
// If a user is logged in, but doesn't have permissions, return 403
$app->halt(403, 'ACCESS DENIED');
}
} else {
// If a user is not logged in at all, return a 401
$app->halt(401, 'PLEASE LOGIN FIRST');
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment