@@ -31,36 +31,36 @@ |
||
31 | 31 | */ |
32 | 32 | class DeleteOrphanedSharesJob extends TimedJob { |
33 | 33 | |
34 | - /** |
|
35 | - * Default interval in minutes |
|
36 | - * |
|
37 | - * @var int $defaultIntervalMin |
|
38 | - **/ |
|
39 | - protected $defaultIntervalMin = 15; |
|
34 | + /** |
|
35 | + * Default interval in minutes |
|
36 | + * |
|
37 | + * @var int $defaultIntervalMin |
|
38 | + **/ |
|
39 | + protected $defaultIntervalMin = 15; |
|
40 | 40 | |
41 | - /** |
|
42 | - * sets the correct interval for this timed job |
|
43 | - */ |
|
44 | - public function __construct(){ |
|
45 | - $this->interval = $this->defaultIntervalMin * 60; |
|
46 | - } |
|
41 | + /** |
|
42 | + * sets the correct interval for this timed job |
|
43 | + */ |
|
44 | + public function __construct(){ |
|
45 | + $this->interval = $this->defaultIntervalMin * 60; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Makes the background job do its work |
|
50 | - * |
|
51 | - * @param array $argument unused argument |
|
52 | - */ |
|
53 | - public function run($argument) { |
|
54 | - $connection = \OC::$server->getDatabaseConnection(); |
|
55 | - $logger = \OC::$server->getLogger(); |
|
48 | + /** |
|
49 | + * Makes the background job do its work |
|
50 | + * |
|
51 | + * @param array $argument unused argument |
|
52 | + */ |
|
53 | + public function run($argument) { |
|
54 | + $connection = \OC::$server->getDatabaseConnection(); |
|
55 | + $logger = \OC::$server->getLogger(); |
|
56 | 56 | |
57 | - $sql = |
|
58 | - 'DELETE FROM `*PREFIX*share` ' . |
|
59 | - 'WHERE `item_type` in (\'file\', \'folder\') ' . |
|
60 | - 'AND NOT EXISTS (SELECT `fileid` FROM `*PREFIX*filecache` WHERE `file_source` = `fileid`)'; |
|
57 | + $sql = |
|
58 | + 'DELETE FROM `*PREFIX*share` ' . |
|
59 | + 'WHERE `item_type` in (\'file\', \'folder\') ' . |
|
60 | + 'AND NOT EXISTS (SELECT `fileid` FROM `*PREFIX*filecache` WHERE `file_source` = `fileid`)'; |
|
61 | 61 | |
62 | - $deletedEntries = $connection->executeUpdate($sql); |
|
63 | - $logger->debug("$deletedEntries orphaned share(s) deleted", ['app' => 'DeleteOrphanedSharesJob']); |
|
64 | - } |
|
62 | + $deletedEntries = $connection->executeUpdate($sql); |
|
63 | + $logger->debug("$deletedEntries orphaned share(s) deleted", ['app' => 'DeleteOrphanedSharesJob']); |
|
64 | + } |
|
65 | 65 | |
66 | 66 | } |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | /** |
42 | 42 | * sets the correct interval for this timed job |
43 | 43 | */ |
44 | - public function __construct(){ |
|
44 | + public function __construct() { |
|
45 | 45 | $this->interval = $this->defaultIntervalMin * 60; |
46 | 46 | } |
47 | 47 | |
@@ -55,8 +55,8 @@ discard block |
||
55 | 55 | $logger = \OC::$server->getLogger(); |
56 | 56 | |
57 | 57 | $sql = |
58 | - 'DELETE FROM `*PREFIX*share` ' . |
|
59 | - 'WHERE `item_type` in (\'file\', \'folder\') ' . |
|
58 | + 'DELETE FROM `*PREFIX*share` '. |
|
59 | + 'WHERE `item_type` in (\'file\', \'folder\') '. |
|
60 | 60 | 'AND NOT EXISTS (SELECT `fileid` FROM `*PREFIX*filecache` WHERE `file_source` = `fileid`)'; |
61 | 61 | |
62 | 62 | $deletedEntries = $connection->executeUpdate($sql); |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | */ |
97 | 97 | public function generateTarget($filePath, $shareWith, $exclude = null) { |
98 | 98 | $shareFolder = \OCA\Files_Sharing\Helper::getShareFolder(); |
99 | - $target = \OC\Files\Filesystem::normalizePath($shareFolder . '/' . basename($filePath)); |
|
99 | + $target = \OC\Files\Filesystem::normalizePath($shareFolder.'/'.basename($filePath)); |
|
100 | 100 | |
101 | 101 | // for group shares we return the target right away |
102 | 102 | if ($shareWith === false) { |
@@ -104,13 +104,13 @@ discard block |
||
104 | 104 | } |
105 | 105 | |
106 | 106 | \OC\Files\Filesystem::initMountPoints($shareWith); |
107 | - $view = new \OC\Files\View('/' . $shareWith . '/files'); |
|
107 | + $view = new \OC\Files\View('/'.$shareWith.'/files'); |
|
108 | 108 | |
109 | 109 | if (!$view->is_dir($shareFolder)) { |
110 | 110 | $dir = ''; |
111 | 111 | $subdirs = explode('/', $shareFolder); |
112 | 112 | foreach ($subdirs as $subdir) { |
113 | - $dir = $dir . '/' . $subdir; |
|
113 | + $dir = $dir.'/'.$subdir; |
|
114 | 114 | if (!$view->is_dir($dir)) { |
115 | 115 | $view->mkdir($dir); |
116 | 116 | } |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | if ($share['item_type'] === 'folder' && $target !== '') { |
238 | 238 | // note: in case of ext storage mount points the path might be empty |
239 | 239 | // which would cause a leading slash to appear |
240 | - $share['path'] = ltrim($share['path'] . '/' . $target, '/'); |
|
240 | + $share['path'] = ltrim($share['path'].'/'.$target, '/'); |
|
241 | 241 | } |
242 | 242 | return self::resolveReshares($share); |
243 | 243 | } |
@@ -37,215 +37,215 @@ |
||
37 | 37 | |
38 | 38 | class File implements \OCP\Share_Backend_File_Dependent { |
39 | 39 | |
40 | - const FORMAT_SHARED_STORAGE = 0; |
|
41 | - const FORMAT_GET_FOLDER_CONTENTS = 1; |
|
42 | - const FORMAT_FILE_APP_ROOT = 2; |
|
43 | - const FORMAT_OPENDIR = 3; |
|
44 | - const FORMAT_GET_ALL = 4; |
|
45 | - const FORMAT_PERMISSIONS = 5; |
|
46 | - const FORMAT_TARGET_NAMES = 6; |
|
47 | - |
|
48 | - private $path; |
|
49 | - |
|
50 | - /** @var FederatedShareProvider */ |
|
51 | - private $federatedShareProvider; |
|
52 | - |
|
53 | - public function __construct(FederatedShareProvider $federatedShareProvider = null) { |
|
54 | - if ($federatedShareProvider) { |
|
55 | - $this->federatedShareProvider = $federatedShareProvider; |
|
56 | - } else { |
|
57 | - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
58 | - $this->federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); |
|
59 | - } |
|
60 | - } |
|
61 | - |
|
62 | - public function isValidSource($itemSource, $uidOwner) { |
|
63 | - try { |
|
64 | - $path = \OC\Files\Filesystem::getPath($itemSource); |
|
65 | - // FIXME: attributes should not be set here, |
|
66 | - // keeping this pattern for now to avoid unexpected |
|
67 | - // regressions |
|
68 | - $this->path = \OC\Files\Filesystem::normalizePath(basename($path)); |
|
69 | - return true; |
|
70 | - } catch (\OCP\Files\NotFoundException $e) { |
|
71 | - return false; |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - public function getFilePath($itemSource, $uidOwner) { |
|
76 | - if (isset($this->path)) { |
|
77 | - $path = $this->path; |
|
78 | - $this->path = null; |
|
79 | - return $path; |
|
80 | - } else { |
|
81 | - try { |
|
82 | - $path = \OC\Files\Filesystem::getPath($itemSource); |
|
83 | - return $path; |
|
84 | - } catch (\OCP\Files\NotFoundException $e) { |
|
85 | - return false; |
|
86 | - } |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * create unique target |
|
92 | - * @param string $filePath |
|
93 | - * @param string $shareWith |
|
94 | - * @param array $exclude (optional) |
|
95 | - * @return string |
|
96 | - */ |
|
97 | - public function generateTarget($filePath, $shareWith, $exclude = null) { |
|
98 | - $shareFolder = \OCA\Files_Sharing\Helper::getShareFolder(); |
|
99 | - $target = \OC\Files\Filesystem::normalizePath($shareFolder . '/' . basename($filePath)); |
|
100 | - |
|
101 | - // for group shares we return the target right away |
|
102 | - if ($shareWith === false) { |
|
103 | - return $target; |
|
104 | - } |
|
105 | - |
|
106 | - \OC\Files\Filesystem::initMountPoints($shareWith); |
|
107 | - $view = new \OC\Files\View('/' . $shareWith . '/files'); |
|
108 | - |
|
109 | - if (!$view->is_dir($shareFolder)) { |
|
110 | - $dir = ''; |
|
111 | - $subdirs = explode('/', $shareFolder); |
|
112 | - foreach ($subdirs as $subdir) { |
|
113 | - $dir = $dir . '/' . $subdir; |
|
114 | - if (!$view->is_dir($dir)) { |
|
115 | - $view->mkdir($dir); |
|
116 | - } |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - $excludeList = is_array($exclude) ? $exclude : array(); |
|
121 | - |
|
122 | - return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view); |
|
123 | - } |
|
124 | - |
|
125 | - public function formatItems($items, $format, $parameters = null) { |
|
126 | - if ($format === self::FORMAT_SHARED_STORAGE) { |
|
127 | - // Only 1 item should come through for this format call |
|
128 | - $item = array_shift($items); |
|
129 | - return array( |
|
130 | - 'parent' => $item['parent'], |
|
131 | - 'path' => $item['path'], |
|
132 | - 'storage' => $item['storage'], |
|
133 | - 'permissions' => $item['permissions'], |
|
134 | - 'uid_owner' => $item['uid_owner'], |
|
135 | - ); |
|
136 | - } else if ($format === self::FORMAT_GET_FOLDER_CONTENTS) { |
|
137 | - $files = array(); |
|
138 | - foreach ($items as $item) { |
|
139 | - $file = array(); |
|
140 | - $file['fileid'] = $item['file_source']; |
|
141 | - $file['storage'] = $item['storage']; |
|
142 | - $file['path'] = $item['file_target']; |
|
143 | - $file['parent'] = $item['file_parent']; |
|
144 | - $file['name'] = basename($item['file_target']); |
|
145 | - $file['mimetype'] = $item['mimetype']; |
|
146 | - $file['mimepart'] = $item['mimepart']; |
|
147 | - $file['mtime'] = $item['mtime']; |
|
148 | - $file['encrypted'] = $item['encrypted']; |
|
149 | - $file['etag'] = $item['etag']; |
|
150 | - $file['uid_owner'] = $item['uid_owner']; |
|
151 | - $file['displayname_owner'] = $item['displayname_owner']; |
|
152 | - |
|
153 | - $storage = \OC\Files\Filesystem::getStorage('/'); |
|
154 | - $cache = $storage->getCache(); |
|
155 | - $file['size'] = $item['size']; |
|
156 | - $files[] = $file; |
|
157 | - } |
|
158 | - return $files; |
|
159 | - } else if ($format === self::FORMAT_OPENDIR) { |
|
160 | - $files = array(); |
|
161 | - foreach ($items as $item) { |
|
162 | - $files[] = basename($item['file_target']); |
|
163 | - } |
|
164 | - return $files; |
|
165 | - } else if ($format === self::FORMAT_GET_ALL) { |
|
166 | - $ids = array(); |
|
167 | - foreach ($items as $item) { |
|
168 | - $ids[] = $item['file_source']; |
|
169 | - } |
|
170 | - return $ids; |
|
171 | - } else if ($format === self::FORMAT_PERMISSIONS) { |
|
172 | - $filePermissions = array(); |
|
173 | - foreach ($items as $item) { |
|
174 | - $filePermissions[$item['file_source']] = $item['permissions']; |
|
175 | - } |
|
176 | - return $filePermissions; |
|
177 | - } else if ($format === self::FORMAT_TARGET_NAMES) { |
|
178 | - $targets = array(); |
|
179 | - foreach ($items as $item) { |
|
180 | - $targets[] = $item['file_target']; |
|
181 | - } |
|
182 | - return $targets; |
|
183 | - } |
|
184 | - return array(); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * check if server2server share is enabled |
|
189 | - * |
|
190 | - * @param int $shareType |
|
191 | - * @return boolean |
|
192 | - */ |
|
193 | - public function isShareTypeAllowed($shareType) { |
|
194 | - if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
195 | - return $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
196 | - } |
|
197 | - |
|
198 | - return true; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * resolve reshares to return the correct source item |
|
203 | - * @param array $source |
|
204 | - * @return array source item |
|
205 | - */ |
|
206 | - protected static function resolveReshares($source) { |
|
207 | - if (isset($source['parent'])) { |
|
208 | - $parent = $source['parent']; |
|
209 | - while (isset($parent)) { |
|
210 | - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
211 | - $qb->select('parent', 'uid_owner') |
|
212 | - ->from('share') |
|
213 | - ->where( |
|
214 | - $qb->expr()->eq('id', $qb->createNamedParameter($parent)) |
|
215 | - ); |
|
216 | - $result = $qb->execute(); |
|
217 | - $item = $result->fetch(); |
|
218 | - $result->closeCursor(); |
|
219 | - if (isset($item['parent'])) { |
|
220 | - $parent = $item['parent']; |
|
221 | - } else { |
|
222 | - $fileOwner = $item['uid_owner']; |
|
223 | - break; |
|
224 | - } |
|
225 | - } |
|
226 | - } else { |
|
227 | - $fileOwner = $source['uid_owner']; |
|
228 | - } |
|
229 | - if (isset($fileOwner)) { |
|
230 | - $source['fileOwner'] = $fileOwner; |
|
231 | - } else { |
|
232 | - \OCP\Util::writeLog('files_sharing', "No owner found for reshare", \OCP\Util::ERROR); |
|
233 | - } |
|
234 | - |
|
235 | - return $source; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @param string $target |
|
240 | - * @param array $share |
|
241 | - * @return array|false source item |
|
242 | - */ |
|
243 | - public static function getSource($target, $share) { |
|
244 | - if ($share['item_type'] === 'folder' && $target !== '') { |
|
245 | - // note: in case of ext storage mount points the path might be empty |
|
246 | - // which would cause a leading slash to appear |
|
247 | - $share['path'] = ltrim($share['path'] . '/' . $target, '/'); |
|
248 | - } |
|
249 | - return self::resolveReshares($share); |
|
250 | - } |
|
40 | + const FORMAT_SHARED_STORAGE = 0; |
|
41 | + const FORMAT_GET_FOLDER_CONTENTS = 1; |
|
42 | + const FORMAT_FILE_APP_ROOT = 2; |
|
43 | + const FORMAT_OPENDIR = 3; |
|
44 | + const FORMAT_GET_ALL = 4; |
|
45 | + const FORMAT_PERMISSIONS = 5; |
|
46 | + const FORMAT_TARGET_NAMES = 6; |
|
47 | + |
|
48 | + private $path; |
|
49 | + |
|
50 | + /** @var FederatedShareProvider */ |
|
51 | + private $federatedShareProvider; |
|
52 | + |
|
53 | + public function __construct(FederatedShareProvider $federatedShareProvider = null) { |
|
54 | + if ($federatedShareProvider) { |
|
55 | + $this->federatedShareProvider = $federatedShareProvider; |
|
56 | + } else { |
|
57 | + $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
58 | + $this->federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); |
|
59 | + } |
|
60 | + } |
|
61 | + |
|
62 | + public function isValidSource($itemSource, $uidOwner) { |
|
63 | + try { |
|
64 | + $path = \OC\Files\Filesystem::getPath($itemSource); |
|
65 | + // FIXME: attributes should not be set here, |
|
66 | + // keeping this pattern for now to avoid unexpected |
|
67 | + // regressions |
|
68 | + $this->path = \OC\Files\Filesystem::normalizePath(basename($path)); |
|
69 | + return true; |
|
70 | + } catch (\OCP\Files\NotFoundException $e) { |
|
71 | + return false; |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + public function getFilePath($itemSource, $uidOwner) { |
|
76 | + if (isset($this->path)) { |
|
77 | + $path = $this->path; |
|
78 | + $this->path = null; |
|
79 | + return $path; |
|
80 | + } else { |
|
81 | + try { |
|
82 | + $path = \OC\Files\Filesystem::getPath($itemSource); |
|
83 | + return $path; |
|
84 | + } catch (\OCP\Files\NotFoundException $e) { |
|
85 | + return false; |
|
86 | + } |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * create unique target |
|
92 | + * @param string $filePath |
|
93 | + * @param string $shareWith |
|
94 | + * @param array $exclude (optional) |
|
95 | + * @return string |
|
96 | + */ |
|
97 | + public function generateTarget($filePath, $shareWith, $exclude = null) { |
|
98 | + $shareFolder = \OCA\Files_Sharing\Helper::getShareFolder(); |
|
99 | + $target = \OC\Files\Filesystem::normalizePath($shareFolder . '/' . basename($filePath)); |
|
100 | + |
|
101 | + // for group shares we return the target right away |
|
102 | + if ($shareWith === false) { |
|
103 | + return $target; |
|
104 | + } |
|
105 | + |
|
106 | + \OC\Files\Filesystem::initMountPoints($shareWith); |
|
107 | + $view = new \OC\Files\View('/' . $shareWith . '/files'); |
|
108 | + |
|
109 | + if (!$view->is_dir($shareFolder)) { |
|
110 | + $dir = ''; |
|
111 | + $subdirs = explode('/', $shareFolder); |
|
112 | + foreach ($subdirs as $subdir) { |
|
113 | + $dir = $dir . '/' . $subdir; |
|
114 | + if (!$view->is_dir($dir)) { |
|
115 | + $view->mkdir($dir); |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + $excludeList = is_array($exclude) ? $exclude : array(); |
|
121 | + |
|
122 | + return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view); |
|
123 | + } |
|
124 | + |
|
125 | + public function formatItems($items, $format, $parameters = null) { |
|
126 | + if ($format === self::FORMAT_SHARED_STORAGE) { |
|
127 | + // Only 1 item should come through for this format call |
|
128 | + $item = array_shift($items); |
|
129 | + return array( |
|
130 | + 'parent' => $item['parent'], |
|
131 | + 'path' => $item['path'], |
|
132 | + 'storage' => $item['storage'], |
|
133 | + 'permissions' => $item['permissions'], |
|
134 | + 'uid_owner' => $item['uid_owner'], |
|
135 | + ); |
|
136 | + } else if ($format === self::FORMAT_GET_FOLDER_CONTENTS) { |
|
137 | + $files = array(); |
|
138 | + foreach ($items as $item) { |
|
139 | + $file = array(); |
|
140 | + $file['fileid'] = $item['file_source']; |
|
141 | + $file['storage'] = $item['storage']; |
|
142 | + $file['path'] = $item['file_target']; |
|
143 | + $file['parent'] = $item['file_parent']; |
|
144 | + $file['name'] = basename($item['file_target']); |
|
145 | + $file['mimetype'] = $item['mimetype']; |
|
146 | + $file['mimepart'] = $item['mimepart']; |
|
147 | + $file['mtime'] = $item['mtime']; |
|
148 | + $file['encrypted'] = $item['encrypted']; |
|
149 | + $file['etag'] = $item['etag']; |
|
150 | + $file['uid_owner'] = $item['uid_owner']; |
|
151 | + $file['displayname_owner'] = $item['displayname_owner']; |
|
152 | + |
|
153 | + $storage = \OC\Files\Filesystem::getStorage('/'); |
|
154 | + $cache = $storage->getCache(); |
|
155 | + $file['size'] = $item['size']; |
|
156 | + $files[] = $file; |
|
157 | + } |
|
158 | + return $files; |
|
159 | + } else if ($format === self::FORMAT_OPENDIR) { |
|
160 | + $files = array(); |
|
161 | + foreach ($items as $item) { |
|
162 | + $files[] = basename($item['file_target']); |
|
163 | + } |
|
164 | + return $files; |
|
165 | + } else if ($format === self::FORMAT_GET_ALL) { |
|
166 | + $ids = array(); |
|
167 | + foreach ($items as $item) { |
|
168 | + $ids[] = $item['file_source']; |
|
169 | + } |
|
170 | + return $ids; |
|
171 | + } else if ($format === self::FORMAT_PERMISSIONS) { |
|
172 | + $filePermissions = array(); |
|
173 | + foreach ($items as $item) { |
|
174 | + $filePermissions[$item['file_source']] = $item['permissions']; |
|
175 | + } |
|
176 | + return $filePermissions; |
|
177 | + } else if ($format === self::FORMAT_TARGET_NAMES) { |
|
178 | + $targets = array(); |
|
179 | + foreach ($items as $item) { |
|
180 | + $targets[] = $item['file_target']; |
|
181 | + } |
|
182 | + return $targets; |
|
183 | + } |
|
184 | + return array(); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * check if server2server share is enabled |
|
189 | + * |
|
190 | + * @param int $shareType |
|
191 | + * @return boolean |
|
192 | + */ |
|
193 | + public function isShareTypeAllowed($shareType) { |
|
194 | + if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
195 | + return $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
196 | + } |
|
197 | + |
|
198 | + return true; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * resolve reshares to return the correct source item |
|
203 | + * @param array $source |
|
204 | + * @return array source item |
|
205 | + */ |
|
206 | + protected static function resolveReshares($source) { |
|
207 | + if (isset($source['parent'])) { |
|
208 | + $parent = $source['parent']; |
|
209 | + while (isset($parent)) { |
|
210 | + $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
211 | + $qb->select('parent', 'uid_owner') |
|
212 | + ->from('share') |
|
213 | + ->where( |
|
214 | + $qb->expr()->eq('id', $qb->createNamedParameter($parent)) |
|
215 | + ); |
|
216 | + $result = $qb->execute(); |
|
217 | + $item = $result->fetch(); |
|
218 | + $result->closeCursor(); |
|
219 | + if (isset($item['parent'])) { |
|
220 | + $parent = $item['parent']; |
|
221 | + } else { |
|
222 | + $fileOwner = $item['uid_owner']; |
|
223 | + break; |
|
224 | + } |
|
225 | + } |
|
226 | + } else { |
|
227 | + $fileOwner = $source['uid_owner']; |
|
228 | + } |
|
229 | + if (isset($fileOwner)) { |
|
230 | + $source['fileOwner'] = $fileOwner; |
|
231 | + } else { |
|
232 | + \OCP\Util::writeLog('files_sharing', "No owner found for reshare", \OCP\Util::ERROR); |
|
233 | + } |
|
234 | + |
|
235 | + return $source; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @param string $target |
|
240 | + * @param array $share |
|
241 | + * @return array|false source item |
|
242 | + */ |
|
243 | + public static function getSource($target, $share) { |
|
244 | + if ($share['item_type'] === 'folder' && $target !== '') { |
|
245 | + // note: in case of ext storage mount points the path might be empty |
|
246 | + // which would cause a leading slash to appear |
|
247 | + $share['path'] = ltrim($share['path'] . '/' . $target, '/'); |
|
248 | + } |
|
249 | + return self::resolveReshares($share); |
|
250 | + } |
|
251 | 251 | } |
@@ -335,7 +335,7 @@ |
||
335 | 335 | } |
336 | 336 | if($quota === 0) { |
337 | 337 | $quota = 'default'; |
338 | - }else if($quota === -1) { |
|
338 | + } else if($quota === -1) { |
|
339 | 339 | $quota = 'none'; |
340 | 340 | } else { |
341 | 341 | $quota = \OCP\Util::humanFileSize($quota); |
@@ -54,917 +54,917 @@ |
||
54 | 54 | |
55 | 55 | class UsersController extends OCSController { |
56 | 56 | |
57 | - /** @var IUserManager */ |
|
58 | - private $userManager; |
|
59 | - /** @var IConfig */ |
|
60 | - private $config; |
|
61 | - /** @var IAppManager */ |
|
62 | - private $appManager; |
|
63 | - /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface |
|
64 | - private $groupManager; |
|
65 | - /** @var IUserSession */ |
|
66 | - private $userSession; |
|
67 | - /** @var AccountManager */ |
|
68 | - private $accountManager; |
|
69 | - /** @var ILogger */ |
|
70 | - private $logger; |
|
71 | - /** @var IFactory */ |
|
72 | - private $l10nFactory; |
|
73 | - /** @var NewUserMailHelper */ |
|
74 | - private $newUserMailHelper; |
|
75 | - /** @var FederatedFileSharingFactory */ |
|
76 | - private $federatedFileSharingFactory; |
|
77 | - /** @var ISecureRandom */ |
|
78 | - private $secureRandom; |
|
79 | - |
|
80 | - /** |
|
81 | - * @param string $appName |
|
82 | - * @param IRequest $request |
|
83 | - * @param IUserManager $userManager |
|
84 | - * @param IConfig $config |
|
85 | - * @param IAppManager $appManager |
|
86 | - * @param IGroupManager $groupManager |
|
87 | - * @param IUserSession $userSession |
|
88 | - * @param AccountManager $accountManager |
|
89 | - * @param ILogger $logger |
|
90 | - * @param IFactory $l10nFactory |
|
91 | - * @param NewUserMailHelper $newUserMailHelper |
|
92 | - * @param FederatedFileSharingFactory $federatedFileSharingFactory |
|
93 | - * @param ISecureRandom $secureRandom |
|
94 | - */ |
|
95 | - public function __construct(string $appName, |
|
96 | - IRequest $request, |
|
97 | - IUserManager $userManager, |
|
98 | - IConfig $config, |
|
99 | - IAppManager $appManager, |
|
100 | - IGroupManager $groupManager, |
|
101 | - IUserSession $userSession, |
|
102 | - AccountManager $accountManager, |
|
103 | - ILogger $logger, |
|
104 | - IFactory $l10nFactory, |
|
105 | - NewUserMailHelper $newUserMailHelper, |
|
106 | - FederatedFileSharingFactory $federatedFileSharingFactory, |
|
107 | - ISecureRandom $secureRandom) { |
|
108 | - parent::__construct($appName, $request); |
|
109 | - |
|
110 | - $this->userManager = $userManager; |
|
111 | - $this->config = $config; |
|
112 | - $this->appManager = $appManager; |
|
113 | - $this->groupManager = $groupManager; |
|
114 | - $this->userSession = $userSession; |
|
115 | - $this->accountManager = $accountManager; |
|
116 | - $this->logger = $logger; |
|
117 | - $this->l10nFactory = $l10nFactory; |
|
118 | - $this->newUserMailHelper = $newUserMailHelper; |
|
119 | - $this->federatedFileSharingFactory = $federatedFileSharingFactory; |
|
120 | - $this->secureRandom = $secureRandom; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @NoAdminRequired |
|
125 | - * |
|
126 | - * returns a list of users |
|
127 | - * |
|
128 | - * @param string $search |
|
129 | - * @param int $limit |
|
130 | - * @param int $offset |
|
131 | - * @return DataResponse |
|
132 | - */ |
|
133 | - public function getUsers(string $search = '', $limit = null, $offset = 0): DataResponse { |
|
134 | - $user = $this->userSession->getUser(); |
|
135 | - $users = []; |
|
136 | - |
|
137 | - // Admin? Or SubAdmin? |
|
138 | - $uid = $user->getUID(); |
|
139 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
140 | - if($this->groupManager->isAdmin($uid)){ |
|
141 | - $users = $this->userManager->search($search, $limit, $offset); |
|
142 | - } else if ($subAdminManager->isSubAdmin($user)) { |
|
143 | - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
144 | - foreach ($subAdminOfGroups as $key => $group) { |
|
145 | - $subAdminOfGroups[$key] = $group->getGID(); |
|
146 | - } |
|
147 | - |
|
148 | - $users = []; |
|
149 | - foreach ($subAdminOfGroups as $group) { |
|
150 | - $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - $users = array_keys($users); |
|
155 | - |
|
156 | - return new DataResponse([ |
|
157 | - 'users' => $users |
|
158 | - ]); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @NoAdminRequired |
|
163 | - * |
|
164 | - * returns a list of users and their data |
|
165 | - */ |
|
166 | - public function getUsersDetails(string $search = '', $limit = null, $offset = 0): DataResponse { |
|
167 | - $user = $this->userSession->getUser(); |
|
168 | - $users = []; |
|
169 | - |
|
170 | - // Admin? Or SubAdmin? |
|
171 | - $uid = $user->getUID(); |
|
172 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
173 | - if($this->groupManager->isAdmin($uid)){ |
|
174 | - $users = $this->userManager->search($search, $limit, $offset); |
|
175 | - } else if ($subAdminManager->isSubAdmin($user)) { |
|
176 | - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
177 | - foreach ($subAdminOfGroups as $key => $group) { |
|
178 | - $subAdminOfGroups[$key] = $group->getGID(); |
|
179 | - } |
|
180 | - |
|
181 | - $users = []; |
|
182 | - foreach ($subAdminOfGroups as $group) { |
|
183 | - $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - $users = array_keys($users); |
|
188 | - $usersDetails = []; |
|
189 | - foreach ($users as $key => $userId) { |
|
190 | - $userData = $this->getUserData($userId); |
|
191 | - // Do not insert empty entry |
|
192 | - if(!empty($userData)) { |
|
193 | - $usersDetails[$userId] = $userData; |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - return new DataResponse([ |
|
198 | - 'users' => $usersDetails |
|
199 | - ]); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @PasswordConfirmationRequired |
|
204 | - * @NoAdminRequired |
|
205 | - * |
|
206 | - * @param string $userid |
|
207 | - * @param string $password |
|
208 | - * @param string $email |
|
209 | - * @param array $groups |
|
210 | - * @return DataResponse |
|
211 | - * @throws OCSException |
|
212 | - */ |
|
213 | - public function addUser(string $userid, string $password = '', string $email='', array $groups = []): DataResponse { |
|
214 | - $user = $this->userSession->getUser(); |
|
215 | - $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
216 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
217 | - |
|
218 | - if($this->userManager->userExists($userid)) { |
|
219 | - $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
|
220 | - throw new OCSException('User already exists', 102); |
|
221 | - } |
|
222 | - |
|
223 | - if($groups !== []) { |
|
224 | - foreach ($groups as $group) { |
|
225 | - if(!$this->groupManager->groupExists($group)) { |
|
226 | - throw new OCSException('group '.$group.' does not exist', 104); |
|
227 | - } |
|
228 | - if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
229 | - throw new OCSException('insufficient privileges for group '. $group, 105); |
|
230 | - } |
|
231 | - } |
|
232 | - } else { |
|
233 | - if(!$isAdmin) { |
|
234 | - throw new OCSException('no group specified (required for subadmins)', 106); |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - $generatePasswordResetToken = false; |
|
239 | - if ($password === '') { |
|
240 | - if ($email === '') { |
|
241 | - throw new OCSException('To send a password link to the user an email address is required.', 108); |
|
242 | - } |
|
243 | - |
|
244 | - $password = $this->secureRandom->generate(10); |
|
245 | - // Make sure we pass the password_policy |
|
246 | - $password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()'); |
|
247 | - $generatePasswordResetToken = true; |
|
248 | - } |
|
249 | - |
|
250 | - try { |
|
251 | - $newUser = $this->userManager->createUser($userid, $password); |
|
252 | - $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); |
|
253 | - |
|
254 | - foreach ($groups as $group) { |
|
255 | - $this->groupManager->get($group)->addUser($newUser); |
|
256 | - $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); |
|
257 | - } |
|
258 | - |
|
259 | - // Send new user mail only if a mail is set |
|
260 | - if ($email !== '') { |
|
261 | - $newUser->setEMailAddress($email); |
|
262 | - try { |
|
263 | - $emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken); |
|
264 | - $this->newUserMailHelper->sendMail($newUser, $emailTemplate); |
|
265 | - } catch (\Exception $e) { |
|
266 | - $this->logger->logException($e, [ |
|
267 | - 'message' => "Can't send new user mail to $email", |
|
268 | - 'level' => \OCP\Util::ERROR, |
|
269 | - 'app' => 'ocs_api', |
|
270 | - ]); |
|
271 | - throw new OCSException('Unable to send the invitation mail', 109); |
|
272 | - } |
|
273 | - } |
|
274 | - |
|
275 | - return new DataResponse(); |
|
276 | - |
|
277 | - } catch (HintException $e ) { |
|
278 | - $this->logger->logException($e, [ |
|
279 | - 'message' => 'Failed addUser attempt with hint exception.', |
|
280 | - 'level' => \OCP\Util::WARN, |
|
281 | - 'app' => 'ocs_api', |
|
282 | - ]); |
|
283 | - throw new OCSException($e->getHint(), 107); |
|
284 | - } catch (\Exception $e) { |
|
285 | - $this->logger->logException($e, [ |
|
286 | - 'message' => 'Failed addUser attempt with exception.', |
|
287 | - 'level' => \OCP\Util::ERROR, |
|
288 | - 'app' => 'ocs_api', |
|
289 | - ]); |
|
290 | - throw new OCSException('Bad request', 101); |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * @NoAdminRequired |
|
296 | - * @NoSubAdminRequired |
|
297 | - * |
|
298 | - * gets user info |
|
299 | - * |
|
300 | - * @param string $userId |
|
301 | - * @return DataResponse |
|
302 | - * @throws OCSException |
|
303 | - */ |
|
304 | - public function getUser(string $userId): DataResponse { |
|
305 | - $data = $this->getUserData($userId); |
|
306 | - // getUserData returns empty array if not enough permissions |
|
307 | - if(empty($data)) { |
|
308 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
309 | - } |
|
310 | - return new DataResponse($data); |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @NoAdminRequired |
|
315 | - * @NoSubAdminRequired |
|
316 | - * |
|
317 | - * gets user info from the currently logged in user |
|
318 | - * |
|
319 | - * @return DataResponse |
|
320 | - * @throws OCSException |
|
321 | - */ |
|
322 | - public function getCurrentUser(): DataResponse { |
|
323 | - $user = $this->userSession->getUser(); |
|
324 | - if ($user) { |
|
325 | - $data = $this->getUserData($user->getUID()); |
|
326 | - // rename "displayname" to "display-name" only for this call to keep |
|
327 | - // the API stable. |
|
328 | - $data['display-name'] = $data['displayname']; |
|
329 | - unset($data['displayname']); |
|
330 | - return new DataResponse($data); |
|
331 | - |
|
332 | - } |
|
333 | - |
|
334 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * creates a array with all user data |
|
339 | - * |
|
340 | - * @param $userId |
|
341 | - * @return array |
|
342 | - * @throws OCSException |
|
343 | - */ |
|
344 | - protected function getUserData(string $userId): array { |
|
345 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
346 | - |
|
347 | - $data = []; |
|
348 | - |
|
349 | - // Check if the target user exists |
|
350 | - $targetUserObject = $this->userManager->get($userId); |
|
351 | - if($targetUserObject === null) { |
|
352 | - throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
353 | - } |
|
354 | - |
|
355 | - // Should be at least Admin Or SubAdmin! |
|
356 | - if( $this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
357 | - || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
358 | - $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
|
359 | - } else { |
|
360 | - // Check they are looking up themselves |
|
361 | - if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
362 | - return $data; |
|
363 | - } |
|
364 | - } |
|
365 | - |
|
366 | - // Get groups data |
|
367 | - $userAccount = $this->accountManager->getUser($targetUserObject); |
|
368 | - $groups = $this->groupManager->getUserGroups($targetUserObject); |
|
369 | - $gids = []; |
|
370 | - foreach ($groups as $group) { |
|
371 | - $gids[] = $group->getDisplayName(); |
|
372 | - } |
|
373 | - |
|
374 | - // Find the data |
|
375 | - $data['id'] = $targetUserObject->getUID(); |
|
376 | - $data['storageLocation'] = $targetUserObject->getHome(); |
|
377 | - $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000; |
|
378 | - $data['backend'] = $targetUserObject->getBackendClassName(); |
|
379 | - $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID()); |
|
380 | - $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
381 | - $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
382 | - $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
383 | - $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value']; |
|
384 | - $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value']; |
|
385 | - $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value']; |
|
386 | - $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value']; |
|
387 | - $data['groups'] = $gids; |
|
388 | - $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang'); |
|
389 | - |
|
390 | - return $data; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * @NoAdminRequired |
|
395 | - * @NoSubAdminRequired |
|
396 | - */ |
|
397 | - public function getEditableFields(): DataResponse { |
|
398 | - $permittedFields = []; |
|
399 | - |
|
400 | - // Editing self (display, email) |
|
401 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
402 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
403 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
404 | - } |
|
405 | - |
|
406 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
407 | - $federatedFileSharing = $this->federatedFileSharingFactory->get(); |
|
408 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
409 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
410 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
411 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
412 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
413 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - return new DataResponse($permittedFields); |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * @NoAdminRequired |
|
422 | - * @NoSubAdminRequired |
|
423 | - * @PasswordConfirmationRequired |
|
424 | - * |
|
425 | - * edit users |
|
426 | - * |
|
427 | - * @param string $userId |
|
428 | - * @param string $key |
|
429 | - * @param string $value |
|
430 | - * @return DataResponse |
|
431 | - * @throws OCSException |
|
432 | - */ |
|
433 | - public function editUser(string $userId, string $key, string $value): DataResponse { |
|
434 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
435 | - |
|
436 | - $targetUser = $this->userManager->get($userId); |
|
437 | - if($targetUser === null) { |
|
438 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
439 | - } |
|
440 | - |
|
441 | - $permittedFields = []; |
|
442 | - if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
443 | - // Editing self (display, email) |
|
444 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
445 | - $permittedFields[] = 'display'; |
|
446 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
447 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
448 | - } |
|
449 | - |
|
450 | - $permittedFields[] = 'password'; |
|
451 | - if ($this->config->getSystemValue('force_language', false) === false || |
|
452 | - $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
453 | - $permittedFields[] = 'language'; |
|
454 | - } |
|
455 | - |
|
456 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
457 | - $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
458 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
459 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
460 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
461 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
462 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
463 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
464 | - } |
|
465 | - } |
|
466 | - |
|
467 | - // If admin they can edit their own quota |
|
468 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
469 | - $permittedFields[] = 'quota'; |
|
470 | - } |
|
471 | - } else { |
|
472 | - // Check if admin / subadmin |
|
473 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
474 | - if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
475 | - || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
476 | - // They have permissions over the user |
|
477 | - $permittedFields[] = 'display'; |
|
478 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
479 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
480 | - $permittedFields[] = 'password'; |
|
481 | - $permittedFields[] = 'language'; |
|
482 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
483 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
484 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
485 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
486 | - $permittedFields[] = 'quota'; |
|
487 | - } else { |
|
488 | - // No rights |
|
489 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
490 | - } |
|
491 | - } |
|
492 | - // Check if permitted to edit this field |
|
493 | - if(!in_array($key, $permittedFields)) { |
|
494 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
495 | - } |
|
496 | - // Process the edit |
|
497 | - switch($key) { |
|
498 | - case 'display': |
|
499 | - case AccountManager::PROPERTY_DISPLAYNAME: |
|
500 | - $targetUser->setDisplayName($value); |
|
501 | - break; |
|
502 | - case 'quota': |
|
503 | - $quota = $value; |
|
504 | - if($quota !== 'none' && $quota !== 'default') { |
|
505 | - if (is_numeric($quota)) { |
|
506 | - $quota = (float) $quota; |
|
507 | - } else { |
|
508 | - $quota = \OCP\Util::computerFileSize($quota); |
|
509 | - } |
|
510 | - if ($quota === false) { |
|
511 | - throw new OCSException('Invalid quota value '.$value, 103); |
|
512 | - } |
|
513 | - if($quota === 0) { |
|
514 | - $quota = 'default'; |
|
515 | - }else if($quota === -1) { |
|
516 | - $quota = 'none'; |
|
517 | - } else { |
|
518 | - $quota = \OCP\Util::humanFileSize($quota); |
|
519 | - } |
|
520 | - } |
|
521 | - $targetUser->setQuota($quota); |
|
522 | - break; |
|
523 | - case 'password': |
|
524 | - $targetUser->setPassword($value); |
|
525 | - break; |
|
526 | - case 'language': |
|
527 | - $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
|
528 | - if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
|
529 | - throw new OCSException('Invalid language', 102); |
|
530 | - } |
|
531 | - $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
|
532 | - break; |
|
533 | - case AccountManager::PROPERTY_EMAIL: |
|
534 | - if(filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { |
|
535 | - $targetUser->setEMailAddress($value); |
|
536 | - } else { |
|
537 | - throw new OCSException('', 102); |
|
538 | - } |
|
539 | - break; |
|
540 | - case AccountManager::PROPERTY_PHONE: |
|
541 | - case AccountManager::PROPERTY_ADDRESS: |
|
542 | - case AccountManager::PROPERTY_WEBSITE: |
|
543 | - case AccountManager::PROPERTY_TWITTER: |
|
544 | - $userAccount = $this->accountManager->getUser($targetUser); |
|
545 | - if ($userAccount[$key]['value'] !== $value) { |
|
546 | - $userAccount[$key]['value'] = $value; |
|
547 | - $this->accountManager->updateUser($targetUser, $userAccount); |
|
548 | - } |
|
549 | - break; |
|
550 | - default: |
|
551 | - throw new OCSException('', 103); |
|
552 | - } |
|
553 | - return new DataResponse(); |
|
554 | - } |
|
555 | - |
|
556 | - /** |
|
557 | - * @PasswordConfirmationRequired |
|
558 | - * @NoAdminRequired |
|
559 | - * |
|
560 | - * @param string $userId |
|
561 | - * @return DataResponse |
|
562 | - * @throws OCSException |
|
563 | - */ |
|
564 | - public function deleteUser(string $userId): DataResponse { |
|
565 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
566 | - |
|
567 | - $targetUser = $this->userManager->get($userId); |
|
568 | - |
|
569 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
570 | - throw new OCSException('', 101); |
|
571 | - } |
|
572 | - |
|
573 | - // If not permitted |
|
574 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
575 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
576 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
577 | - } |
|
578 | - |
|
579 | - // Go ahead with the delete |
|
580 | - if($targetUser->delete()) { |
|
581 | - return new DataResponse(); |
|
582 | - } else { |
|
583 | - throw new OCSException('', 101); |
|
584 | - } |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * @PasswordConfirmationRequired |
|
589 | - * @NoAdminRequired |
|
590 | - * |
|
591 | - * @param string $userId |
|
592 | - * @return DataResponse |
|
593 | - * @throws OCSException |
|
594 | - * @throws OCSForbiddenException |
|
595 | - */ |
|
596 | - public function disableUser(string $userId): DataResponse { |
|
597 | - return $this->setEnabled($userId, false); |
|
598 | - } |
|
599 | - |
|
600 | - /** |
|
601 | - * @PasswordConfirmationRequired |
|
602 | - * @NoAdminRequired |
|
603 | - * |
|
604 | - * @param string $userId |
|
605 | - * @return DataResponse |
|
606 | - * @throws OCSException |
|
607 | - * @throws OCSForbiddenException |
|
608 | - */ |
|
609 | - public function enableUser(string $userId): DataResponse { |
|
610 | - return $this->setEnabled($userId, true); |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * @param string $userId |
|
615 | - * @param bool $value |
|
616 | - * @return DataResponse |
|
617 | - * @throws OCSException |
|
618 | - */ |
|
619 | - private function setEnabled(string $userId, bool $value): DataResponse { |
|
620 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
621 | - |
|
622 | - $targetUser = $this->userManager->get($userId); |
|
623 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
624 | - throw new OCSException('', 101); |
|
625 | - } |
|
626 | - |
|
627 | - // If not permitted |
|
628 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
629 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
630 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
631 | - } |
|
632 | - |
|
633 | - // enable/disable the user now |
|
634 | - $targetUser->setEnabled($value); |
|
635 | - return new DataResponse(); |
|
636 | - } |
|
637 | - |
|
638 | - /** |
|
639 | - * @NoAdminRequired |
|
640 | - * @NoSubAdminRequired |
|
641 | - * |
|
642 | - * @param string $userId |
|
643 | - * @return DataResponse |
|
644 | - * @throws OCSException |
|
645 | - */ |
|
646 | - public function getUsersGroups(string $userId): DataResponse { |
|
647 | - $loggedInUser = $this->userSession->getUser(); |
|
648 | - |
|
649 | - $targetUser = $this->userManager->get($userId); |
|
650 | - if($targetUser === null) { |
|
651 | - throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
652 | - } |
|
653 | - |
|
654 | - if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
655 | - // Self lookup or admin lookup |
|
656 | - return new DataResponse([ |
|
657 | - 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
|
658 | - ]); |
|
659 | - } else { |
|
660 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
661 | - |
|
662 | - // Looking up someone else |
|
663 | - if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
664 | - // Return the group that the method caller is subadmin of for the user in question |
|
665 | - /** @var IGroup[] $getSubAdminsGroups */ |
|
666 | - $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
667 | - foreach ($getSubAdminsGroups as $key => $group) { |
|
668 | - $getSubAdminsGroups[$key] = $group->getGID(); |
|
669 | - } |
|
670 | - $groups = array_intersect( |
|
671 | - $getSubAdminsGroups, |
|
672 | - $this->groupManager->getUserGroupIds($targetUser) |
|
673 | - ); |
|
674 | - return new DataResponse(['groups' => $groups]); |
|
675 | - } else { |
|
676 | - // Not permitted |
|
677 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
678 | - } |
|
679 | - } |
|
680 | - |
|
681 | - } |
|
682 | - |
|
683 | - /** |
|
684 | - * @PasswordConfirmationRequired |
|
685 | - * @NoAdminRequired |
|
686 | - * |
|
687 | - * @param string $userId |
|
688 | - * @param string $groupid |
|
689 | - * @return DataResponse |
|
690 | - * @throws OCSException |
|
691 | - */ |
|
692 | - public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
|
693 | - if($groupid === '') { |
|
694 | - throw new OCSException('', 101); |
|
695 | - } |
|
696 | - |
|
697 | - $group = $this->groupManager->get($groupid); |
|
698 | - $targetUser = $this->userManager->get($userId); |
|
699 | - if($group === null) { |
|
700 | - throw new OCSException('', 102); |
|
701 | - } |
|
702 | - if($targetUser === null) { |
|
703 | - throw new OCSException('', 103); |
|
704 | - } |
|
705 | - |
|
706 | - // If they're not an admin, check they are a subadmin of the group in question |
|
707 | - $loggedInUser = $this->userSession->getUser(); |
|
708 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
709 | - if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
710 | - throw new OCSException('', 104); |
|
711 | - } |
|
712 | - |
|
713 | - // Add user to group |
|
714 | - $group->addUser($targetUser); |
|
715 | - return new DataResponse(); |
|
716 | - } |
|
717 | - |
|
718 | - /** |
|
719 | - * @PasswordConfirmationRequired |
|
720 | - * @NoAdminRequired |
|
721 | - * |
|
722 | - * @param string $userId |
|
723 | - * @param string $groupid |
|
724 | - * @return DataResponse |
|
725 | - * @throws OCSException |
|
726 | - */ |
|
727 | - public function removeFromGroup(string $userId, string $groupid): DataResponse { |
|
728 | - $loggedInUser = $this->userSession->getUser(); |
|
729 | - |
|
730 | - if($groupid === null || trim($groupid) === '') { |
|
731 | - throw new OCSException('', 101); |
|
732 | - } |
|
733 | - |
|
734 | - $group = $this->groupManager->get($groupid); |
|
735 | - if($group === null) { |
|
736 | - throw new OCSException('', 102); |
|
737 | - } |
|
738 | - |
|
739 | - $targetUser = $this->userManager->get($userId); |
|
740 | - if($targetUser === null) { |
|
741 | - throw new OCSException('', 103); |
|
742 | - } |
|
743 | - |
|
744 | - // If they're not an admin, check they are a subadmin of the group in question |
|
745 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
746 | - if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
747 | - throw new OCSException('', 104); |
|
748 | - } |
|
749 | - |
|
750 | - // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
|
751 | - if ($targetUser->getUID() === $loggedInUser->getUID()) { |
|
752 | - if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
753 | - if ($group->getGID() === 'admin') { |
|
754 | - throw new OCSException('Cannot remove yourself from the admin group', 105); |
|
755 | - } |
|
756 | - } else { |
|
757 | - // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
|
758 | - throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
|
759 | - } |
|
760 | - |
|
761 | - } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
762 | - /** @var IGroup[] $subAdminGroups */ |
|
763 | - $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
764 | - $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
765 | - return $subAdminGroup->getGID(); |
|
766 | - }, $subAdminGroups); |
|
767 | - $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
|
768 | - $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
|
769 | - |
|
770 | - if (count($userSubAdminGroups) <= 1) { |
|
771 | - // Subadmin must not be able to remove a user from all their subadmin groups. |
|
772 | - throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105); |
|
773 | - } |
|
774 | - } |
|
775 | - |
|
776 | - // Remove user from group |
|
777 | - $group->removeUser($targetUser); |
|
778 | - return new DataResponse(); |
|
779 | - } |
|
780 | - |
|
781 | - /** |
|
782 | - * Creates a subadmin |
|
783 | - * |
|
784 | - * @PasswordConfirmationRequired |
|
785 | - * |
|
786 | - * @param string $userId |
|
787 | - * @param string $groupid |
|
788 | - * @return DataResponse |
|
789 | - * @throws OCSException |
|
790 | - */ |
|
791 | - public function addSubAdmin(string $userId, string $groupid): DataResponse { |
|
792 | - $group = $this->groupManager->get($groupid); |
|
793 | - $user = $this->userManager->get($userId); |
|
794 | - |
|
795 | - // Check if the user exists |
|
796 | - if($user === null) { |
|
797 | - throw new OCSException('User does not exist', 101); |
|
798 | - } |
|
799 | - // Check if group exists |
|
800 | - if($group === null) { |
|
801 | - throw new OCSException('Group does not exist', 102); |
|
802 | - } |
|
803 | - // Check if trying to make subadmin of admin group |
|
804 | - if($group->getGID() === 'admin') { |
|
805 | - throw new OCSException('Cannot create subadmins for admin group', 103); |
|
806 | - } |
|
807 | - |
|
808 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
809 | - |
|
810 | - // We cannot be subadmin twice |
|
811 | - if ($subAdminManager->isSubAdminofGroup($user, $group)) { |
|
812 | - return new DataResponse(); |
|
813 | - } |
|
814 | - // Go |
|
815 | - if($subAdminManager->createSubAdmin($user, $group)) { |
|
816 | - return new DataResponse(); |
|
817 | - } else { |
|
818 | - throw new OCSException('Unknown error occurred', 103); |
|
819 | - } |
|
820 | - } |
|
821 | - |
|
822 | - /** |
|
823 | - * Removes a subadmin from a group |
|
824 | - * |
|
825 | - * @PasswordConfirmationRequired |
|
826 | - * |
|
827 | - * @param string $userId |
|
828 | - * @param string $groupid |
|
829 | - * @return DataResponse |
|
830 | - * @throws OCSException |
|
831 | - */ |
|
832 | - public function removeSubAdmin(string $userId, string $groupid): DataResponse { |
|
833 | - $group = $this->groupManager->get($groupid); |
|
834 | - $user = $this->userManager->get($userId); |
|
835 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
836 | - |
|
837 | - // Check if the user exists |
|
838 | - if($user === null) { |
|
839 | - throw new OCSException('User does not exist', 101); |
|
840 | - } |
|
841 | - // Check if the group exists |
|
842 | - if($group === null) { |
|
843 | - throw new OCSException('Group does not exist', 101); |
|
844 | - } |
|
845 | - // Check if they are a subadmin of this said group |
|
846 | - if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
847 | - throw new OCSException('User is not a subadmin of this group', 102); |
|
848 | - } |
|
849 | - |
|
850 | - // Go |
|
851 | - if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
852 | - return new DataResponse(); |
|
853 | - } else { |
|
854 | - throw new OCSException('Unknown error occurred', 103); |
|
855 | - } |
|
856 | - } |
|
857 | - |
|
858 | - /** |
|
859 | - * Get the groups a user is a subadmin of |
|
860 | - * |
|
861 | - * @param string $userId |
|
862 | - * @return array |
|
863 | - * @throws OCSException |
|
864 | - */ |
|
865 | - protected function getUserSubAdminGroupsData(string $userId): array { |
|
866 | - $user = $this->userManager->get($userId); |
|
867 | - // Check if the user exists |
|
868 | - if($user === null) { |
|
869 | - throw new OCSException('User does not exist', 101); |
|
870 | - } |
|
871 | - |
|
872 | - // Get the subadmin groups |
|
873 | - $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
874 | - $groups = []; |
|
875 | - foreach ($subAdminGroups as $key => $group) { |
|
876 | - $groups[] = $group->getGID(); |
|
877 | - } |
|
878 | - |
|
879 | - return $groups; |
|
880 | - } |
|
881 | - |
|
882 | - /** |
|
883 | - * Get the groups a user is a subadmin of |
|
884 | - * |
|
885 | - * @param string $userId |
|
886 | - * @return DataResponse |
|
887 | - * @throws OCSException |
|
888 | - */ |
|
889 | - public function getUserSubAdminGroups(string $userId): DataResponse { |
|
890 | - $groups = $this->getUserSubAdminGroupsData($userId); |
|
891 | - return new DataResponse($groups); |
|
892 | - } |
|
893 | - |
|
894 | - /** |
|
895 | - * @param string $userId |
|
896 | - * @return array |
|
897 | - * @throws \OCP\Files\NotFoundException |
|
898 | - */ |
|
899 | - protected function fillStorageInfo(string $userId): array { |
|
900 | - try { |
|
901 | - \OC_Util::tearDownFS(); |
|
902 | - \OC_Util::setupFS($userId); |
|
903 | - $storage = OC_Helper::getStorageInfo('/'); |
|
904 | - $data = [ |
|
905 | - 'free' => $storage['free'], |
|
906 | - 'used' => $storage['used'], |
|
907 | - 'total' => $storage['total'], |
|
908 | - 'relative' => $storage['relative'], |
|
909 | - 'quota' => $storage['quota'], |
|
910 | - ]; |
|
911 | - } catch (NotFoundException $ex) { |
|
912 | - $data = []; |
|
913 | - } |
|
914 | - return $data; |
|
915 | - } |
|
916 | - |
|
917 | - /** |
|
918 | - * @NoAdminRequired |
|
919 | - * @PasswordConfirmationRequired |
|
920 | - * |
|
921 | - * resend welcome message |
|
922 | - * |
|
923 | - * @param string $userId |
|
924 | - * @return DataResponse |
|
925 | - * @throws OCSException |
|
926 | - */ |
|
927 | - public function resendWelcomeMessage(string $userId): DataResponse { |
|
928 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
929 | - |
|
930 | - $targetUser = $this->userManager->get($userId); |
|
931 | - if($targetUser === null) { |
|
932 | - throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
933 | - } |
|
934 | - |
|
935 | - // Check if admin / subadmin |
|
936 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
937 | - if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
938 | - && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
939 | - // No rights |
|
940 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
941 | - } |
|
942 | - |
|
943 | - $email = $targetUser->getEMailAddress(); |
|
944 | - if ($email === '' || $email === null) { |
|
945 | - throw new OCSException('Email address not available', 101); |
|
946 | - } |
|
947 | - $username = $targetUser->getUID(); |
|
948 | - $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); |
|
949 | - if (!$this->l10nFactory->languageExists('settings', $lang)) { |
|
950 | - $lang = 'en'; |
|
951 | - } |
|
952 | - |
|
953 | - $l10n = $this->l10nFactory->get('settings', $lang); |
|
954 | - |
|
955 | - try { |
|
956 | - $this->newUserMailHelper->setL10N($l10n); |
|
957 | - $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
|
958 | - $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
|
959 | - } catch(\Exception $e) { |
|
960 | - $this->logger->logException($e, [ |
|
961 | - 'message' => "Can't send new user mail to $email", |
|
962 | - 'level' => \OCP\Util::ERROR, |
|
963 | - 'app' => 'settings', |
|
964 | - ]); |
|
965 | - throw new OCSException('Sending email failed', 102); |
|
966 | - } |
|
967 | - |
|
968 | - return new DataResponse(); |
|
969 | - } |
|
57 | + /** @var IUserManager */ |
|
58 | + private $userManager; |
|
59 | + /** @var IConfig */ |
|
60 | + private $config; |
|
61 | + /** @var IAppManager */ |
|
62 | + private $appManager; |
|
63 | + /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface |
|
64 | + private $groupManager; |
|
65 | + /** @var IUserSession */ |
|
66 | + private $userSession; |
|
67 | + /** @var AccountManager */ |
|
68 | + private $accountManager; |
|
69 | + /** @var ILogger */ |
|
70 | + private $logger; |
|
71 | + /** @var IFactory */ |
|
72 | + private $l10nFactory; |
|
73 | + /** @var NewUserMailHelper */ |
|
74 | + private $newUserMailHelper; |
|
75 | + /** @var FederatedFileSharingFactory */ |
|
76 | + private $federatedFileSharingFactory; |
|
77 | + /** @var ISecureRandom */ |
|
78 | + private $secureRandom; |
|
79 | + |
|
80 | + /** |
|
81 | + * @param string $appName |
|
82 | + * @param IRequest $request |
|
83 | + * @param IUserManager $userManager |
|
84 | + * @param IConfig $config |
|
85 | + * @param IAppManager $appManager |
|
86 | + * @param IGroupManager $groupManager |
|
87 | + * @param IUserSession $userSession |
|
88 | + * @param AccountManager $accountManager |
|
89 | + * @param ILogger $logger |
|
90 | + * @param IFactory $l10nFactory |
|
91 | + * @param NewUserMailHelper $newUserMailHelper |
|
92 | + * @param FederatedFileSharingFactory $federatedFileSharingFactory |
|
93 | + * @param ISecureRandom $secureRandom |
|
94 | + */ |
|
95 | + public function __construct(string $appName, |
|
96 | + IRequest $request, |
|
97 | + IUserManager $userManager, |
|
98 | + IConfig $config, |
|
99 | + IAppManager $appManager, |
|
100 | + IGroupManager $groupManager, |
|
101 | + IUserSession $userSession, |
|
102 | + AccountManager $accountManager, |
|
103 | + ILogger $logger, |
|
104 | + IFactory $l10nFactory, |
|
105 | + NewUserMailHelper $newUserMailHelper, |
|
106 | + FederatedFileSharingFactory $federatedFileSharingFactory, |
|
107 | + ISecureRandom $secureRandom) { |
|
108 | + parent::__construct($appName, $request); |
|
109 | + |
|
110 | + $this->userManager = $userManager; |
|
111 | + $this->config = $config; |
|
112 | + $this->appManager = $appManager; |
|
113 | + $this->groupManager = $groupManager; |
|
114 | + $this->userSession = $userSession; |
|
115 | + $this->accountManager = $accountManager; |
|
116 | + $this->logger = $logger; |
|
117 | + $this->l10nFactory = $l10nFactory; |
|
118 | + $this->newUserMailHelper = $newUserMailHelper; |
|
119 | + $this->federatedFileSharingFactory = $federatedFileSharingFactory; |
|
120 | + $this->secureRandom = $secureRandom; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @NoAdminRequired |
|
125 | + * |
|
126 | + * returns a list of users |
|
127 | + * |
|
128 | + * @param string $search |
|
129 | + * @param int $limit |
|
130 | + * @param int $offset |
|
131 | + * @return DataResponse |
|
132 | + */ |
|
133 | + public function getUsers(string $search = '', $limit = null, $offset = 0): DataResponse { |
|
134 | + $user = $this->userSession->getUser(); |
|
135 | + $users = []; |
|
136 | + |
|
137 | + // Admin? Or SubAdmin? |
|
138 | + $uid = $user->getUID(); |
|
139 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
140 | + if($this->groupManager->isAdmin($uid)){ |
|
141 | + $users = $this->userManager->search($search, $limit, $offset); |
|
142 | + } else if ($subAdminManager->isSubAdmin($user)) { |
|
143 | + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
144 | + foreach ($subAdminOfGroups as $key => $group) { |
|
145 | + $subAdminOfGroups[$key] = $group->getGID(); |
|
146 | + } |
|
147 | + |
|
148 | + $users = []; |
|
149 | + foreach ($subAdminOfGroups as $group) { |
|
150 | + $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + $users = array_keys($users); |
|
155 | + |
|
156 | + return new DataResponse([ |
|
157 | + 'users' => $users |
|
158 | + ]); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @NoAdminRequired |
|
163 | + * |
|
164 | + * returns a list of users and their data |
|
165 | + */ |
|
166 | + public function getUsersDetails(string $search = '', $limit = null, $offset = 0): DataResponse { |
|
167 | + $user = $this->userSession->getUser(); |
|
168 | + $users = []; |
|
169 | + |
|
170 | + // Admin? Or SubAdmin? |
|
171 | + $uid = $user->getUID(); |
|
172 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
173 | + if($this->groupManager->isAdmin($uid)){ |
|
174 | + $users = $this->userManager->search($search, $limit, $offset); |
|
175 | + } else if ($subAdminManager->isSubAdmin($user)) { |
|
176 | + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
177 | + foreach ($subAdminOfGroups as $key => $group) { |
|
178 | + $subAdminOfGroups[$key] = $group->getGID(); |
|
179 | + } |
|
180 | + |
|
181 | + $users = []; |
|
182 | + foreach ($subAdminOfGroups as $group) { |
|
183 | + $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + $users = array_keys($users); |
|
188 | + $usersDetails = []; |
|
189 | + foreach ($users as $key => $userId) { |
|
190 | + $userData = $this->getUserData($userId); |
|
191 | + // Do not insert empty entry |
|
192 | + if(!empty($userData)) { |
|
193 | + $usersDetails[$userId] = $userData; |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + return new DataResponse([ |
|
198 | + 'users' => $usersDetails |
|
199 | + ]); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @PasswordConfirmationRequired |
|
204 | + * @NoAdminRequired |
|
205 | + * |
|
206 | + * @param string $userid |
|
207 | + * @param string $password |
|
208 | + * @param string $email |
|
209 | + * @param array $groups |
|
210 | + * @return DataResponse |
|
211 | + * @throws OCSException |
|
212 | + */ |
|
213 | + public function addUser(string $userid, string $password = '', string $email='', array $groups = []): DataResponse { |
|
214 | + $user = $this->userSession->getUser(); |
|
215 | + $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
216 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
217 | + |
|
218 | + if($this->userManager->userExists($userid)) { |
|
219 | + $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
|
220 | + throw new OCSException('User already exists', 102); |
|
221 | + } |
|
222 | + |
|
223 | + if($groups !== []) { |
|
224 | + foreach ($groups as $group) { |
|
225 | + if(!$this->groupManager->groupExists($group)) { |
|
226 | + throw new OCSException('group '.$group.' does not exist', 104); |
|
227 | + } |
|
228 | + if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
229 | + throw new OCSException('insufficient privileges for group '. $group, 105); |
|
230 | + } |
|
231 | + } |
|
232 | + } else { |
|
233 | + if(!$isAdmin) { |
|
234 | + throw new OCSException('no group specified (required for subadmins)', 106); |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + $generatePasswordResetToken = false; |
|
239 | + if ($password === '') { |
|
240 | + if ($email === '') { |
|
241 | + throw new OCSException('To send a password link to the user an email address is required.', 108); |
|
242 | + } |
|
243 | + |
|
244 | + $password = $this->secureRandom->generate(10); |
|
245 | + // Make sure we pass the password_policy |
|
246 | + $password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()'); |
|
247 | + $generatePasswordResetToken = true; |
|
248 | + } |
|
249 | + |
|
250 | + try { |
|
251 | + $newUser = $this->userManager->createUser($userid, $password); |
|
252 | + $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); |
|
253 | + |
|
254 | + foreach ($groups as $group) { |
|
255 | + $this->groupManager->get($group)->addUser($newUser); |
|
256 | + $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); |
|
257 | + } |
|
258 | + |
|
259 | + // Send new user mail only if a mail is set |
|
260 | + if ($email !== '') { |
|
261 | + $newUser->setEMailAddress($email); |
|
262 | + try { |
|
263 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken); |
|
264 | + $this->newUserMailHelper->sendMail($newUser, $emailTemplate); |
|
265 | + } catch (\Exception $e) { |
|
266 | + $this->logger->logException($e, [ |
|
267 | + 'message' => "Can't send new user mail to $email", |
|
268 | + 'level' => \OCP\Util::ERROR, |
|
269 | + 'app' => 'ocs_api', |
|
270 | + ]); |
|
271 | + throw new OCSException('Unable to send the invitation mail', 109); |
|
272 | + } |
|
273 | + } |
|
274 | + |
|
275 | + return new DataResponse(); |
|
276 | + |
|
277 | + } catch (HintException $e ) { |
|
278 | + $this->logger->logException($e, [ |
|
279 | + 'message' => 'Failed addUser attempt with hint exception.', |
|
280 | + 'level' => \OCP\Util::WARN, |
|
281 | + 'app' => 'ocs_api', |
|
282 | + ]); |
|
283 | + throw new OCSException($e->getHint(), 107); |
|
284 | + } catch (\Exception $e) { |
|
285 | + $this->logger->logException($e, [ |
|
286 | + 'message' => 'Failed addUser attempt with exception.', |
|
287 | + 'level' => \OCP\Util::ERROR, |
|
288 | + 'app' => 'ocs_api', |
|
289 | + ]); |
|
290 | + throw new OCSException('Bad request', 101); |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * @NoAdminRequired |
|
296 | + * @NoSubAdminRequired |
|
297 | + * |
|
298 | + * gets user info |
|
299 | + * |
|
300 | + * @param string $userId |
|
301 | + * @return DataResponse |
|
302 | + * @throws OCSException |
|
303 | + */ |
|
304 | + public function getUser(string $userId): DataResponse { |
|
305 | + $data = $this->getUserData($userId); |
|
306 | + // getUserData returns empty array if not enough permissions |
|
307 | + if(empty($data)) { |
|
308 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
309 | + } |
|
310 | + return new DataResponse($data); |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @NoAdminRequired |
|
315 | + * @NoSubAdminRequired |
|
316 | + * |
|
317 | + * gets user info from the currently logged in user |
|
318 | + * |
|
319 | + * @return DataResponse |
|
320 | + * @throws OCSException |
|
321 | + */ |
|
322 | + public function getCurrentUser(): DataResponse { |
|
323 | + $user = $this->userSession->getUser(); |
|
324 | + if ($user) { |
|
325 | + $data = $this->getUserData($user->getUID()); |
|
326 | + // rename "displayname" to "display-name" only for this call to keep |
|
327 | + // the API stable. |
|
328 | + $data['display-name'] = $data['displayname']; |
|
329 | + unset($data['displayname']); |
|
330 | + return new DataResponse($data); |
|
331 | + |
|
332 | + } |
|
333 | + |
|
334 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * creates a array with all user data |
|
339 | + * |
|
340 | + * @param $userId |
|
341 | + * @return array |
|
342 | + * @throws OCSException |
|
343 | + */ |
|
344 | + protected function getUserData(string $userId): array { |
|
345 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
346 | + |
|
347 | + $data = []; |
|
348 | + |
|
349 | + // Check if the target user exists |
|
350 | + $targetUserObject = $this->userManager->get($userId); |
|
351 | + if($targetUserObject === null) { |
|
352 | + throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
353 | + } |
|
354 | + |
|
355 | + // Should be at least Admin Or SubAdmin! |
|
356 | + if( $this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
357 | + || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
358 | + $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
|
359 | + } else { |
|
360 | + // Check they are looking up themselves |
|
361 | + if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
362 | + return $data; |
|
363 | + } |
|
364 | + } |
|
365 | + |
|
366 | + // Get groups data |
|
367 | + $userAccount = $this->accountManager->getUser($targetUserObject); |
|
368 | + $groups = $this->groupManager->getUserGroups($targetUserObject); |
|
369 | + $gids = []; |
|
370 | + foreach ($groups as $group) { |
|
371 | + $gids[] = $group->getDisplayName(); |
|
372 | + } |
|
373 | + |
|
374 | + // Find the data |
|
375 | + $data['id'] = $targetUserObject->getUID(); |
|
376 | + $data['storageLocation'] = $targetUserObject->getHome(); |
|
377 | + $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000; |
|
378 | + $data['backend'] = $targetUserObject->getBackendClassName(); |
|
379 | + $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID()); |
|
380 | + $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
381 | + $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
382 | + $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
383 | + $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value']; |
|
384 | + $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value']; |
|
385 | + $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value']; |
|
386 | + $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value']; |
|
387 | + $data['groups'] = $gids; |
|
388 | + $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang'); |
|
389 | + |
|
390 | + return $data; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * @NoAdminRequired |
|
395 | + * @NoSubAdminRequired |
|
396 | + */ |
|
397 | + public function getEditableFields(): DataResponse { |
|
398 | + $permittedFields = []; |
|
399 | + |
|
400 | + // Editing self (display, email) |
|
401 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
402 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
403 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
404 | + } |
|
405 | + |
|
406 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
407 | + $federatedFileSharing = $this->federatedFileSharingFactory->get(); |
|
408 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
409 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
410 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
411 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
412 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
413 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + return new DataResponse($permittedFields); |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * @NoAdminRequired |
|
422 | + * @NoSubAdminRequired |
|
423 | + * @PasswordConfirmationRequired |
|
424 | + * |
|
425 | + * edit users |
|
426 | + * |
|
427 | + * @param string $userId |
|
428 | + * @param string $key |
|
429 | + * @param string $value |
|
430 | + * @return DataResponse |
|
431 | + * @throws OCSException |
|
432 | + */ |
|
433 | + public function editUser(string $userId, string $key, string $value): DataResponse { |
|
434 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
435 | + |
|
436 | + $targetUser = $this->userManager->get($userId); |
|
437 | + if($targetUser === null) { |
|
438 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
439 | + } |
|
440 | + |
|
441 | + $permittedFields = []; |
|
442 | + if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
443 | + // Editing self (display, email) |
|
444 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
445 | + $permittedFields[] = 'display'; |
|
446 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
447 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
448 | + } |
|
449 | + |
|
450 | + $permittedFields[] = 'password'; |
|
451 | + if ($this->config->getSystemValue('force_language', false) === false || |
|
452 | + $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
453 | + $permittedFields[] = 'language'; |
|
454 | + } |
|
455 | + |
|
456 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
457 | + $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
458 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
459 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
460 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
461 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
462 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
463 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
464 | + } |
|
465 | + } |
|
466 | + |
|
467 | + // If admin they can edit their own quota |
|
468 | + if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
469 | + $permittedFields[] = 'quota'; |
|
470 | + } |
|
471 | + } else { |
|
472 | + // Check if admin / subadmin |
|
473 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
474 | + if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
475 | + || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
476 | + // They have permissions over the user |
|
477 | + $permittedFields[] = 'display'; |
|
478 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
479 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
480 | + $permittedFields[] = 'password'; |
|
481 | + $permittedFields[] = 'language'; |
|
482 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
483 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
484 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
485 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
486 | + $permittedFields[] = 'quota'; |
|
487 | + } else { |
|
488 | + // No rights |
|
489 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
490 | + } |
|
491 | + } |
|
492 | + // Check if permitted to edit this field |
|
493 | + if(!in_array($key, $permittedFields)) { |
|
494 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
495 | + } |
|
496 | + // Process the edit |
|
497 | + switch($key) { |
|
498 | + case 'display': |
|
499 | + case AccountManager::PROPERTY_DISPLAYNAME: |
|
500 | + $targetUser->setDisplayName($value); |
|
501 | + break; |
|
502 | + case 'quota': |
|
503 | + $quota = $value; |
|
504 | + if($quota !== 'none' && $quota !== 'default') { |
|
505 | + if (is_numeric($quota)) { |
|
506 | + $quota = (float) $quota; |
|
507 | + } else { |
|
508 | + $quota = \OCP\Util::computerFileSize($quota); |
|
509 | + } |
|
510 | + if ($quota === false) { |
|
511 | + throw new OCSException('Invalid quota value '.$value, 103); |
|
512 | + } |
|
513 | + if($quota === 0) { |
|
514 | + $quota = 'default'; |
|
515 | + }else if($quota === -1) { |
|
516 | + $quota = 'none'; |
|
517 | + } else { |
|
518 | + $quota = \OCP\Util::humanFileSize($quota); |
|
519 | + } |
|
520 | + } |
|
521 | + $targetUser->setQuota($quota); |
|
522 | + break; |
|
523 | + case 'password': |
|
524 | + $targetUser->setPassword($value); |
|
525 | + break; |
|
526 | + case 'language': |
|
527 | + $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
|
528 | + if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
|
529 | + throw new OCSException('Invalid language', 102); |
|
530 | + } |
|
531 | + $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
|
532 | + break; |
|
533 | + case AccountManager::PROPERTY_EMAIL: |
|
534 | + if(filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { |
|
535 | + $targetUser->setEMailAddress($value); |
|
536 | + } else { |
|
537 | + throw new OCSException('', 102); |
|
538 | + } |
|
539 | + break; |
|
540 | + case AccountManager::PROPERTY_PHONE: |
|
541 | + case AccountManager::PROPERTY_ADDRESS: |
|
542 | + case AccountManager::PROPERTY_WEBSITE: |
|
543 | + case AccountManager::PROPERTY_TWITTER: |
|
544 | + $userAccount = $this->accountManager->getUser($targetUser); |
|
545 | + if ($userAccount[$key]['value'] !== $value) { |
|
546 | + $userAccount[$key]['value'] = $value; |
|
547 | + $this->accountManager->updateUser($targetUser, $userAccount); |
|
548 | + } |
|
549 | + break; |
|
550 | + default: |
|
551 | + throw new OCSException('', 103); |
|
552 | + } |
|
553 | + return new DataResponse(); |
|
554 | + } |
|
555 | + |
|
556 | + /** |
|
557 | + * @PasswordConfirmationRequired |
|
558 | + * @NoAdminRequired |
|
559 | + * |
|
560 | + * @param string $userId |
|
561 | + * @return DataResponse |
|
562 | + * @throws OCSException |
|
563 | + */ |
|
564 | + public function deleteUser(string $userId): DataResponse { |
|
565 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
566 | + |
|
567 | + $targetUser = $this->userManager->get($userId); |
|
568 | + |
|
569 | + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
570 | + throw new OCSException('', 101); |
|
571 | + } |
|
572 | + |
|
573 | + // If not permitted |
|
574 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
575 | + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
576 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
577 | + } |
|
578 | + |
|
579 | + // Go ahead with the delete |
|
580 | + if($targetUser->delete()) { |
|
581 | + return new DataResponse(); |
|
582 | + } else { |
|
583 | + throw new OCSException('', 101); |
|
584 | + } |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * @PasswordConfirmationRequired |
|
589 | + * @NoAdminRequired |
|
590 | + * |
|
591 | + * @param string $userId |
|
592 | + * @return DataResponse |
|
593 | + * @throws OCSException |
|
594 | + * @throws OCSForbiddenException |
|
595 | + */ |
|
596 | + public function disableUser(string $userId): DataResponse { |
|
597 | + return $this->setEnabled($userId, false); |
|
598 | + } |
|
599 | + |
|
600 | + /** |
|
601 | + * @PasswordConfirmationRequired |
|
602 | + * @NoAdminRequired |
|
603 | + * |
|
604 | + * @param string $userId |
|
605 | + * @return DataResponse |
|
606 | + * @throws OCSException |
|
607 | + * @throws OCSForbiddenException |
|
608 | + */ |
|
609 | + public function enableUser(string $userId): DataResponse { |
|
610 | + return $this->setEnabled($userId, true); |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * @param string $userId |
|
615 | + * @param bool $value |
|
616 | + * @return DataResponse |
|
617 | + * @throws OCSException |
|
618 | + */ |
|
619 | + private function setEnabled(string $userId, bool $value): DataResponse { |
|
620 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
621 | + |
|
622 | + $targetUser = $this->userManager->get($userId); |
|
623 | + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
624 | + throw new OCSException('', 101); |
|
625 | + } |
|
626 | + |
|
627 | + // If not permitted |
|
628 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
629 | + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
630 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
631 | + } |
|
632 | + |
|
633 | + // enable/disable the user now |
|
634 | + $targetUser->setEnabled($value); |
|
635 | + return new DataResponse(); |
|
636 | + } |
|
637 | + |
|
638 | + /** |
|
639 | + * @NoAdminRequired |
|
640 | + * @NoSubAdminRequired |
|
641 | + * |
|
642 | + * @param string $userId |
|
643 | + * @return DataResponse |
|
644 | + * @throws OCSException |
|
645 | + */ |
|
646 | + public function getUsersGroups(string $userId): DataResponse { |
|
647 | + $loggedInUser = $this->userSession->getUser(); |
|
648 | + |
|
649 | + $targetUser = $this->userManager->get($userId); |
|
650 | + if($targetUser === null) { |
|
651 | + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
652 | + } |
|
653 | + |
|
654 | + if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
655 | + // Self lookup or admin lookup |
|
656 | + return new DataResponse([ |
|
657 | + 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
|
658 | + ]); |
|
659 | + } else { |
|
660 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
661 | + |
|
662 | + // Looking up someone else |
|
663 | + if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
664 | + // Return the group that the method caller is subadmin of for the user in question |
|
665 | + /** @var IGroup[] $getSubAdminsGroups */ |
|
666 | + $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
667 | + foreach ($getSubAdminsGroups as $key => $group) { |
|
668 | + $getSubAdminsGroups[$key] = $group->getGID(); |
|
669 | + } |
|
670 | + $groups = array_intersect( |
|
671 | + $getSubAdminsGroups, |
|
672 | + $this->groupManager->getUserGroupIds($targetUser) |
|
673 | + ); |
|
674 | + return new DataResponse(['groups' => $groups]); |
|
675 | + } else { |
|
676 | + // Not permitted |
|
677 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
678 | + } |
|
679 | + } |
|
680 | + |
|
681 | + } |
|
682 | + |
|
683 | + /** |
|
684 | + * @PasswordConfirmationRequired |
|
685 | + * @NoAdminRequired |
|
686 | + * |
|
687 | + * @param string $userId |
|
688 | + * @param string $groupid |
|
689 | + * @return DataResponse |
|
690 | + * @throws OCSException |
|
691 | + */ |
|
692 | + public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
|
693 | + if($groupid === '') { |
|
694 | + throw new OCSException('', 101); |
|
695 | + } |
|
696 | + |
|
697 | + $group = $this->groupManager->get($groupid); |
|
698 | + $targetUser = $this->userManager->get($userId); |
|
699 | + if($group === null) { |
|
700 | + throw new OCSException('', 102); |
|
701 | + } |
|
702 | + if($targetUser === null) { |
|
703 | + throw new OCSException('', 103); |
|
704 | + } |
|
705 | + |
|
706 | + // If they're not an admin, check they are a subadmin of the group in question |
|
707 | + $loggedInUser = $this->userSession->getUser(); |
|
708 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
709 | + if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
710 | + throw new OCSException('', 104); |
|
711 | + } |
|
712 | + |
|
713 | + // Add user to group |
|
714 | + $group->addUser($targetUser); |
|
715 | + return new DataResponse(); |
|
716 | + } |
|
717 | + |
|
718 | + /** |
|
719 | + * @PasswordConfirmationRequired |
|
720 | + * @NoAdminRequired |
|
721 | + * |
|
722 | + * @param string $userId |
|
723 | + * @param string $groupid |
|
724 | + * @return DataResponse |
|
725 | + * @throws OCSException |
|
726 | + */ |
|
727 | + public function removeFromGroup(string $userId, string $groupid): DataResponse { |
|
728 | + $loggedInUser = $this->userSession->getUser(); |
|
729 | + |
|
730 | + if($groupid === null || trim($groupid) === '') { |
|
731 | + throw new OCSException('', 101); |
|
732 | + } |
|
733 | + |
|
734 | + $group = $this->groupManager->get($groupid); |
|
735 | + if($group === null) { |
|
736 | + throw new OCSException('', 102); |
|
737 | + } |
|
738 | + |
|
739 | + $targetUser = $this->userManager->get($userId); |
|
740 | + if($targetUser === null) { |
|
741 | + throw new OCSException('', 103); |
|
742 | + } |
|
743 | + |
|
744 | + // If they're not an admin, check they are a subadmin of the group in question |
|
745 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
746 | + if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
747 | + throw new OCSException('', 104); |
|
748 | + } |
|
749 | + |
|
750 | + // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
|
751 | + if ($targetUser->getUID() === $loggedInUser->getUID()) { |
|
752 | + if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
753 | + if ($group->getGID() === 'admin') { |
|
754 | + throw new OCSException('Cannot remove yourself from the admin group', 105); |
|
755 | + } |
|
756 | + } else { |
|
757 | + // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
|
758 | + throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
|
759 | + } |
|
760 | + |
|
761 | + } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
762 | + /** @var IGroup[] $subAdminGroups */ |
|
763 | + $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
764 | + $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
765 | + return $subAdminGroup->getGID(); |
|
766 | + }, $subAdminGroups); |
|
767 | + $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
|
768 | + $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
|
769 | + |
|
770 | + if (count($userSubAdminGroups) <= 1) { |
|
771 | + // Subadmin must not be able to remove a user from all their subadmin groups. |
|
772 | + throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105); |
|
773 | + } |
|
774 | + } |
|
775 | + |
|
776 | + // Remove user from group |
|
777 | + $group->removeUser($targetUser); |
|
778 | + return new DataResponse(); |
|
779 | + } |
|
780 | + |
|
781 | + /** |
|
782 | + * Creates a subadmin |
|
783 | + * |
|
784 | + * @PasswordConfirmationRequired |
|
785 | + * |
|
786 | + * @param string $userId |
|
787 | + * @param string $groupid |
|
788 | + * @return DataResponse |
|
789 | + * @throws OCSException |
|
790 | + */ |
|
791 | + public function addSubAdmin(string $userId, string $groupid): DataResponse { |
|
792 | + $group = $this->groupManager->get($groupid); |
|
793 | + $user = $this->userManager->get($userId); |
|
794 | + |
|
795 | + // Check if the user exists |
|
796 | + if($user === null) { |
|
797 | + throw new OCSException('User does not exist', 101); |
|
798 | + } |
|
799 | + // Check if group exists |
|
800 | + if($group === null) { |
|
801 | + throw new OCSException('Group does not exist', 102); |
|
802 | + } |
|
803 | + // Check if trying to make subadmin of admin group |
|
804 | + if($group->getGID() === 'admin') { |
|
805 | + throw new OCSException('Cannot create subadmins for admin group', 103); |
|
806 | + } |
|
807 | + |
|
808 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
809 | + |
|
810 | + // We cannot be subadmin twice |
|
811 | + if ($subAdminManager->isSubAdminofGroup($user, $group)) { |
|
812 | + return new DataResponse(); |
|
813 | + } |
|
814 | + // Go |
|
815 | + if($subAdminManager->createSubAdmin($user, $group)) { |
|
816 | + return new DataResponse(); |
|
817 | + } else { |
|
818 | + throw new OCSException('Unknown error occurred', 103); |
|
819 | + } |
|
820 | + } |
|
821 | + |
|
822 | + /** |
|
823 | + * Removes a subadmin from a group |
|
824 | + * |
|
825 | + * @PasswordConfirmationRequired |
|
826 | + * |
|
827 | + * @param string $userId |
|
828 | + * @param string $groupid |
|
829 | + * @return DataResponse |
|
830 | + * @throws OCSException |
|
831 | + */ |
|
832 | + public function removeSubAdmin(string $userId, string $groupid): DataResponse { |
|
833 | + $group = $this->groupManager->get($groupid); |
|
834 | + $user = $this->userManager->get($userId); |
|
835 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
836 | + |
|
837 | + // Check if the user exists |
|
838 | + if($user === null) { |
|
839 | + throw new OCSException('User does not exist', 101); |
|
840 | + } |
|
841 | + // Check if the group exists |
|
842 | + if($group === null) { |
|
843 | + throw new OCSException('Group does not exist', 101); |
|
844 | + } |
|
845 | + // Check if they are a subadmin of this said group |
|
846 | + if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
847 | + throw new OCSException('User is not a subadmin of this group', 102); |
|
848 | + } |
|
849 | + |
|
850 | + // Go |
|
851 | + if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
852 | + return new DataResponse(); |
|
853 | + } else { |
|
854 | + throw new OCSException('Unknown error occurred', 103); |
|
855 | + } |
|
856 | + } |
|
857 | + |
|
858 | + /** |
|
859 | + * Get the groups a user is a subadmin of |
|
860 | + * |
|
861 | + * @param string $userId |
|
862 | + * @return array |
|
863 | + * @throws OCSException |
|
864 | + */ |
|
865 | + protected function getUserSubAdminGroupsData(string $userId): array { |
|
866 | + $user = $this->userManager->get($userId); |
|
867 | + // Check if the user exists |
|
868 | + if($user === null) { |
|
869 | + throw new OCSException('User does not exist', 101); |
|
870 | + } |
|
871 | + |
|
872 | + // Get the subadmin groups |
|
873 | + $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
874 | + $groups = []; |
|
875 | + foreach ($subAdminGroups as $key => $group) { |
|
876 | + $groups[] = $group->getGID(); |
|
877 | + } |
|
878 | + |
|
879 | + return $groups; |
|
880 | + } |
|
881 | + |
|
882 | + /** |
|
883 | + * Get the groups a user is a subadmin of |
|
884 | + * |
|
885 | + * @param string $userId |
|
886 | + * @return DataResponse |
|
887 | + * @throws OCSException |
|
888 | + */ |
|
889 | + public function getUserSubAdminGroups(string $userId): DataResponse { |
|
890 | + $groups = $this->getUserSubAdminGroupsData($userId); |
|
891 | + return new DataResponse($groups); |
|
892 | + } |
|
893 | + |
|
894 | + /** |
|
895 | + * @param string $userId |
|
896 | + * @return array |
|
897 | + * @throws \OCP\Files\NotFoundException |
|
898 | + */ |
|
899 | + protected function fillStorageInfo(string $userId): array { |
|
900 | + try { |
|
901 | + \OC_Util::tearDownFS(); |
|
902 | + \OC_Util::setupFS($userId); |
|
903 | + $storage = OC_Helper::getStorageInfo('/'); |
|
904 | + $data = [ |
|
905 | + 'free' => $storage['free'], |
|
906 | + 'used' => $storage['used'], |
|
907 | + 'total' => $storage['total'], |
|
908 | + 'relative' => $storage['relative'], |
|
909 | + 'quota' => $storage['quota'], |
|
910 | + ]; |
|
911 | + } catch (NotFoundException $ex) { |
|
912 | + $data = []; |
|
913 | + } |
|
914 | + return $data; |
|
915 | + } |
|
916 | + |
|
917 | + /** |
|
918 | + * @NoAdminRequired |
|
919 | + * @PasswordConfirmationRequired |
|
920 | + * |
|
921 | + * resend welcome message |
|
922 | + * |
|
923 | + * @param string $userId |
|
924 | + * @return DataResponse |
|
925 | + * @throws OCSException |
|
926 | + */ |
|
927 | + public function resendWelcomeMessage(string $userId): DataResponse { |
|
928 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
929 | + |
|
930 | + $targetUser = $this->userManager->get($userId); |
|
931 | + if($targetUser === null) { |
|
932 | + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
933 | + } |
|
934 | + |
|
935 | + // Check if admin / subadmin |
|
936 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
937 | + if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
938 | + && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
939 | + // No rights |
|
940 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
941 | + } |
|
942 | + |
|
943 | + $email = $targetUser->getEMailAddress(); |
|
944 | + if ($email === '' || $email === null) { |
|
945 | + throw new OCSException('Email address not available', 101); |
|
946 | + } |
|
947 | + $username = $targetUser->getUID(); |
|
948 | + $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); |
|
949 | + if (!$this->l10nFactory->languageExists('settings', $lang)) { |
|
950 | + $lang = 'en'; |
|
951 | + } |
|
952 | + |
|
953 | + $l10n = $this->l10nFactory->get('settings', $lang); |
|
954 | + |
|
955 | + try { |
|
956 | + $this->newUserMailHelper->setL10N($l10n); |
|
957 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
|
958 | + $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
|
959 | + } catch(\Exception $e) { |
|
960 | + $this->logger->logException($e, [ |
|
961 | + 'message' => "Can't send new user mail to $email", |
|
962 | + 'level' => \OCP\Util::ERROR, |
|
963 | + 'app' => 'settings', |
|
964 | + ]); |
|
965 | + throw new OCSException('Sending email failed', 102); |
|
966 | + } |
|
967 | + |
|
968 | + return new DataResponse(); |
|
969 | + } |
|
970 | 970 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -declare(strict_types=1); |
|
2 | +declare(strict_types = 1); |
|
3 | 3 | /** |
4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
5 | 5 | * |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | // Admin? Or SubAdmin? |
138 | 138 | $uid = $user->getUID(); |
139 | 139 | $subAdminManager = $this->groupManager->getSubAdmin(); |
140 | - if($this->groupManager->isAdmin($uid)){ |
|
140 | + if ($this->groupManager->isAdmin($uid)) { |
|
141 | 141 | $users = $this->userManager->search($search, $limit, $offset); |
142 | 142 | } else if ($subAdminManager->isSubAdmin($user)) { |
143 | 143 | $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | // Admin? Or SubAdmin? |
171 | 171 | $uid = $user->getUID(); |
172 | 172 | $subAdminManager = $this->groupManager->getSubAdmin(); |
173 | - if($this->groupManager->isAdmin($uid)){ |
|
173 | + if ($this->groupManager->isAdmin($uid)) { |
|
174 | 174 | $users = $this->userManager->search($search, $limit, $offset); |
175 | 175 | } else if ($subAdminManager->isSubAdmin($user)) { |
176 | 176 | $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | foreach ($users as $key => $userId) { |
190 | 190 | $userData = $this->getUserData($userId); |
191 | 191 | // Do not insert empty entry |
192 | - if(!empty($userData)) { |
|
192 | + if (!empty($userData)) { |
|
193 | 193 | $usersDetails[$userId] = $userData; |
194 | 194 | } |
195 | 195 | } |
@@ -210,27 +210,27 @@ discard block |
||
210 | 210 | * @return DataResponse |
211 | 211 | * @throws OCSException |
212 | 212 | */ |
213 | - public function addUser(string $userid, string $password = '', string $email='', array $groups = []): DataResponse { |
|
213 | + public function addUser(string $userid, string $password = '', string $email = '', array $groups = []): DataResponse { |
|
214 | 214 | $user = $this->userSession->getUser(); |
215 | 215 | $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
216 | 216 | $subAdminManager = $this->groupManager->getSubAdmin(); |
217 | 217 | |
218 | - if($this->userManager->userExists($userid)) { |
|
218 | + if ($this->userManager->userExists($userid)) { |
|
219 | 219 | $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
220 | 220 | throw new OCSException('User already exists', 102); |
221 | 221 | } |
222 | 222 | |
223 | - if($groups !== []) { |
|
223 | + if ($groups !== []) { |
|
224 | 224 | foreach ($groups as $group) { |
225 | - if(!$this->groupManager->groupExists($group)) { |
|
225 | + if (!$this->groupManager->groupExists($group)) { |
|
226 | 226 | throw new OCSException('group '.$group.' does not exist', 104); |
227 | 227 | } |
228 | - if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
229 | - throw new OCSException('insufficient privileges for group '. $group, 105); |
|
228 | + if (!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
229 | + throw new OCSException('insufficient privileges for group '.$group, 105); |
|
230 | 230 | } |
231 | 231 | } |
232 | 232 | } else { |
233 | - if(!$isAdmin) { |
|
233 | + if (!$isAdmin) { |
|
234 | 234 | throw new OCSException('no group specified (required for subadmins)', 106); |
235 | 235 | } |
236 | 236 | } |
@@ -249,11 +249,11 @@ discard block |
||
249 | 249 | |
250 | 250 | try { |
251 | 251 | $newUser = $this->userManager->createUser($userid, $password); |
252 | - $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); |
|
252 | + $this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']); |
|
253 | 253 | |
254 | 254 | foreach ($groups as $group) { |
255 | 255 | $this->groupManager->get($group)->addUser($newUser); |
256 | - $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); |
|
256 | + $this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']); |
|
257 | 257 | } |
258 | 258 | |
259 | 259 | // Send new user mail only if a mail is set |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | |
275 | 275 | return new DataResponse(); |
276 | 276 | |
277 | - } catch (HintException $e ) { |
|
277 | + } catch (HintException $e) { |
|
278 | 278 | $this->logger->logException($e, [ |
279 | 279 | 'message' => 'Failed addUser attempt with hint exception.', |
280 | 280 | 'level' => \OCP\Util::WARN, |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | public function getUser(string $userId): DataResponse { |
305 | 305 | $data = $this->getUserData($userId); |
306 | 306 | // getUserData returns empty array if not enough permissions |
307 | - if(empty($data)) { |
|
307 | + if (empty($data)) { |
|
308 | 308 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
309 | 309 | } |
310 | 310 | return new DataResponse($data); |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | public function getCurrentUser(): DataResponse { |
323 | 323 | $user = $this->userSession->getUser(); |
324 | 324 | if ($user) { |
325 | - $data = $this->getUserData($user->getUID()); |
|
325 | + $data = $this->getUserData($user->getUID()); |
|
326 | 326 | // rename "displayname" to "display-name" only for this call to keep |
327 | 327 | // the API stable. |
328 | 328 | $data['display-name'] = $data['displayname']; |
@@ -348,17 +348,17 @@ discard block |
||
348 | 348 | |
349 | 349 | // Check if the target user exists |
350 | 350 | $targetUserObject = $this->userManager->get($userId); |
351 | - if($targetUserObject === null) { |
|
351 | + if ($targetUserObject === null) { |
|
352 | 352 | throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
353 | 353 | } |
354 | 354 | |
355 | 355 | // Should be at least Admin Or SubAdmin! |
356 | - if( $this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
356 | + if ($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
357 | 357 | || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
358 | 358 | $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
359 | 359 | } else { |
360 | 360 | // Check they are looking up themselves |
361 | - if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
361 | + if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
362 | 362 | return $data; |
363 | 363 | } |
364 | 364 | } |
@@ -434,12 +434,12 @@ discard block |
||
434 | 434 | $currentLoggedInUser = $this->userSession->getUser(); |
435 | 435 | |
436 | 436 | $targetUser = $this->userManager->get($userId); |
437 | - if($targetUser === null) { |
|
437 | + if ($targetUser === null) { |
|
438 | 438 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
439 | 439 | } |
440 | 440 | |
441 | 441 | $permittedFields = []; |
442 | - if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
442 | + if ($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
443 | 443 | // Editing self (display, email) |
444 | 444 | if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
445 | 445 | $permittedFields[] = 'display'; |
@@ -465,13 +465,13 @@ discard block |
||
465 | 465 | } |
466 | 466 | |
467 | 467 | // If admin they can edit their own quota |
468 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
468 | + if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
469 | 469 | $permittedFields[] = 'quota'; |
470 | 470 | } |
471 | 471 | } else { |
472 | 472 | // Check if admin / subadmin |
473 | 473 | $subAdminManager = $this->groupManager->getSubAdmin(); |
474 | - if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
474 | + if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
475 | 475 | || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
476 | 476 | // They have permissions over the user |
477 | 477 | $permittedFields[] = 'display'; |
@@ -490,18 +490,18 @@ discard block |
||
490 | 490 | } |
491 | 491 | } |
492 | 492 | // Check if permitted to edit this field |
493 | - if(!in_array($key, $permittedFields)) { |
|
493 | + if (!in_array($key, $permittedFields)) { |
|
494 | 494 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
495 | 495 | } |
496 | 496 | // Process the edit |
497 | - switch($key) { |
|
497 | + switch ($key) { |
|
498 | 498 | case 'display': |
499 | 499 | case AccountManager::PROPERTY_DISPLAYNAME: |
500 | 500 | $targetUser->setDisplayName($value); |
501 | 501 | break; |
502 | 502 | case 'quota': |
503 | 503 | $quota = $value; |
504 | - if($quota !== 'none' && $quota !== 'default') { |
|
504 | + if ($quota !== 'none' && $quota !== 'default') { |
|
505 | 505 | if (is_numeric($quota)) { |
506 | 506 | $quota = (float) $quota; |
507 | 507 | } else { |
@@ -510,9 +510,9 @@ discard block |
||
510 | 510 | if ($quota === false) { |
511 | 511 | throw new OCSException('Invalid quota value '.$value, 103); |
512 | 512 | } |
513 | - if($quota === 0) { |
|
513 | + if ($quota === 0) { |
|
514 | 514 | $quota = 'default'; |
515 | - }else if($quota === -1) { |
|
515 | + } else if ($quota === -1) { |
|
516 | 516 | $quota = 'none'; |
517 | 517 | } else { |
518 | 518 | $quota = \OCP\Util::humanFileSize($quota); |
@@ -531,7 +531,7 @@ discard block |
||
531 | 531 | $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
532 | 532 | break; |
533 | 533 | case AccountManager::PROPERTY_EMAIL: |
534 | - if(filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { |
|
534 | + if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { |
|
535 | 535 | $targetUser->setEMailAddress($value); |
536 | 536 | } else { |
537 | 537 | throw new OCSException('', 102); |
@@ -566,18 +566,18 @@ discard block |
||
566 | 566 | |
567 | 567 | $targetUser = $this->userManager->get($userId); |
568 | 568 | |
569 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
569 | + if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
570 | 570 | throw new OCSException('', 101); |
571 | 571 | } |
572 | 572 | |
573 | 573 | // If not permitted |
574 | 574 | $subAdminManager = $this->groupManager->getSubAdmin(); |
575 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
575 | + if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
576 | 576 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
577 | 577 | } |
578 | 578 | |
579 | 579 | // Go ahead with the delete |
580 | - if($targetUser->delete()) { |
|
580 | + if ($targetUser->delete()) { |
|
581 | 581 | return new DataResponse(); |
582 | 582 | } else { |
583 | 583 | throw new OCSException('', 101); |
@@ -620,13 +620,13 @@ discard block |
||
620 | 620 | $currentLoggedInUser = $this->userSession->getUser(); |
621 | 621 | |
622 | 622 | $targetUser = $this->userManager->get($userId); |
623 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
623 | + if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
624 | 624 | throw new OCSException('', 101); |
625 | 625 | } |
626 | 626 | |
627 | 627 | // If not permitted |
628 | 628 | $subAdminManager = $this->groupManager->getSubAdmin(); |
629 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
629 | + if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
630 | 630 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
631 | 631 | } |
632 | 632 | |
@@ -647,11 +647,11 @@ discard block |
||
647 | 647 | $loggedInUser = $this->userSession->getUser(); |
648 | 648 | |
649 | 649 | $targetUser = $this->userManager->get($userId); |
650 | - if($targetUser === null) { |
|
650 | + if ($targetUser === null) { |
|
651 | 651 | throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
652 | 652 | } |
653 | 653 | |
654 | - if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
654 | + if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
655 | 655 | // Self lookup or admin lookup |
656 | 656 | return new DataResponse([ |
657 | 657 | 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
@@ -660,7 +660,7 @@ discard block |
||
660 | 660 | $subAdminManager = $this->groupManager->getSubAdmin(); |
661 | 661 | |
662 | 662 | // Looking up someone else |
663 | - if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
663 | + if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
664 | 664 | // Return the group that the method caller is subadmin of for the user in question |
665 | 665 | /** @var IGroup[] $getSubAdminsGroups */ |
666 | 666 | $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
@@ -690,16 +690,16 @@ discard block |
||
690 | 690 | * @throws OCSException |
691 | 691 | */ |
692 | 692 | public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
693 | - if($groupid === '') { |
|
693 | + if ($groupid === '') { |
|
694 | 694 | throw new OCSException('', 101); |
695 | 695 | } |
696 | 696 | |
697 | 697 | $group = $this->groupManager->get($groupid); |
698 | 698 | $targetUser = $this->userManager->get($userId); |
699 | - if($group === null) { |
|
699 | + if ($group === null) { |
|
700 | 700 | throw new OCSException('', 102); |
701 | 701 | } |
702 | - if($targetUser === null) { |
|
702 | + if ($targetUser === null) { |
|
703 | 703 | throw new OCSException('', 103); |
704 | 704 | } |
705 | 705 | |
@@ -727,17 +727,17 @@ discard block |
||
727 | 727 | public function removeFromGroup(string $userId, string $groupid): DataResponse { |
728 | 728 | $loggedInUser = $this->userSession->getUser(); |
729 | 729 | |
730 | - if($groupid === null || trim($groupid) === '') { |
|
730 | + if ($groupid === null || trim($groupid) === '') { |
|
731 | 731 | throw new OCSException('', 101); |
732 | 732 | } |
733 | 733 | |
734 | 734 | $group = $this->groupManager->get($groupid); |
735 | - if($group === null) { |
|
735 | + if ($group === null) { |
|
736 | 736 | throw new OCSException('', 102); |
737 | 737 | } |
738 | 738 | |
739 | 739 | $targetUser = $this->userManager->get($userId); |
740 | - if($targetUser === null) { |
|
740 | + if ($targetUser === null) { |
|
741 | 741 | throw new OCSException('', 103); |
742 | 742 | } |
743 | 743 | |
@@ -761,7 +761,7 @@ discard block |
||
761 | 761 | } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
762 | 762 | /** @var IGroup[] $subAdminGroups */ |
763 | 763 | $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
764 | - $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
764 | + $subAdminGroups = array_map(function(IGroup $subAdminGroup) { |
|
765 | 765 | return $subAdminGroup->getGID(); |
766 | 766 | }, $subAdminGroups); |
767 | 767 | $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
@@ -793,15 +793,15 @@ discard block |
||
793 | 793 | $user = $this->userManager->get($userId); |
794 | 794 | |
795 | 795 | // Check if the user exists |
796 | - if($user === null) { |
|
796 | + if ($user === null) { |
|
797 | 797 | throw new OCSException('User does not exist', 101); |
798 | 798 | } |
799 | 799 | // Check if group exists |
800 | - if($group === null) { |
|
801 | - throw new OCSException('Group does not exist', 102); |
|
800 | + if ($group === null) { |
|
801 | + throw new OCSException('Group does not exist', 102); |
|
802 | 802 | } |
803 | 803 | // Check if trying to make subadmin of admin group |
804 | - if($group->getGID() === 'admin') { |
|
804 | + if ($group->getGID() === 'admin') { |
|
805 | 805 | throw new OCSException('Cannot create subadmins for admin group', 103); |
806 | 806 | } |
807 | 807 | |
@@ -812,7 +812,7 @@ discard block |
||
812 | 812 | return new DataResponse(); |
813 | 813 | } |
814 | 814 | // Go |
815 | - if($subAdminManager->createSubAdmin($user, $group)) { |
|
815 | + if ($subAdminManager->createSubAdmin($user, $group)) { |
|
816 | 816 | return new DataResponse(); |
817 | 817 | } else { |
818 | 818 | throw new OCSException('Unknown error occurred', 103); |
@@ -835,20 +835,20 @@ discard block |
||
835 | 835 | $subAdminManager = $this->groupManager->getSubAdmin(); |
836 | 836 | |
837 | 837 | // Check if the user exists |
838 | - if($user === null) { |
|
838 | + if ($user === null) { |
|
839 | 839 | throw new OCSException('User does not exist', 101); |
840 | 840 | } |
841 | 841 | // Check if the group exists |
842 | - if($group === null) { |
|
842 | + if ($group === null) { |
|
843 | 843 | throw new OCSException('Group does not exist', 101); |
844 | 844 | } |
845 | 845 | // Check if they are a subadmin of this said group |
846 | - if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
846 | + if (!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
847 | 847 | throw new OCSException('User is not a subadmin of this group', 102); |
848 | 848 | } |
849 | 849 | |
850 | 850 | // Go |
851 | - if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
851 | + if ($subAdminManager->deleteSubAdmin($user, $group)) { |
|
852 | 852 | return new DataResponse(); |
853 | 853 | } else { |
854 | 854 | throw new OCSException('Unknown error occurred', 103); |
@@ -865,7 +865,7 @@ discard block |
||
865 | 865 | protected function getUserSubAdminGroupsData(string $userId): array { |
866 | 866 | $user = $this->userManager->get($userId); |
867 | 867 | // Check if the user exists |
868 | - if($user === null) { |
|
868 | + if ($user === null) { |
|
869 | 869 | throw new OCSException('User does not exist', 101); |
870 | 870 | } |
871 | 871 | |
@@ -928,13 +928,13 @@ discard block |
||
928 | 928 | $currentLoggedInUser = $this->userSession->getUser(); |
929 | 929 | |
930 | 930 | $targetUser = $this->userManager->get($userId); |
931 | - if($targetUser === null) { |
|
931 | + if ($targetUser === null) { |
|
932 | 932 | throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
933 | 933 | } |
934 | 934 | |
935 | 935 | // Check if admin / subadmin |
936 | 936 | $subAdminManager = $this->groupManager->getSubAdmin(); |
937 | - if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
937 | + if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
938 | 938 | && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
939 | 939 | // No rights |
940 | 940 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
@@ -956,7 +956,7 @@ discard block |
||
956 | 956 | $this->newUserMailHelper->setL10N($l10n); |
957 | 957 | $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
958 | 958 | $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
959 | - } catch(\Exception $e) { |
|
959 | + } catch (\Exception $e) { |
|
960 | 960 | $this->logger->logException($e, [ |
961 | 961 | 'message' => "Can't send new user mail to $email", |
962 | 962 | 'level' => \OCP\Util::ERROR, |
@@ -5,7 +5,7 @@ |
||
5 | 5 | use OCP\AppFramework\Http; |
6 | 6 | |
7 | 7 | class NotSubAdminException extends \Exception { |
8 | - public function __construct() { |
|
9 | - parent::__construct('Logged in user must be at least a sub admin', Http::STATUS_FORBIDDEN); |
|
10 | - } |
|
8 | + public function __construct() { |
|
9 | + parent::__construct('Logged in user must be at least a sub admin', Http::STATUS_FORBIDDEN); |
|
10 | + } |
|
11 | 11 | } |
12 | 12 | \ No newline at end of file |
@@ -1,6 +1,5 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - |
|
4 | 3 | * |
5 | 4 | * @author Bjoern Schiessle <[email protected]> |
6 | 5 | * @author Jan-Christoph Borchardt <[email protected]> |
@@ -28,55 +28,55 @@ |
||
28 | 28 | use OCP\Settings\IIconSection; |
29 | 29 | |
30 | 30 | class Section implements IIconSection { |
31 | - /** @var IL10N */ |
|
32 | - private $l; |
|
33 | - /** @var IURLGenerator */ |
|
34 | - private $url; |
|
31 | + /** @var IL10N */ |
|
32 | + private $l; |
|
33 | + /** @var IURLGenerator */ |
|
34 | + private $url; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param IURLGenerator $url |
|
38 | - * @param IL10N $l |
|
39 | - */ |
|
40 | - public function __construct(IURLGenerator $url, IL10N $l) { |
|
41 | - $this->url = $url; |
|
42 | - $this->l = $l; |
|
43 | - } |
|
36 | + /** |
|
37 | + * @param IURLGenerator $url |
|
38 | + * @param IL10N $l |
|
39 | + */ |
|
40 | + public function __construct(IURLGenerator $url, IL10N $l) { |
|
41 | + $this->url = $url; |
|
42 | + $this->l = $l; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * returns the ID of the section. It is supposed to be a lower case string, |
|
47 | - * e.g. 'ldap' |
|
48 | - * |
|
49 | - * @returns string |
|
50 | - */ |
|
51 | - public function getID() { |
|
52 | - return 'theming'; |
|
53 | - } |
|
45 | + /** |
|
46 | + * returns the ID of the section. It is supposed to be a lower case string, |
|
47 | + * e.g. 'ldap' |
|
48 | + * |
|
49 | + * @returns string |
|
50 | + */ |
|
51 | + public function getID() { |
|
52 | + return 'theming'; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * returns the translated name as it should be displayed, e.g. 'LDAP / AD |
|
57 | - * integration'. Use the L10N service to translate it. |
|
58 | - * |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - public function getName() { |
|
62 | - return $this->l->t('Theming'); |
|
63 | - } |
|
55 | + /** |
|
56 | + * returns the translated name as it should be displayed, e.g. 'LDAP / AD |
|
57 | + * integration'. Use the L10N service to translate it. |
|
58 | + * |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + public function getName() { |
|
62 | + return $this->l->t('Theming'); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return int whether the form should be rather on the top or bottom of |
|
67 | - * the settings navigation. The sections are arranged in ascending order of |
|
68 | - * the priority values. It is required to return a value between 0 and 99. |
|
69 | - * |
|
70 | - * E.g.: 70 |
|
71 | - */ |
|
72 | - public function getPriority() { |
|
73 | - return 30; |
|
74 | - } |
|
65 | + /** |
|
66 | + * @return int whether the form should be rather on the top or bottom of |
|
67 | + * the settings navigation. The sections are arranged in ascending order of |
|
68 | + * the priority values. It is required to return a value between 0 and 99. |
|
69 | + * |
|
70 | + * E.g.: 70 |
|
71 | + */ |
|
72 | + public function getPriority() { |
|
73 | + return 30; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * {@inheritdoc} |
|
78 | - */ |
|
79 | - public function getIcon() { |
|
80 | - return $this->url->imagePath('theming', 'app-dark.svg'); |
|
81 | - } |
|
76 | + /** |
|
77 | + * {@inheritdoc} |
|
78 | + */ |
|
79 | + public function getIcon() { |
|
80 | + return $this->url->imagePath('theming', 'app-dark.svg'); |
|
81 | + } |
|
82 | 82 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | |
79 | 79 | $uid = $user->getUID(); |
80 | 80 | foreach (range(1, min([$number, 20])) as $i) { |
81 | - $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS); |
|
81 | + $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS); |
|
82 | 82 | |
83 | 83 | $dbCode = new BackupCode(); |
84 | 84 | $dbCode->setUserId($uid); |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | $codes = $this->mapper->getBackupCodes($user); |
133 | 133 | $total = count($codes); |
134 | 134 | $used = 0; |
135 | - array_walk($codes, function (BackupCode $code) use (&$used) { |
|
135 | + array_walk($codes, function(BackupCode $code) use (&$used) { |
|
136 | 136 | if (1 === (int) $code->getUsed()) { |
137 | 137 | $used++; |
138 | 138 | } |
@@ -33,133 +33,133 @@ |
||
33 | 33 | |
34 | 34 | class BackupCodeStorage { |
35 | 35 | |
36 | - private static $CODE_LENGTH = 16; |
|
37 | - |
|
38 | - /** @var BackupCodeMapper */ |
|
39 | - private $mapper; |
|
40 | - |
|
41 | - /** @var IHasher */ |
|
42 | - private $hasher; |
|
43 | - |
|
44 | - /** @var ISecureRandom */ |
|
45 | - private $random; |
|
46 | - |
|
47 | - /** @var IManager */ |
|
48 | - private $activityManager; |
|
49 | - |
|
50 | - /** @var ILogger */ |
|
51 | - private $logger; |
|
52 | - |
|
53 | - /** |
|
54 | - * @param BackupCodeMapper $mapper |
|
55 | - * @param ISecureRandom $random |
|
56 | - * @param IHasher $hasher |
|
57 | - * @param IManager $activityManager |
|
58 | - * @param ILogger $logger |
|
59 | - */ |
|
60 | - public function __construct(BackupCodeMapper $mapper, ISecureRandom $random, IHasher $hasher, |
|
61 | - IManager $activityManager, ILogger $logger) { |
|
62 | - $this->mapper = $mapper; |
|
63 | - $this->hasher = $hasher; |
|
64 | - $this->random = $random; |
|
65 | - $this->activityManager = $activityManager; |
|
66 | - $this->logger = $logger; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @param IUser $user |
|
71 | - * @return string[] |
|
72 | - */ |
|
73 | - public function createCodes(IUser $user, $number = 10) { |
|
74 | - $result = []; |
|
75 | - |
|
76 | - // Delete existing ones |
|
77 | - $this->mapper->deleteCodes($user); |
|
78 | - |
|
79 | - $uid = $user->getUID(); |
|
80 | - foreach (range(1, min([$number, 20])) as $i) { |
|
81 | - $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS); |
|
82 | - |
|
83 | - $dbCode = new BackupCode(); |
|
84 | - $dbCode->setUserId($uid); |
|
85 | - $dbCode->setCode($this->hasher->hash($code)); |
|
86 | - $dbCode->setUsed(0); |
|
87 | - $this->mapper->insert($dbCode); |
|
88 | - |
|
89 | - $result[] = $code; |
|
90 | - } |
|
91 | - |
|
92 | - $this->publishEvent($user, 'codes_generated'); |
|
93 | - |
|
94 | - return $result; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Push an event the user's activity stream |
|
99 | - * |
|
100 | - * @param IUser $user |
|
101 | - * @param string $event |
|
102 | - */ |
|
103 | - private function publishEvent(IUser $user, $event) { |
|
104 | - $activity = $this->activityManager->generateEvent(); |
|
105 | - $activity->setApp('twofactor_backupcodes') |
|
106 | - ->setType('security') |
|
107 | - ->setAuthor($user->getUID()) |
|
108 | - ->setAffectedUser($user->getUID()) |
|
109 | - ->setSubject($event); |
|
110 | - try { |
|
111 | - $this->activityManager->publish($activity); |
|
112 | - } catch (BadMethodCallException $e) { |
|
113 | - $this->logger->warning('could not publish backup code creation activity', ['app' => 'twofactor_backupcodes']); |
|
114 | - $this->logger->logException($e, ['app' => 'twofactor_backupcodes']); |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param IUser $user |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - public function hasBackupCodes(IUser $user) { |
|
123 | - $codes = $this->mapper->getBackupCodes($user); |
|
124 | - return count($codes) > 0; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @param IUser $user |
|
129 | - * @return array |
|
130 | - */ |
|
131 | - public function getBackupCodesState(IUser $user) { |
|
132 | - $codes = $this->mapper->getBackupCodes($user); |
|
133 | - $total = count($codes); |
|
134 | - $used = 0; |
|
135 | - array_walk($codes, function (BackupCode $code) use (&$used) { |
|
136 | - if (1 === (int) $code->getUsed()) { |
|
137 | - $used++; |
|
138 | - } |
|
139 | - }); |
|
140 | - return [ |
|
141 | - 'enabled' => $total > 0, |
|
142 | - 'total' => $total, |
|
143 | - 'used' => $used, |
|
144 | - ]; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param IUser $user |
|
149 | - * @param string $code |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - public function validateCode(IUser $user, $code) { |
|
153 | - $dbCodes = $this->mapper->getBackupCodes($user); |
|
154 | - |
|
155 | - foreach ($dbCodes as $dbCode) { |
|
156 | - if (0 === (int) $dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) { |
|
157 | - $dbCode->setUsed(1); |
|
158 | - $this->mapper->update($dbCode); |
|
159 | - return true; |
|
160 | - } |
|
161 | - } |
|
162 | - return false; |
|
163 | - } |
|
36 | + private static $CODE_LENGTH = 16; |
|
37 | + |
|
38 | + /** @var BackupCodeMapper */ |
|
39 | + private $mapper; |
|
40 | + |
|
41 | + /** @var IHasher */ |
|
42 | + private $hasher; |
|
43 | + |
|
44 | + /** @var ISecureRandom */ |
|
45 | + private $random; |
|
46 | + |
|
47 | + /** @var IManager */ |
|
48 | + private $activityManager; |
|
49 | + |
|
50 | + /** @var ILogger */ |
|
51 | + private $logger; |
|
52 | + |
|
53 | + /** |
|
54 | + * @param BackupCodeMapper $mapper |
|
55 | + * @param ISecureRandom $random |
|
56 | + * @param IHasher $hasher |
|
57 | + * @param IManager $activityManager |
|
58 | + * @param ILogger $logger |
|
59 | + */ |
|
60 | + public function __construct(BackupCodeMapper $mapper, ISecureRandom $random, IHasher $hasher, |
|
61 | + IManager $activityManager, ILogger $logger) { |
|
62 | + $this->mapper = $mapper; |
|
63 | + $this->hasher = $hasher; |
|
64 | + $this->random = $random; |
|
65 | + $this->activityManager = $activityManager; |
|
66 | + $this->logger = $logger; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @param IUser $user |
|
71 | + * @return string[] |
|
72 | + */ |
|
73 | + public function createCodes(IUser $user, $number = 10) { |
|
74 | + $result = []; |
|
75 | + |
|
76 | + // Delete existing ones |
|
77 | + $this->mapper->deleteCodes($user); |
|
78 | + |
|
79 | + $uid = $user->getUID(); |
|
80 | + foreach (range(1, min([$number, 20])) as $i) { |
|
81 | + $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS); |
|
82 | + |
|
83 | + $dbCode = new BackupCode(); |
|
84 | + $dbCode->setUserId($uid); |
|
85 | + $dbCode->setCode($this->hasher->hash($code)); |
|
86 | + $dbCode->setUsed(0); |
|
87 | + $this->mapper->insert($dbCode); |
|
88 | + |
|
89 | + $result[] = $code; |
|
90 | + } |
|
91 | + |
|
92 | + $this->publishEvent($user, 'codes_generated'); |
|
93 | + |
|
94 | + return $result; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Push an event the user's activity stream |
|
99 | + * |
|
100 | + * @param IUser $user |
|
101 | + * @param string $event |
|
102 | + */ |
|
103 | + private function publishEvent(IUser $user, $event) { |
|
104 | + $activity = $this->activityManager->generateEvent(); |
|
105 | + $activity->setApp('twofactor_backupcodes') |
|
106 | + ->setType('security') |
|
107 | + ->setAuthor($user->getUID()) |
|
108 | + ->setAffectedUser($user->getUID()) |
|
109 | + ->setSubject($event); |
|
110 | + try { |
|
111 | + $this->activityManager->publish($activity); |
|
112 | + } catch (BadMethodCallException $e) { |
|
113 | + $this->logger->warning('could not publish backup code creation activity', ['app' => 'twofactor_backupcodes']); |
|
114 | + $this->logger->logException($e, ['app' => 'twofactor_backupcodes']); |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param IUser $user |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + public function hasBackupCodes(IUser $user) { |
|
123 | + $codes = $this->mapper->getBackupCodes($user); |
|
124 | + return count($codes) > 0; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @param IUser $user |
|
129 | + * @return array |
|
130 | + */ |
|
131 | + public function getBackupCodesState(IUser $user) { |
|
132 | + $codes = $this->mapper->getBackupCodes($user); |
|
133 | + $total = count($codes); |
|
134 | + $used = 0; |
|
135 | + array_walk($codes, function (BackupCode $code) use (&$used) { |
|
136 | + if (1 === (int) $code->getUsed()) { |
|
137 | + $used++; |
|
138 | + } |
|
139 | + }); |
|
140 | + return [ |
|
141 | + 'enabled' => $total > 0, |
|
142 | + 'total' => $total, |
|
143 | + 'used' => $used, |
|
144 | + ]; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param IUser $user |
|
149 | + * @param string $code |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + public function validateCode(IUser $user, $code) { |
|
153 | + $dbCodes = $this->mapper->getBackupCodes($user); |
|
154 | + |
|
155 | + foreach ($dbCodes as $dbCode) { |
|
156 | + if (0 === (int) $dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) { |
|
157 | + $dbCode->setUsed(1); |
|
158 | + $this->mapper->update($dbCode); |
|
159 | + return true; |
|
160 | + } |
|
161 | + } |
|
162 | + return false; |
|
163 | + } |
|
164 | 164 | |
165 | 165 | } |
@@ -30,46 +30,46 @@ |
||
30 | 30 | |
31 | 31 | class SettingsController extends Controller { |
32 | 32 | |
33 | - /** @var BackupCodeStorage */ |
|
34 | - private $storage; |
|
33 | + /** @var BackupCodeStorage */ |
|
34 | + private $storage; |
|
35 | 35 | |
36 | - /** @var IUserSession */ |
|
37 | - private $userSession; |
|
36 | + /** @var IUserSession */ |
|
37 | + private $userSession; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param string $appName |
|
41 | - * @param IRequest $request |
|
42 | - * @param BackupCodeStorage $storage |
|
43 | - * @param IUserSession $userSession |
|
44 | - */ |
|
45 | - public function __construct($appName, IRequest $request, BackupCodeStorage $storage, IUserSession $userSession) { |
|
46 | - parent::__construct($appName, $request); |
|
47 | - $this->userSession = $userSession; |
|
48 | - $this->storage = $storage; |
|
49 | - } |
|
39 | + /** |
|
40 | + * @param string $appName |
|
41 | + * @param IRequest $request |
|
42 | + * @param BackupCodeStorage $storage |
|
43 | + * @param IUserSession $userSession |
|
44 | + */ |
|
45 | + public function __construct($appName, IRequest $request, BackupCodeStorage $storage, IUserSession $userSession) { |
|
46 | + parent::__construct($appName, $request); |
|
47 | + $this->userSession = $userSession; |
|
48 | + $this->storage = $storage; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @NoAdminRequired |
|
53 | - * @return JSONResponse |
|
54 | - */ |
|
55 | - public function state() { |
|
56 | - $user = $this->userSession->getUser(); |
|
57 | - return $this->storage->getBackupCodesState($user); |
|
58 | - } |
|
51 | + /** |
|
52 | + * @NoAdminRequired |
|
53 | + * @return JSONResponse |
|
54 | + */ |
|
55 | + public function state() { |
|
56 | + $user = $this->userSession->getUser(); |
|
57 | + return $this->storage->getBackupCodesState($user); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @NoAdminRequired |
|
62 | - * @PasswordConfirmationRequired |
|
63 | - * |
|
64 | - * @return JSONResponse |
|
65 | - */ |
|
66 | - public function createCodes() { |
|
67 | - $user = $this->userSession->getUser(); |
|
68 | - $codes = $this->storage->createCodes($user); |
|
69 | - return new JSONResponse([ |
|
70 | - 'codes' => $codes, |
|
71 | - 'state' => $this->storage->getBackupCodesState($user), |
|
72 | - ]); |
|
73 | - } |
|
60 | + /** |
|
61 | + * @NoAdminRequired |
|
62 | + * @PasswordConfirmationRequired |
|
63 | + * |
|
64 | + * @return JSONResponse |
|
65 | + */ |
|
66 | + public function createCodes() { |
|
67 | + $user = $this->userSession->getUser(); |
|
68 | + $codes = $this->storage->createCodes($user); |
|
69 | + return new JSONResponse([ |
|
70 | + 'codes' => $codes, |
|
71 | + 'state' => $this->storage->getBackupCodesState($user), |
|
72 | + ]); |
|
73 | + } |
|
74 | 74 | |
75 | 75 | } |
@@ -47,7 +47,7 @@ |
||
47 | 47 | $rows = $result->fetchAll(); |
48 | 48 | $result->closeCursor(); |
49 | 49 | |
50 | - return array_map(function ($row) { |
|
50 | + return array_map(function($row) { |
|
51 | 51 | return BackupCode::fromRow($row); |
52 | 52 | }, $rows); |
53 | 53 | } |
@@ -28,48 +28,48 @@ |
||
28 | 28 | |
29 | 29 | class BackupCodeMapper extends Mapper { |
30 | 30 | |
31 | - public function __construct(IDBConnection $db) { |
|
32 | - parent::__construct($db, 'twofactor_backupcodes'); |
|
33 | - } |
|
31 | + public function __construct(IDBConnection $db) { |
|
32 | + parent::__construct($db, 'twofactor_backupcodes'); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param IUser $user |
|
37 | - * @return BackupCode[] |
|
38 | - */ |
|
39 | - public function getBackupCodes(IUser $user) { |
|
40 | - /* @var IQueryBuilder $qb */ |
|
41 | - $qb = $this->db->getQueryBuilder(); |
|
35 | + /** |
|
36 | + * @param IUser $user |
|
37 | + * @return BackupCode[] |
|
38 | + */ |
|
39 | + public function getBackupCodes(IUser $user) { |
|
40 | + /* @var IQueryBuilder $qb */ |
|
41 | + $qb = $this->db->getQueryBuilder(); |
|
42 | 42 | |
43 | - $qb->select('id', 'user_id', 'code', 'used') |
|
44 | - ->from('twofactor_backupcodes') |
|
45 | - ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); |
|
46 | - $result = $qb->execute(); |
|
43 | + $qb->select('id', 'user_id', 'code', 'used') |
|
44 | + ->from('twofactor_backupcodes') |
|
45 | + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); |
|
46 | + $result = $qb->execute(); |
|
47 | 47 | |
48 | - $rows = $result->fetchAll(); |
|
49 | - $result->closeCursor(); |
|
48 | + $rows = $result->fetchAll(); |
|
49 | + $result->closeCursor(); |
|
50 | 50 | |
51 | - return array_map(function ($row) { |
|
52 | - return BackupCode::fromRow($row); |
|
53 | - }, $rows); |
|
54 | - } |
|
51 | + return array_map(function ($row) { |
|
52 | + return BackupCode::fromRow($row); |
|
53 | + }, $rows); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param IUser $user |
|
58 | - */ |
|
59 | - public function deleteCodes(IUser $user) { |
|
60 | - $this->deleteCodesByUserId($user->getUID()); |
|
61 | - } |
|
56 | + /** |
|
57 | + * @param IUser $user |
|
58 | + */ |
|
59 | + public function deleteCodes(IUser $user) { |
|
60 | + $this->deleteCodesByUserId($user->getUID()); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @param string $uid |
|
65 | - */ |
|
66 | - public function deleteCodesByUserId($uid) { |
|
67 | - /* @var IQueryBuilder $qb */ |
|
68 | - $qb = $this->db->getQueryBuilder(); |
|
63 | + /** |
|
64 | + * @param string $uid |
|
65 | + */ |
|
66 | + public function deleteCodesByUserId($uid) { |
|
67 | + /* @var IQueryBuilder $qb */ |
|
68 | + $qb = $this->db->getQueryBuilder(); |
|
69 | 69 | |
70 | - $qb->delete('twofactor_backupcodes') |
|
71 | - ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid))); |
|
72 | - $qb->execute(); |
|
73 | - } |
|
70 | + $qb->delete('twofactor_backupcodes') |
|
71 | + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid))); |
|
72 | + $qb->execute(); |
|
73 | + } |
|
74 | 74 | |
75 | 75 | } |