Completed
Push — stable10 ( d1b390...0bd063 )
by Lukas
27:03 queued 26:40
created
apps/dav/lib/CardDAV/Plugin.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -30,51 +30,51 @@
 block discarded – undo
30 30
 
31 31
 class Plugin extends \Sabre\CardDAV\Plugin {
32 32
 
33
-	function initialize(Server $server) {
34
-		$server->on('propFind', [$this, 'propFind']);
35
-		parent::initialize($server);
36
-	}
33
+    function initialize(Server $server) {
34
+        $server->on('propFind', [$this, 'propFind']);
35
+        parent::initialize($server);
36
+    }
37 37
 
38
-	/**
39
-	 * Returns the addressbook home for a given principal
40
-	 *
41
-	 * @param string $principal
42
-	 * @return string
43
-	 */
44
-	protected function getAddressbookHomeForPrincipal($principal) {
38
+    /**
39
+     * Returns the addressbook home for a given principal
40
+     *
41
+     * @param string $principal
42
+     * @return string
43
+     */
44
+    protected function getAddressbookHomeForPrincipal($principal) {
45 45
 
46
-		if (strrpos($principal, 'principals/users', -strlen($principal)) !== false) {
47
-			list(, $principalId) = URLUtil::splitPath($principal);
48
-			return self::ADDRESSBOOK_ROOT . '/users/' . $principalId;
49
-		}
50
-		if (strrpos($principal, 'principals/groups', -strlen($principal)) !== false) {
51
-			list(, $principalId) = URLUtil::splitPath($principal);
52
-			return self::ADDRESSBOOK_ROOT . '/groups/' . $principalId;
53
-		}
54
-		if (strrpos($principal, 'principals/system', -strlen($principal)) !== false) {
55
-			list(, $principalId) = URLUtil::splitPath($principal);
56
-			return self::ADDRESSBOOK_ROOT . '/system/' . $principalId;
57
-		}
46
+        if (strrpos($principal, 'principals/users', -strlen($principal)) !== false) {
47
+            list(, $principalId) = URLUtil::splitPath($principal);
48
+            return self::ADDRESSBOOK_ROOT . '/users/' . $principalId;
49
+        }
50
+        if (strrpos($principal, 'principals/groups', -strlen($principal)) !== false) {
51
+            list(, $principalId) = URLUtil::splitPath($principal);
52
+            return self::ADDRESSBOOK_ROOT . '/groups/' . $principalId;
53
+        }
54
+        if (strrpos($principal, 'principals/system', -strlen($principal)) !== false) {
55
+            list(, $principalId) = URLUtil::splitPath($principal);
56
+            return self::ADDRESSBOOK_ROOT . '/system/' . $principalId;
57
+        }
58 58
 
59
-		throw new \LogicException('This is not supposed to happen');
60
-	}
59
+        throw new \LogicException('This is not supposed to happen');
60
+    }
61 61
 
62
-	/**
63
-	 * Adds all CardDAV-specific properties
64
-	 *
65
-	 * @param PropFind $propFind
66
-	 * @param INode $node
67
-	 * @return void
68
-	 */
69
-	function propFind(PropFind $propFind, INode $node) {
62
+    /**
63
+     * Adds all CardDAV-specific properties
64
+     *
65
+     * @param PropFind $propFind
66
+     * @param INode $node
67
+     * @return void
68
+     */
69
+    function propFind(PropFind $propFind, INode $node) {
70 70
 
71
-		$ns = '{http://owncloud.org/ns}';
71
+        $ns = '{http://owncloud.org/ns}';
72 72
 
73
-		if ($node instanceof AddressBook) {
73
+        if ($node instanceof AddressBook) {
74 74
 
75
-			$propFind->handle($ns . 'groups', function () use ($node) {
76
-				return new Groups($node->getContactsGroups());
77
-			});
78
-		}
79
-	}
75
+            $propFind->handle($ns . 'groups', function () use ($node) {
76
+                return new Groups($node->getContactsGroups());
77
+            });
78
+        }
79
+    }
80 80
 }
Please login to merge, or discard this patch.
apps/dav/lib/CardDAV/ImageExportPlugin.php 1 patch
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -34,136 +34,136 @@
 block discarded – undo
34 34
 
