Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
26 / 26
Client
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
26 / 26
 perm
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
13 / 13
 role
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
13 / 13
1<?php
2
3namespace Qmp\Laravel\Acls\Client;
4
5use Illuminate\Http\Response;
6use Qmp\Laravel\Acls\Exceptions\RolePermissionException;
7use \Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest;
8use Qmp\Laravel\MicroService\Client\Client as ServiceClient;
9
10abstract class Client
11{
12    const SERVICE_NAME = 'service_acls';
13    const TIMEOUT = 45;
14
15    /**
16     * @param $userId
17     * @param array $permissions
18     * @param bool $all
19     * @return void
20     * @throws RolePermissionException
21     */
22    public static function perm($userId, array $permissions, $all = false)
23    {
24        $body = ['permissions' => $permissions];
25        if ($all) {
26            $body['all_needed'] = true;
27        }
28
29        $clientRequest = new ClientRequest();
30        $clientRequest->setUrl('http://' . self::SERVICE_NAME)
31            ->setPath("/api/user/$userId/can")
32            ->setBody($body)
33            ->setTimeout(self::TIMEOUT);
34
35        $response = ServiceClient::post($clientRequest);
36        if ($response->code !== Response::HTTP_OK) {
37            $message = !empty(($response->content['error'])) ? $response->content['error'] : 'User not permitted.';
38            throw new RolePermissionException($message);
39        }
40    }
41
42    public static function role($userId, array $roles, $all = false)
43    {
44        $body = ['roles' => $roles];
45        if ($all) {
46            $body['all_needed'] = true;
47        }
48
49        $clientRequest = new ClientRequest();
50        $clientRequest->setUrl('http://' . self::SERVICE_NAME)
51            ->setPath("/api/user/$userId/is")
52            ->setBody($body)
53            ->setTimeout(self::TIMEOUT);
54
55        $response = ServiceClient::post($clientRequest);
56        if ($response->code !== Response::HTTP_OK) {
57            $message = !empty(($response->content['error'])) ? $response->content['error'] : 'User not permitted.';
58            throw new RolePermissionException($message);
59        }
60    }
61}