check() public static method

Checks if a session key is set in any adapter, or if a particular adapter configuration is specified (via 'name' in $options), only that configuration is checked.
public static check ( string $key, array $options = [] ) : boolean
$key string The session key to check.
$options array Optional parameters that this method accepts.
return boolean
Ejemplo n.º 1
0
 public function trychar($char)
 {
     // do we have an active game session?
     if (!Session::check("game_id", array('name' => 'default'))) {
         return $this->redirect('games::index');
     }
     // state: 0 = in-game, 1 = won, 2 = lost, 3 = already won, 4 = already lost
     $game = Games::find('first', array('conditions' => array('id' => Session::read('game_id'))));
     $word = Words::find('first', array('conditions' => array('id' => $game->word_id)));
     $resp = array("success" => 0, "wrong_tries_left" => $game->max_wrong_tries - $game->wrong_tries, "placeholders" => Games::getPlaceholders(), "image" => Games::getImage(), "message" => "", "state" => 0, "word" => "");
     // game already won
     if ($game->state == 1) {
         $resp['message'] = "You already won!";
         $resp['state'] = 3;
         return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
         // game already lost
     } elseif ($game->state == 2) {
         $resp['message'] = "You already lost!";
         $resp['state'] = 4;
         return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
     }
     // ist der gesendete buchstabe erlaubt?
     if (!Games::isValidChar($char)) {
         $resp['success'] = 0;
         $resp['message'] = "Non-valid char used.";
         return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
         // falls der gesendete buchstabe schonmal gesendet wurde
     } elseif (stripos($game->input_text, $char) !== false) {
         $resp['success'] = 0;
         $resp['message'] = "Char was already used.";
         return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
         // buchstabe ist valid und wurde nicht schon gesendet
     } else {
         $game->input_text .= $char;
         $game->save();
         $resp['success'] = 1;
         $resp['placeholders'] = Games::getPlaceholders();
         // buchstabe kommt in gesuchtem wort vor
         if (!(stripos($word->value, $char) === false) && stripos($word->value, $char) >= 0) {
             $resp['image'] = Games::getImage();
             // alle zeichen erraten, gewonnen
             // WIN!
             if (stripos(Games::getPlaceholders(), self::PLACEHOLDER) === false) {
                 $game->state = 1;
                 // won
                 $game->save();
                 $resp['state'] = 1;
                 // won
                 $resp['message'] = "You won! Play a new game?";
                 $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries;
                 return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
                 // buchstabe stimmt, aber noch nicht alles erraten
             } else {
                 $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries;
                 $resp['message'] = "Nice! Guess the next char!";
                 return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
             }
             // buchstabe kommt NICHT vor
         } else {
             $game->wrong_tries++;
             $game->save();
             $resp['image'] = Games::getImage();
             // hat der benutzer die maximale anzahl versuche überschritten?
             // LOOSE!
             if ($game->wrong_tries >= $game->max_wrong_tries) {
                 $resp['wrong_tries_left'] = 0;
                 $resp['message'] = "Out of tries. New Game?";
                 $resp['state'] = 2;
                 // lost
                 $game->state = 2;
                 // lost
                 $resp['word'] = $word->value;
                 $game->save();
                 return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
                 // falscher buchstaben eingegeben, benutzer hat weiteren versuch
             } else {
                 $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries;
                 $resp['message'] = "Nope. Try again!";
                 return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
             }
         }
     }
     return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
 }