35 35
 class ImageExportPlugin extends ServerPlugin {
36 36
 
37
-	/** @var Server */
38
-	protected $server;
39
-	/** @var ILogger */
40
-	private $logger;
41
-
42
-	public function __construct(ILogger $logger) {
43
-		$this->logger = $logger;
44
-	}
45
-
46
-	/**
47
-	 * Initializes the plugin and registers event handlers
48
-	 *
49
-	 * @param Server $server
50
-	 * @return void
51
-	 */
52
-	function initialize(Server $server) {
53
-
54
-		$this->server = $server;
55
-		$this->server->on('method:GET', [$this, 'httpGet'], 90);
56
-	}
57
-
58
-	/**
59
-	 * Intercepts GET requests on addressbook urls ending with ?photo.
60
-	 *
61
-	 * @param RequestInterface $request
62
-	 * @param ResponseInterface $response
63
-	 * @return bool|void
64
-	 */
65
-	function httpGet(RequestInterface $request, ResponseInterface $response) {
66
-
67
-		$queryParams = $request->getQueryParameters();
68
-		// TODO: in addition to photo we should also add logo some point in time
69
-		if (!array_key_exists('photo', $queryParams)) {
70
-			return true;
71
-		}
72
-
73
-		$path = $request->getPath();
74
-		$node = $this->server->tree->getNodeForPath($path);
75
-
76
-		if (!($node instanceof Card)) {
77
-			return true;
78
-		}
79
-
80
-		$this->server->transactionType = 'carddav-image-export';
81
-
82
-		// Checking ACL, if available.
83
-		if ($aclPlugin = $this->server->getPlugin('acl')) {
84
-			/** @var \Sabre\DAVACL\Plugin $aclPlugin */
85
-			$aclPlugin->checkPrivileges($path, '{DAV:}read');
86
-		}
87
-
88
-		if ($result = $this->getPhoto($node)) {
89
-			$response->setHeader('Content-Type', $result['Content-Type']);
90
-			$response->setHeader('Content-Disposition', 'attachment');
91
-			$response->setStatus(200);
92
-
93
-			$response->setBody($result['body']);
94
-
95
-			// Returning false to break the event chain
96
-			return false;
97
-		}
98
-		return true;
99
-	}
100
-
101
-	function getPhoto(Card $node) {
102
-		// TODO: this is kind of expensive - load carddav data from database and parse it
103
-		//       we might want to build up a cache one day
104
-		try {
105
-			$vObject = $this->readCard($node->get());
106
-			if (!$vObject->PHOTO) {
107
-				return false;
108
-			}
109
-
110
-			$photo = $vObject->PHOTO;
111
-			$type = $this->getType($photo);
112
-
113
-			$val = $photo->getValue();
114
-			if ($photo->getValueType() === 'URI') {
115
-				$parsed = \Sabre\URI\parse($val);
116
-				//only allow data://
117
-				if ($parsed['scheme'] !== 'data') {
118
-					return false;
119
-				}
120
-				if (substr_count($parsed['path'], ';') === 1) {
121
-					list($type,) = explode(';', $parsed['path']);
122
-				}
123
-				$val = file_get_contents($val);
124
-			}
125
-
126
-			$allowedContentTypes = [
127
-				'image/png',
128
-				'image/jpeg',
129
-				'image/gif',
130
-			];
131
-
132
-			if(!in_array($type, $allowedContentTypes, true)) {
133
-				$type = 'application/octet-stream';
134
-			}
135
-
136
-			return [
137
-				'Content-Type' => $type,
138
-				'body' => $val
139
-			];
140
-		} catch(\Exception $ex) {
141
-			$this->logger->logException($ex);
142
-		}
143
-		return false;
144
-	}
145
-
146
-	private function readCard($cardData) {
147
-		return Reader::read($cardData);
148
-	}
149
-
150
-	/**
151
-	 * @param Binary $photo
152
-	 * @return Parameter
153
-	 */
154
-	private function getType($photo) {
155
-		$params = $photo->parameters();
156
-		if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) {
157
-			/** @var Parameter $typeParam */
158
-			$typeParam = isset($params['TYPE']) ? $params['TYPE'] : $params['MEDIATYPE'];
159
-			$type = $typeParam->getValue();
160
-
161
-			if (strpos($type, 'image/') === 0) {
162
-				return $type;
163
-			} else {
164
-				return 'image/' . strtolower($type);
165
-			}
166
-		}
167
-		return '';
168
-	}
37
+    /** @var Server */
38
+    protected $server;
39
+    /** @var ILogger */
40
+    private $logger;
41
+
42
+    public function __construct(ILogger $logger) {
43
+        $this->logger = $logger;
44
+    }
45
+
46
+    /**
47
+     * Initializes the plugin and registers event handlers
48
+     *
49
+     * @param Server $server
50
+     * @return void
51
+     */
52
+    function initialize(Server $server) {
53
+
54
+        $this->server = $server;
55
+        $this->server->on('method:GET', [$this, 'httpGet'], 90);
56
+    }
57
+
58
+    /**
59
+     * Intercepts GET requests on addressbook urls ending with ?photo.
60
+     *
61
+     * @param RequestInterface $request
62
+     * @param ResponseInterface $response
63
+     * @return bool|void
64
+     */
65
+    function httpGet(RequestInterface $request, ResponseInterface $response) {
66
+
67
+        $queryParams = $request->getQueryParameters();
68
+        // TODO: in addition to photo we should also add logo some point in time
69
+        if (!array_key_exists('photo', $queryParams)) {
70
+            return true;
71
+        }
72
+
73
+        $path = $request->getPath();
74
+        $node = $this->server->tree->getNodeForPath($path);
75
+
76
+        if (!($node instanceof Card)) {
77
+            return true;
78
+        }
79
+
80
+        $this->server->transactionType = 'carddav-image-export';
81
+
82
+        // Checking ACL, if available.
83
+        if ($aclPlugin = $this->server->getPlugin('acl')) {
84
+            /** @var \Sabre\DAVACL\Plugin $aclPlugin */
85
+            $aclPlugin->checkPrivileges($path, '{DAV:}read');
86
+        }
87
+
88
+        if ($result = $this->getPhoto($node)) {
89
+            $response->setHeader('Content-Type', $result['Content-Type']);
90
+            $response->setHeader('Content-Disposition', 'attachment');
91
+            $response->setStatus(200);
92
+
93
+            $response->setBody($result['body']);
94
+
95
+            // Returning false to break the event chain
96
+            return false;
97
+        }
98
+        return true;
99
+    }
100
+
101
+    function getPhoto(Card $node) {
102
+        // TODO: this is kind of expensive - load carddav data from database and parse it
103
+        //       we might want to build up a cache one day
104
+        try {
105
+            $vObject = $this->readCard($node->get());
106
+            if (!$vObject->PHOTO) {
107
+                return false;
108
+            }
109
+
110
+            $photo = $vObject->PHOTO;
111
+            $type = $this->getType($photo);
112
+
113
+            $val = $photo->getValue();
114
+            if ($photo->getValueType() === 'URI') {
115
+                $parsed = \Sabre\URI\parse($val);
116
+                //only allow data://
117
+                if ($parsed['scheme'] !== 'data') {
118
+                    return false;
119
+                }
120
+                if (substr_count($parsed['path'], ';') === 1) {
121
+                    list($type,) = explode(';', $parsed['path']);
122
+                }
123
+                $val = file_get_contents($val);
124
+            }
125
+
126
+            $allowedContentTypes = [
127
+                'image/png',
128
+                'image/jpeg',
129
+                'image/gif',
130
+            ];
131
+
132
+            if(!in_array($type, $allowedContentTypes, true)) {
133
+                $type = 'application/octet-stream';
134
+            }
135
+
136
+            return [
137
+                'Content-Type' => $type,
138
+                'body' => $val
139
+            ];
140
+        } catch(\Exception $ex) {
141
+            $this->logger->logException($ex);
142
+        }
143
+        return false;
144
+    }
145
+
146
+    private function readCard($cardData) {
147
+        return Reader::read($cardData);
148
+    }
149
+
150
+    /**
151
+     * @param Binary $photo
152
+     * @return Parameter
153
+     */
154
+    private function getType($photo) {
155
+        $params = $photo->parameters();
156
+        if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) {
157
+            /** @var Parameter $typeParam */
158
+            $typeParam = isset($params['TYPE']) ? $params['TYPE'] : $params['MEDIATYPE'];
159
+            $type = $typeParam->getValue();
160
+
161
+            if (strpos($type, 'image/') === 0) {
162
+                return $type;
163
+            } else {
164
+                return 'image/' . strtolower($type);
165
+            }
166
+        }
167
+        return '';
168
+    }
169 169
 }
Please login to merge, or discard this patch.
apps/dav/lib/CardDAV/SyncJob.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -28,15 +28,15 @@
 block discarded – undo
28 28
 
29 29
 class SyncJob extends TimedJob {
30 30
 
31
-	public function __construct() {
32
-		// Run once a day
33
-		$this->setInterval(24 * 60 * 60);
34
-	}
31
+    public function __construct() {
32
+        // Run once a day
33
+        $this->setInterval(24 * 60 * 60);
34
+    }
35 35
 
36
-	protected function run($argument) {
37
-		$app = new Application();
38
-		/** @var SyncService $ss */
39
-		$ss = $app->getSyncService();
40
-		$ss->syncInstance();
41
-	}
36
+    protected function run($argument) {
37
+        $app = new Application();
38
+        /** @var SyncService $ss */
39
+        $ss = $app->getSyncService();
40
+        $ss->syncInstance();
41
+    }
42 42
 }
Please login to merge, or discard this patch.
apps/dav/lib/SystemTag/SystemTagsByIdCollection.php 1 patch
Indentation   +140 added lines, -140 removed lines patch added patch discarded remove patch
@@ -37,144 +37,144 @@
 block discarded – undo
37 37
 
