Passed
Push — master ( 84827d...08ecb2 )
by Patrick
06:45
created

EventAPI::userCanAuth()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
1
<?php
2
class EventAPI extends VolunteerAPI
3
{
4
    use Processor;
0 ignored issues
show
introduced by
The trait Processor requires some properties which are not provided by EventAPI: $startTime, $certs, $endTime, $mail, $uid, $title
Loading history...
5
6
    public function __construct()
7
    {
8
        parent::__construct('events');
9
    }
10
11
    public function setup($app)
12
    {
13
        parent::setup($app);
14
        $app->get('/{event}/shifts[/]', array($this, 'getShiftsForEvent'));
15
        $app->post('/{event}/shifts[/]', array($this, 'createShiftForEvent'));
16
        $app->get('/{event}/Actions/GetEEShiftReport', array($this, 'getEEShiftReportForEvent'));
17
        $app->post('/{event}/Actions/GetEEShiftReport', array($this, 'getEEShiftReportForEvent'));
18
        $app->post('/{event}/Actions/ApproveEE', array($this, 'approveEEForEvent'));
19
    }
20
21
    protected function getFilterForPrimaryKey($value)
22
    {
23
        return new \Data\Filter($this->primaryKeyName." eq '$value' or alias eq '$value'");
24
    }
25
26
    protected function canUpdate($request, $entity)
27
    {
28
        if($this->isVolunteerAdmin($request))
29
        {
30
            return true;
31
        }       
32
        return false;
33
    }
34
35
    protected function canDelete($request, $entity)
36
    {
37
        if($this->isVolunteerAdmin($request))
38
        {
39
            return true;
40
        }
41
        return false;
42
    }
43
44
    public function processEntry($entry, $request)
45
    {
46
        $entry['available'] = true;
47
        $endTime = new DateTime($entry['endTime']);
48
        $now = new DateTime();
49
        if($endTime < $now)
50
        {
51
            $entry['available'] = false;
52
            $entry['why'] = 'Event is in the past';
53
        }
54
        if(isset($entry['volList']) && !is_array($entry['volList']))
55
        {
56
            $entry['volList'] = explode(',', $entry['volList']);
57
            $count = count($entry['volList']);
58
            for($i = 0; $i < $count; $i++)
59
            {
60
                $entry['volList'][$i] = trim($entry['volList'][$i]);
61
            }
62
        }
63
        if($entry['private'] && !in_array($this->user->mail, $entry['volList']))
64
        {
65
            $entry['available'] = false;
66
            $entry['why'] = 'Event is private and you are not invited';
67
        }
68
        if(!$entry['available'] && !$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user))
69
        {
70
            return null;
71
        }
72
        if(!$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user) && isset($entry['eeLists']))
73
        {
74
            unset($entry['eeLists']);
75
        }
76
        return $entry;
77
    }
78
79
    public function getShiftsForEvent($request, $response, $args)
80
    {
81
        $this->validateLoggedIn($request);
82
        $eventId = $args['event'];
83
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
84
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
85
        $filter = $this->addRequiredFilter('eventID', $eventId, $odata);
86
        if($filter === false)
87
        {
88
            return $response->withStatus(409);
89
        }
90
        $shifts = $dataTable->read($filter, $odata->select, $odata->top,
91
                                    $odata->skip, $odata->orderby);
92
        if($shifts === false)
93
        {
94
            $shifts = array();
95
        }
96
        $count = count($shifts);
0 ignored issues
show
Bug introduced by
It seems like $shifts can also be of type true; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $count = count(/** @scrutinizer ignore-type */ $shifts);
Loading history...
97
        for($i = 0; $i < $count; $i++)
98
        {
99
            $shifts[$i] = $this->processShift($shifts[$i], $request);
100
        }
101
        $shifts = array_values(array_filter($shifts));
0 ignored issues
show
Bug introduced by
It seems like $shifts can also be of type true; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        $shifts = array_values(array_filter(/** @scrutinizer ignore-type */ $shifts));
Loading history...
102
        return $response->withJson($shifts);
103
    }
