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

PendingUserAPI::showPendingUser()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 3
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
class PendingUserAPI extends Http\Rest\RestAPI
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 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...
13
    {
14
        $user = $request->getAttribute('user');
15
        if($user === false)
16
        {
17
            throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
18
        }
19
        if(!$user->isInGroupNamed('LDAPAdmins'))
20
        {
21
            throw new Exception('Must be Admin', \Http\Rest\ACCESS_DENIED);
22
        }
23
    }
24
25
    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...
26
    {
27
        $this->validateIsAdmin($request);
28
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
29
        $auth = AuthProvider::getInstance();
30
        $users = $auth->getPendingUsersByFilter($odata->filter, $odata->select, $odata->top, $odata->skip, 
31
                                                $odata->orderby);
32
        return $response->withJson($users);
33
    }
34
35
    public function showPendingUser($request, $response, $args)
36
    {
37
        $this->validateIsAdmin($request);
38
        $user = \AuthProvider::getInstance()->getPendingUsersByFilter(new \Data\Filter("hash eq '".$args['hash']."'"));
39
        if($user === false)
40
        {
41
            return $response->withStatus(404);
42
        }
43
        if(!is_object($user) && isset($user[0]))
44
        {
45
            $user = $user[0];
46
        }
47
        return $response->withJson($user);
48
    }
49
50
    public function deletePendingUser($request, $response, $args)
51
    {
52
        $this->validateIsAdmin($request);
53
        $auth = \AuthProvider::getInstance();
54
        $res = $auth->deletePendingUsersByFilter(new \Data\Filter("hash eq '".$args['hash']."'"));
55
        return $response->withJson($res);
56
    }
57
58
    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...
59
    {
60
        $user = $request->getAttribute('user');
61
        if($user === false)
62
        {
63
            throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
64
        }
65
        $auth = \AuthProvider::getInstance();
66
        $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...
67
        if($user === false || !isset($user[0]))
68
        {
69
            return $app->withStatus(404);
0 ignored issues
show
Bug introduced by
The variable $app 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...
70
        }
71
        $res = $auth->activatePendingUser($user[0]);
72
        if($request->isGet())
73
        {
74
            $uri = '../../activate_error.php';
75
            if($res)
76
            {
77
                $uri = '../../';
78
            }
79
            return $response->withStatus(302)->withHeader('Location', $uri);
80
        }
81
        else
82
        {
83
            return $response->withJson($res);
84
        }
85
    }
86
}
87
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
88