if (is_object($input)) { $input = get_object_vars($input); } // Verify if an user with that name and password exists. $users = User::all(array('conditions' => array('username = ? AND password = ?', $input['username'], sha1($input['password'])))); if (count($users) > 0) { // The user exists, verify if is enabled. $user = $users[0]; if ($user->disabled != 1) { // The user is enabled, save his data in the session and return a success message. $_SESSION["user"] = $users[0]->attributes(); echo json_encode(array("error" => null, "user" => $users[0]->attributes())); } else { // The user is disabled, return error message. ApiUtils::returnError($app, 'UserDisabled'); } } else { // The user do not exists, return error message. ApiUtils::returnError($app, 'InvalidLogin'); } } catch (Exception $e) { // An exception ocurred. Return an error message. ApiUtils::handleException($app, $e); } }); // Logout service. $app->get('/logout', function () { // Clear session and return success message. $_SESSION["user"] = null; echo json_encode(array("error" => null)); });
}); // Service for update the values of an assistance list. $app->put('/assistanceList', function () use($app) { try { // Verify that the user is logged. if (ApiUtils::isLogged()) { // Get input. $request = $app->request(); $input = json_decode($request->getBody()); $rows = is_object($input) ? get_object_vars($input) : $input; // Update all rows. for ($i = 0; $i < count($rows); $i++) { $entry = Assistance::find($rows[$i]->id); $attributes = is_object($rows[$i]) ? get_object_vars($rows[$i]) : $rows[$i]; $creation = is_object($rows[$i]->creation) ? get_object_vars($rows[$i]->creation) : $rows[$i]->creation; $attributes['creation'] = $creation['date']; if ($entry != null) { $entry->update_attributes($attributes); } } // Return result. echo json_encode(array("error" => null)); } else { // Return error message. ApiUtils::returnError($app, 'UserNotAdmin'); } } catch (Exception $e) { // An exception ocurred. Return an error message. ApiUtils::handleException($app, $e); } });
} else { // Return error message. ApiUtils::returnError($app, 'UserNotLogged'); } } catch (Exception $e) { // An exception ocurred. Return an error message. ApiUtils::handleException($app, $e); } }); // Service for get all persons from a group. $app->get('/assistanceList/:groupId/:eventId/:date', function ($groupId, $eventId, $date) use($app) { try { // Verify that the user is logged. if (isset($_SESSION['user'])) { // Get list of persons in the group. $now = new DateTime(); $rows = ApiUtils::rowsToMaps(Assistance::find_by_sql("select * from get_assistance(" . $_SESSION['user']['id'] . "," . $groupId . "," . $eventId . "," . "'" . ($date !== 'today' ? $date : $now->format('Y-m-d')) . "')")); // Get name of event and group. $event = ApiUtils::rowToMap(Event::find($eventId)); $group = ApiUtils::rowToMap(Group::find($groupId)); // Return result. echo json_encode(array("rows" => $rows, "date" => $date !== 'today' ? $date : $now->format('Y-m-d'), "event" => $event, "group" => $group, "error" => null)); } else { // Return error message. ApiUtils::returnError($app, 'UserNotLogged'); } } catch (Exception $e) { // An exception ocurred. Return an error message. ApiUtils::handleException($app, $e); } });