Completed
Push — api/develop ( dd11de...3cb7c9 )
by Bertrand
06:21
created

Request::hasAccess()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.0342

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 16
cts 18
cp 0.8889
rs 8.439
cc 5
eloc 17
nc 5
nop 1
crap 5.0342
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 94
    public function __construct()
23
    {
24 94
        $this->app_list_limit = env('APP_LIST_LIMIT', 50);
25 94
        $token = JWTAuth::getToken();
26 94
        if (!empty($token)) {
27 94
            $user = JWTAuth::toUser($token);
28 94
            $this->logged_user = User::find($user->id);
29 94
        }
30 94
    }
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
     * @return bool
61
     */
62 94
    public function hasAccess($permission)
63
    {
64 94
        switch($this->getMethod())
65
        {
66 94
            case 'DELETE':
67 28
                $hasAccess = $this->logged_user->hasAccess($permission.'.delete');
68 28
                break;
69
70 80
            case 'GET':
71 10
                $hasAccess = $this->logged_user->hasAccess($permission.'.view');
72 10
                break;
73
74 70
            case 'PATCH':
75 28
                $hasAccess = $this->logged_user->hasAccess($permission.'.update');
76 28
                break;
77
78 56
            case 'POST':
79 56
                $hasAccess = $this->logged_user->hasAccess($permission.'.create');
80 56
                break;
81
82
            default:
83
                $hasAccess = false;
84 94
        }
85
86 94
        return $hasAccess;
87
    }
88
}
89