38 38
 class SystemTagsByIdCollection implements ICollection {
39 39
 
40
-	/**
41
-	 * @var ISystemTagManager
42
-	 */
43
-	private $tagManager;
44
-
45
-	/**
46
-	 * @var IGroupManager
47
-	 */
48
-	private $groupManager;
49
-
50
-	/**
51
-	 * @var IUserSession
52
-	 */
53
-	private $userSession;
54
-
55
-	/**
56
-	 * SystemTagsByIdCollection constructor.
57
-	 *
58
-	 * @param ISystemTagManager $tagManager
59
-	 * @param IUserSession $userSession
60
-	 * @param IGroupManager $groupManager
61
-	 */
62
-	public function __construct(
63
-		ISystemTagManager $tagManager,
64
-		IUserSession $userSession,
65
-		IGroupManager $groupManager
66
-	) {
67
-		$this->tagManager = $tagManager;
68
-		$this->userSession = $userSession;
69
-		$this->groupManager = $groupManager;
70
-	}
71
-
72
-	/**
73
-	 * Returns whether the currently logged in user is an administrator
74
-	 *
75
-	 * @return bool true if the user is an admin
76
-	 */
77
-	private function isAdmin() {
78
-		$user = $this->userSession->getUser();
79
-		if ($user !== null) {
80
-			return $this->groupManager->isAdmin($user->getUID());
81
-		}
82
-		return false;
83
-	}
84
-
85
-	/**
86
-	 * @param string $name
87
-	 * @param resource|string $data Initial payload
88
-	 * @throws Forbidden
89
-	 */
90
-	function createFile($name, $data = null) {
91
-		throw new Forbidden('Cannot create tags by id');
92
-	}
93
-
94
-	/**
95
-	 * @param string $name
96
-	 */
97
-	function createDirectory($name) {
98
-		throw new Forbidden('Permission denied to create collections');
99
-	}
100
-
101
-	/**
102
-	 * @param string $name
103
-	 */
104
-	function getChild($name) {
105
-		try {
106
-			$tag = $this->tagManager->getTagsByIds([$name]);
107
-			$tag = current($tag);
108
-			if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) {
109
-				throw new NotFound('Tag with id ' . $name . ' not found');
110
-			}
111
-			return $this->makeNode($tag);
112
-		} catch (\InvalidArgumentException $e) {
113
-			throw new BadRequest('Invalid tag id', 0, $e);
114
-		} catch (TagNotFoundException $e) {
115
-			throw new NotFound('Tag with id ' . $name . ' not found', 0, $e);
116
-		}
117
-	}
118
-
119
-	function getChildren() {
120
-		$visibilityFilter = true;
121
-		if ($this->isAdmin()) {
122
-			$visibilityFilter = null;
123
-		}
124
-
125
-		$tags = $this->tagManager->getAllTags($visibilityFilter);
126
-		return array_map(function($tag) {
127
-			return $this->makeNode($tag);
128
-		}, $tags);
129
-	}
130
-
131
-	/**
132
-	 * @param string $name
133
-	 */
134
-	function childExists($name) {
135
-		try {
136
-			$tag = $this->tagManager->getTagsByIds([$name]);
137
-			$tag = current($tag);
138
-			if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) {
139
-				return false;
140
-			}
141
-			return true;
142
-		} catch (\InvalidArgumentException $e) {
143
-			throw new BadRequest('Invalid tag id', 0, $e);
144
-		} catch (TagNotFoundException $e) {
145
-			return false;
146
-		}
147
-	}
148
-
149
-	function delete() {
150
-		throw new Forbidden('Permission denied to delete this collection');
151
-	}
152
-
153
-	function getName() {
154
-		return 'systemtags';
155
-	}
156
-
157
-	function setName($name) {
158
-		throw new Forbidden('Permission denied to rename this collection');
159
-	}
160
-
161
-	/**
162
-	 * Returns the last modification time, as a unix timestamp
163
-	 *
164
-	 * @return int
165
-	 */
166
-	function getLastModified() {
167
-		return null;
168
-	}
169
-
170
-	/**
171
-	 * Create a sabre node for the given system tag
172
-	 *
173
-	 * @param ISystemTag $tag
174
-	 *
175
-	 * @return SystemTagNode
176
-	 */
177
-	private function makeNode(ISystemTag $tag) {
178
-		return new SystemTagNode($tag, $this->userSession->getUser(), $this->isAdmin(), $this->tagManager);
179
-	}
40
+    /**
41
+     * @var ISystemTagManager
42
+     */
43
+    private $tagManager;
44
+
45
+    /**
46
+     * @var IGroupManager
47
+     */
48
+    private $groupManager;
49
+
50
+    /**
51
+     * @var IUserSession
52
+     */
53
+    private $userSession;
54
+
55
+    /**
56
+     * SystemTagsByIdCollection constructor.
57
+     *
58
+     * @param ISystemTagManager $tagManager
59
+     * @param IUserSession $userSession
60
+     * @param IGroupManager $groupManager
61
+     */
62
+    public function __construct(
63
+        ISystemTagManager $tagManager,
64
+        IUserSession $userSession,
65
+        IGroupManager $groupManager
66
+    ) {
67
+        $this->tagManager = $tagManager;
68
+        $this->userSession = $userSession;
69
+        $this->groupManager = $groupManager;
70
+    }
71
+
72
+    /**
73
+     * Returns whether the currently logged in user is an administrator
74
+     *
75
+     * @return bool true if the user is an admin
76
+     */
77
+    private function isAdmin() {
78
+        $user = $this->userSession->getUser();
79
+        if ($user !== null) {
80
+            return $this->groupManager->isAdmin($user->getUID());
81
+        }
82
+        return false;
83
+    }
84
+
85
+    /**
86
+     * @param string $name
87
+     * @param resource|string $data Initial payload
88
+     * @throws Forbidden
89
+     */
90
+    function createFile($name, $data = null) {
91
+        throw new Forbidden('Cannot create tags by id');
92
+    }
93
+
94
+    /**
95
+     * @param string $name
96
+     */
97
+    function createDirectory($name) {
98
+        throw new Forbidden('Permission denied to create collections');
99
+    }
100
+
101
+    /**
102
+     * @param string $name
103
+     */
104
+    function getChild($name) {
105
+        try {
106
+            $tag = $this->tagManager->getTagsByIds([$name]);
107
+            $tag = current($tag);
108
+            if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) {
109
+                throw new NotFound('Tag with id ' . $name . ' not found');
110
+            }
111
+            return $this->makeNode($tag);
112
+        } catch (\InvalidArgumentException $e) {
113
+            throw new BadRequest('Invalid tag id', 0, $e);
114
+        } catch (TagNotFoundException $e) {
115
+            throw new NotFound('Tag with id ' . $name . ' not found', 0, $e);
116
+        }
117
+    }
118
+
119
+    function getChildren() {
120
+        $visibilityFilter = true;
121
+        if ($this->isAdmin()) {
122
+            $visibilityFilter = null;
123
+        }
124
+
125
+        $tags = $this->tagManager->getAllTags($visibilityFilter);
126
+        return array_map(function($tag) {
127
+            return $this->makeNode($tag);
128
+        }, $tags);
129
+    }
130
+
131
+    /**
132
+     * @param string $name
133
+     */
134
+    function childExists($name) {
135
+        try {
136
+            $tag = $this->tagManager->getTagsByIds([$name]);
137
+            $tag = current($tag);
138
+            if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) {
139
+                return false;
140
+            }
141
+            return true;
142
+        } catch (\InvalidArgumentException $e) {
143
+            throw new BadRequest('Invalid tag id', 0, $e);
144
+        } catch (TagNotFoundException $e) {
145
+            return false;
146
+        }
147
+    }
148
+
149
+    function delete() {
150
+        throw new Forbidden('Permission denied to delete this collection');
151
+    }
152
+
153
+    function getName() {
154
+        return 'systemtags';
155
+    }
156
+
157
+    function setName($name) {
158
+        throw new Forbidden('Permission denied to rename this collection');
159
+    }
160
+
161
+    /**
162
+     * Returns the last modification time, as a unix timestamp
163
+     *
164
+     * @return int
165
+     */
166
+    function getLastModified() {
167
+        return null;
168
+    }
169
+
170
+    /**
171
+     * Create a sabre node for the given system tag
172
+     *
173
+     * @param ISystemTag $tag
174
+     *
175
+     * @return SystemTagNode
176
+     */
177
+    private function makeNode(ISystemTag $tag) {
178
+        return new SystemTagNode($tag, $this->userSession->getUser(), $this->isAdmin(), $this->tagManager);
179
+    }
180 180
 }
Please login to merge, or discard this patch.
apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php 1 patch
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -39,136 +39,136 @@
 block discarded – undo
39 39
  */
