Example #1
0
// Setup the LDAP connection
if (!$ldap->connect()) {
    $error->send(502, 'ldap_unavailable', 'LDAP server not responding', 'The API cannot connect to the LDAP server');
}
if (!$ldap->login()) {
    $error->send(500, 'ldap_login_failure', 'Cannot login to LDAP server', 'The API cannot login to the LDAP server');
}
/*
 * API endpoint definition
 */
$app = new \Slim\Slim();
// JSON-encoded data of all current members with passes
$app->get('/users', function () use($ldap, $database) {
    // Construct required data
    $users = $ldap->getAllUsers();
    $timestamps = $database->getLastEntries();
    $data = array_map(function ($user) use($timestamps) {
        $user['last_entry'] = isset($timestamps[$user['uid']]) ? $timestamps[$user['uid']] : 'Voor 1 september 2015 (of nooit)';
        return $user;
    }, $users);
    echo json_encode($data);
});
// Grant a user access to the door
$app->post('/users/:uid', function ($uid) use($app, $ldap, $error) {
    if ($ldap->grantAccess($uid)) {
        $app->response->setStatus(204);
        // HTTP 204 No Content
    } else {
        $error->send(500, 'internal_error', 'Access grant failed', 'The API cannot grant access to this user. The exact error is unknown.');
    }
});