/** * Prepares Session object for di container. * * @return Session */ private function configureSession() { $session = new Session(); if (isset($_SESSION['userId'])) { $session->setUserId($_SESSION['userId']); } if (isset($_SESSION['userLogin'])) { $session->setUserLogin($_SESSION['userLogin']); } return $session; }
/** Сохранение токена в БД */ public function createToken($expire = null) { $u = $this->getUser(); $t = sha1(uniqid()); $s = new \Session($this->getManager()); $s->setToken($t); $s->setUserId($u->getId()); $s->setIp($this->getRequest()->getClientIp()); $s->setExpiresAt(is_numeric($expire) ? date('d.m.Y', $expire) : $expire); $s->save(); return $t; }
/** * Establece las variables de sesión del usuario. * * @param User $User * @throws SPException */ public static function loadUserSession(User $User) { Session::setUserId($User->getUserId()); Session::setUserName($User->getUserName()); Session::setUserLogin($User->getUserLogin()); Session::setUserProfileId($User->getUserProfileId()); Session::setUserGroupId($User->getUserGroupId()); Session::setUserGroupName($User->getUserGroupName()); Session::setUserEMail($User->getUserEmail()); Session::setUserIsAdminApp($User->isUserIsAdminApp()); Session::setUserIsAdminAcc($User->isUserIsAdminAcc()); Session::setUserIsLdap($User->isUserIsLdap()); Session::setUserProfile(Profile::getProfile($User->getUserProfileId())); }
/** * Retrieve a session by ID. * @param $sessionId string * @return Session */ function &getSession($sessionId) { $result =& $this->retrieve('SELECT * FROM sessions WHERE session_id = ?', array($sessionId)); $session = null; if ($result->RecordCount() != 0) { $row =& $result->GetRowAssoc(false); $session = new Session(); $session->setId($row['session_id']); $session->setUserId($row['user_id']); $session->setIpAddress($row['ip_address']); $session->setUserAgent($row['user_agent']); $session->setSecondsCreated($row['created']); $session->setSecondsLastUsed($row['last_used']); $session->setRemember($row['remember']); $session->setSessionData($row['data']); } $result->Close(); unset($result); return $session; }
<?php ini_set('display_errors', 1); require_once 'config.php'; require_once 'Component/Session.php'; require_once 'Component/DatabaseConnection.php'; require_once '../src/Medical/OAuth/Client.php'; $session = new Session(); $myStorage = new DatabaseConnection(DB_HOST, DB_USER, DB_PASS, DB_NAME); $username = $_POST['username']; $password = $_POST['password']; $user = $myStorage->authenticateUser($username, $password); if ($user) { $session->setUserId($user->id); header('Location: index.php'); } else { header('Location: index.php?error=1'); }
$session = new Session(); $myStorage = new DatabaseConnection(DB_HOST, DB_USER, DB_PASS, DB_NAME); // sent by OAuth, you should know that $state = $_GET['state']; // personal state $code = $_GET['code']; // follow the OAuth protocol and get an access token and a refresh token if ($client->authorizationCodeCaught()) { // make the next OAuth call to get those tokens (crendentials) $response = $client->getCredentials(); if ($response->getCode() !== 200) { throw new \Exception(sprintf("An OAuth error occured: %s", $response->getErrorMessage())); } // now merge or create our user if it does not exist ! // $appUser = $myStorage->getUser($session->getUserId()); $mediUser = $client->getUser(); $appUser = $myStorage->getUserByUsername($mediUser->email); // i chose to merge by email/username if ($appUser) { // my user alreay exists ! // I just have to connect him to my app and update his tokens if they are expired $myStorage->updateUserCredentials($appUser->id, array('access_token' => $client->getAccessToken(), 'refresh_token' => $client->getRefreshToken(), 'expires_in' => $client->getTokenLifeDuration())); $session->setUserId($appUser->id); header('Location: index.php'); } else { // user dont exist on my app, lets create it $userId = $myStorage->createUser(array('username' => $mediUser->email, 'password' => 'test', 'access_token' => $client->getAccessToken(), 'refresh_token' => $client->getRefreshToken(), 'expires_in' => $client->getTokenLifeDuration())); $session->setUserId($userId); header('Location: index.php'); } }