Ejemplo n.º 2
0
 public function testEncryptedStrategy()
 {
     $this->skipIf(!MockEncrypt::enabled(), 'The Mcrypt extension is not installed or enabled.');
     $key = 'foobar';
     $adapter = new Memory();
     Session::config(array('primary' => array('adapter' => $adapter, 'filters' => array(), 'strategies' => array('lithium\\tests\\mocks\\storage\\session\\strategy\\MockEncrypt' => array('secret' => $key)))));
     $value = array('foo' => 'bar');
     Session::write('test', $value);
     $this->assertEqual(array('foo' => 'bar'), Session::read('test'));
     $this->assertTrue(Session::check('test'));
     $this->assertTrue(Session::check('test', array('strategies' => false)));
     $encrypted = Session::read('test', array('strategies' => false));
     $this->assertNotEqual($value, $encrypted);
     $this->assertTrue(is_string($encrypted));
     $result = Session::read('test');
     $this->assertEqual($value, $result);
     $result = Session::clear(array('strategies' => false));
     $this->assertNull(Session::read('test'));
     $this->assertFalse(Session::check('test'));
     $this->assertFalse(Session::check('test', array('strategies' => false)));
     $savedData = array('test' => $value);
     $encrypt = new MockEncrypt(array('secret' => $key));
     $result = $encrypt->encrypt($savedData);
     $this->assertEqual($encrypted, $result);
     $result = $encrypt->decrypt($encrypted);
     $this->assertEqual($savedData, $result);
 }
Ejemplo n.º 3
0
 /**
  * Provides a login page for users to login.
  *
  * @return type
  */
 public function login()
 {
     $user = Auth::check('li3b_user', $this->request);
     // 'triedAuthRedirect' so we don't end up in a redirect loop
     if (!Session::check('triedAuthRedirect', array('name' => 'cookie'))) {
         Session::write('triedAuthRedirect', 'false', array('name' => 'cookie', 'expires' => '+1 hour'));
     }
     // Facebook returns a session querystring... We don't want to show this to the user.
     // Just redirect back so it ditches the querystring. If the user is logged in, then
     // it will redirect like expected using the $url variable that has been set below.
     // Not sure why we need to do this, I'd figured $user would be set...And I think there's
     // a session just fine if there was no redirect and the user navigated away...
     // But for some reason it doesn't see $user and get to the redirect() part...
     if (isset($_GET['session'])) {
         $this->redirect(array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'login'));
     }
     if ($user) {
         // Users will be redirected after logging in, but where to?
         $url = '/';
         // Default redirects for certain user roles
         switch ($user['role']) {
             case 'administrator':
             case 'content_editor':
                 $url = '/admin';
                 break;
             default:
                 $url = '/';
                 break;
         }
         // Second, look to see if a cookie was set. The could have ended up at the login page
         // because he/she tried to go to a restricted area. That URL was noted in a cookie.
         if (Session::check('beforeAuthURL', array('name' => 'cookie'))) {
             $url = Session::read('beforeAuthURL', array('name' => 'cookie'));
             // 'triedAuthRedirect' so we don't end up in a redirect loop
             $triedAuthRedirect = Session::read('triedAuthRedirect', array('name' => 'cookie'));
             if ($triedAuthRedirect == 'true') {
                 $url = '/';
                 Session::delete('triedAuthRedirect', array('name' => 'cookie'));
             } else {
                 Session::write('triedAuthRedirect', 'true', array('name' => 'cookie', 'expires' => '+1 hour'));
             }
             Session::delete('beforeAuthURL', array('name' => 'cookie'));
         }
         // Save last login IP and time
         $user_document = User::find('first', array('conditions' => array('_id' => $user['_id'])));
         if ($user_document) {
             $user_document->save(array('lastLoginIp' => $_SERVER['REMOTE_ADDR'], 'lastLoginTime' => new MongoDate()));
         }
         // only set a flash message if this is a login. it could be a redirect from somewhere else that has restricted access
         // $flash_message = FlashMessage::read('default');
         // if(!isset($flash_message['message']) || empty($flash_message['message'])) {
         FlashMessage::write('You\'ve successfully logged in.', 'default');
         // }
         $this->redirect($url);
     } else {
         if ($this->request->data) {
             FlashMessage::write('You entered an incorrect username and/or password.', 'default');
         }
     }
     $data = $this->request->data;
     return compact('data');
 }