40 40
 class SystemTagsObjectTypeCollection implements ICollection {
41 41
 
42
-	/**
43
-	 * @var string
44
-	 */
45
-	private $objectType;
46
-
47
-	/**
48
-	 * @var ISystemTagManager
49
-	 */
50
-	private $tagManager;
51
-
52
-	/**
53
-	 * @var ISystemTagObjectMapper
54
-	 */
55
-	private $tagMapper;
56
-
57
-	/**
58
-	 * @var IGroupManager
59
-	 */
60
-	private $groupManager;
61
-
62
-	/**
63
-	 * @var IUserSession
64
-	 */
65
-	private $userSession;
66
-
67
-	/**
68
-	 * @var \Closure
69
-	 **/
70
-	protected $childExistsFunction;
71
-
72
-	/**
73
-	 * Constructor
74
-	 *
75
-	 * @param string $objectType object type
76
-	 * @param ISystemTagManager $tagManager
77
-	 * @param ISystemTagObjectMapper $tagMapper
78
-	 * @param IUserSession $userSession
79
-	 * @param IGroupManager $groupManager
80
-	 * @param \Closure $childExistsFunction
81
-	 */
82
-	public function __construct(
83
-		$objectType, 
84
-		ISystemTagManager $tagManager,
85
-		ISystemTagObjectMapper $tagMapper,
86
-		IUserSession $userSession,
87
-		IGroupManager $groupManager,
88
-		\Closure $childExistsFunction
89
-	) {
90
-		$this->tagManager = $tagManager;
91
-		$this->tagMapper = $tagMapper;
92
-		$this->objectType = $objectType;
93
-		$this->userSession = $userSession;
94
-		$this->groupManager = $groupManager;
95
-		$this->childExistsFunction = $childExistsFunction;
96
-	}
97
-
98
-	/**
99
-	 * @param string $name
100
-	 * @param resource|string $data Initial payload
101
-	 * @return null|string
102
-	 * @throws Forbidden
103
-	 */
104
-	function createFile($name, $data = null) {
105
-		throw new Forbidden('Permission denied to create nodes');
106
-	}
107
-
108
-	/**
109
-	 * @param string $name
110
-	 * @throws Forbidden
111
-	 */
112
-	function createDirectory($name) {
113
-		throw new Forbidden('Permission denied to create collections');
114
-	}
115
-
116
-	/**
117
-	 * @param string $objectId
118
-	 * @return SystemTagsObjectMappingCollection
119
-	 * @throws NotFound
120
-	 */
121
-	function getChild($objectId) {
122
-		// make sure the object exists and is reachable
123
-		if(!$this->childExists($objectId)) {
124
-			throw new NotFound('Entity does not exist or is not available');
125
-		}
126
-		return new SystemTagsObjectMappingCollection(
127
-			$objectId,
128
-			$this->objectType,
129
-			$this->userSession->getUser(),
130
-			$this->tagManager,
131
-			$this->tagMapper
132
-		);
133
-	}
134
-
135
-	function getChildren() {
136
-		// do not list object ids
137
-		throw new MethodNotAllowed();
138
-	}
139
-
140
-	/**
141
-	 * Checks if a child-node with the specified name exists
142
-	 *
143
-	 * @param string $name
144
-	 * @return bool
145
-	 */
146
-	function childExists($name) {
147
-		return call_user_func($this->childExistsFunction, $name);
148
-	}
149
-
150
-	function delete() {
151
-		throw new Forbidden('Permission denied to delete this collection');
152
-	}
153
-
154
-	function getName() {
155
-		return $this->objectType;
156
-	}
157
-
158
-	/**
159
-	 * @param string $name
160
-	 * @throws Forbidden
161
-	 */
162
-	function setName($name) {
163
-		throw new Forbidden('Permission denied to rename this collection');
164
-	}
165
-
166
-	/**
167
-	 * Returns the last modification time, as a unix timestamp
168
-	 *
169
-	 * @return int
170
-	 */
171
-	function getLastModified() {
172
-		return null;
173
-	}
42
+    /**
43
+     * @var string
44
+     */
45
+    private $objectType;
46
+
47
+    /**
48
+     * @var ISystemTagManager
49
+     */
50
+    private $tagManager;
51
+
52
+    /**
53
+     * @var ISystemTagObjectMapper
54
+     */
55
+    private $tagMapper;
56
+
57
+    /**
58
+     * @var IGroupManager
59
+     */
60
+    private $groupManager;
61
+
62
+    /**
63
+     * @var IUserSession
64
+     */
65
+    private $userSession;
66
+
67
+    /**
68
+     * @var \Closure
69
+     **/
70
+    protected $childExistsFunction;
71
+
72
+    /**
73
+     * Constructor
74
+     *
75
+     * @param string $objectType object type
76
+     * @param ISystemTagManager $tagManager
77
+     * @param ISystemTagObjectMapper $tagMapper
78
+     * @param IUserSession $userSession
79
+     * @param IGroupManager $groupManager
80
+     * @param \Closure $childExistsFunction
81
+     */
82
+    public function __construct(
83
+        $objectType, 
84
+        ISystemTagManager $tagManager,
85
+        ISystemTagObjectMapper $tagMapper,
86
+        IUserSession $userSession,
87
+        IGroupManager $groupManager,
88
+        \Closure $childExistsFunction
89
+    ) {
90
+        $this->tagManager = $tagManager;
91
+        $this->tagMapper = $tagMapper;
92
+        $this->objectType = $objectType;
93
+        $this->userSession = $userSession;
94
+        $this->groupManager = $groupManager;
95
+        $this->childExistsFunction = $childExistsFunction;
96
+    }
97
+
98
+    /**
99
+     * @param string $name
100
+     * @param resource|string $data Initial payload
101
+     * @return null|string
102
+     * @throws Forbidden
103
+     */
104
+    function createFile($name, $data = null) {
105
+        throw new Forbidden('Permission denied to create nodes');
106
+    }
107
+
108
+    /**
109
+     * @param string $name
110
+     * @throws Forbidden
111
+     */
112
+    function createDirectory($name) {
113
+        throw new Forbidden('Permission denied to create collections');
114
+    }
115
+
116
+    /**
117
+     * @param string $objectId
118
+     * @return SystemTagsObjectMappingCollection
119
+     * @throws NotFound
120
+     */
121
+    function getChild($objectId) {
122
+        // make sure the object exists and is reachable
123
+        if(!$this->childExists($objectId)) {
124
+            throw new NotFound('Entity does not exist or is not available');
125
+        }
126
+        return new SystemTagsObjectMappingCollection(
127
+            $objectId,
128
+            $this->objectType,
129
+            $this->userSession->getUser(),
130
+            $this->tagManager,
131
+            $this->tagMapper
132
+        );
133
+    }
134
+
135
+    function getChildren() {
136
+        // do not list object ids
137
+        throw new MethodNotAllowed();
138
+    }
139
+
140
+    /**
141
+     * Checks if a child-node with the specified name exists
142
+     *
143
+     * @param string $name
144
+     * @return bool
145
+     */
146
+    function childExists($name) {
147
+        return call_user_func($this->childExistsFunction, $name);
148
+    }
149
+
150
+    function delete() {
151
+        throw new Forbidden('Permission denied to delete this collection');
152
+    }
153
+
154
+    function getName() {
155
+        return $this->objectType;
156
+    }
157
+
158
+    /**
159
+     * @param string $name
160
+     * @throws Forbidden
161
+     */
162
+    function setName($name) {
163
+        throw new Forbidden('Permission denied to rename this collection');
164
+    }
165
+
166
+    /**
167
+     * Returns the last modification time, as a unix timestamp
168
+     *
169
+     * @return int
170
+     */
171
+    function getLastModified() {
172
+        return null;
173
+    }
174 174
 }
Please login to merge, or discard this patch.
apps/dav/lib/SystemTag/SystemTagMappingNode.php 1 patch
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -37,134 +37,134 @@
 block discarded – undo
37 37
  * Mapping node for system tag to object id
38 38
  */
