Completed
Push — FVSv2 ( 427c44...e6ffd8 )
by Patrick
01:43
created

ParticipantAPI::canUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
class ParticipantAPI extends VolunteerAPI
3
{
4
    public function __construct()
5
    {
6
        parent::__construct('participants', 'uid');
7
    }
8
9
    protected function canCreate($request)
10
    {
11
        $this->validateLoggedIn($request);
12
        return true;
13
    }
14
15
    protected function canRead($request)
16
    {
17
        if($this->isVolunteerAdmin($request))
18
        {
19
            return true;
20
        }
21
        //TODO give access to department leads
22
        return true;
23
    }
24
25
    protected function canUpdate($request, $entity)
26
    {
27
 	if($this->isVolunteerAdmin($request))
28
        {
29
            return true;
30
        }       
31
        //TODO give access to department lead
32
        return false;
33
    }
34
35
    protected function canDelete($request, $entity)
36
    {
37
        return $this->canUpdate($request, $entity);
38
    }
39
40
    protected function validateCreate(&$obj, $request)
41
    {
42
        if(isset($obj['uid']))
43
        {
44
            return false;
45
        }
46
        $uid = $this->user->uid;
47
        $dataTable = $this->getDataTable();
48
        $filter = $this->getFilterForPrimaryKey($uid);
49
        $users = $dataTable->read($filter);
50
        if(!empty($users))
51
        {
52
            //User is already created...
53
            return false;
54
        }
55
        $obj['uid'] = $uid;
56
        return true;
57
    }
58
59
    public function readEntry($request, $response, $args)
60
    {
61
        $this->validateLoggedIn($request);
62
        $uid = $args['name'];
63
        if($uid === 'me')
64
        {
65
            $uid = $this->user->uid;
66
        }
67
        else if($uid !== $this->user->uid && $this->canRead($request) === false)
68
        {
69
            return $response->withStatus(401);
70
        }
71
        $dataTable = $this->getDataTable();
72
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
73
        $filter = $this->getFilterForPrimaryKey($uid);
74
        $areas = $dataTable->read($filter, $odata->select, $odata->top,
75
                                  $odata->skip, $odata->orderby);
76
        if(empty($areas))
77
        {
78
            return $response->withStatus(404);
79
        }
80
        return $response->withJson($areas[0]);
81
    }
82
}
83