|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractController extends Controller |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Guzzle HTTP client instance linked to the OpenLRW API |
|
14
|
|
|
* |
|
15
|
|
|
* @var Client |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $http; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor. |
|
21
|
|
|
* @param ContainerInterface $container |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(ContainerInterface $container) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$this->http = new Client(['base_uri' => env('API_URI')]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Creates and return a JSON Web Token through the OpenLRW API by using credentials filled in parameters.yml |
|
30
|
|
|
* |
|
31
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function createJWT() |
|
34
|
|
|
{ |
|
35
|
|
|
return json_decode( $this->http->request('POST', 'auth/login', [ |
|
36
|
|
|
'headers' => [ 'X-Requested-With' => 'XMLHttpRequest' ], |
|
37
|
|
|
'json' => [ |
|
38
|
|
|
'username' => env('API_USERNAME'), |
|
39
|
|
|
'password' => env('API_PASSWORD') |
|
40
|
|
|
] |
|
41
|
|
|
])->getBody() |
|
42
|
|
|
->getContents()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Stops the script and prints info about a variable |
|
47
|
|
|
* |
|
48
|
|
|
* @param mixed $variable |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function debug($variable) |
|
51
|
|
|
{ |
|
52
|
|
|
die('<pre>' . print_r($variable, true) . '</pre>'); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Gets a service from the container. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $service |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __get($service) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->container->get($service); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Executes a LDAP query |
|
70
|
|
|
* |
|
71
|
|
|
* @param $filter |
|
72
|
|
|
* @param array $arg |
|
73
|
|
|
* @return resource |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function searchLDAP($filter, $arg = []) |
|
76
|
|
|
{ |
|
77
|
|
|
return ldap_search($this->__get('ldap'), env('LDAP_BASE_DN'), $filter, $arg); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns data from a LDAP query |
|
82
|
|
|
* |
|
83
|
|
|
* @param $filter |
|
84
|
|
|
* @param array $arg |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function ldap($filter, $arg = []) |
|
88
|
|
|
{ |
|
89
|
|
|
return ldap_get_entries($this->__get('ldap'), $this->searchLDAP($filter, $arg)); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the first tuple from a LDAP query |
|
94
|
|
|
* |
|
95
|
|
|
* @param $filter |
|
96
|
|
|
* @param array $arg |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function ldapFirst($filter, $arg = []) |
|
100
|
|
|
{ |
|
101
|
|
|
return ldap_get_entries($this->__get('ldap'), $this->searchLDAP($filter, $arg))[0]; |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.