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

PublicController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 40.63%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 82
ccs 13
cts 32
cp 0.4063
rs 10
wmc 11
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A newJsonErrorMessage() 0 6 1
A __construct() 0 7 1
D returnAsJson() 0 42 9
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