|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller as BaseController; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Translation\Translator; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
9
|
|
|
use Terranet\Administrator\Middleware\Authenticate; |
|
10
|
|
|
use Terranet\Administrator\Middleware\AuthProvider; |
|
11
|
|
|
use Terranet\Administrator\Middleware\Resources; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AdminController extends BaseController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var null|\Illuminate\Translation\Translator |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $translator; |
|
19
|
|
|
|
|
20
|
6 |
|
public function __construct(Translator $translator) |
|
21
|
|
|
{ |
|
22
|
6 |
|
$this->middleware([ |
|
23
|
6 |
|
AuthProvider::class, |
|
24
|
|
|
Authenticate::class, |
|
25
|
|
|
Resources::class, |
|
26
|
|
|
]); |
|
27
|
|
|
|
|
28
|
6 |
|
$this->translator = $translator; |
|
29
|
6 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Authorize a given action against a set of arguments. |
|
33
|
|
|
* |
|
34
|
|
|
* @param mixed $ability |
|
35
|
|
|
* @param array|mixed $arguments |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function authorize($ability, $arguments = []) |
|
40
|
|
|
{ |
|
41
|
2 |
|
if (!$response = app('scaffold.actions')->authorize($ability, $arguments)) { |
|
|
|
|
|
|
42
|
1 |
|
throw $this->createGateUnauthorizedException( |
|
43
|
1 |
|
$ability, |
|
44
|
1 |
|
$this->translator->trans('administrator::errors.unauthorized') |
|
|
|
|
|
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return $response; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
protected function redirectTo($module, $key, Request $request) |
|
52
|
|
|
{ |
|
53
|
3 |
|
if ($next = $request->get('back_to')) { |
|
54
|
1 |
|
return redirect()->to($next); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
if ($request->exists('save')) { |
|
58
|
1 |
|
return redirect()->route('scaffold.edit', ['module' => $module, 'id' => $key]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
return redirect()->route( |
|
62
|
1 |
|
$request->exists('save_return') ? 'scaffold.index' : 'scaffold.create', |
|
63
|
1 |
|
['module' => $module] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Throw an unauthorized exception based on gate results. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $ability |
|
71
|
|
|
* @param string $message |
|
72
|
|
|
* @param \Exception $previousException |
|
73
|
|
|
* |
|
74
|
|
|
* @return \Symfony\Component\HttpKernel\Exception\HttpException |
|
75
|
|
|
*/ |
|
76
|
1 |
|
protected function createGateUnauthorizedException( |
|
77
|
|
|
$ability, |
|
78
|
|
|
$message = 'This action is unauthorized.', |
|
79
|
|
|
$previousException = null |
|
80
|
|
|
) { |
|
81
|
1 |
|
$message = sprintf($message.' [%s]', $ability); |
|
82
|
|
|
|
|
83
|
1 |
|
return new HttpException(403, $message, $previousException); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.