Passed
Push — master ( 2b951a...bd06e7 )
by Nils
06:45
created

FolderController::writableFoldersAction()   B

Complexity

Conditions 8
Paths 42

Size

Total Lines 64
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 37
c 0
b 0
f 0
nc 42
nop 1
dl 0
loc 64
rs 8.0835

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /**
147
     * Get list of writable folders
148
     *
149
     * @return void
150
     */
151
    public function writableFoldersAction(array $userData)
152
    {
153
        $request = symfonyRequest::createFromGlobals();
154
        $requestMethod = $request->getMethod();
155
        $strErrorDesc = $responseData = $strErrorHeader = '';
156
157
        if (strtoupper($requestMethod) === 'GET') {
158
            try {
159
                $userFolders = !empty($userData['folders_list']) ? explode(',', $userData['folders_list']) : [];
160
                $rows = DB::query(
161
                    'SELECT nt.id AS folder_id, nt.title, nt.nlevel, nt.parent_id
162
                    FROM ' . prefixTable('nested_tree') . ' AS nt
163
                    LEFT JOIN ' . prefixTable('nested_tree') . ' AS personal 
164
                        ON personal.personal_folder = 1 
165
                        AND personal.title = %s
166
                    WHERE nt.id IN %li
167
                    AND (
168
                        nt.personal_folder = 0
169
                        OR (
170
                            personal.id IS NOT NULL
171
                            AND nt.nleft >= personal.nleft 
172
                            AND nt.nright <= personal.nright
173
                        )
174
                    )
175
                    GROUP BY nt.id, nt.title, nt.nlevel, nt.parent_id
176
                    ORDER BY nt.nlevel ASC, nt.title ASC',
177
                    $userData['id'],
178
                    $userFolders
179
                );
180
181
                $userId = (string) $userData['id'];
182
                $username = $userData['username'];
183
                $writableFolders = [];
184
                foreach ($rows as $row) {
185
                    $writableFolders[] = [
186
                        'id' => (int) $row['folder_id'],
187
                        'label' => $row['title'] === $userId ? $username : $row['title'],
188
                        'level' => (int) $row['nlevel'],
189
                        'parent_id' => (int) $row['parent_id'],
190
                        'first_position' => $row['title'] === $userId ? 1 : 0,
191
                    ];
192
                }
193
194
                $responseData = json_encode($writableFolders);
195
196
            } catch (Error $e) {
197
                $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.';
198
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
199
            }
200
        } else {
201
            $strErrorDesc = 'Method not supported';
202
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
203
        }
204
205
        // send output
206
        if (empty($strErrorDesc) === true) {
207
            $this->sendOutput(
208
                $responseData,
209
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
210
            );
211
        } else {
212
            $this->sendOutput(
213
                json_encode(['error' => $strErrorDesc]),
214
                ['Content-Type: application/json', $strErrorHeader]
215
            );
216
        }
217
    }
218
    //end writableFoldersAction()
219
}
220