104
105
    public function createShiftForEvent($request, $response, $args)
106
    {
107
        $eventId = $args['event'];
108
        if($this->canUpdate($request, null) === false)
109
        {
110
            return $response->withStatus(401);
111
        }
112
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
113
        $obj = $request->getParsedBody();
114
        if($obj == NULL)
115
        {
116
            $obj = json_decode($request->getBody()->getContents(), true);
117
        }
118
        $obj['eventID'] = $eventId;
119
        $ret = $dataTable->create($obj);
120
        return $response->withJson($ret);
121
    }
122
123
    public function getEEShiftReportForEvent($request, $response, $args)
124
    {
125
        $eventId = $args['event'];
126
        if($this->canUpdate($request, null) === false)
127
        {
128
            return $response->withStatus(401);
129
        }
130
        $shiftDataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
131
        $obj = $request->getParsedBody();
132
        if($obj == NULL)
133
        {
134
            $obj = json_decode($request->getBody()->getContents(), true);
135
        }
136
        $filterStr = 'eventID eq '.$eventId.' and status eq filled';
137
        if(isset($obj['earlyLate']))
138
        {
139
            $filterStr .= ' and earlyLate eq \''.$obj['earlyLate'].'\'';
140
        }
141
        else
142
        {
143
            $filterStr .= " and earlyLate ne '-1'";
144
        }
145
        $filter = new \Data\Filter($filterStr);
146
        $shifts = $shiftDataTable->read($filter);
147
        $ret = array();
148
        $count = count($shifts);
0 ignored issues
show
Bug introduced by
It seems like $shifts can also be of type boolean; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

148
        $count = count(/** @scrutinizer ignore-type */ $shifts);
Loading history...
149
        for($i = 0; $i < $count; $i++)
150
        {
151
            $shift = new \VolunteerShift(false, $shifts[$i]);
152
            $vol = $shift->participantObj;
0 ignored issues
show
Bug Best Practice introduced by
The property $participantObj is declared protected in VolunteerShift. Since you implement __get, consider adding a @property or @property-read.
Loading history...
153
            $role = $shift->role;
0 ignored issues
show
Bug Best Practice introduced by
The property role does not exist on VolunteerShift. Since you implemented __get, consider adding a @property annotation.
Loading history...
154
            $entry = array('name' => $vol->getDisplayName('paperName'), 'email'=> $vol->email, 'dept'=> $shift->departmentID, 'role' => $role->display_name, 'earlyLate'=>$shift->earlyLate);
155
            array_push($ret, $entry);
156
        }
157
        return $response->withJson($ret);
158
    }
159
160
    protected function userCanAuth($type)
161
    {
162
        switch($type)
163
        {
164
            case 'aar':
165
                return $this->user->isInGroupNamed('AAR');
166
            case 'af':
167
                return $this->user->isInGroupNamed('AFs');
168
            case 'lead':
169
                return $this->user->isInGroupNamed('Leads');
170
            default:
171
                error_log('Unknown auth type: '.$type);
172
                return false;
173
        }
174
    }
175
176
    public function approveEEForEvent($request, $response, $args)
177
    {
178
        $eventId = $args['event'];
179
        if($this->canUpdate($request, null) === false)
180
        {
181
            return $response->withStatus(401);
182
        }
183
        $event = new \VolunteerEvent($eventId);
184
        $obj = $this->getParsedBody($request);
185
        //First make sure the current user can do the auth they are trying...
186
        if($this->userCanAuth($obj['approvalType']) === false)
187
        {
188
            return $response->withStatus(401);
189
        }
190
        $eeList = $event->eeLists[intval($obj['eeList'])];
0 ignored issues
show
Bug Best Practice introduced by
The property eeLists does not exist on VolunteerEvent. Since you implemented __get, consider adding a @property annotation.
Loading history...
191
        if(!isset($eeList[$obj['uid']]))
192
        {
193
            return $response->withStatus(404);
194
        }
195
        $ret = $event->approveEE($obj['uid'], intval($obj['eeList']), $obj['approvalType']);
196
        return $response->withJson($ret);
197
    }
198
}
199
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
200