示例#1
0
/**
 * Altenticação do usuário.
 * 
 * @param \Slim\Route $route
 */
function authenticate(\Slim\Route $route)
{
    // Getting request headers
    $headers = apache_request_headers();
    // Verifying Authorization Header
    if (isset($headers['Authorization'])) {
        $db = new DbHandler();
        // get the api key
        $apiKey = $headers['Authorization'];
        // validating api key
        if (!$db->isValidApiKey($apiKey)) {
            // api key is not present in users table
            $erro = MapaErro::singleton()->getErro(APIKEY_INVALIDA);
            echoRespnse(HTTP_CONFLITO, $erro);
        } else {
            global $usuarioAutenticado;
            // Recuperar usuário pelo ApiKey.
            $usuario = $db->getUserByApiKey($apiKey);
            if (!empty($usuario)) {
                $usuarioAutenticado = $usuario;
            }
        }
    } else {
        // api key is missing in header
        $erro = MapaErro::singleton()->getErro(NECESSARIO_LOGIN);
        echoRespnse(HTTP_CONFLITO, $erro);
    }
}