Completed
Push — master ( 87680b...8a04a7 )
by Blizzz
9s
created

PublicController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 35.9%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 86
ccs 14
cts 39
cp 0.359
rs 10
wmc 13
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C returnAsJson() 0 46 11
A newJsonErrorMessage() 0 6 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
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 3
				$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 (!is_array($tags)) {
47
			if(is_string($tags) && $tags !== '') {
48
				$tags = [ $tags ];
49
			} else {
50
				$tags = array();
51
			}
52
		}
53
54
		$public = true;
55
56
		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...
57
			$public = false;
58
		}
59
60
61
		if (!$public && !$this->userManager->checkPassword($user, $password)) {
62
63
			$msg = 'REST API accessed with wrong password';
64
			Util::writeLog('bookmarks', $msg, Util::WARN);
65
66
			return $this->newJsonErrorMessage("Wrong password for user " . $user);
67
		}
68
69
		$attributesToSelect = array('url', 'title');
70
71
		if ($select != null) {
72
			$attributesToSelect = array_merge($attributesToSelect, $select);
73
			$attributesToSelect = array_unique($attributesToSelect);
74
		}
75
76
		$output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction);
77
78
		if (count($output) == 0) {
79
			$output["status"] = 'error';
80
			$output["message"] = "No results from this query";
81
			return new JSONResponse($output);
82
		}
83
84
		return new JSONResponse($output);
85
	}
86
87
	/**
88
	 * @param string $message
89
	 * @return JSONResponse
90
	 */
91 3
	public function newJsonErrorMessage($message) {
92 3
		$output = array();
93 3
		$output["status"] = 'error';
94 3
		$output["message"] = $message;
95 3
		return new JSONResponse($output);
96
	}
97
98
}
99