39 39
 class SystemTagMappingNode implements \Sabre\DAV\INode {
40
-	/**
41
-	 * @var ISystemTag
42
-	 */
43
-	protected $tag;
44
-
45
-	/**
46
-	 * @var string
47
-	 */
48
-	private $objectId;
49
-
50
-	/**
51
-	 * @var string
52
-	 */
53
-	private $objectType;
54
-
55
-	/**
56
-	 * User
57
-	 *
58
-	 * @var IUser
59
-	 */
60
-	protected $user;
61
-
62
-	/**
63
-	 * @var ISystemTagManager
64
-	 */
65
-	protected $tagManager;
66
-
67
-	/**
68
-	 * @var ISystemTagObjectMapper
69
-	 */
70
-	private $tagMapper;
71
-
72
-	/**
73
-	 * Sets up the node, expects a full path name
74
-	 *
75
-	 * @param ISystemTag $tag system tag
76
-	 * @param string $objectId
77
-	 * @param string $objectType
78
-	 * @param IUser $user user
79
-	 * @param ISystemTagManager $tagManager
80
-	 * @param ISystemTagObjectMapper $tagMapper
81
-	 */
82
-	public function __construct(
83
-		ISystemTag $tag,
84
-		$objectId,
85
-		$objectType,
86
-		IUser $user,
87
-		ISystemTagManager $tagManager,
88
-		ISystemTagObjectMapper $tagMapper
89
-	) {
90
-		$this->tag = $tag;
91
-		$this->objectId = $objectId;
92
-		$this->objectType = $objectType;
93
-		$this->user = $user;
94
-		$this->tagManager = $tagManager;
95
-		$this->tagMapper = $tagMapper;
96
-	}
97
-
98
-	/**
99
-	 * Returns the object id of the relationship
100
-	 *
101
-	 * @return string object id
102
-	 */
103
-	public function getObjectId() {
104
-		return $this->objectId;
105
-	}
106
-
107
-	/**
108
-	 * Returns the object type of the relationship
109
-	 *
110
-	 * @return string object type
111
-	 */
112
-	public function getObjectType() {
113
-		return $this->objectType;
114
-	}
115
-
116
-	/**
117
-	 * Returns the system tag represented by this node
118
-	 *
119
-	 * @return ISystemTag system tag
120
-	 */
121
-	public function getSystemTag() {
122
-		return $this->tag;
123
-	}
124
-
125
-	/**
126
-	 *  Returns the id of the tag
127
-	 *
128
-	 * @return string
129
-	 */
130
-	public function getName() {
131
-		return $this->tag->getId();
132
-	}
133
-
134
-	/**
135
-	 * Renames the node
136
-	 *
137
-	 * @param string $name The new name
138
-	 *
139
-	 * @throws MethodNotAllowed not allowed to rename node
140
-	 */
141
-	public function setName($name) {
142
-		throw new MethodNotAllowed();
143
-	}
144
-
145
-	/**
146
-	 * Returns null, not supported
147
-	 *
148
-	 */
149
-	public function getLastModified() {
150
-		return null;
151
-	}
152
-
153
-	/**
154
-	 * Delete tag to object association
155
-	 */
156
-	public function delete() {
157
-		try {
158
-			if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
159
-				throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found');
160
-			}
161
-			if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) {
162
-				throw new Forbidden('No permission to unassign tag ' . $this->tag->getId());
163
-			}
164
-			$this->tagMapper->unassignTags($this->objectId, $this->objectType, $this->tag->getId());
165
-		} catch (TagNotFoundException $e) {
166
-			// can happen if concurrent deletion occurred
167
-			throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e);
168
-		}
169
-	}
40
+    /**
41
+     * @var ISystemTag
42
+     */
43
+    protected $tag;
44
+
45
+    /**
46
+     * @var string
47
+     */
48
+    private $objectId;
49
+
50
+    /**
51
+     * @var string
52
+     */
53
+    private $objectType;
54
+
55
+    /**
56
+     * User
57
+     *
58
+     * @var IUser
59
+     */
60
+    protected $user;
61
+
62
+    /**
63
+     * @var ISystemTagManager
64
+     */
65
+    protected $tagManager;
66
+
67
+    /**
68
+     * @var ISystemTagObjectMapper
69
+     */
70
+    private $tagMapper;
71
+
72
+    /**
73
+     * Sets up the node, expects a full path name
74
+     *
75
+     * @param ISystemTag $tag system tag
76
+     * @param string $objectId
77
+     * @param string $objectType
78
+     * @param IUser $user user
79
+     * @param ISystemTagManager $tagManager
80
+     * @param ISystemTagObjectMapper $tagMapper
81
+     */
82
+    public function __construct(
83
+        ISystemTag $tag,
84
+        $objectId,
85
+        $objectType,
86
+        IUser $user,
87
+        ISystemTagManager $tagManager,
88
+        ISystemTagObjectMapper $tagMapper
89
+    ) {
90
+        $this->tag = $tag;
91
+        $this->objectId = $objectId;
92
+        $this->objectType = $objectType;
93
+        $this->user = $user;
94
+        $this->tagManager = $tagManager;
95
+        $this->tagMapper = $tagMapper;
96
+    }
97
+
98
+    /**
99
+     * Returns the object id of the relationship
100
+     *
101
+     * @return string object id
102
+     */
103
+    public function getObjectId() {
104
+        return $this->objectId;
105
+    }
106
+
107
+    /**
108
+     * Returns the object type of the relationship
109
+     *
110
+     * @return string object type
111
+     */
112
+    public function getObjectType() {
113
+        return $this->objectType;
114
+    }
115
+
116
+    /**
117
+     * Returns the system tag represented by this node
118
+     *
119
+     * @return ISystemTag system tag
120
+     */
121
+    public function getSystemTag() {
122
+        return $this->tag;
123
+    }
124
+
125
+    /**
126
+     *  Returns the id of the tag
127
+     *
128
+     * @return string
129
+     */
130
+    public function getName() {
131
+        return $this->tag->getId();
132
+    }
133
+
134
+    /**
135
+     * Renames the node
136
+     *
137
+     * @param string $name The new name
138
+     *
139
+     * @throws MethodNotAllowed not allowed to rename node
140
+     */
141
+    public function setName($name) {
142
+        throw new MethodNotAllowed();
143
+    }
144
+
145
+    /**
146
+     * Returns null, not supported
147
+     *
148
+     */
149
+    public function getLastModified() {
150
+        return null;
151
+    }
152
+
153
+    /**
154
+     * Delete tag to object association
155
+     */
156
+    public function delete() {
157
+        try {
158
+            if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
159
+                throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found');
160
+            }
161
+            if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) {
162
+                throw new Forbidden('No permission to unassign tag ' . $this->tag->getId());
163
+            }
164
+            $this->tagMapper->unassignTags($this->objectId, $this->objectType, $this->tag->getId());
165
+        } catch (TagNotFoundException $e) {
166
+            // can happen if concurrent deletion occurred
167
+            throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e);
168
+        }
169
+    }
170 170
 }
Please login to merge, or discard this patch.
apps/dav/lib/SystemTag/SystemTagPlugin.php 1 patch
Indentation   +276 added lines, -276 removed lines patch added patch discarded remove patch
@@ -49,280 +49,280 @@
 block discarded – undo
49 49
  */
50 50
 class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
51 51
 
