Completed
Pull Request — master (#55)
by Sander
01:51
created

NextNoteShareBackend::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 * @copyright Copyright (c) 2015, Ben Curtis <[email protected]>
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\ShareBackend;
25
26
use OC\Share\Share;
27
use OCA\NextNote\Db\NextNote;
28
use OCA\NextNote\Fixtures\ShareFix;
29
use \OCP\Share_Backend;
30
31
class NextNoteShareBackend implements Share_Backend {
32
33
	private $db;
34
	public function __construct() {
35
		$this->db = \OC::$server->getDatabaseConnection();
36
	}
37
38
	public function getSharedNotes(){
39
		return Share::getItemsSharedWith('nextnote', 'populated_shares');
40
	}
41
42
	/**
43
	 * Check if this $itemSource exist for the user
44
	 * @param string $itemSource
45
	 * @param string $uidOwner Owner of the item
46
	 * @return boolean|null Source
47
	 *
48
	 * Return false if the item does not exist for the user
49
	 * @since 5.0.0
50
	 */
51
	public function isValidSource($itemSource, $uidOwner) {
52
		// todo: real test
53
		// id => 1, has admin 
54
		// has owner this note?
55
		return true;
56
	}
57
	
58
	/**
59
	 * Get a unique name of the item for the specified user
60
	 * @param string $itemSource
61
	 * @param string|false $shareWith User the item is being shared with
62
	 * @param array|null $exclude List of similar item names already existing as shared items @deprecated since version OC7
63
	 * @return string Target name
64
	 *
65
	 * This function needs to verify that the user does not already have an item with this name.
66
	 * If it does generate a new name e.g. name_#
67
	 * @since 5.0.0
68
	 */
69
	public function generateTarget($itemSource, $shareWith, $exclude = null) {
70
		// note id (should be unique)
71
		return $itemSource;
72
	}
73
74
	/**
75
	 * Converts the shared item sources back into the item in the specified format
76
	 * @param array $items Shared items
77
	 * @param int|string $format
78
	 * @return array
79
	 *
80
	 * The items array is a 3-dimensional array with the item_source as the
81
	 * first key and the share id as the second key to an array with the share
82
	 * info.
83
	 *
84
	 * The key/value pairs included in the share info depend on the function originally called:
85
	 * If called by getItem(s)Shared: id, item_type, item, item_source,
86
	 * share_type, share_with, permissions, stime, file_source
87
	 *
88
	 * If called by getItem(s)SharedWith: id, item_type, item, item_source,
89
	 * item_target, share_type, share_with, permissions, stime, file_source,
90
	 * file_target
91
	 *
92
	 * This function allows the backend to control the output of shared items with custom formats.
93
	 * It is only called through calls to the public getItem(s)Shared(With) functions.
94
	 * @since 5.0.0
95
	 */
96
	public function formatItems($items, $format, $parameters = null) {
97
98
		// get the ownnote ids
99
		$ids = Array();
100
		foreach($items as $item) {
101
			$ids[] = $item['item_source'];
102
		}
103
104
		// get notes from database
105
		$select_clause = "SELECT id, uid, name, grouping, mtime, deleted FROM *PREFIX*nextnote WHERE id in (";
106
		$select_clause .= implode(',', $ids);
107
		$select_clause .= ") ORDER BY id";
108
		$q = $this->db->executeQuery($select_clause, array());
109
		//$query = \OCP\DB::prepare($select_clause);
110
		$results = $q->fetchAll();
111
112
		// add permissions to items
113
		if (is_string($format) && $format === 'populated_shares') {
114
			$full_items = Array();
115
			foreach($results as $index => $result) {
116
				$full_items[] = array_merge($items[$index], $result);
117
			}
118
			$results = $full_items;
119
		}
120
121
		return $results;
122
	}
123
	
124
	/**
125
	 * Check if a given share type is allowd by the back-end
126
	 *
127
	 * @param int $shareType share type
128
	 * @return boolean
129
	 *
130
	 * The back-end can enable/disable specific share types. Just return true if
131
	 * the back-end doesn't provide any specific settings for it and want to allow
132
	 * all share types defined by the share API
133
	 * @since 8.0.0
134
	 */
135
	public function isShareTypeAllowed($shareType) {
136
		return true;
137
	}
138
139
	/**
140
	 * Check if the current user has the requested permission.
141
	 * For permissions
142
	 * @see \OCP\Constants
143
	 * @param $permission
144
	 * @param $note NextNote
145
	 * @return bool|int
146
	 */
147
	public function checkPermissions($permission, $note) {
148
		// gather information
149
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
150
		// owner is allowed to do everything
151
		if ($uid === $note->getUid()) {
152
			return true;
153
		}
154
155
		// check share permissions
156
		$gotPerm = ShareFix::getPermissions('nextnote', $note->getId(), $uid);
157
158
		return $gotPerm & $permission;
159
	}
160
}