Completed
Push — api/develop ( c97cc3...64624d )
by Bertrand
10:00
created

Request::hasAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the HRis Software package.
5
 *
6
 * HRis - Human Resource and Payroll System
7
 *
8
 * @link http://github.com/HB-Co/HRis
9
 */
10
namespace HRis\Http\Requests;
11
12
use Dingo\Api\Http\FormRequest;
13
use HRis\Api\Eloquent\User;
14
use Tymon\JWTAuth\Facades\JWTAuth;
15
16
abstract class Request extends FormRequest
17
{
18
    public $app_list_limit;
19
20
    public $logged_user;
21
22 164
    public function __construct()
23
    {
24 164
        $this->app_list_limit = env('APP_LIST_LIMIT', 50);
25 164
        $token = JWTAuth::getToken();
26 164
        if (!empty($token)) {
27 138
            $user = JWTAuth::toUser($token);
28 138
            $this->logged_user = User::find($user->id);
29 138
        }
30 164
    }
31
32
    /**
33
     * Get the sort param.
34
     *
35
     * @return string
36
     *
37
     * @author Harlequin Doyon
38
     */
39
    public function sort()
40
    {
41
        return $this->get('sort') != '' ? $this->get('sort') : 'id';
42
    }
43
44
    /**
45
     * Get the direction param.
46
     *
47
     * @return string
48
     *
49
     * @author Harlequin Doyon
50
     */
51
    public function direction()
52
    {
53
        return $this->get('direction') != '' ? $this->get('direction') : 'asc';
54
    }
55
56
    /**
57
     * Check if logged user has access.
58
     *
59
     * @param $permission
60
     *
61
     * @return bool
62
     *
63
     * @author Bertrand Kintanar <[email protected]>
64
     */
65 132
    public function hasAccess($permission)
66
    {
67
        $methods = [
68 132
            'DELETE' => $this->logged_user->hasAccess($permission.'.delete'),
69 132
            'GET'    => $this->logged_user->hasAccess($permission.'.view'),
70 132
            'PATCH'  => $this->logged_user->hasAccess($permission.'.update'),
71 132
            'POST'   => $this->logged_user->hasAccess($permission.'.create'),
72 132
        ];
73
74 132
        return $methods[$this->getMethod()];
75
    }
76
}
77