52
-	// namespace
53
-	const NS_OWNCLOUD = 'http://owncloud.org/ns';
54
-	const ID_PROPERTYNAME = '{http://owncloud.org/ns}id';
55
-	const DISPLAYNAME_PROPERTYNAME = '{http://owncloud.org/ns}display-name';
56
-	const USERVISIBLE_PROPERTYNAME = '{http://owncloud.org/ns}user-visible';
57
-	const USERASSIGNABLE_PROPERTYNAME = '{http://owncloud.org/ns}user-assignable';
58
-	const GROUPS_PROPERTYNAME = '{http://owncloud.org/ns}groups';
59
-	const CANASSIGN_PROPERTYNAME = '{http://owncloud.org/ns}can-assign';
60
-
61
-	/**
62
-	 * @var \Sabre\DAV\Server $server
63
-	 */
64
-	private $server;
65
-
66
-	/**
67
-	 * @var ISystemTagManager
68
-	 */
69
-	protected $tagManager;
70
-
71
-	/**
72
-	 * @var IUserSession
73
-	 */
74
-	protected $userSession;
75
-
76
-	/**
77
-	 * @var IGroupManager
78
-	 */
79
-	protected $groupManager;
80
-
81
-	/**
82
-	 * @param ISystemTagManager $tagManager tag manager
83
-	 * @param IGroupManager $groupManager
84
-	 * @param IUserSession $userSession
85
-	 */
86
-	public function __construct(ISystemTagManager $tagManager,
87
-								IGroupManager $groupManager,
88
-								IUserSession $userSession) {
89
-		$this->tagManager = $tagManager;
90
-		$this->userSession = $userSession;
91
-		$this->groupManager = $groupManager;
92
-	}
93
-
94
-	/**
95
-	 * This initializes the plugin.
96
-	 *
97
-	 * This function is called by \Sabre\DAV\Server, after
98
-	 * addPlugin is called.
99
-	 *
100
-	 * This method should set up the required event subscriptions.
101
-	 *
102
-	 * @param \Sabre\DAV\Server $server
103
-	 * @return void
104
-	 */
105
-	public function initialize(\Sabre\DAV\Server $server) {
106
-
107
-		$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
108
-
109
-		$server->protectedProperties[] = self::ID_PROPERTYNAME;
110
-
111
-		$server->on('propFind', array($this, 'handleGetProperties'));
112
-		$server->on('propPatch', array($this, 'handleUpdateProperties'));
113
-		$server->on('method:POST', [$this, 'httpPost']);
114
-
115
-		$this->server = $server;
116
-	}
117
-
118
-	/**
119
-	 * POST operation on system tag collections
120
-	 *
121
-	 * @param RequestInterface $request request object
122
-	 * @param ResponseInterface $response response object
123
-	 * @return null|false
124
-	 */
125
-	public function httpPost(RequestInterface $request, ResponseInterface $response) {
126
-		$path = $request->getPath();
127
-
128
-		// Making sure the node exists
129
-		$node = $this->server->tree->getNodeForPath($path);
130
-		if ($node instanceof SystemTagsByIdCollection || $node instanceof SystemTagsObjectMappingCollection) {
131
-			$data = $request->getBodyAsString();
132
-
133
-			$tag = $this->createTag($data, $request->getHeader('Content-Type'));
134
-
135
-			if ($node instanceof SystemTagsObjectMappingCollection) {
136
-				// also add to collection
137
-				$node->createFile($tag->getId());
138
-				$url = $request->getBaseUrl() . 'systemtags/';
139
-			} else {
140
-				$url = $request->getUrl();
141
-			}
142
-
143
-			if ($url[strlen($url) - 1] !== '/') {
144
-				$url .= '/';
145
-			}
146
-
147
-			$response->setHeader('Content-Location', $url . $tag->getId());
148
-
149
-			// created
150
-			$response->setStatus(201);
151
-			return false;
152
-		}
153
-	}
154
-
155
-	/**
156
-	 * Creates a new tag
157
-	 *
158
-	 * @param string $data JSON encoded string containing the properties of the tag to create
159
-	 * @param string $contentType content type of the data
160
-	 * @return ISystemTag newly created system tag
161
-	 *
162
-	 * @throws BadRequest if a field was missing
163
-	 * @throws Conflict if a tag with the same properties already exists
164
-	 * @throws UnsupportedMediaType if the content type is not supported
165
-	 */
166
-	private function createTag($data, $contentType = 'application/json') {
167
-		if (explode(';', $contentType)[0] === 'application/json') {
168
-			$data = json_decode($data, true);
169
-		} else {
170
-			throw new UnsupportedMediaType();
171
-		}
172
-
173
-		if (!isset($data['name'])) {
174
-			throw new BadRequest('Missing "name" attribute');
175
-		}
176
-
177
-		$tagName = $data['name'];
178
-		$userVisible = true;
179
-		$userAssignable = true;
180
-
181
-		if (isset($data['userVisible'])) {
182
-			$userVisible = (bool)$data['userVisible'];
183
-		}
184
-
185
-		if (isset($data['userAssignable'])) {
186
-			$userAssignable = (bool)$data['userAssignable'];
187
-		}
188
-
189
-		$groups = [];
190
-		if (isset($data['groups'])) {
191
-			$groups = $data['groups'];
192
-			if (is_string($groups)) {
193
-				$groups = explode('|', $groups);
194
-			}
195
-		}
196
-
197
-		if($userVisible === false || $userAssignable === false || !empty($groups)) {
198
-			if(!$this->userSession->isLoggedIn() || !$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
199
-				throw new BadRequest('Not sufficient permissions');
200
-			}
201
-		}
202
-
203
-		try {
204
-			$tag = $this->tagManager->createTag($tagName, $userVisible, $userAssignable);
205
-			if (!empty($groups)) {
206
-				$this->tagManager->setTagGroups($tag, $groups);
207
-			}
208
-			return $tag;
209
-		} catch (TagAlreadyExistsException $e) {
210
-			throw new Conflict('Tag already exists', 0, $e);
211
-		}
212
-	}
213
-
214
-
215
-	/**
216
-	 * Retrieves system tag properties
217
-	 *
218
-	 * @param PropFind $propFind
219
-	 * @param \Sabre\DAV\INode $node
220
-	 */
221
-	public function handleGetProperties(
222
-		PropFind $propFind,
223
-		\Sabre\DAV\INode $node
224
-	) {
225
-		if (!($node instanceof SystemTagNode) && !($node instanceof SystemTagMappingNode)) {
226
-			return;
227
-		}
228
-
229
-		$propFind->handle(self::ID_PROPERTYNAME, function() use ($node) {
230
-			return $node->getSystemTag()->getId();
231
-		});
232
-
233
-		$propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function() use ($node) {
234
-			return $node->getSystemTag()->getName();
235
-		});
236
-
237
-		$propFind->handle(self::USERVISIBLE_PROPERTYNAME, function() use ($node) {
238
-			return $node->getSystemTag()->isUserVisible() ? 'true' : 'false';
239
-		});
240
-
241
-		$propFind->handle(self::USERASSIGNABLE_PROPERTYNAME, function() use ($node) {
242
-			// this is the tag's inherent property "is user assignable"
243
-			return $node->getSystemTag()->isUserAssignable() ? 'true' : 'false';
244
-		});
245
-
246
-		$propFind->handle(self::CANASSIGN_PROPERTYNAME, function() use ($node) {
247
-			// this is the effective permission for the current user
248
-			return $this->tagManager->canUserAssignTag($node->getSystemTag(), $this->userSession->getUser()) ? 'true' : 'false';
249
-		});
250
-
251
-		$propFind->handle(self::GROUPS_PROPERTYNAME, function() use ($node) {
252
-			if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
253
-				// property only available for admins
254
-				throw new Forbidden();
255
-			}
256
-			$groups = [];
257
-			// no need to retrieve groups for namespaces that don't qualify
258
-			if ($node->getSystemTag()->isUserVisible() && !$node->getSystemTag()->isUserAssignable()) {
259
-				$groups = $this->tagManager->getTagGroups($node->getSystemTag());
260
-			}
261
-			return implode('|', $groups);
262
-		});
263
-	}
264
-
265
-	/**
266
-	 * Updates tag attributes
267
-	 *
268
-	 * @param string $path
269
-	 * @param PropPatch $propPatch
270
-	 *
271
-	 * @return void
272
-	 */
273
-	public function handleUpdateProperties($path, PropPatch $propPatch) {
274
-		$propPatch->handle([
275
-			self::DISPLAYNAME_PROPERTYNAME,
276
-			self::USERVISIBLE_PROPERTYNAME,
277
-			self::USERASSIGNABLE_PROPERTYNAME,
278
-			self::GROUPS_PROPERTYNAME,
279
-		], function($props) use ($path) {
280
-			$node = $this->server->tree->getNodeForPath($path);
281
-			if (!($node instanceof SystemTagNode)) {
282
-				return;
283
-			}
284
-
285
-			$tag = $node->getSystemTag();
286
-			$name = $tag->getName();
287
-			$userVisible = $tag->isUserVisible();
288
-			$userAssignable = $tag->isUserAssignable();
289
-
290
-			$updateTag = false;
291
-
292
-			if (isset($props[self::DISPLAYNAME_PROPERTYNAME])) {
293
-				$name = $props[self::DISPLAYNAME_PROPERTYNAME];
294
-				$updateTag = true;
295
-			}
296
-
297
-			if (isset($props[self::USERVISIBLE_PROPERTYNAME])) {
298
-				$propValue = $props[self::USERVISIBLE_PROPERTYNAME];
299
-				$userVisible = ($propValue !== 'false' && $propValue !== '0');
300
-				$updateTag = true;
301
-			}
302
-
303
-			if (isset($props[self::USERASSIGNABLE_PROPERTYNAME])) {
304
-				$propValue = $props[self::USERASSIGNABLE_PROPERTYNAME];
305
-				$userAssignable = ($propValue !== 'false' && $propValue !== '0');
306
-				$updateTag = true;
307
-			}
308
-
309
-			if (isset($props[self::GROUPS_PROPERTYNAME])) {
310
-				if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
311
-					// property only available for admins
312
-					throw new Forbidden();
313
-				}
314
-
315
-				$propValue = $props[self::GROUPS_PROPERTYNAME];
316
-				$groupIds = explode('|', $propValue);
317
-				$this->tagManager->setTagGroups($tag, $groupIds);
318
-			}
319
-
320
-			if ($updateTag) {
321
-				$node->update($name, $userVisible, $userAssignable);
322
-			}
323
-
324
-			return true;
325
-		});
326
-
327
-	}
52
+    // namespace
53
+    const NS_OWNCLOUD = 'http://owncloud.org/ns';
54
+    const ID_PROPERTYNAME = '{http://owncloud.org/ns}id';
55
+    const DISPLAYNAME_PROPERTYNAME = '{http://owncloud.org/ns}display-name';
56
+    const USERVISIBLE_PROPERTYNAME = '{http://owncloud.org/ns}user-visible';
57
+    const USERASSIGNABLE_PROPERTYNAME = '{http://owncloud.org/ns}user-assignable';
58
+    const GROUPS_PROPERTYNAME = '{http://owncloud.org/ns}groups';
59
+    const CANASSIGN_PROPERTYNAME = '{http://owncloud.org/ns}can-assign';
60
+
61
+    /**
62
+     * @var \Sabre\DAV\Server $server
63
+     */
64
+    private $server;
65
+
66
+    /**
67
+     * @var ISystemTagManager
68
+     */
69
+    protected $tagManager;
70
+
71
+    /**
72
+     * @var IUserSession
73
+     */
74
+    protected $userSession;
75
+
76
+    /**
77
+     * @var IGroupManager
78
+     */
79
+    protected $groupManager;
80
+
81
+    /**
82
+     * @param ISystemTagManager $tagManager tag manager
83
+     * @param IGroupManager $groupManager
84
+     * @param IUserSession $userSession
85
+     */
86
+    public function __construct(ISystemTagManager $tagManager,
87
+                                IGroupManager $groupManager,
88
+                                IUserSession $userSession) {
89
+        $this->tagManager = $tagManager;
90
+        $this->userSession = $userSession;
91
+        $this->groupManager = $groupManager;
92
+    }
93
+
94
+    /**
95
+     * This initializes the plugin.
96
+     *
97
+     * This function is called by \Sabre\DAV\Server, after
98
+     * addPlugin is called.
99
+     *
100
+     * This method should set up the required event subscriptions.
101
+     *
102
+     * @param \Sabre\DAV\Server $server
103
+     * @return void
104
+     */
105
+    public function initialize(\Sabre\DAV\Server $server) {
106
+
107
+        $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
108
+
109
+        $server->protectedProperties[] = self::ID_PROPERTYNAME;
110
+
111
+        $server->on('propFind', array($this, 'handleGetProperties'));
112
+        $server->on('propPatch', array($this, 'handleUpdateProperties'));
113
+        $server->on('method:POST', [$this, 'httpPost']);
114
+
115
+        $this->server = $server;
116
+    }
117
+
118
+    /**
119
+     * POST operation on system tag collections
120
+     *
121
+     * @param RequestInterface $request request object
122
+     * @param ResponseInterface $response response object
123
+     * @return null|false
124
+     */
125
+    public function httpPost(RequestInterface $request, ResponseInterface $response) {
126
+        $path = $request->getPath();
127
+
128
+        // Making sure the node exists
129
+        $node = $this->server->tree->getNodeForPath($path);
130
+        if ($node instanceof SystemTagsByIdCollection || $node instanceof SystemTagsObjectMappingCollection) {
131
+            $data = $request->getBodyAsString();
132
+
133
+            $tag = $this->createTag($data, $request->getHeader('Content-Type'));
134
+
135
+            if ($node instanceof SystemTagsObjectMappingCollection) {
136
+                // also add to collection
137
+                $node->createFile($tag->getId());
138
+                $url = $request->getBaseUrl() . 'systemtags/';
139
+            } else {
140
+                $url = $request->getUrl();
141
+            }
142
+
143
+            if ($url[strlen($url) - 1] !== '/') {
144
+                $url .= '/';
145
+            }
146
+
147
+            $response->setHeader('Content-Location', $url . $tag->getId());
148
+
149
+            // created
150
+            $response->setStatus(201);
151
+            return false;
152
+        }
153
+    }
154
+
155
+    /**
156
+     * Creates a new tag
157
+     *
158
+     * @param string $data JSON encoded string containing the properties of the tag to create
159
+     * @param string $contentType content type of the data
160
+     * @return ISystemTag newly created system tag
161
+     *
162
+     * @throws BadRequest if a field was missing
163
+     * @throws Conflict if a tag with the same properties already exists
164
+     * @throws UnsupportedMediaType if the content type is not supported
165
+     */
166
+    private function createTag($data, $contentType = 'application/json') {
167
+        if (explode(';', $contentType)[0] === 'application/json') {
168
+            $data = json_decode($data, true);
169
+        } else {
170
+            throw new UnsupportedMediaType();
171
+        }
172
+
173
+        if (!isset($data['name'])) {
174
+            throw new BadRequest('Missing "name" attribute');
175
+        }
176
+
177
+        $tagName = $data['name'];
178
+        $userVisible = true;
179
+        $userAssignable = true;
180
+
181
+        if (isset($data['userVisible'])) {
182
+            $userVisible = (bool)$data['userVisible'];
183
+        }
184
+
185
+        if (isset($data['userAssignable'])) {
186
+            $userAssignable = (bool)$data['userAssignable'];
187
+        }
188
+
189
+        $groups = [];
190
+        if (isset($data['groups'])) {
191
+            $groups = $data['groups'];
192
+            if (is_string($groups)) {
193
+                $groups = explode('|', $groups);
194
+            }
195
+        }
196
+
197
+        if($userVisible === false || $userAssignable === false || !empty($groups)) {
198
+            if(!$this->userSession->isLoggedIn() || !$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
199
+                throw new BadRequest('Not sufficient permissions');
200
+            }
201
+        }
202
+
203
+        try {
204
+            $tag = $this->tagManager->createTag($tagName, $userVisible, $userAssignable);
205
+            if (!empty($groups)) {
206
+                $this->tagManager->setTagGroups($tag, $groups);
207
+            }
208
+            return $tag;
209
+        } catch (TagAlreadyExistsException $e) {
210
+            throw new Conflict('Tag already exists', 0, $e);
211
+        }
212
+    }
213
+
214
+
215
+    /**
216
+     * Retrieves system tag properties
217
+     *
218
+     * @param PropFind $propFind
219
+     * @param \Sabre\DAV\INode $node
220
+     */
221
+    public function handleGetProperties(
222
+        PropFind $propFind,
223
+        \Sabre\DAV\INode $node
224
+    ) {
225
+        if (!($node instanceof SystemTagNode) && !($node instanceof SystemTagMappingNode)) {
226
+            return;
227
+        }
228
+
229
+        $propFind->handle(self::ID_PROPERTYNAME, function() use ($node) {
230
+            return $node->getSystemTag()->getId();
231
+        });
232
+
233
+        $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function() use ($node) {
234
+            return $node->getSystemTag()->getName();
235
+        });
236
+
237
+        $propFind->handle(self::USERVISIBLE_PROPERTYNAME, function() use ($node) {
238
+            return $node->getSystemTag()->isUserVisible() ? 'true' : 'false';
239
+        });
240
+
241
+        $propFind->handle(self::USERASSIGNABLE_PROPERTYNAME, function() use ($node) {
242
+            // this is the tag's inherent property "is user assignable"
243
+            return $node->getSystemTag()->isUserAssignable() ? 'true' : 'false';
244
+        });
245
+
246
+        $propFind->handle(self::CANASSIGN_PROPERTYNAME, function() use ($node) {
247
+            // this is the effective permission for the current user
248
+            return $this->tagManager->canUserAssignTag($node->getSystemTag(), $this->userSession->getUser()) ? 'true' : 'false';
249
+        });
250
+
251
+        $propFind->handle(self::GROUPS_PROPERTYNAME, function() use ($node) {
252
+            if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
253
+                // property only available for admins
254
+                throw new Forbidden();
255
+            }
256
+            $groups = [];
257
+            // no need to retrieve groups for namespaces that don't qualify
258
+            if ($node->getSystemTag()->isUserVisible() && !$node->getSystemTag()->isUserAssignable()) {
259
+                $groups = $this->tagManager->getTagGroups($node->getSystemTag());
260
+            }
261
+            return implode('|', $groups);
262
+        });
263
+    }
264
+
265
+    /**
266
+     * Updates tag attributes
267
+     *
268
+     * @param string $path
269
+     * @param PropPatch $propPatch
270
+     *
271
+     * @return void
272
+     */
273
+    public function handleUpdateProperties($path, PropPatch $propPatch) {
274
+        $propPatch->handle([
275
+            self::DISPLAYNAME_PROPERTYNAME,
276
+            self::USERVISIBLE_PROPERTYNAME,
277
+            self::USERASSIGNABLE_PROPERTYNAME,
278
+            self::GROUPS_PROPERTYNAME,
279
+        ], function($props) use ($path) {
280
+            $node = $this->server->tree->getNodeForPath($path);
281
+            if (!($node instanceof SystemTagNode)) {
282
+                return;
283
+            }
284
+
285
+            $tag = $node->getSystemTag();
286
+            $name = $tag->getName();
287
+            $userVisible = $tag->isUserVisible();
288
+            $userAssignable = $tag->isUserAssignable();
289
+
290
+            $updateTag = false;
291
+
292
+            if (isset($props[self::DISPLAYNAME_PROPERTYNAME])) {
293
+                $name = $props[self::DISPLAYNAME_PROPERTYNAME];
294
+                $updateTag = true;
295
+            }
296
+
297
+            if (isset($props[self::USERVISIBLE_PROPERTYNAME])) {
298
+                $propValue = $props[self::USERVISIBLE_PROPERTYNAME];
299
+                $userVisible = ($propValue !== 'false' && $propValue !== '0');
300
+                $updateTag = true;
301
+            }
302
+
303
+            if (isset($props[self::USERASSIGNABLE_PROPERTYNAME])) {
304
+                $propValue = $props[self::USERASSIGNABLE_PROPERTYNAME];
305
+                $userAssignable = ($propValue !== 'false' && $propValue !== '0');
306
+                $updateTag = true;
307
+            }
308
+
309
+            if (isset($props[self::GROUPS_PROPERTYNAME])) {
310
+                if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
311
+                    // property only available for admins
312
+                    throw new Forbidden();
313
+                }
314
+
315
+                $propValue = $props[self::GROUPS_PROPERTYNAME];
316
+                $groupIds = explode('|', $propValue);
317
+                $this->tagManager->setTagGroups($tag, $groupIds);
318
+            }
319
+
320
+            if ($updateTag) {
321
+                $node->update($name, $userVisible, $userAssignable);
322
+            }
323
+
324
+            return true;
325
+        });
326
+
327
+    }
328 328
 }
