function validatePrivileges($privileges = array(), $no_error = false) { return function () use($privileges, $no_error) { try { // Inicjalizacja modelu użytkownika $user = new \Model\User(); // Pobranie instancji szkieletu Slim Framework $app = \Slim\Slim::getInstance(); // Pobranie tokenu z pola Authorization nagłówka HTTP i jego walidacja $token = validateToken($app->request->headers->get('Authorization')); try { // Pobranie danych uzytkownika o zadanym tokenie $u = $user->getByToken($token, 'administrator'); } catch (Exception $e) { throw new Exception('Użytkownik nie jest zalogowany.', 401); } // Sprawdzenie wymaganych uprawnień if (!in_array($u['privileges'], $privileges)) { throw new Exception('Brak uprawnień.', 401); } // Zapamiętanie danych potrzebnych do logowania jako zmienne globalne $GLOBALS['user_id'] = $u['user_id']; $GLOBALS['token'] = $token; $GLOBALS['privileges'] = $u['privileges']; try { // Jeśli różnica pomiędzy ostatnią aktywnością, a bierzącym czasem // wynosi 60 sekund, to odświeżamy autoryzację $diff = abs(strtotime(date('Y-m-d H:i:s')) - strtotime($u['last_auth'])); if ($diff > 60) { $res = $user->refreshAuth($token); } } catch (Exception $e) { } } catch (Exception $e) { $GLOBALS['user_id'] = -1; $GLOBALS['token'] = ''; $GLOBALS['privileges'] = 'guest'; if (!$no_error) { jsonError($app, $e, true); } } }; }