public function testClear()
 {
     FlashMessage::write('Foo');
     FlashMessage::clear();
     $result = Session::read('message.default', array('name' => 'default'));
     $this->assertNull($result);
     FlashMessage::write('Foo 2', 'test1');
     FlashMessage::clear('test1');
     $result = Session::read('message.test1', array('name' => 'default'));
     $this->assertNull($result);
     FlashMessage::write('Foo 3', 'test2');
     FlashMessage::write('Foo 4', 'test3');
     FlashMessage::clear(null);
     $result = Session::read('message', array('name' => 'default'));
     $this->assertNull($result);
 }
 /**
  * Binds the messaging system to a controller to enable `'message'` option flags in various
  * controller methods, such as `render()` and `redirect()`.
  *
  * @param object $controller An instance of `lithium\action\Controller`.
  * @param array $options Options.
  * @return object Returns the passed `$controller` instance.
  */
 public static function bindTo($controller, array $options = array())
 {
     $controller->applyFilter('redirect', function ($self, $params, $chain) use($options) {
         $options =& $params['options'];
         if (isset($params['options']['message'])) {
             FlashMessage::write($params['options']['message']);
             unset($params['options']['message']);
         }
         return $chain->next($self, $params, $chain);
     });
     return $controller;
 }
Example #3
0
 protected function flashMessage($msg, $viewAttribs = array(), $namespace = 'global')
 {
     return FlashMessage::write($msg, $viewAttribs, $namespace);
 }
Example #4
0
 /**
  * Public view action, for user profiles and such.
  *
  * @param $url The user's pretty URL
  */
 public function read($url = null)
 {
     $conditions = array('url' => $url);
     /**
      * If nothing is passed, get the currently logged in user's profile.
      * This is safer to use for logged in users, because if they update
      * their profile and change their name...The pretty URL changes.
      */
     if (empty($url) && isset($this->request->user)) {
         $conditions = array('_id' => $this->request->user['_id']);
     }
     $user = User::find('first', array('conditions' => $conditions));
     if (empty($user)) {
         FlashMessage::write('Sorry, that user does not exist.', 'default');
         return $this->redirect('/');
     }
     /**
      * Protect the password in case changes are made where this action
      * could be called with a handler like JSON or XML, etc. This way,
      * even if the user document is returned, it won't contain any
      * sensitive password information. Not even the _id.
      */
     $user->set(array('password' => null, '_id' => null));
     $this->set(compact('user'));
 }
    public function testMessageTranslation()
    {
        $testApp = Libraries::get(true, 'resources') . '/tmp/tests/test_app';
        mkdir($testApp . '/config', 0777, true);
        $body = <<<EOD
<?php
return array(
\t'hello' => 'Hello World.',
\t'advice' => 'Whatever advice you give, be short.',
\t'error' => 'To err is human, but for a real disaster you need a computer.'
);
?>
EOD;
        $filepath = $testApp . '/config/messages.php';
        file_put_contents($filepath, $body);
        Libraries::add('test_app', array('path' => $testApp));
        FlashMessage::config(array('library' => 'test_app'));
        $messages = array('hello', 'advice', 'error');
        FlashMessage::write($messages);
        $expected = array('message' => array('Hello World.', 'Whatever advice you give, be short.', 'To err is human, but for a real disaster you need a computer.'), 'attrs' => array());
        $result = FlashMessage::read('flash_message');
        $this->assertEqual($expected, $result);
        $message = 'hello';
        FlashMessage::write($message);
        $expected = array('message' => 'Hello World.', 'attrs' => array());
        $result = FlashMessage::read('flash_message');
        $this->assertEqual($expected, $result);
        $this->_cleanUp();
    }
Example #6
0
// Adding the library here if it hasn't already been added.
if (!class_exists('li3_access\\security\\Access')) {
    Libraries::add('li3_access');
}
Dispatcher::applyFilter('_callable', function ($self, $params, $chain) {
    // Run other filters first. This allows this one to not exactly be overwritten or excluded...But it does allow for a different login action to be used...
    // TODO: Perhaps allow this to be skipped...
    $next = $chain->next($self, $params, $chain);
    $request = $params['request'];
    $action = $request->action;
    $user = Auth::check('li3b_user');
    // Protect all admin methods except for login and logout.
    if ($request->admin === true && $action != 'login' && $action != 'logout') {
        $action_access = Access::check('default', $user, $request, array('rules' => array('allowManagers')));
        if (!empty($action_access)) {
            FlashMessage::write($action_access['message'], 'default');
            if ($user) {
                header('Location: ' . Router::match($action_access['redirect']));
            } else {
                header('Location: ' . Router::match(array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'login')));
            }
            // None shall pass.
            exit;
        }
    }
    // Sets the current user in each request for convenience.
    $params['request']->user = $user;
    return $next;
    // return $chain->next($self, $params, $chain);
});
Access::config(array('default' => array('adapter' => 'Rules', 'filters' => array())));