FolderController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A listFoldersAction() 0 34 5
B createAction() 0 61 6
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
11
 * @version    API
12
 *
13
 * @file      FolderControler.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2025 Teampass.net
19
 *
20
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
21
 * ---
22
 *
23
 * @see       https://www.teampass.net
24
 */
25
26
use Symfony\Component\HttpFoundation\Request AS symfonyRequest;
27
28
class FolderController extends BaseController
29
{
30
31
    /**
32
     * Get list of Folders
33
     *
34
     * @return void
35
     */
36
    public function listFoldersAction(array $userData)
37
    {
38
        $request = symfonyRequest::createFromGlobals();
39
        $requestMethod = $request->getMethod();
40
        $strErrorDesc = $responseData = $strErrorHeader = '';
41
42
        if (strtoupper($requestMethod) === 'GET') {
43
            if (empty($userData['folders_list'])) {
44
                $this->sendOutput("", ['HTTP/1.1 204 No Content']);
45
            } else {
46
                try {
47
                    $folderModel = new FolderModel();
48
                    $arrFolders = $folderModel->getFoldersInfo(explode(",", $userData['folders_list']));
49
                    $responseData = json_encode($arrFolders);
50
                } catch (Error $e) {
51
                    $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.3';
52
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
53
                }
54
            }
55
        } else {
56
            $strErrorDesc = 'Method not supported';
57
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
58
        }
59
60
        // send output
61
        if (empty($strErrorDesc) === true) {
62
            $this->sendOutput(
63
                $responseData,
64
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
65
            );
66
        } else {
67
            $this->sendOutput(
68
                json_encode(['error' => $strErrorDesc]),
69
                ['Content-Type: application/json', $strErrorHeader]
70
            );
71
        }
72
    }
73
    //end listInFoldersAction()
74
75
    /**
76
     * create new folder
77
     *
78
     * @return void
79
     */
80
    public function createAction(array $userData)
81
    {
82
        $request = symfonyRequest::createFromGlobals();
83
        $requestMethod = $request->getMethod();
84
        $strErrorDesc = $responseData = $strErrorHeader = '';
85
86
        if (strtoupper($requestMethod) === 'POST') {
87
            if (empty($userData['folders_list'])) {
88
                $this->sendOutput("", ['HTTP/1.1 204 No Content']);
89
            } else {
90
                // Is user allowed to create a folder
91
                // We check if allowed_to_create
92
                if ((int) $userData['allowed_to_create'] !== 1) {
93
                    $strErrorDesc = 'User is not allowed to create a folder';
94
                    $strErrorHeader = 'HTTP/1.1 401 Unauthorized';
95
                } else {
96
                    // get parameters
97
                    $arrQueryStringParams = $this->getQueryStringParams();
98
                    try {
99
                        $folderModel = new FolderModel();
100
                        $arrFolder = $folderModel->createFolder(
101
                            (string) $arrQueryStringParams['title'],
102
                            (int) $arrQueryStringParams['parent_id'],
103
                            (int) $arrQueryStringParams['complexity'],
104
                            (int) $arrQueryStringParams['duration'],
105
                            (int) $arrQueryStringParams['create_auth_without'],
106
                            (int) $arrQueryStringParams['edit_auth_without'],
107
                            (string) $arrQueryStringParams['icon'],
108
                            (string) $arrQueryStringParams['icon_selected'],
109
                            (string) $arrQueryStringParams['access_rights'],
110
                            (int) $userData['is_admin'],
111
                            (array) explode(',', $userData['folders_list']),
112
                            (int) $userData['is_manager'],
113
                            (int) $userData['user_can_create_root_folder'],
114
                            (int) $userData['user_can_manage_all_users'],
115
                            (int) $userData['id'],
116
                            (string) $userData['roles'],
117
                        );
118
                        
119
                        $responseData = json_encode($arrFolder);
120
                    } catch (Error $e) {
121
                        $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.1';
122
                        $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
123
                    }
124
                }
125
            }
126
        } else {
127
            $strErrorDesc = 'Method not supported';
128
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
129
        }
130
131
        // send output
132
        if (empty($strErrorDesc) === true) {
133
            $this->sendOutput(
134
                $responseData,
135
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
136
            );
137
        } else {
138
            $this->sendOutput(
139
                json_encode(['error' => $strErrorDesc]),
140
                ['Content-Type: application/json', $strErrorHeader]
141
            );
142
        }
143
    }
144
    //end createFolderAction() 
145
}
146