Please login to merge, or discard this patch.
apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php 1 patch
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -36,59 +36,59 @@
 block discarded – undo
36 36
 
37 37
 class SystemTagsRelationsCollection extends SimpleCollection {
38 38
 
39
-	/**
40
-	 * SystemTagsRelationsCollection constructor.
41
-	 *
42
-	 * @param ISystemTagManager $tagManager
43
-	 * @param ISystemTagObjectMapper $tagMapper
44
-	 * @param IUserSession $userSession
45
-	 * @param IGroupManager $groupManager
46
-	 * @param EventDispatcherInterface $dispatcher
47
-	 */
48
-	public function __construct(
49
-		ISystemTagManager $tagManager,
50
-		ISystemTagObjectMapper $tagMapper,
51
-		IUserSession $userSession,
52
-		IGroupManager $groupManager,
53
-		EventDispatcherInterface $dispatcher
54
-	) {
55
-		$children = [
56
-			new SystemTagsObjectTypeCollection(
57
-				'files',
58
-				$tagManager,
59
-				$tagMapper,
60
-				$userSession,
61
-				$groupManager,
62
-				function($name) {
63
-					$nodes = \OC::$server->getUserFolder()->getById(intval($name));
64
-					return !empty($nodes);
65
-				}
66
-			),
67
-		];
39
+    /**
40
+     * SystemTagsRelationsCollection constructor.
41
+     *
42
+     * @param ISystemTagManager $tagManager
43
+     * @param ISystemTagObjectMapper $tagMapper
44
+     * @param IUserSession $userSession
45
+     * @param IGroupManager $groupManager
46
+     * @param EventDispatcherInterface $dispatcher
47
+     */
48
+    public function __construct(
49
+        ISystemTagManager $tagManager,
50
+        ISystemTagObjectMapper $tagMapper,
51
+        IUserSession $userSession,
52
+        IGroupManager $groupManager,
53
+        EventDispatcherInterface $dispatcher
54
+    ) {
55
+        $children = [
56
+            new SystemTagsObjectTypeCollection(
57
+                'files',
58
+                $tagManager,
59
+                $tagMapper,
60
+                $userSession,
61
+                $groupManager,
62
+                function($name) {
63
+                    $nodes = \OC::$server->getUserFolder()->getById(intval($name));
64
+                    return !empty($nodes);
65
+                }
66
+            ),
67
+        ];
68 68
 
69
-		$event = new SystemTagsEntityEvent(SystemTagsEntityEvent::EVENT_ENTITY);
70
-		$dispatcher->dispatch(SystemTagsEntityEvent::EVENT_ENTITY, $event);
69
+        $event = new SystemTagsEntityEvent(SystemTagsEntityEvent::EVENT_ENTITY);
70
+        $dispatcher->dispatch(SystemTagsEntityEvent::EVENT_ENTITY, $event);
71 71
 
72
-		foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) {
73
-			$children[] = new SystemTagsObjectTypeCollection(
74
-				$entity,
75
-				$tagManager,
76
-				$tagMapper,
77
-				$userSession,
78
-				$groupManager,
79
-				$entityExistsFunction
80
-			);
81
-		}
72
+        foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) {
73
+            $children[] = new SystemTagsObjectTypeCollection(
74
+                $entity,
75
+                $tagManager,
76
+                $tagMapper,
77
+                $userSession,
78
+                $groupManager,
79
+                $entityExistsFunction
80
+            );
81
+        }
82 82
 
83
-		parent::__construct('root', $children);
84
-	}
83
+        parent::__construct('root', $children);
84
+    }
85 85
 