Ejemplo n.º 4
0
    public function login() {
        $user = Auth::check('minerva_user', $this->request);
        if ($user) {
            // TODO: Put in a $redirectURL property so it can be controlled and option for the following redirect true/false for taking a user back to the page they first requested.
            // Also TODO: Make flash messages set in some sort of config, possibly even the model properties too
            $url = '/';
            if (Session::check('beforeAuthURL')) {
                $url = Session::read('beforeAuthURL');
                Session::delete('beforeAuthURL');				
            }
            // Save last login IP and time
            //$user_record = User::find('first', array('conditions' => array('_id' => new \MongoId($user['_id']))));
			$user_record = $this->getDocument(array(
				'action' => __METHOD__,
				'request' => $this->request,
				'find_type' => 'first',
				'conditions' => array('_id' => new \MongoId($user['_id']))
			));
			
            if($user_record) {
				$user_record->save(array('last_login_ip' => $_SERVER['REMOTE_ADDR'], 'last_login_time' => date('Y-m-d h:i:s')));
			}
            
            FlashMessage::set('You\'ve successfully logged in.');
            $this->redirect($url);
            //$this->redirect(array('controller' => 'pages', 'action' => 'index'));
        } else {
            if($this->request->data) {
                FlashMessage::set('You entered an incorrect username and/or password.', array('type' => 'error'));
            }
        }
        $data = $this->request->data;
        return compact('data');
    }
Ejemplo n.º 5
0
 public function testStrategies()
 {
     Session::config(array('primary' => array('adapter' => new Memory(), 'filters' => array(), 'strategies' => array('lithium\\storage\\cache\\strategy\\Json'))));
     Session::write('test', array('foo' => 'bar'));
     $this->assertEqual(array('foo' => 'bar'), Session::read('test'));
     $this->assertTrue(Session::check('test'));
     $this->assertTrue(Session::check('test', array('strategies' => false)));
     $result = Session::read('test', array('strategies' => false));
     $this->assertEqual('{"foo":"bar"}', $result);
     $result = Session::clear(array('strategies' => false));
     $this->assertNull(Session::read('test'));
     $this->assertFalse(Session::check('test'));
     $this->assertFalse(Session::check('test', array('strategies' => false)));
 }
Ejemplo n.º 6
0
 /**
  * Tests clearing all session data from one or all adapters.
  *
  * @return void
  */
 public function testSessionClear()
 {
     Session::config(array('primary' => array('adapter' => new Memory(), 'filters' => array()), 'secondary' => array('adapter' => new Memory(), 'filters' => array())));
     Session::write('key1', 'value', array('name' => 'primary'));
     Session::write('key2', 'value', array('name' => 'secondary'));
     Session::clear(array('name' => 'secondary'));
     $this->assertTrue(Session::check('key1'));
     $this->assertFalse(Session::check('key2'));
     Session::write('key2', 'value', array('name' => 'secondary'));
     Session::clear();
     $this->assertFalse(Session::check('key1'));
     $this->assertFalse(Session::check('key2'));
 }
Ejemplo n.º 7
0
 /**
  * Tests deleting a session key from one or all adapters.
  *
  * @return void
  */
 public function testSessionKeyCheckAndDelete()
 {
     Session::config(array('temp' => array('adapter' => new Memory(), 'filters' => array()), 'persistent' => array('adapter' => new Memory(), 'filters' => array())));
     Session::write('key1', 'value', array('name' => 'persistent'));
     Session::write('key2', 'value', array('name' => 'temp'));
     $result = Session::check('key1');
     $this->assertTrue($result);
     $result = Session::check('key2');
     $this->assertTrue($result);
     $result = Session::check('key1', array('name' => 'persistent'));
     $this->assertTrue($result);
     $result = Session::check('key1', array('name' => 'temp'));
     $this->assertFalse($result);
     $result = Session::check('key2', array('name' => 'persistent'));
     $this->assertFalse($result);
     $result = Session::check('key2', array('name' => 'temp'));
     $this->assertTrue($result);
     Session::delete('key1');
     $result = Session::check('key1');
     $this->assertFalse($result);
     Session::write('key1', 'value', array('name' => 'persistent'));
     $result = Session::check('key1');
     $this->assertTrue($result);
     Session::delete('key1', array('name' => 'temp'));
     $result = Session::check('key1');
     $this->assertTrue($result);
     Session::delete('key1', array('name' => 'persistent'));
     $result = Session::check('key1');
     $this->assertFalse($result);
 }