|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Cheka - Authorization |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (C) 2016 Jake Johns |
|
8
|
|
|
* |
|
9
|
|
|
* This software may be modified and distributed under the terms |
|
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
11
|
|
|
* |
|
12
|
|
|
* @category Middleware |
|
13
|
|
|
* @package Jnjxp\Cheka |
|
14
|
|
|
* @author Jake Johns <[email protected]> |
|
15
|
|
|
* @copyright 2016 Jake Johns |
|
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
17
|
|
|
* @link https://github.com/jnjxp/jnjxp.cheka |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Jnjxp\Cheka; |
|
21
|
|
|
|
|
22
|
|
|
use Vperyod\AuthHandler\AuthRequestAwareTrait; |
|
23
|
|
|
use Zend\Permissions\Acl\Role\RoleInterface; |
|
24
|
|
|
|
|
25
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
26
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Request Route Resource |
|
30
|
|
|
* |
|
31
|
|
|
* @category Resource |
|
32
|
|
|
* @package Jnjxp\Cheka |
|
33
|
|
|
* @author Jake Johns <[email protected]> |
|
34
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
35
|
|
|
* @link https://github.com/jnjxp/jnjxp.cheka |
|
36
|
|
|
* |
|
37
|
|
|
* @see ResourceInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
class RoleHandler |
|
40
|
|
|
{ |
|
41
|
|
|
use AuthRequestAwareTrait; |
|
42
|
|
|
use RoleRequestAwareTrait; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Role factory |
|
46
|
|
|
* |
|
47
|
|
|
* @var callable |
|
48
|
|
|
* |
|
49
|
|
|
* @access protected |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $roleFactory; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create a RoleHandler |
|
55
|
|
|
* |
|
56
|
|
|
* @param callable $roleFactory factory to create role from request |
|
57
|
|
|
* |
|
58
|
|
|
* @access public |
|
59
|
|
|
*/ |
|
60
|
5 |
|
public function __construct(callable $roleFactory = null) |
|
61
|
|
|
{ |
|
62
|
5 |
|
$this->roleFactory = $roleFactory ?: [$this, 'newRole']; |
|
63
|
5 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Add Role object to Request |
|
67
|
|
|
* |
|
68
|
|
|
* @param Request $request PSR7 Request |
|
69
|
|
|
* @param Response $response PSR7 Response |
|
70
|
|
|
* @param callable $next next middleware |
|
71
|
|
|
* |
|
72
|
|
|
* @return Response |
|
73
|
|
|
* |
|
74
|
|
|
* @access public |
|
75
|
|
|
*/ |
|
76
|
4 |
|
public function __invoke(Request $request, Response $response, callable $next) |
|
77
|
|
|
{ |
|
78
|
4 |
|
$factory = $this->roleFactory; |
|
79
|
|
|
|
|
80
|
4 |
|
$request = $request->withAttribute( |
|
81
|
4 |
|
$this->roleAttribute, |
|
82
|
4 |
|
$factory($request) |
|
83
|
3 |
|
); |
|
84
|
|
|
|
|
85
|
3 |
|
return $next($request, $response); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Create a new Role from Auth stored in Request |
|
90
|
|
|
* |
|
91
|
|
|
* @param Request $request PSR7 Request |
|
92
|
|
|
* |
|
93
|
|
|
* @return Acl\Role |
|
94
|
|
|
* |
|
95
|
|
|
* @access protected |
|
96
|
|
|
*/ |
|
97
|
3 |
|
protected function newRole(Request $request) |
|
98
|
|
|
{ |
|
99
|
3 |
|
$auth = $this->getAuth($request); |
|
100
|
2 |
|
if ($auth instanceof RoleInterface) { |
|
101
|
1 |
|
return $auth; |
|
102
|
|
|
} |
|
103
|
1 |
|
return new Acl\Role($auth); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|