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 Responder |
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\Responder; |
21
|
|
|
|
22
|
|
|
use Jnjxp\Cheka\RoleRequestAwareTrait; |
23
|
|
|
use Jnjxp\Cheka\ResourceRouteAwareTrait; |
24
|
|
|
|
25
|
|
|
use Aura\Router\Route; |
26
|
|
|
|
27
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
28
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AbstractResponder |
32
|
|
|
* |
33
|
|
|
* @category Responder |
34
|
|
|
* @package Jnjxp\Cheka |
35
|
|
|
* @author Jake Johns <[email protected]> |
36
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
37
|
|
|
* @link https://github.com/jnjxp/jnjxp.cheka |
38
|
|
|
* |
39
|
|
|
* @abstract |
40
|
|
|
*/ |
41
|
|
|
abstract class AbstractResponder |
42
|
|
|
{ |
43
|
|
|
use RoleRequestAwareTrait; |
44
|
|
|
use ResourceRouteAwareTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* PSR7 Request |
48
|
|
|
* |
49
|
|
|
* @var Request |
50
|
|
|
* |
51
|
|
|
* @access protected |
52
|
|
|
*/ |
53
|
|
|
protected $request; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* PSR7 Response |
57
|
|
|
* |
58
|
|
|
* @var Response |
59
|
|
|
* |
60
|
|
|
* @access protected |
61
|
|
|
*/ |
62
|
|
|
protected $response; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Failed Route |
66
|
|
|
* |
67
|
|
|
* @var Route |
68
|
|
|
* |
69
|
|
|
* @access protected |
70
|
|
|
*/ |
71
|
|
|
protected $route; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* __invoke |
75
|
|
|
* |
76
|
|
|
* @param Request $request DESCRIPTION |
77
|
|
|
* @param Response $response DESCRIPTION |
78
|
|
|
* @param Route $route DESCRIPTION |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
*/ |
84
|
1 |
|
public function __invoke(Request $request, Response $response, Route $route) |
85
|
|
|
{ |
86
|
1 |
|
$this->request = $request; |
87
|
1 |
|
$this->response = $response; |
88
|
1 |
|
$this->route = $route; |
89
|
|
|
|
90
|
1 |
|
$this->notAuthorized(); |
91
|
|
|
|
92
|
1 |
|
return $this->response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* GetMessage |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
* |
100
|
|
|
* @access protected |
101
|
|
|
*/ |
102
|
1 |
|
protected function getAuthorizationMessage() |
103
|
|
|
{ |
104
|
1 |
|
$role = $this->getRole($this->request)->getRoleId(); |
105
|
1 |
|
$resource = $this->getResourceIdFromRoute($this->route); |
106
|
1 |
|
$privilege = $this->getPrivilegeFromRoute($this->route); |
107
|
|
|
|
108
|
|
|
$needed = $privilege |
109
|
1 |
|
? sprintf('"%s" privilege', $privilege) |
110
|
1 |
|
: 'privileges'; |
111
|
|
|
|
112
|
1 |
|
return sprintf( |
113
|
1 |
|
'"%s" does not have %s on "%s"', |
114
|
1 |
|
$role, |
115
|
1 |
|
$needed, |
116
|
|
|
$resource |
117
|
1 |
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Not Authorized |
122
|
|
|
* |
123
|
|
|
* @return Response |
124
|
|
|
* |
125
|
|
|
* @access protected |
126
|
|
|
*/ |
127
|
|
|
abstract protected function notAuthorized(); |
128
|
|
|
} |
129
|
|
|
|