Passed
Push — master ( bda737...e0ed8e )
by Nils
09:32
created

UserController::listAction()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 24
c 1
b 0
f 0
nc 20
nop 0
dl 0
loc 35
rs 8.9137
1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass API
11
 *
12
 * @file      UserControler.php
13
 * ---
14
 *
15
 * @author    Nils Laumaillé ([email protected])
16
 *
17
 * @copyright 2009-2022 Teampass.net
18
 *
19
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
20
 * ---
21
 *
22
 * @see       https://www.teampass.net
23
 */
24
class UserController extends BaseController
25
{
26
    /**
27
     * "/user/list" Endpoint - Get list of users
28
     */
29
    public function listAction()
30
    {
31
        $strErrorDesc = '';
32
        $requestMethod = $_SERVER["REQUEST_METHOD"];
33
        $arrQueryStringParams = $this->getQueryStringParams();
34
35
        if (strtoupper($requestMethod) == 'GET') {
36
            try {
37
                $userModel = new UserModel();
38
 
39
                $intLimit = 10;
40
                if (isset($arrQueryStringParams['limit']) && $arrQueryStringParams['limit']) {
41
                    $intLimit = $arrQueryStringParams['limit'];
42
                }
43
 
44
                $arrUsers = $userModel->getUsers($intLimit);
45
                $responseData = json_encode($arrUsers);
46
            } catch (Error $e) {
47
                $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.';
48
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
49
            }
50
        } else {
51
            $strErrorDesc = 'Method not supported';
52
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
53
        }
54
 
55
        // send output
56
        if (!$strErrorDesc) {
57
            $this->sendOutput(
58
                $responseData,
59
                array('Content-Type: application/json', 'HTTP/1.1 200 OK')
60
            );
61
        } else {
62
            $this->sendOutput(json_encode(array('error' => $strErrorDesc)), 
63
                array('Content-Type: application/json', $strErrorHeader)
64
            );
65
        }
66
    }
67
}