check() public method

The Rules adapter will use check to test the provided data against a number of given rules. Extra data that may be required to make an informed decision about access can be passed in the $options array. This extra data will vary from app to app and rules will need to be added to handle it. The default rules assume some general cases and more can be added or passed directly to this method.
public check ( mixed $user, mixed $params, array $options = [] ) : array
$user mixed The user data array that holds all necessary information about the user requesting access. Or false (because `Auth::check()` can return `false`).
$params mixed The Lithium `Request` object, or an array with at least 'request', and 'params'
$options array An array of additional options.
return array An empty array if access is allowed and an array with reasons for denial if denied.
Example #1
0
 /**
  * Tests that user information is automatically retrieved via the closure in the `'user'`
  * config.
  */
 public function testAutoUser()
 {
     $request = new Request();
     $user = array('username' => 'Tom');
     $adapter = new Rules(array('rules' => array('user' => function ($user, $request, $options) {
         return isset($user['username']) && $user['username'] == 'Tom';
     }), 'default' => array('user'), 'user' => function () use($user) {
         return $user;
     }));
     $result = $adapter->check($user, compact('request'));
     $this->assertEqual(array(), $result);
     $result = $adapter->check(null, compact('request'));
     $this->assertEqual(array(), $result);
     $result = $adapter->check(array('username' => 'Bob'), compact('request'));
     $this->assertEqual(array('rule' => 'user'), $result);
 }
Example #2
0
 public function testAutoUser()
 {
     $user = ['username' => 'Mehlah'];
     $adapter = new Rules(['rules' => ['isMehlah' => function ($user, $request, $options) {
         return isset($user['username']) && $user['username'] == 'Mehlah';
     }], 'defaults' => ['isMehlah'], 'user' => function () use($user) {
         return $user;
     }]);
     $result = $adapter->check($user, null);
     $this->assertTrue($result);
     $result = $adapter->check(null, null);
     $this->assertTrue($result);
     $result = $adapter->check(['username' => 'Bob'], null);
     $this->assertFalse($result);
     $expected = ['isMehlah' => 'You are not permitted to access this area.'];
     $result = $adapter->error();
     $this->assertEqual($expected, $result);
 }