Skip to content

Commit 289a8d2

Browse files
committed
add request authenticated attribute
1 parent 92e13e2 commit 289a8d2

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

config/request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
use Mvc5\Plugin\NullValue;
7+
use Valar\Plugin\Authenticated;
78
use Valar\Plugin\AcceptsJson;
89
use Valar\Plugin\Args;
910
use Valar\Plugin\Attributes;
@@ -23,6 +24,7 @@
2324
use Valar\Plugin\Version;
2425

2526
return [
27+
'authenticated' => new Authenticated,
2628
'accepts_json' => new AcceptsJson,
2729
'args' => new Args,
2830
//'attributes' => new Attributes,

init.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
include __DIR__ . '/src/Plugin/AcceptsJson.php';
1818
include __DIR__ . '/src/Plugin/Args.php';
1919
include __DIR__ . '/src/Plugin/Attributes.php';
20+
include __DIR__ . '/src/Plugin/Authenticated.php';
2021
include __DIR__ . '/src/Plugin/Body.php';
2122
include __DIR__ . '/src/Plugin/ClientAddress.php';
2223
include __DIR__ . '/src/Plugin/Cookies.php';

src/Plugin/Authenticated.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
6+
namespace Valar\Plugin;
7+
8+
use Mvc5\Arg;
9+
use Mvc5\Plugin\ScopedCall;
10+
use Mvc5\Plugin\Shared;
11+
12+
class Authenticated
13+
extends Shared
14+
{
15+
/**
16+
* @param string $name
17+
*/
18+
function __construct(string $name = 'authenticated')
19+
{
20+
parent::__construct($name, new ScopedCall($this));
21+
}
22+
23+
/**
24+
* @return \Closure
25+
*/
26+
function __invoke() : \Closure
27+
{
28+
return function() {
29+
return $this[Arg::USER][Arg::AUTHENTICATED] ?? false;
30+
};
31+
}
32+
}

tests/Plugin/AuthenticateTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
6+
namespace Valar\Test\Plugin;
7+
8+
use Mvc5\App;
9+
use Mvc5\User\Model as User;
10+
use PHPUnit\Framework\TestCase;
11+
use Valar\Plugin\Authenticated;
12+
use Valar\ServerRequest;
13+
14+
class AuthenticateTest
15+
extends TestCase
16+
{
17+
/**
18+
* @throws \Throwable
19+
*/
20+
function test_authenticated()
21+
{
22+
$plugins = [
23+
'authenticated' => new Authenticated,
24+
'user' => new User(['authenticated' => true])
25+
];
26+
27+
$config = new App(['services' => $plugins], null, true, true);
28+
29+
$request = new ServerRequest($config);
30+
$config->scope($request);
31+
32+
$this->assertTrue($request->authenticated());
33+
}
34+
35+
/**
36+
* @throws \Throwable
37+
*/
38+
function test_not_authenticated()
39+
{
40+
$plugins = [
41+
'authenticated' => new Authenticated,
42+
'user' => new User
43+
];
44+
45+
$config = new App(['services' => $plugins], null, true, true);
46+
47+
$request = new ServerRequest($config);
48+
$config->scope($request);
49+
50+
$this->assertFalse($request->authenticated());
51+
}
52+
}

0 commit comments

Comments
 (0)