Completed
Push — master ( 1b93f9...7b1849 )
by
unknown
06:50
created

PublicController::newJsonErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
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
12
class PublicController extends ApiController {
13
14
	private $db;
15
	private $userManager;
16
17 3
	public function __construct($appName, IRequest $request, IDb $db, Manager $userManager) {
18 3
		parent::__construct(
19 3
				$appName, $request);
20
21 3
		$this->db = $db;
22 3
		$this->userManager = $userManager;
23 3
	}
24
25
	/**
26
	 * @CORS
27
	 * @NoAdminRequired
28
	 * @NoCSRFRequired
29
	 * @PublicPage
30
	 */
31 3
	public function returnAsJson($user, $password = null, $tags = array(), $conjunction = "or", $select = null, $sortby = "") {
32
33 3
		if ($user == null || $this->userManager->userExists($user) == false) {
34 3
			return $this->newJsonErrorMessage("User could not be identified");
35
		}
36
37
		if ($tags[0] == "") {
38
			$tags = array();
39
		}
40
41
		$public = true;
42
43
		if ($password != null) {
44
			$public = false;
45
		}
46
47
48
		if (!$public && !$this->userManager->checkPassword($user, $password)) {
49
50
			$msg = 'REST API accessed with wrong password';
51
			\OCP\Util::writeLog('bookmarks', $msg, \OCP\Util::WARN);
52
53
			return $this->newJsonErrorMessage("Wrong password for user " . $user);
54
		}
55
56
		$attributesToSelect = array('url', 'title');
57
58
		if ($select != null) {
59
			$attributesToSelect = array_merge($attributesToSelect, $select);
60
			$attributesToSelect = array_unique($attributesToSelect);
61
		}
62
63
		$output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction);
64
65
		if (count($output) == 0) {
66
			$output["status"] = 'error';
67
			$output["message"] = "No results from this query";
68
			return new JSONResponse($output);
69
		}
70
71
		return new JSONResponse($output);
72
	}
73
74 3
	public function newJsonErrorMessage($message) {
75 3
		$output = array();
76 3
		$output["status"] = 'error';
77 3
		$output["message"] = $message;
78 3
		return new JSONResponse($output);
79
	}
80
81
}
82