Completed
Pull Request — master (#254)
by Blizzz
09:26
created

PublicController::returnAsJson()   D

Complexity

Conditions 9
Paths 21

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 61.1702

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 22
c 1
b 0
f 0
nc 21
nop 6
dl 0
loc 42
ccs 3
cts 22
cp 0.1364
crap 61.1702
rs 4.909
1
<?php
2
3
namespace OCA\Bookmarks\Controller\Rest;
4
5
use \OCP\AppFramework\ApiController;
6
use \OCP\IRequest;
7
use \OCP\IDb;
8
use \OCP\AppFramework\Http\JSONResponse;
9
use \OC\User\Manager;
10
use OCA\Bookmarks\Controller\Lib\Bookmarks;
11
use OCP\Util;
12
13
class PublicController extends ApiController {
14
15
	private $db;
16
	private $userManager;
17
18 3
	public function __construct($appName, IRequest $request, IDb $db, Manager $userManager) {
19 3
		parent::__construct(
20
				$appName, $request);
21
22 3
		$this->db = $db;
23 3
		$this->userManager = $userManager;
24 3
	}
25
26
	/**
27
	 * @param string $user
28
	 * @param string $password
29
	 * @param array $tags
30
	 * @param string $conjunction
31
	 * @param array $select
32
	 * @param string $sortby
33
	 * @return JSONResponse
34
	 *
35
	 * @CORS
36
	 * @NoAdminRequired
37
	 * @NoCSRFRequired
38
	 * @PublicPage
39
	 */
40 3
	public function returnAsJson($user, $password = null, $tags = array(), $conjunction = "or", $select = null, $sortby = "") {
41
42 3
		if ($user == null || $this->userManager->userExists($user) == false) {
43 3
			return $this->newJsonErrorMessage("User could not be identified");
44
		}
45
46
		if ($tags[0] == "") {
47
			$tags = array();
48
		}
49
50
		$public = true;
51
52
		if ($password != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $password of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
53
			$public = false;
54
		}
55
56
57
		if (!$public && !$this->userManager->checkPassword($user, $password)) {
58
59
			$msg = 'REST API accessed with wrong password';
60
			Util::writeLog('bookmarks', $msg, Util::WARN);
61
62
			return $this->newJsonErrorMessage("Wrong password for user " . $user);
63
		}
64
65
		$attributesToSelect = array('url', 'title');
66
67
		if ($select != null) {
68
			$attributesToSelect = array_merge($attributesToSelect, $select);
69
			$attributesToSelect = array_unique($attributesToSelect);
70
		}
71
72
		$output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction);
73
74
		if (count($output) == 0) {
75
			$output["status"] = 'error';
76
			$output["message"] = "No results from this query";
77
			return new JSONResponse($output);
78
		}
79
80
		return new JSONResponse($output);
81
	}
82
83
	/**
84
	 * @param string $message
85
	 * @return JSONResponse
86
	 */
87 3
	public function newJsonErrorMessage($message) {
88 3
		$output = array();
89 3
		$output["status"] = 'error';
90 3
		$output["message"] = $message;
91 3
		return new JSONResponse($output);
92
	}
93
94
}
95