86
-	function getName() {
87
-		return 'systemtags-relations';
88
-	}
86
+    function getName() {
87
+        return 'systemtags-relations';
88
+    }
89 89
 
90
-	function setName($name) {
91
-		throw new Forbidden('Permission denied to rename this collection');
92
-	}
90
+    function setName($name) {
91
+        throw new Forbidden('Permission denied to rename this collection');
92
+    }
93 93
 
94 94
 }
Please login to merge, or discard this patch.
apps/dav/appinfo/v1/webdav.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -31,30 +31,30 @@
 block discarded – undo
31 31
 \OC_Util::obEnd();
32 32
 
33 33
 $serverFactory = new \OCA\DAV\Connector\Sabre\ServerFactory(
34
-	\OC::$server->getConfig(),
35
-	\OC::$server->getLogger(),
36
-	\OC::$server->getDatabaseConnection(),
37
-	\OC::$server->getUserSession(),
38
-	\OC::$server->getMountManager(),
39
-	\OC::$server->getTagManager(),
40
-	\OC::$server->getRequest(),
41
-	\OC::$server->getPreviewManager()
34
+    \OC::$server->getConfig(),
35
+    \OC::$server->getLogger(),
36
+    \OC::$server->getDatabaseConnection(),
37
+    \OC::$server->getUserSession(),
38
+    \OC::$server->getMountManager(),
39
+    \OC::$server->getTagManager(),
40
+    \OC::$server->getRequest(),
41
+    \OC::$server->getPreviewManager()
42 42
 );
43 43
 
44 44
 // Backends
45 45
 $authBackend = new \OCA\DAV\Connector\Sabre\Auth(
46
-	\OC::$server->getSession(),
47
-	\OC::$server->getUserSession(),
48
-	\OC::$server->getRequest(),
49
-	\OC::$server->getTwoFactorAuthManager(),
50
-	\OC::$server->getBruteForceThrottler(),
51
-	'principals/'
46
+    \OC::$server->getSession(),
47
+    \OC::$server->getUserSession(),
48
+    \OC::$server->getRequest(),
49
+    \OC::$server->getTwoFactorAuthManager(),
50
+    \OC::$server->getBruteForceThrottler(),
51
+    'principals/'
52 52
 );
53 53
 $requestUri = \OC::$server->getRequest()->getRequestUri();
54 54
 
55 55
 $server = $serverFactory->createServer($baseuri, $requestUri, $authBackend, function() {
56
-	// use the view for the logged in user
57
-	return \OC\Files\Filesystem::getView();
56
+    // use the view for the logged in user
57
+    return \OC\Files\Filesystem::getView();
58 58
 });
59 59
 
60 60
 // And off we go!
Please login to merge, or discard this patch.