Failed Conditions
Pull Request — master (#90)
by Sander
04:01
created

controller/notebookapicontroller.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 *
6
 * @copyright Copyright (c) 2017, Sander Brand ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\Controller;
25
26
use OCA\NextNote\Db\Notebook;
27
use OCA\NextNote\Fixtures\ShareFix;
28
use OCA\NextNote\Service\NotebookService;
29
use OCA\NextNote\Service\NoteService;
30
use OCA\NextNote\ShareBackend\NextNoteShareBackend;
31
use OCA\NextNote\Utility\NotFoundJSONResponse;
32
use OCA\NextNote\Utility\UnauthorizedJSONResponse;
33
use OCA\NextNote\Utility\Utils;
34
use \OCP\AppFramework\ApiController;
35
use OCP\AppFramework\Http\JSONResponse;
36
use OCP\Constants;
37
use OCP\IConfig;
38
use OCP\ILogger;
39
use \OCP\IRequest;
40
use OCP\IUserManager;
41
use OCP\Share;
42
43
44
class NotebookApiController extends ApiController {
45
46
	private $config;
47
	private $notebookService;
48
	private $shareBackend;
49
	private $userManager;
50
51
	public function __construct($appName, IRequest $request,
52
								ILogger $logger, IConfig $config, NotebookService $notebookService, NextNoteShareBackend $shareBackend, IUserManager $userManager) {
53
		parent::__construct($appName, $request);
54
		$this->config = $config;
55
		$this->notebookService = $notebookService;
56
		$this->shareBackend = $shareBackend;
57
		$this->userManager = $userManager;
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 * @TODO Add etag / lastmodified
64
	 * @param int|bool $deleted
65
	 * @param int|null $notebook_id
66
	 * @return JSONResponse
67
	 * @internal param bool|string $group
68
	 */
69
	public function index($deleted = false, $notebook_id = null) {
70
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
71
		$results = $this->notebookService->find($notebook_id, $uid, $deleted);
72
73
		return new JSONResponse($results);
74
	}
75
76
	/**
77
	 * @NoAdminRequired
78
	 * @NoCSRFRequired
79
	 * @TODO Add etag / lastmodified
80
	 * @param $id
81
	 * @return NotFoundJSONResponse|JSONResponse
82
	 */
83
	public function get($id) {
84
		$result = $this->notebookService->find($id);
85
		if (!$result) {
86
			return new NotFoundJSONResponse();
87
		}
88
		//@todo Check access
89
		$result = $result->jsonSerialize();
90
		return new JSONResponse($result);
91
	}
92
93
94
	/**
95
	 * @NoAdminRequired
96
	 * @NoCSRFRequired
97
	 * @param $name
98
	 * @param $color
99
	 * @param $parent_id
100
	 * @return JSONResponse
101
	 */
102
	public function create($name, $color, $parent_id) {
103
		if ($name == "" || !$name) {
104
			return new JSONResponse(['error' => 'name is missing']);
105
		}
106
107
108
		$notebook = new Notebook();
109
		$notebook->setName($name);
110
		$notebook->setParentId($parent_id);
111
		$notebook->setColor($color);
112
		$notebook->setGuid(Utils::GUID());
113
		if($this->notebookService->findByName($name)){
114
			return new JSONResponse(['error' => 'Group already exists']);
115
		}
116
117
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
118
		$result = $this->notebookService->create($notebook, $uid)->jsonSerialize();
119
		\OC_Hook::emit('OCA\NextNote', 'post_create_notebook', ['notebook' => $group]);
0 ignored issues
show
The variable $group 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...
120
		return new JSONResponse($result);
121
	}
122
123
	/**
124
	 * @NoAdminRequired
125
	 * @NoCSRFRequired
126
	 * @param $id
127
	 * @param $name
128
	 * @param $color
129
	 * @param $parent_id
130
	 * @return NotFoundJSONResponse|UnauthorizedJSONResponse|JSONResponse
131
	 */
132
	public function update($id, $name, $color, $parent_id) {
133
		if ($name == "" || !$name) {
134
			return new JSONResponse(['error' => 'title is missing']);
135
		}
136
137
		//@TODO for sharing add access check
138
		$notebook = $this->notebookService->find($id);
139
		if (!$notebook) {
140
			return new NotFoundJSONResponse();
141
		}
142
143
		$notebook->setName($name);
144
		$notebook->setParentId($parent_id);
145
		$notebook->setColor($color);
146
147
		$results = $this->notebookService->update($notebook)->jsonSerialize();
148
		\OC_Hook::emit('OCA\NextNote', 'post_update_notebook', ['notebook' => $notebook]);
149
		return new JSONResponse($results);
150
	}
151
152
	/**
153
	 * @NoAdminRequired
154
	 * @NoCSRFRequired
155
	 * @param $id
156
	 * @return NotFoundJSONResponse|UnauthorizedJSONResponse|JSONResponse
157
	 */
158
	public function delete($id) {
159
		$entity = $this->notebookService->find($id);
160
		if (!$entity) {
161
			return new NotFoundJSONResponse();
162
		}
163
164
		$this->notebookService->delete($id);
165
		$result = (object)['success' => true];
166
		\OC_Hook::emit('OCA\NextNote', 'post_delete_notebook', ['notebook_id' => $id]);
167
		return new JSONResponse($result);
168
	}
169
}
170