Completed
Push — master ( bf23bc...4f23d4 )
by Patrick
06:42
created

LeadsAPI::canUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
class LeadsAPI extends Http\Rest\DataTableAPI
3
{
4
    public function __construct()
5
    {
6
        parent::__construct('profiles', 'position', 'short_name');
0 ignored issues
show
Documentation introduced by
'short_name' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
7
    }
8
9
    public function setup($app)
10
    {
11
        parent::setup($app);
12
    }
13
14 View Code Duplication
    protected function validateIsAdmin($request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
    {
16
        $user = $request->getAttribute('user');
17
        if($user === false)
18
        {
19
            throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
20
        }
21
        if(!$user->isInGroupNamed('LDAPAdmins'))
22
        {
23
            throw new Exception('Must be Admin', \Http\Rest\ACCESS_DENIED);
24
        }
25
    }
26
27
    protected function canCreate($request)
28
    {
29
        $this->validateIsAdmin($request);
30
        return true;
31
    }
32
33
    protected function canUpdate($request, $entity)
34
    {
35
        $this->validateIsAdmin($request);
36
        return true;
37
    }
38
39
    protected function hasPositionAccess()
40
    {
41
        return ($this->user->isInGroupNamed('Leads') ||
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
                $this->user->isInGroupNamed('CC') ||
43
                $this->user->isInGroupNamed('AFs'));
44
    }
45
46
    protected function getPositionsByType($type, $auth)
47
    {
48
        switch($type)
49
        {
50
            case 'aar':
51
                $aarGroup = $auth->getGroupByName('AAR');
52
                return $aarGroup->members(true, false);
53
            case 'af':
54
                $afGroup = $auth->getGroupByName('AFs');
55
                return $afGroup->members(true, false);
56
            case 'cc':
57
                $ccGroup = $auth->getGroupByName('CC');
58
                return $ccGroup->members(true, false);
59
            case 'lead':
60
                $leadGroup = $auth->getGroupByName('Leads');
61
                return $leadGroup->members(true, false);
62
            default:
63
                $filter = new \Data\Filter('ou eq '.$type);
64
                return $auth->getUsersByFilter($filter);
65
        }
66
    }
67
68
    protected function getPositionsWithParams($params)
69
    {
70
        $auth = AuthProvider::getInstance();
71
        if(isset($params['type']))
72
        {
73
            return $this->getPositionsByType($params['type'], $auth);
74
        }
75
        $leads = array();
76
        $leadGroup = $auth->getGroupByName('Leads');
77
        $aarGroup  = $auth->getGroupByName('AAR');
78
        $afGroup   = $auth->getGroupByName('AFs');
79
        $ccGroup   = $auth->getGroupByName('CC');
80
        $leads     = array_merge($leads, $leadGroup->members(true, false));
81
        $leads     = array_merge($leads, $aarGroup->members(true, false));
82
        $leads     = array_merge($leads, $afGroup->members(true, false));
83
        $leads     = array_merge($leads, $ccGroup->members(true, false));
84
        return $leads;
85
    }
86
87
    public function readEntries($request, $response, $args)
88
    {
89
        if($this->canRead($request) === false || $this->hasPositionAccess() === false)
90
        {
91
            return $response->withStatus(401);
92
        }
93
        $dataTable = $this->getDataTable();
0 ignored issues
show
Unused Code introduced by
$dataTable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
94
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
95
        $leads = $this->getPositionsWithParams($request->getQueryParams());
96
        $leads = $odata->filterArrayPerSelect($leads);
97
        return $response->withJson($leads);
98
    }
99
}
100
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
101