1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * @author Xavier Chopin <[email protected]> |
||||
5 | * |
||||
6 | * For the full copyright and license information, please view the LICENSE |
||||
7 | * file that was distributed with this source code. |
||||
8 | */ |
||||
9 | |||||
10 | namespace App\Controller; |
||||
11 | |||||
12 | use GuzzleHttp\Client; |
||||
13 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
||||
14 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||||
15 | |||||
16 | abstract class AbstractController extends Controller |
||||
17 | { |
||||
18 | |||||
19 | /** |
||||
20 | * Guzzle HTTP client instance linked to the OpenLRW API |
||||
21 | * |
||||
22 | * @var Client |
||||
23 | */ |
||||
24 | static public $http; |
||||
25 | |||||
26 | /** |
||||
27 | * Constructor. |
||||
28 | * @param ContainerInterface $container |
||||
29 | */ |
||||
30 | public function __construct(ContainerInterface $container) |
||||
31 | { |
||||
32 | self::$http = new Client(['base_uri' => env('API_URI')]); |
||||
33 | $container->set('http', self::$http); |
||||
34 | } |
||||
35 | |||||
36 | /** |
||||
37 | * Creates and return a JSON Web Token through the OpenLRW API by using credentials filled in .env |
||||
38 | * |
||||
39 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||||
40 | * @throws \GuzzleHttp\Exception\GuzzleException |
||||
41 | */ |
||||
42 | public static function generateJwt() |
||||
43 | { |
||||
44 | $_SESSION['JWT'] = json_decode( self::$http->request('POST', 'api/auth/login', [ |
||||
45 | 'headers' => [ 'X-Requested-With' => 'XMLHttpRequest' ], |
||||
46 | 'json' => [ |
||||
47 | 'username' => env('API_USERNAME'), |
||||
48 | 'password' => env('API_PASSWORD') |
||||
49 | ] |
||||
50 | ])->getBody() |
||||
51 | ->getContents())->token; |
||||
52 | |||||
53 | return $_SESSION['JWT']; |
||||
54 | |||||
55 | } |
||||
56 | |||||
57 | |||||
58 | protected static function getJwt() |
||||
59 | { |
||||
60 | return $_SESSION['JWT']; |
||||
61 | } |
||||
62 | |||||
63 | static function makeJwt() |
||||
0 ignored issues
–
show
|
|||||
64 | { |
||||
65 | return isset($_SESSION['JWT']) ? self::getJwt() : self::generateJwt(); |
||||
66 | } |
||||
67 | |||||
68 | /** |
||||
69 | * Stops the script and prints info about a variable |
||||
70 | * |
||||
71 | * @param mixed $variable |
||||
72 | */ |
||||
73 | static protected function debug($variable) |
||||
74 | { |
||||
75 | die('<pre>' . print_r($variable, true) . '</pre>'); |
||||
0 ignored issues
–
show
|
|||||
76 | } |
||||
77 | |||||
78 | |||||
79 | /** |
||||
80 | * Gets a service from the container. |
||||
81 | * |
||||
82 | * @param string $service |
||||
83 | * |
||||
84 | * @return mixed |
||||
85 | */ |
||||
86 | public function __get($service) |
||||
87 | { |
||||
88 | return $this->container->get($service); |
||||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * Executes a LDAP query |
||||
93 | * |
||||
94 | * @param $filter |
||||
95 | * @param array $arg |
||||
96 | * @return resource |
||||
97 | */ |
||||
98 | protected function searchLDAP($filter, $arg = []) |
||||
99 | { |
||||
100 | return ldap_search($this->__get('ldap'), env('LDAP_BASE_DN'), $filter, $arg); |
||||
0 ignored issues
–
show
$this->__get('ldap') of type object is incompatible with the type resource expected by parameter $link_identifier of ldap_search() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
101 | } |
||||
102 | |||||
103 | /** |
||||
104 | * Returns data from a LDAP query |
||||
105 | * |
||||
106 | * @param $filter |
||||
107 | * @param array $arg |
||||
108 | * @return mixed |
||||
109 | */ |
||||
110 | protected function ldap($filter, $arg = []) |
||||
111 | { |
||||
112 | return ldap_get_entries($this->__get('ldap'), $this->searchLDAP($filter, $arg)); |
||||
0 ignored issues
–
show
$this->__get('ldap') of type object is incompatible with the type resource expected by parameter $link_identifier of ldap_get_entries() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * Returns the first tuple from a LDAP query |
||||
117 | * |
||||
118 | * @param $filter |
||||
119 | * @param array $arg |
||||
120 | * @return mixed |
||||
121 | */ |
||||
122 | protected function ldapFirst($filter, $arg = []) |
||||
123 | { |
||||
124 | return ldap_get_entries($this->__get('ldap'), $this->searchLDAP($filter, $arg))[0]; |
||||
0 ignored issues
–
show
$this->__get('ldap') of type object is incompatible with the type resource expected by parameter $link_identifier of ldap_get_entries() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * Function to check if OpenLRW is up |
||||
129 | * |
||||
130 | * @return boolean |
||||
131 | * @throws \GuzzleHttp\Exception\GuzzleException |
||||
132 | */ |
||||
133 | public static function isUp() |
||||
134 | { |
||||
135 | return self::$http->request('GET', '/info.json')->getStatusCode() == 200; |
||||
136 | } |
||||
137 | |||||
138 | /** |
||||
139 | * Get the logged username |
||||
140 | * |
||||
141 | * @return mixed |
||||
142 | */ |
||||
143 | public static function loggedUser() |
||||
144 | { |
||||
145 | return $_SESSION['phpCAS']['user']; |
||||
146 | } |
||||
147 | |||||
148 | |||||
149 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.