@@ -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,208 +37,208 @@ |
||
| 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 | - $query = \OCP\DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1); |
|
| 211 | - $item = $query->execute(array($parent))->fetchRow(); |
|
| 212 | - if (isset($item['parent'])) { |
|
| 213 | - $parent = $item['parent']; |
|
| 214 | - } else { |
|
| 215 | - $fileOwner = $item['uid_owner']; |
|
| 216 | - break; |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - } else { |
|
| 220 | - $fileOwner = $source['uid_owner']; |
|
| 221 | - } |
|
| 222 | - if (isset($fileOwner)) { |
|
| 223 | - $source['fileOwner'] = $fileOwner; |
|
| 224 | - } else { |
|
| 225 | - \OCP\Util::writeLog('files_sharing', "No owner found for reshare", \OCP\Util::ERROR); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - return $source; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * @param string $target |
|
| 233 | - * @param array $share |
|
| 234 | - * @return array|false source item |
|
| 235 | - */ |
|
| 236 | - public static function getSource($target, $share) { |
|
| 237 | - if ($share['item_type'] === 'folder' && $target !== '') { |
|
| 238 | - // note: in case of ext storage mount points the path might be empty |
|
| 239 | - // which would cause a leading slash to appear |
|
| 240 | - $share['path'] = ltrim($share['path'] . '/' . $target, '/'); |
|
| 241 | - } |
|
| 242 | - return self::resolveReshares($share); |
|
| 243 | - } |
|
| 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 | + $query = \OCP\DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1); |
|
| 211 | + $item = $query->execute(array($parent))->fetchRow(); |
|
| 212 | + if (isset($item['parent'])) { |
|
| 213 | + $parent = $item['parent']; |
|
| 214 | + } else { |
|
| 215 | + $fileOwner = $item['uid_owner']; |
|
| 216 | + break; |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + } else { |
|
| 220 | + $fileOwner = $source['uid_owner']; |
|
| 221 | + } |
|
| 222 | + if (isset($fileOwner)) { |
|
| 223 | + $source['fileOwner'] = $fileOwner; |
|
| 224 | + } else { |
|
| 225 | + \OCP\Util::writeLog('files_sharing', "No owner found for reshare", \OCP\Util::ERROR); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + return $source; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * @param string $target |
|
| 233 | + * @param array $share |
|
| 234 | + * @return array|false source item |
|
| 235 | + */ |
|
| 236 | + public static function getSource($target, $share) { |
|
| 237 | + if ($share['item_type'] === 'folder' && $target !== '') { |
|
| 238 | + // note: in case of ext storage mount points the path might be empty |
|
| 239 | + // which would cause a leading slash to appear |
|
| 240 | + $share['path'] = ltrim($share['path'] . '/' . $target, '/'); |
|
| 241 | + } |
|
| 242 | + return self::resolveReshares($share); |
|
| 243 | + } |
|
| 244 | 244 | } |
@@ -28,9 +28,9 @@ |
||
| 28 | 28 | $route = isset($_GET['download']) ? 'files_sharing.sharecontroller.downloadShare' : 'files_sharing.sharecontroller.showShare'; |
| 29 | 29 | |
| 30 | 30 | if($token !== '') { |
| 31 | - OC_Response::redirect($urlGenerator->linkToRoute($route, array('token' => $token))); |
|
| 31 | + OC_Response::redirect($urlGenerator->linkToRoute($route, array('token' => $token))); |
|
| 32 | 32 | } else { |
| 33 | - header('HTTP/1.0 404 Not Found'); |
|
| 34 | - $tmpl = new OCP\Template('', '404', 'guest'); |
|
| 35 | - print_unescaped($tmpl->fetchPage()); |
|
| 33 | + header('HTTP/1.0 404 Not Found'); |
|
| 34 | + $tmpl = new OCP\Template('', '404', 'guest'); |
|
| 35 | + print_unescaped($tmpl->fetchPage()); |
|
| 36 | 36 | } |
@@ -27,7 +27,7 @@ |
||
| 27 | 27 | $token = isset($_GET['t']) ? $_GET['t'] : ''; |
| 28 | 28 | $route = isset($_GET['download']) ? 'files_sharing.sharecontroller.downloadShare' : 'files_sharing.sharecontroller.showShare'; |
| 29 | 29 | |
| 30 | -if($token !== '') { |
|
| 30 | +if ($token !== '') { |
|
| 31 | 31 | OC_Response::redirect($urlGenerator->linkToRoute($route, array('token' => $token))); |
| 32 | 32 | } else { |
| 33 | 33 | header('HTTP/1.0 404 Not Found'); |
@@ -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); |
@@ -53,829 +53,829 @@ |
||
| 53 | 53 | |
| 54 | 54 | class UsersController extends OCSController { |
| 55 | 55 | |
| 56 | - /** @var IUserManager */ |
|
| 57 | - private $userManager; |
|
| 58 | - /** @var IConfig */ |
|
| 59 | - private $config; |
|
| 60 | - /** @var IAppManager */ |
|
| 61 | - private $appManager; |
|
| 62 | - /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface |
|
| 63 | - private $groupManager; |
|
| 64 | - /** @var IUserSession */ |
|
| 65 | - private $userSession; |
|
| 66 | - /** @var AccountManager */ |
|
| 67 | - private $accountManager; |
|
| 68 | - /** @var ILogger */ |
|
| 69 | - private $logger; |
|
| 70 | - /** @var IFactory */ |
|
| 71 | - private $l10nFactory; |
|
| 72 | - /** @var NewUserMailHelper */ |
|
| 73 | - private $newUserMailHelper; |
|
| 74 | - /** @var FederatedFileSharingFactory */ |
|
| 75 | - private $federatedFileSharingFactory; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param string $appName |
|
| 79 | - * @param IRequest $request |
|
| 80 | - * @param IUserManager $userManager |
|
| 81 | - * @param IConfig $config |
|
| 82 | - * @param IAppManager $appManager |
|
| 83 | - * @param IGroupManager $groupManager |
|
| 84 | - * @param IUserSession $userSession |
|
| 85 | - * @param AccountManager $accountManager |
|
| 86 | - * @param ILogger $logger |
|
| 87 | - * @param IFactory $l10nFactory |
|
| 88 | - * @param NewUserMailHelper $newUserMailHelper |
|
| 89 | - * @param FederatedFileSharingFactory $federatedFileSharingFactory |
|
| 90 | - */ |
|
| 91 | - public function __construct(string $appName, |
|
| 92 | - IRequest $request, |
|
| 93 | - IUserManager $userManager, |
|
| 94 | - IConfig $config, |
|
| 95 | - IAppManager $appManager, |
|
| 96 | - IGroupManager $groupManager, |
|
| 97 | - IUserSession $userSession, |
|
| 98 | - AccountManager $accountManager, |
|
| 99 | - ILogger $logger, |
|
| 100 | - IFactory $l10nFactory, |
|
| 101 | - NewUserMailHelper $newUserMailHelper, |
|
| 102 | - FederatedFileSharingFactory $federatedFileSharingFactory) { |
|
| 103 | - parent::__construct($appName, $request); |
|
| 104 | - |
|
| 105 | - $this->userManager = $userManager; |
|
| 106 | - $this->config = $config; |
|
| 107 | - $this->appManager = $appManager; |
|
| 108 | - $this->groupManager = $groupManager; |
|
| 109 | - $this->userSession = $userSession; |
|
| 110 | - $this->accountManager = $accountManager; |
|
| 111 | - $this->logger = $logger; |
|
| 112 | - $this->l10nFactory = $l10nFactory; |
|
| 113 | - $this->newUserMailHelper = $newUserMailHelper; |
|
| 114 | - $this->federatedFileSharingFactory = $federatedFileSharingFactory; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @NoAdminRequired |
|
| 119 | - * |
|
| 120 | - * returns a list of users |
|
| 121 | - * |
|
| 122 | - * @param string $search |
|
| 123 | - * @param int $limit |
|
| 124 | - * @param int $offset |
|
| 125 | - * @return DataResponse |
|
| 126 | - */ |
|
| 127 | - public function getUsers(string $search = '', $limit = null, $offset = null): DataResponse { |
|
| 128 | - $user = $this->userSession->getUser(); |
|
| 129 | - $users = []; |
|
| 130 | - |
|
| 131 | - // Admin? Or SubAdmin? |
|
| 132 | - $uid = $user->getUID(); |
|
| 133 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 134 | - if($this->groupManager->isAdmin($uid)){ |
|
| 135 | - $users = $this->userManager->search($search, $limit, $offset); |
|
| 136 | - } else if ($subAdminManager->isSubAdmin($user)) { |
|
| 137 | - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
| 138 | - foreach ($subAdminOfGroups as $key => $group) { |
|
| 139 | - $subAdminOfGroups[$key] = $group->getGID(); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - if($offset === null) { |
|
| 143 | - $offset = 0; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $users = []; |
|
| 147 | - foreach ($subAdminOfGroups as $group) { |
|
| 148 | - $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search)); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $users = array_slice($users, $offset, $limit); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - $users = array_keys($users); |
|
| 155 | - |
|
| 156 | - return new DataResponse([ |
|
| 157 | - 'users' => $users |
|
| 158 | - ]); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @PasswordConfirmationRequired |
|
| 163 | - * @NoAdminRequired |
|
| 164 | - * |
|
| 165 | - * @param string $userid |
|
| 166 | - * @param string $password |
|
| 167 | - * @param array $groups |
|
| 168 | - * @return DataResponse |
|
| 169 | - * @throws OCSException |
|
| 170 | - */ |
|
| 171 | - public function addUser(string $userid, string $password, array $groups = []): DataResponse { |
|
| 172 | - $user = $this->userSession->getUser(); |
|
| 173 | - $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
| 174 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 175 | - |
|
| 176 | - if($this->userManager->userExists($userid)) { |
|
| 177 | - $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
|
| 178 | - throw new OCSException('User already exists', 102); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - if($groups !== []) { |
|
| 182 | - foreach ($groups as $group) { |
|
| 183 | - if(!$this->groupManager->groupExists($group)) { |
|
| 184 | - throw new OCSException('group '.$group.' does not exist', 104); |
|
| 185 | - } |
|
| 186 | - if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
| 187 | - throw new OCSException('insufficient privileges for group '. $group, 105); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - } else { |
|
| 191 | - if(!$isAdmin) { |
|
| 192 | - throw new OCSException('no group specified (required for subadmins)', 106); |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - try { |
|
| 197 | - $newUser = $this->userManager->createUser($userid, $password); |
|
| 198 | - $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); |
|
| 199 | - |
|
| 200 | - foreach ($groups as $group) { |
|
| 201 | - $this->groupManager->get($group)->addUser($newUser); |
|
| 202 | - $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return new DataResponse(); |
|
| 206 | - } catch (HintException $e ) { |
|
| 207 | - $this->logger->logException($e, [ |
|
| 208 | - 'message' => 'Failed addUser attempt with hint exception.', |
|
| 209 | - 'level' => \OCP\Util::WARN, |
|
| 210 | - 'app' => 'ocs_api', |
|
| 211 | - ]); |
|
| 212 | - throw new OCSException($e->getHint(), 107); |
|
| 213 | - } catch (\Exception $e) { |
|
| 214 | - $this->logger->logException($e, [ |
|
| 215 | - 'message' => 'Failed addUser attempt with exception.', |
|
| 216 | - 'level' => \OCP\Util::ERROR, |
|
| 217 | - 'app' => 'ocs_api', |
|
| 218 | - ]); |
|
| 219 | - throw new OCSException('Bad request', 101); |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * @NoAdminRequired |
|
| 225 | - * @NoSubAdminRequired |
|
| 226 | - * |
|
| 227 | - * gets user info |
|
| 228 | - * |
|
| 229 | - * @param string $userId |
|
| 230 | - * @return DataResponse |
|
| 231 | - * @throws OCSException |
|
| 232 | - */ |
|
| 233 | - public function getUser(string $userId): DataResponse { |
|
| 234 | - $data = $this->getUserData($userId); |
|
| 235 | - return new DataResponse($data); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * @NoAdminRequired |
|
| 240 | - * @NoSubAdminRequired |
|
| 241 | - * |
|
| 242 | - * gets user info from the currently logged in user |
|
| 243 | - * |
|
| 244 | - * @return DataResponse |
|
| 245 | - * @throws OCSException |
|
| 246 | - */ |
|
| 247 | - public function getCurrentUser(): DataResponse { |
|
| 248 | - $user = $this->userSession->getUser(); |
|
| 249 | - if ($user) { |
|
| 250 | - $data = $this->getUserData($user->getUID()); |
|
| 251 | - // rename "displayname" to "display-name" only for this call to keep |
|
| 252 | - // the API stable. |
|
| 253 | - $data['display-name'] = $data['displayname']; |
|
| 254 | - unset($data['displayname']); |
|
| 255 | - return new DataResponse($data); |
|
| 256 | - |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * creates a array with all user data |
|
| 264 | - * |
|
| 265 | - * @param $userId |
|
| 266 | - * @return array |
|
| 267 | - * @throws OCSException |
|
| 268 | - */ |
|
| 269 | - protected function getUserData(string $userId): array { |
|
| 270 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 271 | - |
|
| 272 | - $data = []; |
|
| 273 | - |
|
| 274 | - // Check if the target user exists |
|
| 275 | - $targetUserObject = $this->userManager->get($userId); |
|
| 276 | - if($targetUserObject === null) { |
|
| 277 | - throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - // Admin? Or SubAdmin? |
|
| 281 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 282 | - || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
| 283 | - $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
|
| 284 | - } else { |
|
| 285 | - // Check they are looking up themselves |
|
| 286 | - if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 287 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - $userAccount = $this->accountManager->getUser($targetUserObject); |
|
| 292 | - $groups = $this->groupManager->getUserGroups($targetUserObject); |
|
| 293 | - $gids = []; |
|
| 294 | - foreach ($groups as $group) { |
|
| 295 | - $gids[] = $group->getDisplayName(); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // Find the data |
|
| 299 | - $data['id'] = $targetUserObject->getUID(); |
|
| 300 | - $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
| 301 | - $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
| 302 | - $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
| 303 | - $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value']; |
|
| 304 | - $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value']; |
|
| 305 | - $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value']; |
|
| 306 | - $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value']; |
|
| 307 | - $data['groups'] = $gids; |
|
| 308 | - $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang'); |
|
| 309 | - |
|
| 310 | - return $data; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @NoAdminRequired |
|
| 315 | - * @NoSubAdminRequired |
|
| 316 | - */ |
|
| 317 | - public function getEditableFields(): DataResponse { |
|
| 318 | - $permittedFields = []; |
|
| 319 | - |
|
| 320 | - // Editing self (display, email) |
|
| 321 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 322 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 323 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 327 | - $federatedFileSharing = $this->federatedFileSharingFactory->get(); |
|
| 328 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 329 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 330 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 331 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 332 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 333 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - return new DataResponse($permittedFields); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @NoAdminRequired |
|
| 342 | - * @NoSubAdminRequired |
|
| 343 | - * @PasswordConfirmationRequired |
|
| 344 | - * |
|
| 345 | - * edit users |
|
| 346 | - * |
|
| 347 | - * @param string $userId |
|
| 348 | - * @param string $key |
|
| 349 | - * @param string $value |
|
| 350 | - * @return DataResponse |
|
| 351 | - * @throws OCSException |
|
| 352 | - */ |
|
| 353 | - public function editUser(string $userId, string $key, string $value): DataResponse { |
|
| 354 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 355 | - |
|
| 356 | - $targetUser = $this->userManager->get($userId); |
|
| 357 | - if($targetUser === null) { |
|
| 358 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - $permittedFields = []; |
|
| 362 | - if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 363 | - // Editing self (display, email) |
|
| 364 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 365 | - $permittedFields[] = 'display'; |
|
| 366 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 367 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - $permittedFields[] = 'password'; |
|
| 371 | - if ($this->config->getSystemValue('force_language', false) === false || |
|
| 372 | - $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 373 | - $permittedFields[] = 'language'; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 377 | - $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 378 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 379 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 380 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 381 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 382 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 383 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - // If admin they can edit their own quota |
|
| 388 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 389 | - $permittedFields[] = 'quota'; |
|
| 390 | - } |
|
| 391 | - } else { |
|
| 392 | - // Check if admin / subadmin |
|
| 393 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 394 | - if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 395 | - || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 396 | - // They have permissions over the user |
|
| 397 | - $permittedFields[] = 'display'; |
|
| 398 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 399 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 400 | - $permittedFields[] = 'password'; |
|
| 401 | - $permittedFields[] = 'language'; |
|
| 402 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 403 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 404 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 405 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 406 | - $permittedFields[] = 'quota'; |
|
| 407 | - } else { |
|
| 408 | - // No rights |
|
| 409 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - // Check if permitted to edit this field |
|
| 413 | - if(!in_array($key, $permittedFields)) { |
|
| 414 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 415 | - } |
|
| 416 | - // Process the edit |
|
| 417 | - switch($key) { |
|
| 418 | - case 'display': |
|
| 419 | - case AccountManager::PROPERTY_DISPLAYNAME: |
|
| 420 | - $targetUser->setDisplayName($value); |
|
| 421 | - break; |
|
| 422 | - case 'quota': |
|
| 423 | - $quota = $value; |
|
| 424 | - if($quota !== 'none' && $quota !== 'default') { |
|
| 425 | - if (is_numeric($quota)) { |
|
| 426 | - $quota = (float) $quota; |
|
| 427 | - } else { |
|
| 428 | - $quota = \OCP\Util::computerFileSize($quota); |
|
| 429 | - } |
|
| 430 | - if ($quota === false) { |
|
| 431 | - throw new OCSException('Invalid quota value '.$value, 103); |
|
| 432 | - } |
|
| 433 | - if($quota === 0) { |
|
| 434 | - $quota = 'default'; |
|
| 435 | - }else if($quota === -1) { |
|
| 436 | - $quota = 'none'; |
|
| 437 | - } else { |
|
| 438 | - $quota = \OCP\Util::humanFileSize($quota); |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - $targetUser->setQuota($quota); |
|
| 442 | - break; |
|
| 443 | - case 'password': |
|
| 444 | - $targetUser->setPassword($value); |
|
| 445 | - break; |
|
| 446 | - case 'language': |
|
| 447 | - $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
|
| 448 | - if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
|
| 449 | - throw new OCSException('Invalid language', 102); |
|
| 450 | - } |
|
| 451 | - $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
|
| 452 | - break; |
|
| 453 | - case AccountManager::PROPERTY_EMAIL: |
|
| 454 | - if(filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 455 | - $targetUser->setEMailAddress($value); |
|
| 456 | - } else { |
|
| 457 | - throw new OCSException('', 102); |
|
| 458 | - } |
|
| 459 | - break; |
|
| 460 | - case AccountManager::PROPERTY_PHONE: |
|
| 461 | - case AccountManager::PROPERTY_ADDRESS: |
|
| 462 | - case AccountManager::PROPERTY_WEBSITE: |
|
| 463 | - case AccountManager::PROPERTY_TWITTER: |
|
| 464 | - $userAccount = $this->accountManager->getUser($targetUser); |
|
| 465 | - if ($userAccount[$key]['value'] !== $value) { |
|
| 466 | - $userAccount[$key]['value'] = $value; |
|
| 467 | - $this->accountManager->updateUser($targetUser, $userAccount); |
|
| 468 | - } |
|
| 469 | - break; |
|
| 470 | - default: |
|
| 471 | - throw new OCSException('', 103); |
|
| 472 | - } |
|
| 473 | - return new DataResponse(); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * @PasswordConfirmationRequired |
|
| 478 | - * @NoAdminRequired |
|
| 479 | - * |
|
| 480 | - * @param string $userId |
|
| 481 | - * @return DataResponse |
|
| 482 | - * @throws OCSException |
|
| 483 | - */ |
|
| 484 | - public function deleteUser(string $userId): DataResponse { |
|
| 485 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 486 | - |
|
| 487 | - $targetUser = $this->userManager->get($userId); |
|
| 488 | - |
|
| 489 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 490 | - throw new OCSException('', 101); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - // If not permitted |
|
| 494 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 495 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 496 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - // Go ahead with the delete |
|
| 500 | - if($targetUser->delete()) { |
|
| 501 | - return new DataResponse(); |
|
| 502 | - } else { |
|
| 503 | - throw new OCSException('', 101); |
|
| 504 | - } |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * @PasswordConfirmationRequired |
|
| 509 | - * @NoAdminRequired |
|
| 510 | - * |
|
| 511 | - * @param string $userId |
|
| 512 | - * @return DataResponse |
|
| 513 | - * @throws OCSException |
|
| 514 | - * @throws OCSForbiddenException |
|
| 515 | - */ |
|
| 516 | - public function disableUser(string $userId): DataResponse { |
|
| 517 | - return $this->setEnabled($userId, false); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * @PasswordConfirmationRequired |
|
| 522 | - * @NoAdminRequired |
|
| 523 | - * |
|
| 524 | - * @param string $userId |
|
| 525 | - * @return DataResponse |
|
| 526 | - * @throws OCSException |
|
| 527 | - * @throws OCSForbiddenException |
|
| 528 | - */ |
|
| 529 | - public function enableUser(string $userId): DataResponse { |
|
| 530 | - return $this->setEnabled($userId, true); |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * @param string $userId |
|
| 535 | - * @param bool $value |
|
| 536 | - * @return DataResponse |
|
| 537 | - * @throws OCSException |
|
| 538 | - */ |
|
| 539 | - private function setEnabled(string $userId, bool $value): DataResponse { |
|
| 540 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 541 | - |
|
| 542 | - $targetUser = $this->userManager->get($userId); |
|
| 543 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 544 | - throw new OCSException('', 101); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - // If not permitted |
|
| 548 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 549 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 550 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - // enable/disable the user now |
|
| 554 | - $targetUser->setEnabled($value); |
|
| 555 | - return new DataResponse(); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * @NoAdminRequired |
|
| 560 | - * @NoSubAdminRequired |
|
| 561 | - * |
|
| 562 | - * @param string $userId |
|
| 563 | - * @return DataResponse |
|
| 564 | - * @throws OCSException |
|
| 565 | - */ |
|
| 566 | - public function getUsersGroups(string $userId): DataResponse { |
|
| 567 | - $loggedInUser = $this->userSession->getUser(); |
|
| 568 | - |
|
| 569 | - $targetUser = $this->userManager->get($userId); |
|
| 570 | - if($targetUser === null) { |
|
| 571 | - throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 575 | - // Self lookup or admin lookup |
|
| 576 | - return new DataResponse([ |
|
| 577 | - 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
|
| 578 | - ]); |
|
| 579 | - } else { |
|
| 580 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 581 | - |
|
| 582 | - // Looking up someone else |
|
| 583 | - if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 584 | - // Return the group that the method caller is subadmin of for the user in question |
|
| 585 | - /** @var IGroup[] $getSubAdminsGroups */ |
|
| 586 | - $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 587 | - foreach ($getSubAdminsGroups as $key => $group) { |
|
| 588 | - $getSubAdminsGroups[$key] = $group->getGID(); |
|
| 589 | - } |
|
| 590 | - $groups = array_intersect( |
|
| 591 | - $getSubAdminsGroups, |
|
| 592 | - $this->groupManager->getUserGroupIds($targetUser) |
|
| 593 | - ); |
|
| 594 | - return new DataResponse(['groups' => $groups]); |
|
| 595 | - } else { |
|
| 596 | - // Not permitted |
|
| 597 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 598 | - } |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * @PasswordConfirmationRequired |
|
| 605 | - * @NoAdminRequired |
|
| 606 | - * |
|
| 607 | - * @param string $userId |
|
| 608 | - * @param string $groupid |
|
| 609 | - * @return DataResponse |
|
| 610 | - * @throws OCSException |
|
| 611 | - */ |
|
| 612 | - public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
|
| 613 | - if($groupid === '') { |
|
| 614 | - throw new OCSException('', 101); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - $group = $this->groupManager->get($groupid); |
|
| 618 | - $targetUser = $this->userManager->get($userId); |
|
| 619 | - if($group === null) { |
|
| 620 | - throw new OCSException('', 102); |
|
| 621 | - } |
|
| 622 | - if($targetUser === null) { |
|
| 623 | - throw new OCSException('', 103); |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - // If they're not an admin, check they are a subadmin of the group in question |
|
| 627 | - $loggedInUser = $this->userSession->getUser(); |
|
| 628 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 629 | - if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 630 | - throw new OCSException('', 104); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - // Add user to group |
|
| 634 | - $group->addUser($targetUser); |
|
| 635 | - return new DataResponse(); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * @PasswordConfirmationRequired |
|
| 640 | - * @NoAdminRequired |
|
| 641 | - * |
|
| 642 | - * @param string $userId |
|
| 643 | - * @param string $groupid |
|
| 644 | - * @return DataResponse |
|
| 645 | - * @throws OCSException |
|
| 646 | - */ |
|
| 647 | - public function removeFromGroup(string $userId, string $groupid): DataResponse { |
|
| 648 | - $loggedInUser = $this->userSession->getUser(); |
|
| 649 | - |
|
| 650 | - if($groupid === null || trim($groupid) === '') { |
|
| 651 | - throw new OCSException('', 101); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $group = $this->groupManager->get($groupid); |
|
| 655 | - if($group === null) { |
|
| 656 | - throw new OCSException('', 102); |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - $targetUser = $this->userManager->get($userId); |
|
| 660 | - if($targetUser === null) { |
|
| 661 | - throw new OCSException('', 103); |
|
| 662 | - } |
|
| 663 | - |
|
| 664 | - // If they're not an admin, check they are a subadmin of the group in question |
|
| 665 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 666 | - if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 667 | - throw new OCSException('', 104); |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
|
| 671 | - if ($targetUser->getUID() === $loggedInUser->getUID()) { |
|
| 672 | - if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 673 | - if ($group->getGID() === 'admin') { |
|
| 674 | - throw new OCSException('Cannot remove yourself from the admin group', 105); |
|
| 675 | - } |
|
| 676 | - } else { |
|
| 677 | - // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
|
| 678 | - throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 682 | - /** @var IGroup[] $subAdminGroups */ |
|
| 683 | - $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 684 | - $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
| 685 | - return $subAdminGroup->getGID(); |
|
| 686 | - }, $subAdminGroups); |
|
| 687 | - $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
|
| 688 | - $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
|
| 689 | - |
|
| 690 | - if (count($userSubAdminGroups) <= 1) { |
|
| 691 | - // Subadmin must not be able to remove a user from all their subadmin groups. |
|
| 692 | - throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105); |
|
| 693 | - } |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - // Remove user from group |
|
| 697 | - $group->removeUser($targetUser); |
|
| 698 | - return new DataResponse(); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Creates a subadmin |
|
| 703 | - * |
|
| 704 | - * @PasswordConfirmationRequired |
|
| 705 | - * |
|
| 706 | - * @param string $userId |
|
| 707 | - * @param string $groupid |
|
| 708 | - * @return DataResponse |
|
| 709 | - * @throws OCSException |
|
| 710 | - */ |
|
| 711 | - public function addSubAdmin(string $userId, string $groupid): DataResponse { |
|
| 712 | - $group = $this->groupManager->get($groupid); |
|
| 713 | - $user = $this->userManager->get($userId); |
|
| 714 | - |
|
| 715 | - // Check if the user exists |
|
| 716 | - if($user === null) { |
|
| 717 | - throw new OCSException('User does not exist', 101); |
|
| 718 | - } |
|
| 719 | - // Check if group exists |
|
| 720 | - if($group === null) { |
|
| 721 | - throw new OCSException('Group does not exist', 102); |
|
| 722 | - } |
|
| 723 | - // Check if trying to make subadmin of admin group |
|
| 724 | - if($group->getGID() === 'admin') { |
|
| 725 | - throw new OCSException('Cannot create subadmins for admin group', 103); |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 729 | - |
|
| 730 | - // We cannot be subadmin twice |
|
| 731 | - if ($subAdminManager->isSubAdminofGroup($user, $group)) { |
|
| 732 | - return new DataResponse(); |
|
| 733 | - } |
|
| 734 | - // Go |
|
| 735 | - if($subAdminManager->createSubAdmin($user, $group)) { |
|
| 736 | - return new DataResponse(); |
|
| 737 | - } else { |
|
| 738 | - throw new OCSException('Unknown error occurred', 103); |
|
| 739 | - } |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - /** |
|
| 743 | - * Removes a subadmin from a group |
|
| 744 | - * |
|
| 745 | - * @PasswordConfirmationRequired |
|
| 746 | - * |
|
| 747 | - * @param string $userId |
|
| 748 | - * @param string $groupid |
|
| 749 | - * @return DataResponse |
|
| 750 | - * @throws OCSException |
|
| 751 | - */ |
|
| 752 | - public function removeSubAdmin(string $userId, string $groupid): DataResponse { |
|
| 753 | - $group = $this->groupManager->get($groupid); |
|
| 754 | - $user = $this->userManager->get($userId); |
|
| 755 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 756 | - |
|
| 757 | - // Check if the user exists |
|
| 758 | - if($user === null) { |
|
| 759 | - throw new OCSException('User does not exist', 101); |
|
| 760 | - } |
|
| 761 | - // Check if the group exists |
|
| 762 | - if($group === null) { |
|
| 763 | - throw new OCSException('Group does not exist', 101); |
|
| 764 | - } |
|
| 765 | - // Check if they are a subadmin of this said group |
|
| 766 | - if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 767 | - throw new OCSException('User is not a subadmin of this group', 102); |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - // Go |
|
| 771 | - if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 772 | - return new DataResponse(); |
|
| 773 | - } else { |
|
| 774 | - throw new OCSException('Unknown error occurred', 103); |
|
| 775 | - } |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - /** |
|
| 779 | - * Get the groups a user is a subadmin of |
|
| 780 | - * |
|
| 781 | - * @param string $userId |
|
| 782 | - * @return DataResponse |
|
| 783 | - * @throws OCSException |
|
| 784 | - */ |
|
| 785 | - public function getUserSubAdminGroups(string $userId): DataResponse { |
|
| 786 | - $user = $this->userManager->get($userId); |
|
| 787 | - // Check if the user exists |
|
| 788 | - if($user === null) { |
|
| 789 | - throw new OCSException('User does not exist', 101); |
|
| 790 | - } |
|
| 791 | - |
|
| 792 | - // Get the subadmin groups |
|
| 793 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
| 794 | - foreach ($groups as $key => $group) { |
|
| 795 | - $groups[$key] = $group->getGID(); |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - if(!$groups) { |
|
| 799 | - throw new OCSException('Unknown error occurred', 102); |
|
| 800 | - } else { |
|
| 801 | - return new DataResponse($groups); |
|
| 802 | - } |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * @param string $userId |
|
| 807 | - * @return array |
|
| 808 | - * @throws \OCP\Files\NotFoundException |
|
| 809 | - */ |
|
| 810 | - protected function fillStorageInfo(string $userId): array { |
|
| 811 | - try { |
|
| 812 | - \OC_Util::tearDownFS(); |
|
| 813 | - \OC_Util::setupFS($userId); |
|
| 814 | - $storage = OC_Helper::getStorageInfo('/'); |
|
| 815 | - $data = [ |
|
| 816 | - 'free' => $storage['free'], |
|
| 817 | - 'used' => $storage['used'], |
|
| 818 | - 'total' => $storage['total'], |
|
| 819 | - 'relative' => $storage['relative'], |
|
| 820 | - 'quota' => $storage['quota'], |
|
| 821 | - ]; |
|
| 822 | - } catch (NotFoundException $ex) { |
|
| 823 | - $data = []; |
|
| 824 | - } |
|
| 825 | - return $data; |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * @NoAdminRequired |
|
| 830 | - * @PasswordConfirmationRequired |
|
| 831 | - * |
|
| 832 | - * resend welcome message |
|
| 833 | - * |
|
| 834 | - * @param string $userId |
|
| 835 | - * @return DataResponse |
|
| 836 | - * @throws OCSException |
|
| 837 | - */ |
|
| 838 | - public function resendWelcomeMessage(string $userId): DataResponse { |
|
| 839 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 840 | - |
|
| 841 | - $targetUser = $this->userManager->get($userId); |
|
| 842 | - if($targetUser === null) { |
|
| 843 | - throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - // Check if admin / subadmin |
|
| 847 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 848 | - if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 849 | - && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 850 | - // No rights |
|
| 851 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 852 | - } |
|
| 853 | - |
|
| 854 | - $email = $targetUser->getEMailAddress(); |
|
| 855 | - if ($email === '' || $email === null) { |
|
| 856 | - throw new OCSException('Email address not available', 101); |
|
| 857 | - } |
|
| 858 | - $username = $targetUser->getUID(); |
|
| 859 | - $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); |
|
| 860 | - if (!$this->l10nFactory->languageExists('settings', $lang)) { |
|
| 861 | - $lang = 'en'; |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - $l10n = $this->l10nFactory->get('settings', $lang); |
|
| 865 | - |
|
| 866 | - try { |
|
| 867 | - $this->newUserMailHelper->setL10N($l10n); |
|
| 868 | - $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
|
| 869 | - $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
|
| 870 | - } catch(\Exception $e) { |
|
| 871 | - $this->logger->logException($e, [ |
|
| 872 | - 'message' => "Can't send new user mail to $email", |
|
| 873 | - 'level' => \OCP\Util::ERROR, |
|
| 874 | - 'app' => 'settings', |
|
| 875 | - ]); |
|
| 876 | - throw new OCSException('Sending email failed', 102); |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - return new DataResponse(); |
|
| 880 | - } |
|
| 56 | + /** @var IUserManager */ |
|
| 57 | + private $userManager; |
|
| 58 | + /** @var IConfig */ |
|
| 59 | + private $config; |
|
| 60 | + /** @var IAppManager */ |
|
| 61 | + private $appManager; |
|
| 62 | + /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface |
|
| 63 | + private $groupManager; |
|
| 64 | + /** @var IUserSession */ |
|
| 65 | + private $userSession; |
|
| 66 | + /** @var AccountManager */ |
|
| 67 | + private $accountManager; |
|
| 68 | + /** @var ILogger */ |
|
| 69 | + private $logger; |
|
| 70 | + /** @var IFactory */ |
|
| 71 | + private $l10nFactory; |
|
| 72 | + /** @var NewUserMailHelper */ |
|
| 73 | + private $newUserMailHelper; |
|
| 74 | + /** @var FederatedFileSharingFactory */ |
|
| 75 | + private $federatedFileSharingFactory; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param string $appName |
|
| 79 | + * @param IRequest $request |
|
| 80 | + * @param IUserManager $userManager |
|
| 81 | + * @param IConfig $config |
|
| 82 | + * @param IAppManager $appManager |
|
| 83 | + * @param IGroupManager $groupManager |
|
| 84 | + * @param IUserSession $userSession |
|
| 85 | + * @param AccountManager $accountManager |
|
| 86 | + * @param ILogger $logger |
|
| 87 | + * @param IFactory $l10nFactory |
|
| 88 | + * @param NewUserMailHelper $newUserMailHelper |
|
| 89 | + * @param FederatedFileSharingFactory $federatedFileSharingFactory |
|
| 90 | + */ |
|
| 91 | + public function __construct(string $appName, |
|
| 92 | + IRequest $request, |
|
| 93 | + IUserManager $userManager, |
|
| 94 | + IConfig $config, |
|
| 95 | + IAppManager $appManager, |
|
| 96 | + IGroupManager $groupManager, |
|
| 97 | + IUserSession $userSession, |
|
| 98 | + AccountManager $accountManager, |
|
| 99 | + ILogger $logger, |
|
| 100 | + IFactory $l10nFactory, |
|
| 101 | + NewUserMailHelper $newUserMailHelper, |
|
| 102 | + FederatedFileSharingFactory $federatedFileSharingFactory) { |
|
| 103 | + parent::__construct($appName, $request); |
|
| 104 | + |
|
| 105 | + $this->userManager = $userManager; |
|
| 106 | + $this->config = $config; |
|
| 107 | + $this->appManager = $appManager; |
|
| 108 | + $this->groupManager = $groupManager; |
|
| 109 | + $this->userSession = $userSession; |
|
| 110 | + $this->accountManager = $accountManager; |
|
| 111 | + $this->logger = $logger; |
|
| 112 | + $this->l10nFactory = $l10nFactory; |
|
| 113 | + $this->newUserMailHelper = $newUserMailHelper; |
|
| 114 | + $this->federatedFileSharingFactory = $federatedFileSharingFactory; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @NoAdminRequired |
|
| 119 | + * |
|
| 120 | + * returns a list of users |
|
| 121 | + * |
|
| 122 | + * @param string $search |
|
| 123 | + * @param int $limit |
|
| 124 | + * @param int $offset |
|
| 125 | + * @return DataResponse |
|
| 126 | + */ |
|
| 127 | + public function getUsers(string $search = '', $limit = null, $offset = null): DataResponse { |
|
| 128 | + $user = $this->userSession->getUser(); |
|
| 129 | + $users = []; |
|
| 130 | + |
|
| 131 | + // Admin? Or SubAdmin? |
|
| 132 | + $uid = $user->getUID(); |
|
| 133 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 134 | + if($this->groupManager->isAdmin($uid)){ |
|
| 135 | + $users = $this->userManager->search($search, $limit, $offset); |
|
| 136 | + } else if ($subAdminManager->isSubAdmin($user)) { |
|
| 137 | + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
| 138 | + foreach ($subAdminOfGroups as $key => $group) { |
|
| 139 | + $subAdminOfGroups[$key] = $group->getGID(); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + if($offset === null) { |
|
| 143 | + $offset = 0; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $users = []; |
|
| 147 | + foreach ($subAdminOfGroups as $group) { |
|
| 148 | + $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search)); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $users = array_slice($users, $offset, $limit); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + $users = array_keys($users); |
|
| 155 | + |
|
| 156 | + return new DataResponse([ |
|
| 157 | + 'users' => $users |
|
| 158 | + ]); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @PasswordConfirmationRequired |
|
| 163 | + * @NoAdminRequired |
|
| 164 | + * |
|
| 165 | + * @param string $userid |
|
| 166 | + * @param string $password |
|
| 167 | + * @param array $groups |
|
| 168 | + * @return DataResponse |
|
| 169 | + * @throws OCSException |
|
| 170 | + */ |
|
| 171 | + public function addUser(string $userid, string $password, array $groups = []): DataResponse { |
|
| 172 | + $user = $this->userSession->getUser(); |
|
| 173 | + $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
| 174 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 175 | + |
|
| 176 | + if($this->userManager->userExists($userid)) { |
|
| 177 | + $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
|
| 178 | + throw new OCSException('User already exists', 102); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + if($groups !== []) { |
|
| 182 | + foreach ($groups as $group) { |
|
| 183 | + if(!$this->groupManager->groupExists($group)) { |
|
| 184 | + throw new OCSException('group '.$group.' does not exist', 104); |
|
| 185 | + } |
|
| 186 | + if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
| 187 | + throw new OCSException('insufficient privileges for group '. $group, 105); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + } else { |
|
| 191 | + if(!$isAdmin) { |
|
| 192 | + throw new OCSException('no group specified (required for subadmins)', 106); |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + try { |
|
| 197 | + $newUser = $this->userManager->createUser($userid, $password); |
|
| 198 | + $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); |
|
| 199 | + |
|
| 200 | + foreach ($groups as $group) { |
|
| 201 | + $this->groupManager->get($group)->addUser($newUser); |
|
| 202 | + $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return new DataResponse(); |
|
| 206 | + } catch (HintException $e ) { |
|
| 207 | + $this->logger->logException($e, [ |
|
| 208 | + 'message' => 'Failed addUser attempt with hint exception.', |
|
| 209 | + 'level' => \OCP\Util::WARN, |
|
| 210 | + 'app' => 'ocs_api', |
|
| 211 | + ]); |
|
| 212 | + throw new OCSException($e->getHint(), 107); |
|
| 213 | + } catch (\Exception $e) { |
|
| 214 | + $this->logger->logException($e, [ |
|
| 215 | + 'message' => 'Failed addUser attempt with exception.', |
|
| 216 | + 'level' => \OCP\Util::ERROR, |
|
| 217 | + 'app' => 'ocs_api', |
|
| 218 | + ]); |
|
| 219 | + throw new OCSException('Bad request', 101); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * @NoAdminRequired |
|
| 225 | + * @NoSubAdminRequired |
|
| 226 | + * |
|
| 227 | + * gets user info |
|
| 228 | + * |
|
| 229 | + * @param string $userId |
|
| 230 | + * @return DataResponse |
|
| 231 | + * @throws OCSException |
|
| 232 | + */ |
|
| 233 | + public function getUser(string $userId): DataResponse { |
|
| 234 | + $data = $this->getUserData($userId); |
|
| 235 | + return new DataResponse($data); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * @NoAdminRequired |
|
| 240 | + * @NoSubAdminRequired |
|
| 241 | + * |
|
| 242 | + * gets user info from the currently logged in user |
|
| 243 | + * |
|
| 244 | + * @return DataResponse |
|
| 245 | + * @throws OCSException |
|
| 246 | + */ |
|
| 247 | + public function getCurrentUser(): DataResponse { |
|
| 248 | + $user = $this->userSession->getUser(); |
|
| 249 | + if ($user) { |
|
| 250 | + $data = $this->getUserData($user->getUID()); |
|
| 251 | + // rename "displayname" to "display-name" only for this call to keep |
|
| 252 | + // the API stable. |
|
| 253 | + $data['display-name'] = $data['displayname']; |
|
| 254 | + unset($data['displayname']); |
|
| 255 | + return new DataResponse($data); |
|
| 256 | + |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * creates a array with all user data |
|
| 264 | + * |
|
| 265 | + * @param $userId |
|
| 266 | + * @return array |
|
| 267 | + * @throws OCSException |
|
| 268 | + */ |
|
| 269 | + protected function getUserData(string $userId): array { |
|
| 270 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 271 | + |
|
| 272 | + $data = []; |
|
| 273 | + |
|
| 274 | + // Check if the target user exists |
|
| 275 | + $targetUserObject = $this->userManager->get($userId); |
|
| 276 | + if($targetUserObject === null) { |
|
| 277 | + throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + // Admin? Or SubAdmin? |
|
| 281 | + if($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 282 | + || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
| 283 | + $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
|
| 284 | + } else { |
|
| 285 | + // Check they are looking up themselves |
|
| 286 | + if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 287 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + $userAccount = $this->accountManager->getUser($targetUserObject); |
|
| 292 | + $groups = $this->groupManager->getUserGroups($targetUserObject); |
|
| 293 | + $gids = []; |
|
| 294 | + foreach ($groups as $group) { |
|
| 295 | + $gids[] = $group->getDisplayName(); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // Find the data |
|
| 299 | + $data['id'] = $targetUserObject->getUID(); |
|
| 300 | + $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
| 301 | + $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
| 302 | + $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
| 303 | + $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value']; |
|
| 304 | + $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value']; |
|
| 305 | + $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value']; |
|
| 306 | + $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value']; |
|
| 307 | + $data['groups'] = $gids; |
|
| 308 | + $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang'); |
|
| 309 | + |
|
| 310 | + return $data; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @NoAdminRequired |
|
| 315 | + * @NoSubAdminRequired |
|
| 316 | + */ |
|
| 317 | + public function getEditableFields(): DataResponse { |
|
| 318 | + $permittedFields = []; |
|
| 319 | + |
|
| 320 | + // Editing self (display, email) |
|
| 321 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 322 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 323 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 327 | + $federatedFileSharing = $this->federatedFileSharingFactory->get(); |
|
| 328 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 329 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 330 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 331 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 332 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 333 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + return new DataResponse($permittedFields); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @NoAdminRequired |
|
| 342 | + * @NoSubAdminRequired |
|
| 343 | + * @PasswordConfirmationRequired |
|
| 344 | + * |
|
| 345 | + * edit users |
|
| 346 | + * |
|
| 347 | + * @param string $userId |
|
| 348 | + * @param string $key |
|
| 349 | + * @param string $value |
|
| 350 | + * @return DataResponse |
|
| 351 | + * @throws OCSException |
|
| 352 | + */ |
|
| 353 | + public function editUser(string $userId, string $key, string $value): DataResponse { |
|
| 354 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 355 | + |
|
| 356 | + $targetUser = $this->userManager->get($userId); |
|
| 357 | + if($targetUser === null) { |
|
| 358 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + $permittedFields = []; |
|
| 362 | + if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 363 | + // Editing self (display, email) |
|
| 364 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 365 | + $permittedFields[] = 'display'; |
|
| 366 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 367 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + $permittedFields[] = 'password'; |
|
| 371 | + if ($this->config->getSystemValue('force_language', false) === false || |
|
| 372 | + $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 373 | + $permittedFields[] = 'language'; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 377 | + $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 378 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 379 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 380 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 381 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 382 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 383 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + // If admin they can edit their own quota |
|
| 388 | + if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 389 | + $permittedFields[] = 'quota'; |
|
| 390 | + } |
|
| 391 | + } else { |
|
| 392 | + // Check if admin / subadmin |
|
| 393 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 394 | + if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 395 | + || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 396 | + // They have permissions over the user |
|
| 397 | + $permittedFields[] = 'display'; |
|
| 398 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 399 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 400 | + $permittedFields[] = 'password'; |
|
| 401 | + $permittedFields[] = 'language'; |
|
| 402 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 403 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 404 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 405 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 406 | + $permittedFields[] = 'quota'; |
|
| 407 | + } else { |
|
| 408 | + // No rights |
|
| 409 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + // Check if permitted to edit this field |
|
| 413 | + if(!in_array($key, $permittedFields)) { |
|
| 414 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 415 | + } |
|
| 416 | + // Process the edit |
|
| 417 | + switch($key) { |
|
| 418 | + case 'display': |
|
| 419 | + case AccountManager::PROPERTY_DISPLAYNAME: |
|
| 420 | + $targetUser->setDisplayName($value); |
|
| 421 | + break; |
|
| 422 | + case 'quota': |
|
| 423 | + $quota = $value; |
|
| 424 | + if($quota !== 'none' && $quota !== 'default') { |
|
| 425 | + if (is_numeric($quota)) { |
|
| 426 | + $quota = (float) $quota; |
|
| 427 | + } else { |
|
| 428 | + $quota = \OCP\Util::computerFileSize($quota); |
|
| 429 | + } |
|
| 430 | + if ($quota === false) { |
|
| 431 | + throw new OCSException('Invalid quota value '.$value, 103); |
|
| 432 | + } |
|
| 433 | + if($quota === 0) { |
|
| 434 | + $quota = 'default'; |
|
| 435 | + }else if($quota === -1) { |
|
| 436 | + $quota = 'none'; |
|
| 437 | + } else { |
|
| 438 | + $quota = \OCP\Util::humanFileSize($quota); |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + $targetUser->setQuota($quota); |
|
| 442 | + break; |
|
| 443 | + case 'password': |
|
| 444 | + $targetUser->setPassword($value); |
|
| 445 | + break; |
|
| 446 | + case 'language': |
|
| 447 | + $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
|
| 448 | + if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
|
| 449 | + throw new OCSException('Invalid language', 102); |
|
| 450 | + } |
|
| 451 | + $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
|
| 452 | + break; |
|
| 453 | + case AccountManager::PROPERTY_EMAIL: |
|
| 454 | + if(filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 455 | + $targetUser->setEMailAddress($value); |
|
| 456 | + } else { |
|
| 457 | + throw new OCSException('', 102); |
|
| 458 | + } |
|
| 459 | + break; |
|
| 460 | + case AccountManager::PROPERTY_PHONE: |
|
| 461 | + case AccountManager::PROPERTY_ADDRESS: |
|
| 462 | + case AccountManager::PROPERTY_WEBSITE: |
|
| 463 | + case AccountManager::PROPERTY_TWITTER: |
|
| 464 | + $userAccount = $this->accountManager->getUser($targetUser); |
|
| 465 | + if ($userAccount[$key]['value'] !== $value) { |
|
| 466 | + $userAccount[$key]['value'] = $value; |
|
| 467 | + $this->accountManager->updateUser($targetUser, $userAccount); |
|
| 468 | + } |
|
| 469 | + break; |
|
| 470 | + default: |
|
| 471 | + throw new OCSException('', 103); |
|
| 472 | + } |
|
| 473 | + return new DataResponse(); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * @PasswordConfirmationRequired |
|
| 478 | + * @NoAdminRequired |
|
| 479 | + * |
|
| 480 | + * @param string $userId |
|
| 481 | + * @return DataResponse |
|
| 482 | + * @throws OCSException |
|
| 483 | + */ |
|
| 484 | + public function deleteUser(string $userId): DataResponse { |
|
| 485 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 486 | + |
|
| 487 | + $targetUser = $this->userManager->get($userId); |
|
| 488 | + |
|
| 489 | + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 490 | + throw new OCSException('', 101); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + // If not permitted |
|
| 494 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 495 | + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 496 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + // Go ahead with the delete |
|
| 500 | + if($targetUser->delete()) { |
|
| 501 | + return new DataResponse(); |
|
| 502 | + } else { |
|
| 503 | + throw new OCSException('', 101); |
|
| 504 | + } |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * @PasswordConfirmationRequired |
|
| 509 | + * @NoAdminRequired |
|
| 510 | + * |
|
| 511 | + * @param string $userId |
|
| 512 | + * @return DataResponse |
|
| 513 | + * @throws OCSException |
|
| 514 | + * @throws OCSForbiddenException |
|
| 515 | + */ |
|
| 516 | + public function disableUser(string $userId): DataResponse { |
|
| 517 | + return $this->setEnabled($userId, false); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * @PasswordConfirmationRequired |
|
| 522 | + * @NoAdminRequired |
|
| 523 | + * |
|
| 524 | + * @param string $userId |
|
| 525 | + * @return DataResponse |
|
| 526 | + * @throws OCSException |
|
| 527 | + * @throws OCSForbiddenException |
|
| 528 | + */ |
|
| 529 | + public function enableUser(string $userId): DataResponse { |
|
| 530 | + return $this->setEnabled($userId, true); |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * @param string $userId |
|
| 535 | + * @param bool $value |
|
| 536 | + * @return DataResponse |
|
| 537 | + * @throws OCSException |
|
| 538 | + */ |
|
| 539 | + private function setEnabled(string $userId, bool $value): DataResponse { |
|
| 540 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 541 | + |
|
| 542 | + $targetUser = $this->userManager->get($userId); |
|
| 543 | + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 544 | + throw new OCSException('', 101); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + // If not permitted |
|
| 548 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 549 | + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 550 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + // enable/disable the user now |
|
| 554 | + $targetUser->setEnabled($value); |
|
| 555 | + return new DataResponse(); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * @NoAdminRequired |
|
| 560 | + * @NoSubAdminRequired |
|
| 561 | + * |
|
| 562 | + * @param string $userId |
|
| 563 | + * @return DataResponse |
|
| 564 | + * @throws OCSException |
|
| 565 | + */ |
|
| 566 | + public function getUsersGroups(string $userId): DataResponse { |
|
| 567 | + $loggedInUser = $this->userSession->getUser(); |
|
| 568 | + |
|
| 569 | + $targetUser = $this->userManager->get($userId); |
|
| 570 | + if($targetUser === null) { |
|
| 571 | + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 575 | + // Self lookup or admin lookup |
|
| 576 | + return new DataResponse([ |
|
| 577 | + 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
|
| 578 | + ]); |
|
| 579 | + } else { |
|
| 580 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 581 | + |
|
| 582 | + // Looking up someone else |
|
| 583 | + if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 584 | + // Return the group that the method caller is subadmin of for the user in question |
|
| 585 | + /** @var IGroup[] $getSubAdminsGroups */ |
|
| 586 | + $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 587 | + foreach ($getSubAdminsGroups as $key => $group) { |
|
| 588 | + $getSubAdminsGroups[$key] = $group->getGID(); |
|
| 589 | + } |
|
| 590 | + $groups = array_intersect( |
|
| 591 | + $getSubAdminsGroups, |
|
| 592 | + $this->groupManager->getUserGroupIds($targetUser) |
|
| 593 | + ); |
|
| 594 | + return new DataResponse(['groups' => $groups]); |
|
| 595 | + } else { |
|
| 596 | + // Not permitted |
|
| 597 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 598 | + } |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * @PasswordConfirmationRequired |
|
| 605 | + * @NoAdminRequired |
|
| 606 | + * |
|
| 607 | + * @param string $userId |
|
| 608 | + * @param string $groupid |
|
| 609 | + * @return DataResponse |
|
| 610 | + * @throws OCSException |
|
| 611 | + */ |
|
| 612 | + public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
|
| 613 | + if($groupid === '') { |
|
| 614 | + throw new OCSException('', 101); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + $group = $this->groupManager->get($groupid); |
|
| 618 | + $targetUser = $this->userManager->get($userId); |
|
| 619 | + if($group === null) { |
|
| 620 | + throw new OCSException('', 102); |
|
| 621 | + } |
|
| 622 | + if($targetUser === null) { |
|
| 623 | + throw new OCSException('', 103); |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + // If they're not an admin, check they are a subadmin of the group in question |
|
| 627 | + $loggedInUser = $this->userSession->getUser(); |
|
| 628 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 629 | + if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 630 | + throw new OCSException('', 104); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + // Add user to group |
|
| 634 | + $group->addUser($targetUser); |
|
| 635 | + return new DataResponse(); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * @PasswordConfirmationRequired |
|
| 640 | + * @NoAdminRequired |
|
| 641 | + * |
|
| 642 | + * @param string $userId |
|
| 643 | + * @param string $groupid |
|
| 644 | + * @return DataResponse |
|
| 645 | + * @throws OCSException |
|
| 646 | + */ |
|
| 647 | + public function removeFromGroup(string $userId, string $groupid): DataResponse { |
|
| 648 | + $loggedInUser = $this->userSession->getUser(); |
|
| 649 | + |
|
| 650 | + if($groupid === null || trim($groupid) === '') { |
|
| 651 | + throw new OCSException('', 101); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $group = $this->groupManager->get($groupid); |
|
| 655 | + if($group === null) { |
|
| 656 | + throw new OCSException('', 102); |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + $targetUser = $this->userManager->get($userId); |
|
| 660 | + if($targetUser === null) { |
|
| 661 | + throw new OCSException('', 103); |
|
| 662 | + } |
|
| 663 | + |
|
| 664 | + // If they're not an admin, check they are a subadmin of the group in question |
|
| 665 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 666 | + if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 667 | + throw new OCSException('', 104); |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
|
| 671 | + if ($targetUser->getUID() === $loggedInUser->getUID()) { |
|
| 672 | + if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 673 | + if ($group->getGID() === 'admin') { |
|
| 674 | + throw new OCSException('Cannot remove yourself from the admin group', 105); |
|
| 675 | + } |
|
| 676 | + } else { |
|
| 677 | + // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
|
| 678 | + throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 682 | + /** @var IGroup[] $subAdminGroups */ |
|
| 683 | + $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 684 | + $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
| 685 | + return $subAdminGroup->getGID(); |
|
| 686 | + }, $subAdminGroups); |
|
| 687 | + $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
|
| 688 | + $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
|
| 689 | + |
|
| 690 | + if (count($userSubAdminGroups) <= 1) { |
|
| 691 | + // Subadmin must not be able to remove a user from all their subadmin groups. |
|
| 692 | + throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105); |
|
| 693 | + } |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + // Remove user from group |
|
| 697 | + $group->removeUser($targetUser); |
|
| 698 | + return new DataResponse(); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Creates a subadmin |
|
| 703 | + * |
|
| 704 | + * @PasswordConfirmationRequired |
|
| 705 | + * |
|
| 706 | + * @param string $userId |
|
| 707 | + * @param string $groupid |
|
| 708 | + * @return DataResponse |
|
| 709 | + * @throws OCSException |
|
| 710 | + */ |
|
| 711 | + public function addSubAdmin(string $userId, string $groupid): DataResponse { |
|
| 712 | + $group = $this->groupManager->get($groupid); |
|
| 713 | + $user = $this->userManager->get($userId); |
|
| 714 | + |
|
| 715 | + // Check if the user exists |
|
| 716 | + if($user === null) { |
|
| 717 | + throw new OCSException('User does not exist', 101); |
|
| 718 | + } |
|
| 719 | + // Check if group exists |
|
| 720 | + if($group === null) { |
|
| 721 | + throw new OCSException('Group does not exist', 102); |
|
| 722 | + } |
|
| 723 | + // Check if trying to make subadmin of admin group |
|
| 724 | + if($group->getGID() === 'admin') { |
|
| 725 | + throw new OCSException('Cannot create subadmins for admin group', 103); |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 729 | + |
|
| 730 | + // We cannot be subadmin twice |
|
| 731 | + if ($subAdminManager->isSubAdminofGroup($user, $group)) { |
|
| 732 | + return new DataResponse(); |
|
| 733 | + } |
|
| 734 | + // Go |
|
| 735 | + if($subAdminManager->createSubAdmin($user, $group)) { |
|
| 736 | + return new DataResponse(); |
|
| 737 | + } else { |
|
| 738 | + throw new OCSException('Unknown error occurred', 103); |
|
| 739 | + } |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + /** |
|
| 743 | + * Removes a subadmin from a group |
|
| 744 | + * |
|
| 745 | + * @PasswordConfirmationRequired |
|
| 746 | + * |
|
| 747 | + * @param string $userId |
|
| 748 | + * @param string $groupid |
|
| 749 | + * @return DataResponse |
|
| 750 | + * @throws OCSException |
|
| 751 | + */ |
|
| 752 | + public function removeSubAdmin(string $userId, string $groupid): DataResponse { |
|
| 753 | + $group = $this->groupManager->get($groupid); |
|
| 754 | + $user = $this->userManager->get($userId); |
|
| 755 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 756 | + |
|
| 757 | + // Check if the user exists |
|
| 758 | + if($user === null) { |
|
| 759 | + throw new OCSException('User does not exist', 101); |
|
| 760 | + } |
|
| 761 | + // Check if the group exists |
|
| 762 | + if($group === null) { |
|
| 763 | + throw new OCSException('Group does not exist', 101); |
|
| 764 | + } |
|
| 765 | + // Check if they are a subadmin of this said group |
|
| 766 | + if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 767 | + throw new OCSException('User is not a subadmin of this group', 102); |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + // Go |
|
| 771 | + if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 772 | + return new DataResponse(); |
|
| 773 | + } else { |
|
| 774 | + throw new OCSException('Unknown error occurred', 103); |
|
| 775 | + } |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + /** |
|
| 779 | + * Get the groups a user is a subadmin of |
|
| 780 | + * |
|
| 781 | + * @param string $userId |
|
| 782 | + * @return DataResponse |
|
| 783 | + * @throws OCSException |
|
| 784 | + */ |
|
| 785 | + public function getUserSubAdminGroups(string $userId): DataResponse { |
|
| 786 | + $user = $this->userManager->get($userId); |
|
| 787 | + // Check if the user exists |
|
| 788 | + if($user === null) { |
|
| 789 | + throw new OCSException('User does not exist', 101); |
|
| 790 | + } |
|
| 791 | + |
|
| 792 | + // Get the subadmin groups |
|
| 793 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
| 794 | + foreach ($groups as $key => $group) { |
|
| 795 | + $groups[$key] = $group->getGID(); |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + if(!$groups) { |
|
| 799 | + throw new OCSException('Unknown error occurred', 102); |
|
| 800 | + } else { |
|
| 801 | + return new DataResponse($groups); |
|
| 802 | + } |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * @param string $userId |
|
| 807 | + * @return array |
|
| 808 | + * @throws \OCP\Files\NotFoundException |
|
| 809 | + */ |
|
| 810 | + protected function fillStorageInfo(string $userId): array { |
|
| 811 | + try { |
|
| 812 | + \OC_Util::tearDownFS(); |
|
| 813 | + \OC_Util::setupFS($userId); |
|
| 814 | + $storage = OC_Helper::getStorageInfo('/'); |
|
| 815 | + $data = [ |
|
| 816 | + 'free' => $storage['free'], |
|
| 817 | + 'used' => $storage['used'], |
|
| 818 | + 'total' => $storage['total'], |
|
| 819 | + 'relative' => $storage['relative'], |
|
| 820 | + 'quota' => $storage['quota'], |
|
| 821 | + ]; |
|
| 822 | + } catch (NotFoundException $ex) { |
|
| 823 | + $data = []; |
|
| 824 | + } |
|
| 825 | + return $data; |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * @NoAdminRequired |
|
| 830 | + * @PasswordConfirmationRequired |
|
| 831 | + * |
|
| 832 | + * resend welcome message |
|
| 833 | + * |
|
| 834 | + * @param string $userId |
|
| 835 | + * @return DataResponse |
|
| 836 | + * @throws OCSException |
|
| 837 | + */ |
|
| 838 | + public function resendWelcomeMessage(string $userId): DataResponse { |
|
| 839 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 840 | + |
|
| 841 | + $targetUser = $this->userManager->get($userId); |
|
| 842 | + if($targetUser === null) { |
|
| 843 | + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + // Check if admin / subadmin |
|
| 847 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 848 | + if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 849 | + && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 850 | + // No rights |
|
| 851 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 852 | + } |
|
| 853 | + |
|
| 854 | + $email = $targetUser->getEMailAddress(); |
|
| 855 | + if ($email === '' || $email === null) { |
|
| 856 | + throw new OCSException('Email address not available', 101); |
|
| 857 | + } |
|
| 858 | + $username = $targetUser->getUID(); |
|
| 859 | + $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); |
|
| 860 | + if (!$this->l10nFactory->languageExists('settings', $lang)) { |
|
| 861 | + $lang = 'en'; |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + $l10n = $this->l10nFactory->get('settings', $lang); |
|
| 865 | + |
|
| 866 | + try { |
|
| 867 | + $this->newUserMailHelper->setL10N($l10n); |
|
| 868 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
|
| 869 | + $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
|
| 870 | + } catch(\Exception $e) { |
|
| 871 | + $this->logger->logException($e, [ |
|
| 872 | + 'message' => "Can't send new user mail to $email", |
|
| 873 | + 'level' => \OCP\Util::ERROR, |
|
| 874 | + 'app' => 'settings', |
|
| 875 | + ]); |
|
| 876 | + throw new OCSException('Sending email failed', 102); |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + return new DataResponse(); |
|
| 880 | + } |
|
| 881 | 881 | } |
@@ -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 | * |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | // Admin? Or SubAdmin? |
| 132 | 132 | $uid = $user->getUID(); |
| 133 | 133 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 134 | - if($this->groupManager->isAdmin($uid)){ |
|
| 134 | + if ($this->groupManager->isAdmin($uid)) { |
|
| 135 | 135 | $users = $this->userManager->search($search, $limit, $offset); |
| 136 | 136 | } else if ($subAdminManager->isSubAdmin($user)) { |
| 137 | 137 | $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | $subAdminOfGroups[$key] = $group->getGID(); |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - if($offset === null) { |
|
| 142 | + if ($offset === null) { |
|
| 143 | 143 | $offset = 0; |
| 144 | 144 | } |
| 145 | 145 | |
@@ -173,37 +173,37 @@ discard block |
||
| 173 | 173 | $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
| 174 | 174 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 175 | 175 | |
| 176 | - if($this->userManager->userExists($userid)) { |
|
| 176 | + if ($this->userManager->userExists($userid)) { |
|
| 177 | 177 | $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
| 178 | 178 | throw new OCSException('User already exists', 102); |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - if($groups !== []) { |
|
| 181 | + if ($groups !== []) { |
|
| 182 | 182 | foreach ($groups as $group) { |
| 183 | - if(!$this->groupManager->groupExists($group)) { |
|
| 183 | + if (!$this->groupManager->groupExists($group)) { |
|
| 184 | 184 | throw new OCSException('group '.$group.' does not exist', 104); |
| 185 | 185 | } |
| 186 | - if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
| 187 | - throw new OCSException('insufficient privileges for group '. $group, 105); |
|
| 186 | + if (!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
| 187 | + throw new OCSException('insufficient privileges for group '.$group, 105); |
|
| 188 | 188 | } |
| 189 | 189 | } |
| 190 | 190 | } else { |
| 191 | - if(!$isAdmin) { |
|
| 191 | + if (!$isAdmin) { |
|
| 192 | 192 | throw new OCSException('no group specified (required for subadmins)', 106); |
| 193 | 193 | } |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | try { |
| 197 | 197 | $newUser = $this->userManager->createUser($userid, $password); |
| 198 | - $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); |
|
| 198 | + $this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']); |
|
| 199 | 199 | |
| 200 | 200 | foreach ($groups as $group) { |
| 201 | 201 | $this->groupManager->get($group)->addUser($newUser); |
| 202 | - $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); |
|
| 202 | + $this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']); |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | return new DataResponse(); |
| 206 | - } catch (HintException $e ) { |
|
| 206 | + } catch (HintException $e) { |
|
| 207 | 207 | $this->logger->logException($e, [ |
| 208 | 208 | 'message' => 'Failed addUser attempt with hint exception.', |
| 209 | 209 | 'level' => \OCP\Util::WARN, |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | public function getCurrentUser(): DataResponse { |
| 248 | 248 | $user = $this->userSession->getUser(); |
| 249 | 249 | if ($user) { |
| 250 | - $data = $this->getUserData($user->getUID()); |
|
| 250 | + $data = $this->getUserData($user->getUID()); |
|
| 251 | 251 | // rename "displayname" to "display-name" only for this call to keep |
| 252 | 252 | // the API stable. |
| 253 | 253 | $data['display-name'] = $data['displayname']; |
@@ -273,17 +273,17 @@ discard block |
||
| 273 | 273 | |
| 274 | 274 | // Check if the target user exists |
| 275 | 275 | $targetUserObject = $this->userManager->get($userId); |
| 276 | - if($targetUserObject === null) { |
|
| 276 | + if ($targetUserObject === null) { |
|
| 277 | 277 | throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | // Admin? Or SubAdmin? |
| 281 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 281 | + if ($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 282 | 282 | || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
| 283 | 283 | $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
| 284 | 284 | } else { |
| 285 | 285 | // Check they are looking up themselves |
| 286 | - if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 286 | + if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 287 | 287 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 288 | 288 | } |
| 289 | 289 | } |
@@ -354,12 +354,12 @@ discard block |
||
| 354 | 354 | $currentLoggedInUser = $this->userSession->getUser(); |
| 355 | 355 | |
| 356 | 356 | $targetUser = $this->userManager->get($userId); |
| 357 | - if($targetUser === null) { |
|
| 357 | + if ($targetUser === null) { |
|
| 358 | 358 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | $permittedFields = []; |
| 362 | - if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 362 | + if ($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 363 | 363 | // Editing self (display, email) |
| 364 | 364 | if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
| 365 | 365 | $permittedFields[] = 'display'; |
@@ -385,13 +385,13 @@ discard block |
||
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | // If admin they can edit their own quota |
| 388 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 388 | + if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 389 | 389 | $permittedFields[] = 'quota'; |
| 390 | 390 | } |
| 391 | 391 | } else { |
| 392 | 392 | // Check if admin / subadmin |
| 393 | 393 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 394 | - if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 394 | + if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 395 | 395 | || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
| 396 | 396 | // They have permissions over the user |
| 397 | 397 | $permittedFields[] = 'display'; |
@@ -410,18 +410,18 @@ discard block |
||
| 410 | 410 | } |
| 411 | 411 | } |
| 412 | 412 | // Check if permitted to edit this field |
| 413 | - if(!in_array($key, $permittedFields)) { |
|
| 413 | + if (!in_array($key, $permittedFields)) { |
|
| 414 | 414 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 415 | 415 | } |
| 416 | 416 | // Process the edit |
| 417 | - switch($key) { |
|
| 417 | + switch ($key) { |
|
| 418 | 418 | case 'display': |
| 419 | 419 | case AccountManager::PROPERTY_DISPLAYNAME: |
| 420 | 420 | $targetUser->setDisplayName($value); |
| 421 | 421 | break; |
| 422 | 422 | case 'quota': |
| 423 | 423 | $quota = $value; |
| 424 | - if($quota !== 'none' && $quota !== 'default') { |
|
| 424 | + if ($quota !== 'none' && $quota !== 'default') { |
|
| 425 | 425 | if (is_numeric($quota)) { |
| 426 | 426 | $quota = (float) $quota; |
| 427 | 427 | } else { |
@@ -430,9 +430,9 @@ discard block |
||
| 430 | 430 | if ($quota === false) { |
| 431 | 431 | throw new OCSException('Invalid quota value '.$value, 103); |
| 432 | 432 | } |
| 433 | - if($quota === 0) { |
|
| 433 | + if ($quota === 0) { |
|
| 434 | 434 | $quota = 'default'; |
| 435 | - }else if($quota === -1) { |
|
| 435 | + } else if ($quota === -1) { |
|
| 436 | 436 | $quota = 'none'; |
| 437 | 437 | } else { |
| 438 | 438 | $quota = \OCP\Util::humanFileSize($quota); |
@@ -451,7 +451,7 @@ discard block |
||
| 451 | 451 | $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
| 452 | 452 | break; |
| 453 | 453 | case AccountManager::PROPERTY_EMAIL: |
| 454 | - if(filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 454 | + if (filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 455 | 455 | $targetUser->setEMailAddress($value); |
| 456 | 456 | } else { |
| 457 | 457 | throw new OCSException('', 102); |
@@ -486,18 +486,18 @@ discard block |
||
| 486 | 486 | |
| 487 | 487 | $targetUser = $this->userManager->get($userId); |
| 488 | 488 | |
| 489 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 489 | + if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 490 | 490 | throw new OCSException('', 101); |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | // If not permitted |
| 494 | 494 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 495 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 495 | + if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 496 | 496 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | // Go ahead with the delete |
| 500 | - if($targetUser->delete()) { |
|
| 500 | + if ($targetUser->delete()) { |
|
| 501 | 501 | return new DataResponse(); |
| 502 | 502 | } else { |
| 503 | 503 | throw new OCSException('', 101); |
@@ -540,13 +540,13 @@ discard block |
||
| 540 | 540 | $currentLoggedInUser = $this->userSession->getUser(); |
| 541 | 541 | |
| 542 | 542 | $targetUser = $this->userManager->get($userId); |
| 543 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 543 | + if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 544 | 544 | throw new OCSException('', 101); |
| 545 | 545 | } |
| 546 | 546 | |
| 547 | 547 | // If not permitted |
| 548 | 548 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 549 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 549 | + if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 550 | 550 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 551 | 551 | } |
| 552 | 552 | |
@@ -567,11 +567,11 @@ discard block |
||
| 567 | 567 | $loggedInUser = $this->userSession->getUser(); |
| 568 | 568 | |
| 569 | 569 | $targetUser = $this->userManager->get($userId); |
| 570 | - if($targetUser === null) { |
|
| 570 | + if ($targetUser === null) { |
|
| 571 | 571 | throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
| 572 | 572 | } |
| 573 | 573 | |
| 574 | - if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 574 | + if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 575 | 575 | // Self lookup or admin lookup |
| 576 | 576 | return new DataResponse([ |
| 577 | 577 | 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
@@ -580,7 +580,7 @@ discard block |
||
| 580 | 580 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 581 | 581 | |
| 582 | 582 | // Looking up someone else |
| 583 | - if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 583 | + if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 584 | 584 | // Return the group that the method caller is subadmin of for the user in question |
| 585 | 585 | /** @var IGroup[] $getSubAdminsGroups */ |
| 586 | 586 | $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
@@ -610,16 +610,16 @@ discard block |
||
| 610 | 610 | * @throws OCSException |
| 611 | 611 | */ |
| 612 | 612 | public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
| 613 | - if($groupid === '') { |
|
| 613 | + if ($groupid === '') { |
|
| 614 | 614 | throw new OCSException('', 101); |
| 615 | 615 | } |
| 616 | 616 | |
| 617 | 617 | $group = $this->groupManager->get($groupid); |
| 618 | 618 | $targetUser = $this->userManager->get($userId); |
| 619 | - if($group === null) { |
|
| 619 | + if ($group === null) { |
|
| 620 | 620 | throw new OCSException('', 102); |
| 621 | 621 | } |
| 622 | - if($targetUser === null) { |
|
| 622 | + if ($targetUser === null) { |
|
| 623 | 623 | throw new OCSException('', 103); |
| 624 | 624 | } |
| 625 | 625 | |
@@ -647,17 +647,17 @@ discard block |
||
| 647 | 647 | public function removeFromGroup(string $userId, string $groupid): DataResponse { |
| 648 | 648 | $loggedInUser = $this->userSession->getUser(); |
| 649 | 649 | |
| 650 | - if($groupid === null || trim($groupid) === '') { |
|
| 650 | + if ($groupid === null || trim($groupid) === '') { |
|
| 651 | 651 | throw new OCSException('', 101); |
| 652 | 652 | } |
| 653 | 653 | |
| 654 | 654 | $group = $this->groupManager->get($groupid); |
| 655 | - if($group === null) { |
|
| 655 | + if ($group === null) { |
|
| 656 | 656 | throw new OCSException('', 102); |
| 657 | 657 | } |
| 658 | 658 | |
| 659 | 659 | $targetUser = $this->userManager->get($userId); |
| 660 | - if($targetUser === null) { |
|
| 660 | + if ($targetUser === null) { |
|
| 661 | 661 | throw new OCSException('', 103); |
| 662 | 662 | } |
| 663 | 663 | |
@@ -681,7 +681,7 @@ discard block |
||
| 681 | 681 | } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
| 682 | 682 | /** @var IGroup[] $subAdminGroups */ |
| 683 | 683 | $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
| 684 | - $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
| 684 | + $subAdminGroups = array_map(function(IGroup $subAdminGroup) { |
|
| 685 | 685 | return $subAdminGroup->getGID(); |
| 686 | 686 | }, $subAdminGroups); |
| 687 | 687 | $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
@@ -713,15 +713,15 @@ discard block |
||
| 713 | 713 | $user = $this->userManager->get($userId); |
| 714 | 714 | |
| 715 | 715 | // Check if the user exists |
| 716 | - if($user === null) { |
|
| 716 | + if ($user === null) { |
|
| 717 | 717 | throw new OCSException('User does not exist', 101); |
| 718 | 718 | } |
| 719 | 719 | // Check if group exists |
| 720 | - if($group === null) { |
|
| 721 | - throw new OCSException('Group does not exist', 102); |
|
| 720 | + if ($group === null) { |
|
| 721 | + throw new OCSException('Group does not exist', 102); |
|
| 722 | 722 | } |
| 723 | 723 | // Check if trying to make subadmin of admin group |
| 724 | - if($group->getGID() === 'admin') { |
|
| 724 | + if ($group->getGID() === 'admin') { |
|
| 725 | 725 | throw new OCSException('Cannot create subadmins for admin group', 103); |
| 726 | 726 | } |
| 727 | 727 | |
@@ -732,7 +732,7 @@ discard block |
||
| 732 | 732 | return new DataResponse(); |
| 733 | 733 | } |
| 734 | 734 | // Go |
| 735 | - if($subAdminManager->createSubAdmin($user, $group)) { |
|
| 735 | + if ($subAdminManager->createSubAdmin($user, $group)) { |
|
| 736 | 736 | return new DataResponse(); |
| 737 | 737 | } else { |
| 738 | 738 | throw new OCSException('Unknown error occurred', 103); |
@@ -755,20 +755,20 @@ discard block |
||
| 755 | 755 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 756 | 756 | |
| 757 | 757 | // Check if the user exists |
| 758 | - if($user === null) { |
|
| 758 | + if ($user === null) { |
|
| 759 | 759 | throw new OCSException('User does not exist', 101); |
| 760 | 760 | } |
| 761 | 761 | // Check if the group exists |
| 762 | - if($group === null) { |
|
| 762 | + if ($group === null) { |
|
| 763 | 763 | throw new OCSException('Group does not exist', 101); |
| 764 | 764 | } |
| 765 | 765 | // Check if they are a subadmin of this said group |
| 766 | - if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 766 | + if (!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 767 | 767 | throw new OCSException('User is not a subadmin of this group', 102); |
| 768 | 768 | } |
| 769 | 769 | |
| 770 | 770 | // Go |
| 771 | - if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 771 | + if ($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 772 | 772 | return new DataResponse(); |
| 773 | 773 | } else { |
| 774 | 774 | throw new OCSException('Unknown error occurred', 103); |
@@ -785,7 +785,7 @@ discard block |
||
| 785 | 785 | public function getUserSubAdminGroups(string $userId): DataResponse { |
| 786 | 786 | $user = $this->userManager->get($userId); |
| 787 | 787 | // Check if the user exists |
| 788 | - if($user === null) { |
|
| 788 | + if ($user === null) { |
|
| 789 | 789 | throw new OCSException('User does not exist', 101); |
| 790 | 790 | } |
| 791 | 791 | |
@@ -795,7 +795,7 @@ discard block |
||
| 795 | 795 | $groups[$key] = $group->getGID(); |
| 796 | 796 | } |
| 797 | 797 | |
| 798 | - if(!$groups) { |
|
| 798 | + if (!$groups) { |
|
| 799 | 799 | throw new OCSException('Unknown error occurred', 102); |
| 800 | 800 | } else { |
| 801 | 801 | return new DataResponse($groups); |
@@ -839,13 +839,13 @@ discard block |
||
| 839 | 839 | $currentLoggedInUser = $this->userSession->getUser(); |
| 840 | 840 | |
| 841 | 841 | $targetUser = $this->userManager->get($userId); |
| 842 | - if($targetUser === null) { |
|
| 842 | + if ($targetUser === null) { |
|
| 843 | 843 | throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
| 844 | 844 | } |
| 845 | 845 | |
| 846 | 846 | // Check if admin / subadmin |
| 847 | 847 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 848 | - if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 848 | + if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 849 | 849 | && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
| 850 | 850 | // No rights |
| 851 | 851 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
@@ -867,7 +867,7 @@ discard block |
||
| 867 | 867 | $this->newUserMailHelper->setL10N($l10n); |
| 868 | 868 | $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
| 869 | 869 | $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
| 870 | - } catch(\Exception $e) { |
|
| 870 | + } catch (\Exception $e) { |
|
| 871 | 871 | $this->logger->logException($e, [ |
| 872 | 872 | 'message' => "Can't send new user mail to $email", |
| 873 | 873 | '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 | } |