public function setCookie($expiry = CookieAuthentication::DEF_EXPIRY) { // Set the cookie for autoauth for current user $ca = Cookies::getInstance(Cookies); if (User::isAuthenticated()) { $u = User::getActiveUser(); $name = config::get(CookieAuthentication::KEY_COOKIE_NAME, 'leptonauth'); $c = $this->encryptCookie(array('uid' => $u->userid, 'uname' => $u->username)); if ($expiry == -1) { $expiry = config::get(CookieAuthentication::KEY_COOKIE_EXPIRY, '7d'); } $ca->set($name, $c, array('expires' => $expiry, 'domain' => config::get(CookieAuthentication::KEY_COOKIE_DOMAIN, null), 'path' => '/')); } }
/** * Load the user object into the user property * * @return User */ public static function loadUser($user = null) { try { $classname = self::$_configuration['core']['user_classname']; $request = self::getRequest(); self::$_user = $user === null ? $classname::loginCheck($request->getParameter('csp_username', $request->getCookie('csp_username')), $request->getParameter('csp_password', $request->getCookie('csp_password')), !$request->hasCookie('csp_password')) : $user; if (self::$_user->isAuthenticated()) { self::getResponse()->setCookie('csp_username', self::$_user->getUsername()); self::getResponse()->setCookie('csp_password', self::$_user->getPassword()); Event::createNew('core', 'post_loaduser', self::$_user)->trigger(); } } catch (Exception $e) { throw $e; } return self::$_user; }
public function signUp(Request $request, Response $response, $args) { $this->logger->info("SignUp page action dispatched"); $uri = $request->getUri(); if ($request->getMethod() == 'POST') { $data = $request->getParsedBody(); $v = new $this->validator($data); $v->lang('es'); $v->rule('required', array('email', 'username', 'password', 'company')); if ($v->validate()) { try { $this->logger->info("Signup with parameters: " . $data['email'] . " - " . $data['username'] . " - " . $data['password'] . " - " . $data['company']); $newUser = new User(); $newUser->setUsername($data['username']); $newUser->setEmail($data['email']); $newUser->setPassword($data['password']); $newUser->signUp(); if ($newUser != null && $newUser->isAuthenticated()) { $result = ParseCloud::run('addUserToRole', ['roleName' => 'Manager'], false); $this->logger->info("User added to Manager Role? " . $result); $company = new Company(); $company->setName($data['company']); $company->save(); $this->flash->addMessage('info', 'Sample flash message'); return $response->withStatus(302)->withHeader('Location', $uri->withPath('')); } } catch (ParseException $e) { ParseErrorHandler::handleParseError($e); $this->flash->addMessage('error', $e->getMessage()); return $response->withStatus(302)->withHeader('Location', $uri->withPath('signup')); } } foreach ($v->errors() as $field => $errors) { foreach ($errors as $error) { $this->flash->addMessage('error', $error); } } return $response->withStatus(302)->withHeader('Location', $uri->withPath('signup')); } // Get Messages $messages = $this->flash->getMessages(); // Fetch Template $body = $this->view->fetch('login/signup.twig', ['flash' => $messages]); // Write Response return $response->write($body); }
static function currentUser() { $cookie = new CookieSigner(Config::app()['BASE_KEY']); if (isset($_SESSION['userId']) && ($userId = $_SESSION['userId'])) { $user = new User(); return $user->findOne($userId); } else { if ($userId = $cookie->get('userId')) { $user = new User(); $user->findOne($userId); if ($user && $user->isAuthenticated('remember', $cookie->get('rememberToken'))) { self::logIn($user); return $user; } } } return null; }
* @param string role * @param string resource * @param string privilege * @return bool */ function isAllowed($role = self::ALL, $resource = self::ALL, $privilege = self::ALL) { return $role === 'admin' && strpos($resource, 'jany') === FALSE; } } // delete cookies $_COOKIE = array(); $user = new User(); // guest echo "isAuthenticated?\n"; Debug::dump($user->isAuthenticated()); echo "getRoles()\n"; Debug::dump($user->getRoles()); echo "is admin?\n"; Debug::dump($user->isInRole('admin')); echo "is guest?\n"; Debug::dump($user->isInRole('guest')); // authenticated $handler = new AuthenticationHandler(); $user->setAuthenticationHandler($handler); echo "authenticate as john\n"; $user->authenticate('john', 'xxx'); echo "isAuthenticated?\n"; Debug::dump($user->isAuthenticated()); echo "getRoles()\n"; Debug::dump($user->getRoles());
public function login($request) { User::login($this->userRepository, $request["email"], $request["password"]); setJsonResponse(User::isAuthenticated()); }
throw new RuntimeException('Clone is not allowed against ' . get_class($this)); } } /** * Client */ session_start(); $context = isset($_SESSION['context']) ? $_SESSION['context'] : null; if (is_null($context)) { $context = new User('ほげ'); } $mode = isset($_GET['mode']) ? $_GET['mode'] : ''; switch ($mode) { case 'state': echo '<p style="color: #aa0000">状態を遷移します</p>'; $context->switchState(); break; case 'inc': echo '<p style="color: #008800">カウントアップします</p>'; $context->incrementCount(); break; case 'reset': echo '<p style="color: #008800">カウントをリセットします</p>'; $context->resetCount(); break; } $_SESSION['context'] = $context; echo 'ようこそ、' . $context->getUserName() . 'さん<br>'; echo '現在、ログインして' . ($context->isAuthenticated() ? 'います' : 'いません') . '<br>'; echo '現在のカウント:' . $context->getCount() . '<br>'; echo $context->getMenu() . '<br>';
*/ require_once "init.php"; $email = $headersHandler->getHeader('email'); $password = $headersHandler->getHeader('password'); $outputHandler = new OutputHandler("output-login2.txt"); $outputHandler->write($headersHandler->getHeaders(true)); if ($headersHandler->isAuthenticated()) { // if the user is already authenticated - do not login $headersHandler->sendHeaderCode(400); $headersHandler->sendJSONData(['error' => "User already authenticated"]); $outputHandler->write("user already authenticated"); die; } if ($email && $password) { $user = new User($database, $email, $password); if ($user->isAuthenticated()) { $jwt = $user->getJWT(); $preparedData = ['token' => $jwt]; $headersHandler->sendJSONData($preparedData); $outputHandler->write("successful login"); $outputHandler->write($preparedData); $outputHandler->write($jwt); } else { $outputHandler->write("email and password do not match"); $headersHandler->sendHeaderCode(401); $headersHandler->sendJSONData(['error' => "Email and password do not match"]); } } else { $outputHandler->write("email and password not set"); $headersHandler->sendHeaderCode(401); $headersHandle->sendJSONData(['error' => "Email or password was not sent correctly"]);