Completed
Pull Request — develop (#51)
by Patrick
02:57
created

PendingUserAPI::validateIsAdmin()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
class PendingUserAPI extends ProfilesAdminAPI
3
{
4
    public function setup($app)
5
    {
6
        $app->get('[/]', array($this, 'listPendingUsers'));
7
        $app->get('/{hash}[/]', array($this, 'showPendingUser'));
8
        $app->delete('/{hash}[/]', array($this, 'deletePendingUser'));
9
        $app->map(['GET', 'POST'], '/{hash}/Actions/activate[/]', array($this, 'activatePendingUser'));
10
    }
11
12
    public function listPendingUsers($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
13
    {
14
        $this->validateIsAdmin($request);
15
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
16
        $auth = AuthProvider::getInstance();
17
        $users = $auth->getPendingUsersByFilter($odata->filter, $odata->select, $odata->top, $odata->skip, 
18
                                                $odata->orderby);
19
        return $response->withJson($users);
20
    }
21
22
    public function showPendingUser($request, $response, $args)
23
    {
24
        $this->validateIsAdmin($request);
25
        $user = \AuthProvider::getInstance()->getPendingUsersByFilter(new \Data\Filter("hash eq '".$args['hash']."'"));
26
        if($user === false)
27
        {
28
            return $response->withStatus(404);
29
        }
30
        if(!is_object($user) && isset($user[0]))
31
        {
32
            $user = $user[0];
33
        }
34
        return $response->withJson($user);
35
    }
36
37
    public function deletePendingUser($request, $response, $args)
38
    {
39
        $this->validateIsAdmin($request);
40
        $auth = \AuthProvider::getInstance();
41
        $res = $auth->deletePendingUsersByFilter(new \Data\Filter("hash eq '".$args['hash']."'"));
42
        return $response->withJson($res);
43
    }
44
45
    public function activatePendingUser($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        $user = $request->getAttribute('user');
48
        if($user === false)
49
        {
50
            throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
51
        }
52
        $auth = \AuthProvider::getInstance();
53
        $user = $auth->getPendingUsersByFilter(new \Data\Filter("hash eq '$hash'"));
0 ignored issues
show
Bug introduced by
The variable $hash does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
54
        if($user === false || !isset($user[0]))
55
        {
56
            return $response->withStatus(404);
57
        }
58
        $res = $auth->activatePendingUser($user[0]);
59
        if($request->isGet())
60
        {
61
            $uri = '../../activate_error.php';
62
            if($res)
63
            {
64
                $uri = '../../';
65
            }
66
            return $response->withStatus(302)->withHeader('Location', $uri);
67
        }
68
        else
69
        {
70
            return $response->withJson($res);
71
        }
72
    }
73
}
74
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
75