@@ -47,241 +47,241 @@ |
||
| 47 | 47 | interface IIndex { |
| 48 | 48 | |
| 49 | 49 | |
| 50 | - const INDEX_OK = 1; |
|
| 51 | - const INDEX_IGNORE = 2; |
|
| 52 | - |
|
| 53 | - const INDEX_META = 4; |
|
| 54 | - const INDEX_CONTENT = 8; |
|
| 55 | - const INDEX_PARTS = 16; |
|
| 56 | - const INDEX_FULL = 28; |
|
| 57 | - const INDEX_REMOVE = 32; |
|
| 58 | - const INDEX_DONE = 64; |
|
| 59 | - const INDEX_FAILED = 128; |
|
| 60 | - |
|
| 61 | - const ERROR_FAILED = 1; |
|
| 62 | - const ERROR_FAILED2 = 2; |
|
| 63 | - const ERROR_FAILED3 = 4; |
|
| 64 | - |
|
| 65 | - const ERROR_SEV_1 = 1; |
|
| 66 | - const ERROR_SEV_2 = 2; |
|
| 67 | - const ERROR_SEV_3 = 3; |
|
| 68 | - const ERROR_SEV_4 = 4; |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Get the Id of the Content Provider. |
|
| 73 | - * |
|
| 74 | - * @since 15.0.0 |
|
| 75 | - * |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - public function getProviderId(): string; |
|
| 79 | - |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get the Id of the original document. |
|
| 83 | - * |
|
| 84 | - * @since 15.0.0 |
|
| 85 | - * |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public function getDocumentId(): string; |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Set the source of the original document. |
|
| 93 | - * |
|
| 94 | - * @since 15.0.0 |
|
| 95 | - * |
|
| 96 | - * @param string $source |
|
| 97 | - * |
|
| 98 | - * @return IIndex |
|
| 99 | - */ |
|
| 100 | - public function setSource(string $source): IIndex; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get the source of the original document. |
|
| 104 | - * |
|
| 105 | - * @since 15.0.0 |
|
| 106 | - * |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function getSource(): string; |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Set the owner of the original document. |
|
| 114 | - * |
|
| 115 | - * @since 15.0.0 |
|
| 116 | - * |
|
| 117 | - * @param string $ownerId |
|
| 118 | - * |
|
| 119 | - * @return IIndex |
|
| 120 | - */ |
|
| 121 | - public function setOwnerId(string $ownerId): IIndex; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Get the owner of the original document. |
|
| 125 | - * |
|
| 126 | - * @since 15.0.0 |
|
| 127 | - * |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public function getOwnerId(): string; |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Set the current index status (bit flag) of the original document. |
|
| 135 | - * If $reset is true, the status is reset to the defined value. |
|
| 136 | - * |
|
| 137 | - * @since 15.0.0 |
|
| 138 | - * |
|
| 139 | - * @param int $status |
|
| 140 | - * @param bool $reset |
|
| 141 | - * |
|
| 142 | - * @return IIndex |
|
| 143 | - */ |
|
| 144 | - public function setStatus(int $status, bool $reset = false): IIndex; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the current index status of the original document. |
|
| 148 | - * |
|
| 149 | - * @since 15.0.0 |
|
| 150 | - * |
|
| 151 | - * @return int |
|
| 152 | - */ |
|
| 153 | - public function getStatus(): int; |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Check if the document fit a specific status. |
|
| 157 | - * |
|
| 158 | - * @since 15.0.0 |
|
| 159 | - * |
|
| 160 | - * @param int $status |
|
| 161 | - * |
|
| 162 | - * @return bool |
|
| 163 | - */ |
|
| 164 | - public function isStatus(int $status): bool; |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Remove a status. |
|
| 168 | - * |
|
| 169 | - * @since 15.0.0 |
|
| 170 | - * |
|
| 171 | - * @param int $status |
|
| 172 | - * |
|
| 173 | - * @return IIndex |
|
| 174 | - */ |
|
| 175 | - public function unsetStatus(int $status): IIndex; |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Add an option related to the original document (as string). |
|
| 180 | - * |
|
| 181 | - * @since 15.0.0 |
|
| 182 | - * |
|
| 183 | - * @param string $option |
|
| 184 | - * @param string $value |
|
| 185 | - * |
|
| 186 | - * @return IIndex |
|
| 187 | - */ |
|
| 188 | - public function addOption(string $option, string $value): IIndex; |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Add an option related to the original document (as integer). |
|
| 192 | - * |
|
| 193 | - * @since 15.0.0 |
|
| 194 | - * |
|
| 195 | - * @param string $option |
|
| 196 | - * @param int $value |
|
| 197 | - * |
|
| 198 | - * @return IIndex |
|
| 199 | - */ |
|
| 200 | - public function addOptionInt(string $option, int $value): IIndex; |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Get the option related to the original document (as string). |
|
| 204 | - * |
|
| 205 | - * @since 15.0.0 |
|
| 206 | - * |
|
| 207 | - * @param string $option |
|
| 208 | - * @param string $default |
|
| 209 | - * |
|
| 210 | - * @return string |
|
| 211 | - */ |
|
| 212 | - public function getOption(string $option, string $default = ''): string; |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Get the option related to the original document (as integer). |
|
| 216 | - * |
|
| 217 | - * @since 15.0.0 |
|
| 218 | - * |
|
| 219 | - * @param string $option |
|
| 220 | - * @param int $default |
|
| 221 | - * |
|
| 222 | - * @return int |
|
| 223 | - */ |
|
| 224 | - public function getOptionInt(string $option, int $default = 0): int; |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Get all options related to the original document. |
|
| 228 | - * |
|
| 229 | - * @since 15.0.0 |
|
| 230 | - * |
|
| 231 | - * @return array |
|
| 232 | - */ |
|
| 233 | - public function getOptions(): array; |
|
| 234 | - |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Add an error log related to the Index. |
|
| 238 | - * |
|
| 239 | - * @since 15.0.0 |
|
| 240 | - * |
|
| 241 | - * @param string $message |
|
| 242 | - * @param string $exception |
|
| 243 | - * @param int $sev |
|
| 244 | - * |
|
| 245 | - * @return IIndex |
|
| 246 | - */ |
|
| 247 | - public function addError(string $message, string $exception = '', int $sev = self::ERROR_SEV_3): IIndex; |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Returns the number of known errors related to the Index. |
|
| 251 | - * |
|
| 252 | - * @since 15.0.0 |
|
| 253 | - * |
|
| 254 | - * @return int |
|
| 255 | - */ |
|
| 256 | - public function getErrorCount(): int; |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Reset all error logs related to the Index. |
|
| 260 | - * |
|
| 261 | - * @since 15.0.0 |
|
| 262 | - */ |
|
| 263 | - public function resetErrors(): IIndex; |
|
| 264 | - |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Set the date of the last index. |
|
| 268 | - * |
|
| 269 | - * @since 15.0.0 |
|
| 270 | - * |
|
| 271 | - * @param int $lastIndex |
|
| 272 | - * |
|
| 273 | - * @return IIndex |
|
| 274 | - */ |
|
| 275 | - public function setLastIndex(int $lastIndex = -1): IIndex; |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Get the date of the last index. |
|
| 279 | - * |
|
| 280 | - * @since 15.0.0 |
|
| 281 | - * |
|
| 282 | - * @return int |
|
| 283 | - */ |
|
| 284 | - public function getLastIndex(): int; |
|
| 50 | + const INDEX_OK = 1; |
|
| 51 | + const INDEX_IGNORE = 2; |
|
| 52 | + |
|
| 53 | + const INDEX_META = 4; |
|
| 54 | + const INDEX_CONTENT = 8; |
|
| 55 | + const INDEX_PARTS = 16; |
|
| 56 | + const INDEX_FULL = 28; |
|
| 57 | + const INDEX_REMOVE = 32; |
|
| 58 | + const INDEX_DONE = 64; |
|
| 59 | + const INDEX_FAILED = 128; |
|
| 60 | + |
|
| 61 | + const ERROR_FAILED = 1; |
|
| 62 | + const ERROR_FAILED2 = 2; |
|
| 63 | + const ERROR_FAILED3 = 4; |
|
| 64 | + |
|
| 65 | + const ERROR_SEV_1 = 1; |
|
| 66 | + const ERROR_SEV_2 = 2; |
|
| 67 | + const ERROR_SEV_3 = 3; |
|
| 68 | + const ERROR_SEV_4 = 4; |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Get the Id of the Content Provider. |
|
| 73 | + * |
|
| 74 | + * @since 15.0.0 |
|
| 75 | + * |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + public function getProviderId(): string; |
|
| 79 | + |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get the Id of the original document. |
|
| 83 | + * |
|
| 84 | + * @since 15.0.0 |
|
| 85 | + * |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public function getDocumentId(): string; |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Set the source of the original document. |
|
| 93 | + * |
|
| 94 | + * @since 15.0.0 |
|
| 95 | + * |
|
| 96 | + * @param string $source |
|
| 97 | + * |
|
| 98 | + * @return IIndex |
|
| 99 | + */ |
|
| 100 | + public function setSource(string $source): IIndex; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get the source of the original document. |
|
| 104 | + * |
|
| 105 | + * @since 15.0.0 |
|
| 106 | + * |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function getSource(): string; |
|
| 110 | + |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Set the owner of the original document. |
|
| 114 | + * |
|
| 115 | + * @since 15.0.0 |
|
| 116 | + * |
|
| 117 | + * @param string $ownerId |
|
| 118 | + * |
|
| 119 | + * @return IIndex |
|
| 120 | + */ |
|
| 121 | + public function setOwnerId(string $ownerId): IIndex; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Get the owner of the original document. |
|
| 125 | + * |
|
| 126 | + * @since 15.0.0 |
|
| 127 | + * |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public function getOwnerId(): string; |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Set the current index status (bit flag) of the original document. |
|
| 135 | + * If $reset is true, the status is reset to the defined value. |
|
| 136 | + * |
|
| 137 | + * @since 15.0.0 |
|
| 138 | + * |
|
| 139 | + * @param int $status |
|
| 140 | + * @param bool $reset |
|
| 141 | + * |
|
| 142 | + * @return IIndex |
|
| 143 | + */ |
|
| 144 | + public function setStatus(int $status, bool $reset = false): IIndex; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the current index status of the original document. |
|
| 148 | + * |
|
| 149 | + * @since 15.0.0 |
|
| 150 | + * |
|
| 151 | + * @return int |
|
| 152 | + */ |
|
| 153 | + public function getStatus(): int; |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Check if the document fit a specific status. |
|
| 157 | + * |
|
| 158 | + * @since 15.0.0 |
|
| 159 | + * |
|
| 160 | + * @param int $status |
|
| 161 | + * |
|
| 162 | + * @return bool |
|
| 163 | + */ |
|
| 164 | + public function isStatus(int $status): bool; |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Remove a status. |
|
| 168 | + * |
|
| 169 | + * @since 15.0.0 |
|
| 170 | + * |
|
| 171 | + * @param int $status |
|
| 172 | + * |
|
| 173 | + * @return IIndex |
|
| 174 | + */ |
|
| 175 | + public function unsetStatus(int $status): IIndex; |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Add an option related to the original document (as string). |
|
| 180 | + * |
|
| 181 | + * @since 15.0.0 |
|
| 182 | + * |
|
| 183 | + * @param string $option |
|
| 184 | + * @param string $value |
|
| 185 | + * |
|
| 186 | + * @return IIndex |
|
| 187 | + */ |
|
| 188 | + public function addOption(string $option, string $value): IIndex; |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Add an option related to the original document (as integer). |
|
| 192 | + * |
|
| 193 | + * @since 15.0.0 |
|
| 194 | + * |
|
| 195 | + * @param string $option |
|
| 196 | + * @param int $value |
|
| 197 | + * |
|
| 198 | + * @return IIndex |
|
| 199 | + */ |
|
| 200 | + public function addOptionInt(string $option, int $value): IIndex; |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Get the option related to the original document (as string). |
|
| 204 | + * |
|
| 205 | + * @since 15.0.0 |
|
| 206 | + * |
|
| 207 | + * @param string $option |
|
| 208 | + * @param string $default |
|
| 209 | + * |
|
| 210 | + * @return string |
|
| 211 | + */ |
|
| 212 | + public function getOption(string $option, string $default = ''): string; |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Get the option related to the original document (as integer). |
|
| 216 | + * |
|
| 217 | + * @since 15.0.0 |
|
| 218 | + * |
|
| 219 | + * @param string $option |
|
| 220 | + * @param int $default |
|
| 221 | + * |
|
| 222 | + * @return int |
|
| 223 | + */ |
|
| 224 | + public function getOptionInt(string $option, int $default = 0): int; |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Get all options related to the original document. |
|
| 228 | + * |
|
| 229 | + * @since 15.0.0 |
|
| 230 | + * |
|
| 231 | + * @return array |
|
| 232 | + */ |
|
| 233 | + public function getOptions(): array; |
|
| 234 | + |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Add an error log related to the Index. |
|
| 238 | + * |
|
| 239 | + * @since 15.0.0 |
|
| 240 | + * |
|
| 241 | + * @param string $message |
|
| 242 | + * @param string $exception |
|
| 243 | + * @param int $sev |
|
| 244 | + * |
|
| 245 | + * @return IIndex |
|
| 246 | + */ |
|
| 247 | + public function addError(string $message, string $exception = '', int $sev = self::ERROR_SEV_3): IIndex; |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Returns the number of known errors related to the Index. |
|
| 251 | + * |
|
| 252 | + * @since 15.0.0 |
|
| 253 | + * |
|
| 254 | + * @return int |
|
| 255 | + */ |
|
| 256 | + public function getErrorCount(): int; |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Reset all error logs related to the Index. |
|
| 260 | + * |
|
| 261 | + * @since 15.0.0 |
|
| 262 | + */ |
|
| 263 | + public function resetErrors(): IIndex; |
|
| 264 | + |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Set the date of the last index. |
|
| 268 | + * |
|
| 269 | + * @since 15.0.0 |
|
| 270 | + * |
|
| 271 | + * @param int $lastIndex |
|
| 272 | + * |
|
| 273 | + * @return IIndex |
|
| 274 | + */ |
|
| 275 | + public function setLastIndex(int $lastIndex = -1): IIndex; |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Get the date of the last index. |
|
| 279 | + * |
|
| 280 | + * @since 15.0.0 |
|
| 281 | + * |
|
| 282 | + * @return int |
|
| 283 | + */ |
|
| 284 | + public function getLastIndex(): int; |
|
| 285 | 285 | |
| 286 | 286 | |
| 287 | 287 | } |
@@ -30,122 +30,122 @@ |
||
| 30 | 30 | use OCP\AppFramework\Http\Response; |
| 31 | 31 | |
| 32 | 32 | abstract class BaseResponse extends Response { |
| 33 | - /** @var array */ |
|
| 34 | - protected $data; |
|
| 35 | - |
|
| 36 | - /** @var string */ |
|
| 37 | - protected $format; |
|
| 38 | - |
|
| 39 | - /** @var string */ |
|
| 40 | - protected $statusMessage; |
|
| 41 | - |
|
| 42 | - /** @var int */ |
|
| 43 | - protected $itemsCount; |
|
| 44 | - |
|
| 45 | - /** @var int */ |
|
| 46 | - protected $itemsPerPage; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * BaseResponse constructor. |
|
| 50 | - * |
|
| 51 | - * @param DataResponse $dataResponse |
|
| 52 | - * @param string $format |
|
| 53 | - * @param string|null $statusMessage |
|
| 54 | - * @param int|null $itemsCount |
|
| 55 | - * @param int|null $itemsPerPage |
|
| 56 | - */ |
|
| 57 | - public function __construct(DataResponse $dataResponse, |
|
| 58 | - $format = 'xml', |
|
| 59 | - $statusMessage = null, |
|
| 60 | - $itemsCount = null, |
|
| 61 | - $itemsPerPage = null) { |
|
| 62 | - parent::__construct(); |
|
| 63 | - |
|
| 64 | - $this->format = $format; |
|
| 65 | - $this->statusMessage = $statusMessage; |
|
| 66 | - $this->itemsCount = $itemsCount; |
|
| 67 | - $this->itemsPerPage = $itemsPerPage; |
|
| 68 | - |
|
| 69 | - $this->data = $dataResponse->getData(); |
|
| 70 | - |
|
| 71 | - $this->setHeaders($dataResponse->getHeaders()); |
|
| 72 | - $this->setStatus($dataResponse->getStatus()); |
|
| 73 | - $this->setETag($dataResponse->getETag()); |
|
| 74 | - $this->setLastModified($dataResponse->getLastModified()); |
|
| 75 | - $this->setCookies($dataResponse->getCookies()); |
|
| 76 | - |
|
| 77 | - if ($format === 'json') { |
|
| 78 | - $this->addHeader( |
|
| 79 | - 'Content-Type', 'application/json; charset=utf-8' |
|
| 80 | - ); |
|
| 81 | - } else { |
|
| 82 | - $this->addHeader( |
|
| 83 | - 'Content-Type', 'application/xml; charset=utf-8' |
|
| 84 | - ); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param string[] $meta |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - protected function renderResult(array $meta): string { |
|
| 93 | - $status = $this->getStatus(); |
|
| 94 | - if ($status === Http::STATUS_NO_CONTENT || |
|
| 95 | - $status === Http::STATUS_NOT_MODIFIED || |
|
| 96 | - ($status >= 100 && $status <= 199)) { |
|
| 97 | - // Those status codes are not supposed to have a body: |
|
| 98 | - // https://stackoverflow.com/q/8628725 |
|
| 99 | - return ''; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $response = [ |
|
| 103 | - 'ocs' => [ |
|
| 104 | - 'meta' => $meta, |
|
| 105 | - 'data' => $this->data, |
|
| 106 | - ], |
|
| 107 | - ]; |
|
| 108 | - |
|
| 109 | - if ($this->format === 'json') { |
|
| 110 | - return json_encode($response, JSON_HEX_TAG); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $writer = new \XMLWriter(); |
|
| 114 | - $writer->openMemory(); |
|
| 115 | - $writer->setIndent(true); |
|
| 116 | - $writer->startDocument(); |
|
| 117 | - $this->toXML($response, $writer); |
|
| 118 | - $writer->endDocument(); |
|
| 119 | - return $writer->outputMemory(true); |
|
| 120 | - |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param array $array |
|
| 125 | - * @param \XMLWriter $writer |
|
| 126 | - */ |
|
| 127 | - protected function toXML(array $array, \XMLWriter $writer) { |
|
| 128 | - foreach ($array as $k => $v) { |
|
| 129 | - if (\is_string($k) && strpos($k, '@') === 0) { |
|
| 130 | - $writer->writeAttribute(substr($k, 1), $v); |
|
| 131 | - continue; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if (\is_numeric($k)) { |
|
| 135 | - $k = 'element'; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if (\is_array($v)) { |
|
| 139 | - $writer->startElement($k); |
|
| 140 | - $this->toXML($v, $writer); |
|
| 141 | - $writer->endElement(); |
|
| 142 | - } else { |
|
| 143 | - $writer->writeElement($k, $v); |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - public function getOCSStatus() { |
|
| 149 | - return parent::getStatus(); |
|
| 150 | - } |
|
| 33 | + /** @var array */ |
|
| 34 | + protected $data; |
|
| 35 | + |
|
| 36 | + /** @var string */ |
|
| 37 | + protected $format; |
|
| 38 | + |
|
| 39 | + /** @var string */ |
|
| 40 | + protected $statusMessage; |
|
| 41 | + |
|
| 42 | + /** @var int */ |
|
| 43 | + protected $itemsCount; |
|
| 44 | + |
|
| 45 | + /** @var int */ |
|
| 46 | + protected $itemsPerPage; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * BaseResponse constructor. |
|
| 50 | + * |
|
| 51 | + * @param DataResponse $dataResponse |
|
| 52 | + * @param string $format |
|
| 53 | + * @param string|null $statusMessage |
|
| 54 | + * @param int|null $itemsCount |
|
| 55 | + * @param int|null $itemsPerPage |
|
| 56 | + */ |
|
| 57 | + public function __construct(DataResponse $dataResponse, |
|
| 58 | + $format = 'xml', |
|
| 59 | + $statusMessage = null, |
|
| 60 | + $itemsCount = null, |
|
| 61 | + $itemsPerPage = null) { |
|
| 62 | + parent::__construct(); |
|
| 63 | + |
|
| 64 | + $this->format = $format; |
|
| 65 | + $this->statusMessage = $statusMessage; |
|
| 66 | + $this->itemsCount = $itemsCount; |
|
| 67 | + $this->itemsPerPage = $itemsPerPage; |
|
| 68 | + |
|
| 69 | + $this->data = $dataResponse->getData(); |
|
| 70 | + |
|
| 71 | + $this->setHeaders($dataResponse->getHeaders()); |
|
| 72 | + $this->setStatus($dataResponse->getStatus()); |
|
| 73 | + $this->setETag($dataResponse->getETag()); |
|
| 74 | + $this->setLastModified($dataResponse->getLastModified()); |
|
| 75 | + $this->setCookies($dataResponse->getCookies()); |
|
| 76 | + |
|
| 77 | + if ($format === 'json') { |
|
| 78 | + $this->addHeader( |
|
| 79 | + 'Content-Type', 'application/json; charset=utf-8' |
|
| 80 | + ); |
|
| 81 | + } else { |
|
| 82 | + $this->addHeader( |
|
| 83 | + 'Content-Type', 'application/xml; charset=utf-8' |
|
| 84 | + ); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param string[] $meta |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + protected function renderResult(array $meta): string { |
|
| 93 | + $status = $this->getStatus(); |
|
| 94 | + if ($status === Http::STATUS_NO_CONTENT || |
|
| 95 | + $status === Http::STATUS_NOT_MODIFIED || |
|
| 96 | + ($status >= 100 && $status <= 199)) { |
|
| 97 | + // Those status codes are not supposed to have a body: |
|
| 98 | + // https://stackoverflow.com/q/8628725 |
|
| 99 | + return ''; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $response = [ |
|
| 103 | + 'ocs' => [ |
|
| 104 | + 'meta' => $meta, |
|
| 105 | + 'data' => $this->data, |
|
| 106 | + ], |
|
| 107 | + ]; |
|
| 108 | + |
|
| 109 | + if ($this->format === 'json') { |
|
| 110 | + return json_encode($response, JSON_HEX_TAG); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $writer = new \XMLWriter(); |
|
| 114 | + $writer->openMemory(); |
|
| 115 | + $writer->setIndent(true); |
|
| 116 | + $writer->startDocument(); |
|
| 117 | + $this->toXML($response, $writer); |
|
| 118 | + $writer->endDocument(); |
|
| 119 | + return $writer->outputMemory(true); |
|
| 120 | + |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param array $array |
|
| 125 | + * @param \XMLWriter $writer |
|
| 126 | + */ |
|
| 127 | + protected function toXML(array $array, \XMLWriter $writer) { |
|
| 128 | + foreach ($array as $k => $v) { |
|
| 129 | + if (\is_string($k) && strpos($k, '@') === 0) { |
|
| 130 | + $writer->writeAttribute(substr($k, 1), $v); |
|
| 131 | + continue; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if (\is_numeric($k)) { |
|
| 135 | + $k = 'element'; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if (\is_array($v)) { |
|
| 139 | + $writer->startElement($k); |
|
| 140 | + $this->toXML($v, $writer); |
|
| 141 | + $writer->endElement(); |
|
| 142 | + } else { |
|
| 143 | + $writer->writeElement($k, $v); |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + public function getOCSStatus() { |
|
| 149 | + return parent::getStatus(); |
|
| 150 | + } |
|
| 151 | 151 | } |
@@ -49,342 +49,342 @@ |
||
| 49 | 49 | |
| 50 | 50 | class Manager { |
| 51 | 51 | |
| 52 | - const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
| 53 | - const SESSION_UID_DONE = 'two_factor_auth_passed'; |
|
| 54 | - const REMEMBER_LOGIN = 'two_factor_remember_login'; |
|
| 55 | - const BACKUP_CODES_PROVIDER_ID = 'backup_codes'; |
|
| 56 | - |
|
| 57 | - /** @var ProviderLoader */ |
|
| 58 | - private $providerLoader; |
|
| 59 | - |
|
| 60 | - /** @var IRegistry */ |
|
| 61 | - private $providerRegistry; |
|
| 62 | - |
|
| 63 | - /** @var MandatoryTwoFactor */ |
|
| 64 | - private $mandatoryTwoFactor; |
|
| 65 | - |
|
| 66 | - /** @var ISession */ |
|
| 67 | - private $session; |
|
| 68 | - |
|
| 69 | - /** @var IConfig */ |
|
| 70 | - private $config; |
|
| 71 | - |
|
| 72 | - /** @var IManager */ |
|
| 73 | - private $activityManager; |
|
| 74 | - |
|
| 75 | - /** @var ILogger */ |
|
| 76 | - private $logger; |
|
| 77 | - |
|
| 78 | - /** @var TokenProvider */ |
|
| 79 | - private $tokenProvider; |
|
| 80 | - |
|
| 81 | - /** @var ITimeFactory */ |
|
| 82 | - private $timeFactory; |
|
| 83 | - |
|
| 84 | - /** @var EventDispatcherInterface */ |
|
| 85 | - private $dispatcher; |
|
| 86 | - |
|
| 87 | - public function __construct(ProviderLoader $providerLoader, |
|
| 88 | - IRegistry $providerRegistry, |
|
| 89 | - MandatoryTwoFactor $mandatoryTwoFactor, |
|
| 90 | - ISession $session, IConfig $config, |
|
| 91 | - IManager $activityManager, ILogger $logger, TokenProvider $tokenProvider, |
|
| 92 | - ITimeFactory $timeFactory, EventDispatcherInterface $eventDispatcher) { |
|
| 93 | - $this->providerLoader = $providerLoader; |
|
| 94 | - $this->providerRegistry = $providerRegistry; |
|
| 95 | - $this->mandatoryTwoFactor = $mandatoryTwoFactor; |
|
| 96 | - $this->session = $session; |
|
| 97 | - $this->config = $config; |
|
| 98 | - $this->activityManager = $activityManager; |
|
| 99 | - $this->logger = $logger; |
|
| 100 | - $this->tokenProvider = $tokenProvider; |
|
| 101 | - $this->timeFactory = $timeFactory; |
|
| 102 | - $this->dispatcher = $eventDispatcher; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Determine whether the user must provide a second factor challenge |
|
| 107 | - * |
|
| 108 | - * @param IUser $user |
|
| 109 | - * @return boolean |
|
| 110 | - */ |
|
| 111 | - public function isTwoFactorAuthenticated(IUser $user): bool { |
|
| 112 | - if ($this->mandatoryTwoFactor->isEnforcedFor($user)) { |
|
| 113 | - return true; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $providerStates = $this->providerRegistry->getProviderStates($user); |
|
| 117 | - $providers = $this->providerLoader->getProviders($user); |
|
| 118 | - $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
| 119 | - $enabled = array_filter($fixedStates); |
|
| 120 | - $providerIds = array_keys($enabled); |
|
| 121 | - $providerIdsWithoutBackupCodes = array_diff($providerIds, [self::BACKUP_CODES_PROVIDER_ID]); |
|
| 122 | - |
|
| 123 | - return !empty($providerIdsWithoutBackupCodes); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Get a 2FA provider by its ID |
|
| 128 | - * |
|
| 129 | - * @param IUser $user |
|
| 130 | - * @param string $challengeProviderId |
|
| 131 | - * @return IProvider|null |
|
| 132 | - */ |
|
| 133 | - public function getProvider(IUser $user, string $challengeProviderId) { |
|
| 134 | - $providers = $this->getProviderSet($user)->getProviders(); |
|
| 135 | - return $providers[$challengeProviderId] ?? null; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @param IUser $user |
|
| 140 | - * @return IActivatableAtLogin[] |
|
| 141 | - * @throws Exception |
|
| 142 | - */ |
|
| 143 | - public function getLoginSetupProviders(IUser $user): array { |
|
| 144 | - $providers = $this->providerLoader->getProviders($user); |
|
| 145 | - return array_filter($providers, function(IProvider $provider) { |
|
| 146 | - return ($provider instanceof IActivatableAtLogin); |
|
| 147 | - }); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Check if the persistant mapping of enabled/disabled state of each available |
|
| 152 | - * provider is missing an entry and add it to the registry in that case. |
|
| 153 | - * |
|
| 154 | - * @todo remove in Nextcloud 17 as by then all providers should have been updated |
|
| 155 | - * |
|
| 156 | - * @param string[] $providerStates |
|
| 157 | - * @param IProvider[] $providers |
|
| 158 | - * @param IUser $user |
|
| 159 | - * @return string[] the updated $providerStates variable |
|
| 160 | - */ |
|
| 161 | - private function fixMissingProviderStates(array $providerStates, |
|
| 162 | - array $providers, IUser $user): array { |
|
| 163 | - |
|
| 164 | - foreach ($providers as $provider) { |
|
| 165 | - if (isset($providerStates[$provider->getId()])) { |
|
| 166 | - // All good |
|
| 167 | - continue; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $enabled = $provider->isTwoFactorAuthEnabledForUser($user); |
|
| 171 | - if ($enabled) { |
|
| 172 | - $this->providerRegistry->enableProviderFor($provider, $user); |
|
| 173 | - } else { |
|
| 174 | - $this->providerRegistry->disableProviderFor($provider, $user); |
|
| 175 | - } |
|
| 176 | - $providerStates[$provider->getId()] = $enabled; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $providerStates; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @param array $states |
|
| 184 | - * @param IProvider[] $providers |
|
| 185 | - */ |
|
| 186 | - private function isProviderMissing(array $states, array $providers): bool { |
|
| 187 | - $indexed = []; |
|
| 188 | - foreach ($providers as $provider) { |
|
| 189 | - $indexed[$provider->getId()] = $provider; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - $missing = []; |
|
| 193 | - foreach ($states as $providerId => $enabled) { |
|
| 194 | - if (!$enabled) { |
|
| 195 | - // Don't care |
|
| 196 | - continue; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - if (!isset($indexed[$providerId])) { |
|
| 200 | - $missing[] = $providerId; |
|
| 201 | - $this->logger->alert("two-factor auth provider '$providerId' failed to load", |
|
| 202 | - [ |
|
| 203 | - 'app' => 'core', |
|
| 204 | - ]); |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if (!empty($missing)) { |
|
| 209 | - // There was at least one provider missing |
|
| 210 | - $this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']); |
|
| 211 | - |
|
| 212 | - return true; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - // If we reach this, there was not a single provider missing |
|
| 216 | - return false; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Get the list of 2FA providers for the given user |
|
| 221 | - * |
|
| 222 | - * @param IUser $user |
|
| 223 | - * @throws Exception |
|
| 224 | - */ |
|
| 225 | - public function getProviderSet(IUser $user): ProviderSet { |
|
| 226 | - $providerStates = $this->providerRegistry->getProviderStates($user); |
|
| 227 | - $providers = $this->providerLoader->getProviders($user); |
|
| 228 | - |
|
| 229 | - $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
| 230 | - $isProviderMissing = $this->isProviderMissing($fixedStates, $providers); |
|
| 231 | - |
|
| 232 | - $enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) { |
|
| 233 | - return $fixedStates[$provider->getId()]; |
|
| 234 | - }); |
|
| 235 | - return new ProviderSet($enabled, $isProviderMissing); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Verify the given challenge |
|
| 240 | - * |
|
| 241 | - * @param string $providerId |
|
| 242 | - * @param IUser $user |
|
| 243 | - * @param string $challenge |
|
| 244 | - * @return boolean |
|
| 245 | - */ |
|
| 246 | - public function verifyChallenge(string $providerId, IUser $user, string $challenge): bool { |
|
| 247 | - $provider = $this->getProvider($user, $providerId); |
|
| 248 | - if ($provider === null) { |
|
| 249 | - return false; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $passed = $provider->verifyChallenge($user, $challenge); |
|
| 253 | - if ($passed) { |
|
| 254 | - if ($this->session->get(self::REMEMBER_LOGIN) === true) { |
|
| 255 | - // TODO: resolve cyclic dependency and use DI |
|
| 256 | - \OC::$server->getUserSession()->createRememberMeToken($user); |
|
| 257 | - } |
|
| 258 | - $this->session->remove(self::SESSION_UID_KEY); |
|
| 259 | - $this->session->remove(self::REMEMBER_LOGIN); |
|
| 260 | - $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
| 261 | - |
|
| 262 | - // Clear token from db |
|
| 263 | - $sessionId = $this->session->getId(); |
|
| 264 | - $token = $this->tokenProvider->getToken($sessionId); |
|
| 265 | - $tokenId = $token->getId(); |
|
| 266 | - $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId); |
|
| 267 | - |
|
| 268 | - $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
| 269 | - $this->dispatcher->dispatch(IProvider::EVENT_SUCCESS, $dispatchEvent); |
|
| 270 | - |
|
| 271 | - $this->publishEvent($user, 'twofactor_success', [ |
|
| 272 | - 'provider' => $provider->getDisplayName(), |
|
| 273 | - ]); |
|
| 274 | - } else { |
|
| 275 | - $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
| 276 | - $this->dispatcher->dispatch(IProvider::EVENT_FAILED, $dispatchEvent); |
|
| 277 | - |
|
| 278 | - $this->publishEvent($user, 'twofactor_failed', [ |
|
| 279 | - 'provider' => $provider->getDisplayName(), |
|
| 280 | - ]); |
|
| 281 | - } |
|
| 282 | - return $passed; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Push a 2fa event the user's activity stream |
|
| 287 | - * |
|
| 288 | - * @param IUser $user |
|
| 289 | - * @param string $event |
|
| 290 | - * @param array $params |
|
| 291 | - */ |
|
| 292 | - private function publishEvent(IUser $user, string $event, array $params) { |
|
| 293 | - $activity = $this->activityManager->generateEvent(); |
|
| 294 | - $activity->setApp('core') |
|
| 295 | - ->setType('security') |
|
| 296 | - ->setAuthor($user->getUID()) |
|
| 297 | - ->setAffectedUser($user->getUID()) |
|
| 298 | - ->setSubject($event, $params); |
|
| 299 | - try { |
|
| 300 | - $this->activityManager->publish($activity); |
|
| 301 | - } catch (BadMethodCallException $e) { |
|
| 302 | - $this->logger->warning('could not publish activity', ['app' => 'core']); |
|
| 303 | - $this->logger->logException($e, ['app' => 'core']); |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Check if the currently logged in user needs to pass 2FA |
|
| 309 | - * |
|
| 310 | - * @param IUser $user the currently logged in user |
|
| 311 | - * @return boolean |
|
| 312 | - */ |
|
| 313 | - public function needsSecondFactor(IUser $user = null): bool { |
|
| 314 | - if ($user === null) { |
|
| 315 | - return false; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - // If we are authenticated using an app password skip all this |
|
| 319 | - if ($this->session->exists('app_password')) { |
|
| 320 | - return false; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - // First check if the session tells us we should do 2FA (99% case) |
|
| 324 | - if (!$this->session->exists(self::SESSION_UID_KEY)) { |
|
| 325 | - |
|
| 326 | - // Check if the session tells us it is 2FA authenticated already |
|
| 327 | - if ($this->session->exists(self::SESSION_UID_DONE) && |
|
| 328 | - $this->session->get(self::SESSION_UID_DONE) === $user->getUID()) { |
|
| 329 | - return false; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /* |
|
| 52 | + const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
| 53 | + const SESSION_UID_DONE = 'two_factor_auth_passed'; |
|
| 54 | + const REMEMBER_LOGIN = 'two_factor_remember_login'; |
|
| 55 | + const BACKUP_CODES_PROVIDER_ID = 'backup_codes'; |
|
| 56 | + |
|
| 57 | + /** @var ProviderLoader */ |
|
| 58 | + private $providerLoader; |
|
| 59 | + |
|
| 60 | + /** @var IRegistry */ |
|
| 61 | + private $providerRegistry; |
|
| 62 | + |
|
| 63 | + /** @var MandatoryTwoFactor */ |
|
| 64 | + private $mandatoryTwoFactor; |
|
| 65 | + |
|
| 66 | + /** @var ISession */ |
|
| 67 | + private $session; |
|
| 68 | + |
|
| 69 | + /** @var IConfig */ |
|
| 70 | + private $config; |
|
| 71 | + |
|
| 72 | + /** @var IManager */ |
|
| 73 | + private $activityManager; |
|
| 74 | + |
|
| 75 | + /** @var ILogger */ |
|
| 76 | + private $logger; |
|
| 77 | + |
|
| 78 | + /** @var TokenProvider */ |
|
| 79 | + private $tokenProvider; |
|
| 80 | + |
|
| 81 | + /** @var ITimeFactory */ |
|
| 82 | + private $timeFactory; |
|
| 83 | + |
|
| 84 | + /** @var EventDispatcherInterface */ |
|
| 85 | + private $dispatcher; |
|
| 86 | + |
|
| 87 | + public function __construct(ProviderLoader $providerLoader, |
|
| 88 | + IRegistry $providerRegistry, |
|
| 89 | + MandatoryTwoFactor $mandatoryTwoFactor, |
|
| 90 | + ISession $session, IConfig $config, |
|
| 91 | + IManager $activityManager, ILogger $logger, TokenProvider $tokenProvider, |
|
| 92 | + ITimeFactory $timeFactory, EventDispatcherInterface $eventDispatcher) { |
|
| 93 | + $this->providerLoader = $providerLoader; |
|
| 94 | + $this->providerRegistry = $providerRegistry; |
|
| 95 | + $this->mandatoryTwoFactor = $mandatoryTwoFactor; |
|
| 96 | + $this->session = $session; |
|
| 97 | + $this->config = $config; |
|
| 98 | + $this->activityManager = $activityManager; |
|
| 99 | + $this->logger = $logger; |
|
| 100 | + $this->tokenProvider = $tokenProvider; |
|
| 101 | + $this->timeFactory = $timeFactory; |
|
| 102 | + $this->dispatcher = $eventDispatcher; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Determine whether the user must provide a second factor challenge |
|
| 107 | + * |
|
| 108 | + * @param IUser $user |
|
| 109 | + * @return boolean |
|
| 110 | + */ |
|
| 111 | + public function isTwoFactorAuthenticated(IUser $user): bool { |
|
| 112 | + if ($this->mandatoryTwoFactor->isEnforcedFor($user)) { |
|
| 113 | + return true; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $providerStates = $this->providerRegistry->getProviderStates($user); |
|
| 117 | + $providers = $this->providerLoader->getProviders($user); |
|
| 118 | + $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
| 119 | + $enabled = array_filter($fixedStates); |
|
| 120 | + $providerIds = array_keys($enabled); |
|
| 121 | + $providerIdsWithoutBackupCodes = array_diff($providerIds, [self::BACKUP_CODES_PROVIDER_ID]); |
|
| 122 | + |
|
| 123 | + return !empty($providerIdsWithoutBackupCodes); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Get a 2FA provider by its ID |
|
| 128 | + * |
|
| 129 | + * @param IUser $user |
|
| 130 | + * @param string $challengeProviderId |
|
| 131 | + * @return IProvider|null |
|
| 132 | + */ |
|
| 133 | + public function getProvider(IUser $user, string $challengeProviderId) { |
|
| 134 | + $providers = $this->getProviderSet($user)->getProviders(); |
|
| 135 | + return $providers[$challengeProviderId] ?? null; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @param IUser $user |
|
| 140 | + * @return IActivatableAtLogin[] |
|
| 141 | + * @throws Exception |
|
| 142 | + */ |
|
| 143 | + public function getLoginSetupProviders(IUser $user): array { |
|
| 144 | + $providers = $this->providerLoader->getProviders($user); |
|
| 145 | + return array_filter($providers, function(IProvider $provider) { |
|
| 146 | + return ($provider instanceof IActivatableAtLogin); |
|
| 147 | + }); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Check if the persistant mapping of enabled/disabled state of each available |
|
| 152 | + * provider is missing an entry and add it to the registry in that case. |
|
| 153 | + * |
|
| 154 | + * @todo remove in Nextcloud 17 as by then all providers should have been updated |
|
| 155 | + * |
|
| 156 | + * @param string[] $providerStates |
|
| 157 | + * @param IProvider[] $providers |
|
| 158 | + * @param IUser $user |
|
| 159 | + * @return string[] the updated $providerStates variable |
|
| 160 | + */ |
|
| 161 | + private function fixMissingProviderStates(array $providerStates, |
|
| 162 | + array $providers, IUser $user): array { |
|
| 163 | + |
|
| 164 | + foreach ($providers as $provider) { |
|
| 165 | + if (isset($providerStates[$provider->getId()])) { |
|
| 166 | + // All good |
|
| 167 | + continue; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $enabled = $provider->isTwoFactorAuthEnabledForUser($user); |
|
| 171 | + if ($enabled) { |
|
| 172 | + $this->providerRegistry->enableProviderFor($provider, $user); |
|
| 173 | + } else { |
|
| 174 | + $this->providerRegistry->disableProviderFor($provider, $user); |
|
| 175 | + } |
|
| 176 | + $providerStates[$provider->getId()] = $enabled; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $providerStates; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @param array $states |
|
| 184 | + * @param IProvider[] $providers |
|
| 185 | + */ |
|
| 186 | + private function isProviderMissing(array $states, array $providers): bool { |
|
| 187 | + $indexed = []; |
|
| 188 | + foreach ($providers as $provider) { |
|
| 189 | + $indexed[$provider->getId()] = $provider; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + $missing = []; |
|
| 193 | + foreach ($states as $providerId => $enabled) { |
|
| 194 | + if (!$enabled) { |
|
| 195 | + // Don't care |
|
| 196 | + continue; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + if (!isset($indexed[$providerId])) { |
|
| 200 | + $missing[] = $providerId; |
|
| 201 | + $this->logger->alert("two-factor auth provider '$providerId' failed to load", |
|
| 202 | + [ |
|
| 203 | + 'app' => 'core', |
|
| 204 | + ]); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if (!empty($missing)) { |
|
| 209 | + // There was at least one provider missing |
|
| 210 | + $this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']); |
|
| 211 | + |
|
| 212 | + return true; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + // If we reach this, there was not a single provider missing |
|
| 216 | + return false; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Get the list of 2FA providers for the given user |
|
| 221 | + * |
|
| 222 | + * @param IUser $user |
|
| 223 | + * @throws Exception |
|
| 224 | + */ |
|
| 225 | + public function getProviderSet(IUser $user): ProviderSet { |
|
| 226 | + $providerStates = $this->providerRegistry->getProviderStates($user); |
|
| 227 | + $providers = $this->providerLoader->getProviders($user); |
|
| 228 | + |
|
| 229 | + $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
| 230 | + $isProviderMissing = $this->isProviderMissing($fixedStates, $providers); |
|
| 231 | + |
|
| 232 | + $enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) { |
|
| 233 | + return $fixedStates[$provider->getId()]; |
|
| 234 | + }); |
|
| 235 | + return new ProviderSet($enabled, $isProviderMissing); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Verify the given challenge |
|
| 240 | + * |
|
| 241 | + * @param string $providerId |
|
| 242 | + * @param IUser $user |
|
| 243 | + * @param string $challenge |
|
| 244 | + * @return boolean |
|
| 245 | + */ |
|
| 246 | + public function verifyChallenge(string $providerId, IUser $user, string $challenge): bool { |
|
| 247 | + $provider = $this->getProvider($user, $providerId); |
|
| 248 | + if ($provider === null) { |
|
| 249 | + return false; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $passed = $provider->verifyChallenge($user, $challenge); |
|
| 253 | + if ($passed) { |
|
| 254 | + if ($this->session->get(self::REMEMBER_LOGIN) === true) { |
|
| 255 | + // TODO: resolve cyclic dependency and use DI |
|
| 256 | + \OC::$server->getUserSession()->createRememberMeToken($user); |
|
| 257 | + } |
|
| 258 | + $this->session->remove(self::SESSION_UID_KEY); |
|
| 259 | + $this->session->remove(self::REMEMBER_LOGIN); |
|
| 260 | + $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
| 261 | + |
|
| 262 | + // Clear token from db |
|
| 263 | + $sessionId = $this->session->getId(); |
|
| 264 | + $token = $this->tokenProvider->getToken($sessionId); |
|
| 265 | + $tokenId = $token->getId(); |
|
| 266 | + $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId); |
|
| 267 | + |
|
| 268 | + $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
| 269 | + $this->dispatcher->dispatch(IProvider::EVENT_SUCCESS, $dispatchEvent); |
|
| 270 | + |
|
| 271 | + $this->publishEvent($user, 'twofactor_success', [ |
|
| 272 | + 'provider' => $provider->getDisplayName(), |
|
| 273 | + ]); |
|
| 274 | + } else { |
|
| 275 | + $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
| 276 | + $this->dispatcher->dispatch(IProvider::EVENT_FAILED, $dispatchEvent); |
|
| 277 | + |
|
| 278 | + $this->publishEvent($user, 'twofactor_failed', [ |
|
| 279 | + 'provider' => $provider->getDisplayName(), |
|
| 280 | + ]); |
|
| 281 | + } |
|
| 282 | + return $passed; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Push a 2fa event the user's activity stream |
|
| 287 | + * |
|
| 288 | + * @param IUser $user |
|
| 289 | + * @param string $event |
|
| 290 | + * @param array $params |
|
| 291 | + */ |
|
| 292 | + private function publishEvent(IUser $user, string $event, array $params) { |
|
| 293 | + $activity = $this->activityManager->generateEvent(); |
|
| 294 | + $activity->setApp('core') |
|
| 295 | + ->setType('security') |
|
| 296 | + ->setAuthor($user->getUID()) |
|
| 297 | + ->setAffectedUser($user->getUID()) |
|
| 298 | + ->setSubject($event, $params); |
|
| 299 | + try { |
|
| 300 | + $this->activityManager->publish($activity); |
|
| 301 | + } catch (BadMethodCallException $e) { |
|
| 302 | + $this->logger->warning('could not publish activity', ['app' => 'core']); |
|
| 303 | + $this->logger->logException($e, ['app' => 'core']); |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Check if the currently logged in user needs to pass 2FA |
|
| 309 | + * |
|
| 310 | + * @param IUser $user the currently logged in user |
|
| 311 | + * @return boolean |
|
| 312 | + */ |
|
| 313 | + public function needsSecondFactor(IUser $user = null): bool { |
|
| 314 | + if ($user === null) { |
|
| 315 | + return false; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + // If we are authenticated using an app password skip all this |
|
| 319 | + if ($this->session->exists('app_password')) { |
|
| 320 | + return false; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + // First check if the session tells us we should do 2FA (99% case) |
|
| 324 | + if (!$this->session->exists(self::SESSION_UID_KEY)) { |
|
| 325 | + |
|
| 326 | + // Check if the session tells us it is 2FA authenticated already |
|
| 327 | + if ($this->session->exists(self::SESSION_UID_DONE) && |
|
| 328 | + $this->session->get(self::SESSION_UID_DONE) === $user->getUID()) { |
|
| 329 | + return false; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /* |
|
| 333 | 333 | * If the session is expired check if we are not logged in by a token |
| 334 | 334 | * that still needs 2FA auth |
| 335 | 335 | */ |
| 336 | - try { |
|
| 337 | - $sessionId = $this->session->getId(); |
|
| 338 | - $token = $this->tokenProvider->getToken($sessionId); |
|
| 339 | - $tokenId = $token->getId(); |
|
| 340 | - $tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
| 341 | - |
|
| 342 | - if (!\in_array($tokenId, $tokensNeeding2FA, true)) { |
|
| 343 | - $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
| 344 | - return false; |
|
| 345 | - } |
|
| 346 | - } catch (InvalidTokenException $e) { |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - if (!$this->isTwoFactorAuthenticated($user)) { |
|
| 351 | - // There is no second factor any more -> let the user pass |
|
| 352 | - // This prevents infinite redirect loops when a user is about |
|
| 353 | - // to solve the 2FA challenge, and the provider app is |
|
| 354 | - // disabled the same time |
|
| 355 | - $this->session->remove(self::SESSION_UID_KEY); |
|
| 356 | - |
|
| 357 | - $keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
| 358 | - foreach ($keys as $key) { |
|
| 359 | - $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key); |
|
| 360 | - } |
|
| 361 | - return false; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return true; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Prepare the 2FA login |
|
| 369 | - * |
|
| 370 | - * @param IUser $user |
|
| 371 | - * @param boolean $rememberMe |
|
| 372 | - */ |
|
| 373 | - public function prepareTwoFactorLogin(IUser $user, bool $rememberMe) { |
|
| 374 | - $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
| 375 | - $this->session->set(self::REMEMBER_LOGIN, $rememberMe); |
|
| 376 | - |
|
| 377 | - $id = $this->session->getId(); |
|
| 378 | - $token = $this->tokenProvider->getToken($id); |
|
| 379 | - $this->config->setUserValue($user->getUID(), 'login_token_2fa', $token->getId(), $this->timeFactory->getTime()); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - public function clearTwoFactorPending(string $userId) { |
|
| 383 | - $tokensNeeding2FA = $this->config->getUserKeys($userId, 'login_token_2fa'); |
|
| 384 | - |
|
| 385 | - foreach ($tokensNeeding2FA as $tokenId) { |
|
| 386 | - $this->tokenProvider->invalidateTokenById($userId, $tokenId); |
|
| 387 | - } |
|
| 388 | - } |
|
| 336 | + try { |
|
| 337 | + $sessionId = $this->session->getId(); |
|
| 338 | + $token = $this->tokenProvider->getToken($sessionId); |
|
| 339 | + $tokenId = $token->getId(); |
|
| 340 | + $tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
| 341 | + |
|
| 342 | + if (!\in_array($tokenId, $tokensNeeding2FA, true)) { |
|
| 343 | + $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
| 344 | + return false; |
|
| 345 | + } |
|
| 346 | + } catch (InvalidTokenException $e) { |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + if (!$this->isTwoFactorAuthenticated($user)) { |
|
| 351 | + // There is no second factor any more -> let the user pass |
|
| 352 | + // This prevents infinite redirect loops when a user is about |
|
| 353 | + // to solve the 2FA challenge, and the provider app is |
|
| 354 | + // disabled the same time |
|
| 355 | + $this->session->remove(self::SESSION_UID_KEY); |
|
| 356 | + |
|
| 357 | + $keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
| 358 | + foreach ($keys as $key) { |
|
| 359 | + $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key); |
|
| 360 | + } |
|
| 361 | + return false; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return true; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Prepare the 2FA login |
|
| 369 | + * |
|
| 370 | + * @param IUser $user |
|
| 371 | + * @param boolean $rememberMe |
|
| 372 | + */ |
|
| 373 | + public function prepareTwoFactorLogin(IUser $user, bool $rememberMe) { |
|
| 374 | + $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
| 375 | + $this->session->set(self::REMEMBER_LOGIN, $rememberMe); |
|
| 376 | + |
|
| 377 | + $id = $this->session->getId(); |
|
| 378 | + $token = $this->tokenProvider->getToken($id); |
|
| 379 | + $this->config->setUserValue($user->getUID(), 'login_token_2fa', $token->getId(), $this->timeFactory->getTime()); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + public function clearTwoFactorPending(string $userId) { |
|
| 383 | + $tokensNeeding2FA = $this->config->getUserKeys($userId, 'login_token_2fa'); |
|
| 384 | + |
|
| 385 | + foreach ($tokensNeeding2FA as $tokenId) { |
|
| 386 | + $this->tokenProvider->invalidateTokenById($userId, $tokenId); |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | 389 | |
| 390 | 390 | } |
@@ -40,48 +40,48 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | class GeneratorHelper { |
| 42 | 42 | |
| 43 | - /** @var IRootFolder */ |
|
| 44 | - private $rootFolder; |
|
| 43 | + /** @var IRootFolder */ |
|
| 44 | + private $rootFolder; |
|
| 45 | 45 | |
| 46 | - /** @var IConfig */ |
|
| 47 | - private $config; |
|
| 46 | + /** @var IConfig */ |
|
| 47 | + private $config; |
|
| 48 | 48 | |
| 49 | - public function __construct(IRootFolder $rootFolder, IConfig $config) { |
|
| 50 | - $this->rootFolder = $rootFolder; |
|
| 51 | - $this->config = $config; |
|
| 52 | - } |
|
| 49 | + public function __construct(IRootFolder $rootFolder, IConfig $config) { |
|
| 50 | + $this->rootFolder = $rootFolder; |
|
| 51 | + $this->config = $config; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @param IProviderV2 $provider |
|
| 56 | - * @param File $file |
|
| 57 | - * @param int $maxWidth |
|
| 58 | - * @param int $maxHeight |
|
| 59 | - * |
|
| 60 | - * @return bool|IImage |
|
| 61 | - */ |
|
| 62 | - public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight) { |
|
| 63 | - return $provider->getThumbnail($file, $maxWidth, $maxHeight); |
|
| 64 | - } |
|
| 54 | + /** |
|
| 55 | + * @param IProviderV2 $provider |
|
| 56 | + * @param File $file |
|
| 57 | + * @param int $maxWidth |
|
| 58 | + * @param int $maxHeight |
|
| 59 | + * |
|
| 60 | + * @return bool|IImage |
|
| 61 | + */ |
|
| 62 | + public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight) { |
|
| 63 | + return $provider->getThumbnail($file, $maxWidth, $maxHeight); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @param ISimpleFile $maxPreview |
|
| 68 | - * @return IImage |
|
| 69 | - */ |
|
| 70 | - public function getImage(ISimpleFile $maxPreview) { |
|
| 71 | - $image = new OCPImage(); |
|
| 72 | - $image->loadFromData($maxPreview->getContent()); |
|
| 73 | - return $image; |
|
| 74 | - } |
|
| 66 | + /** |
|
| 67 | + * @param ISimpleFile $maxPreview |
|
| 68 | + * @return IImage |
|
| 69 | + */ |
|
| 70 | + public function getImage(ISimpleFile $maxPreview) { |
|
| 71 | + $image = new OCPImage(); |
|
| 72 | + $image->loadFromData($maxPreview->getContent()); |
|
| 73 | + return $image; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @param callable $providerClosure |
|
| 78 | - * @return IProviderV2 |
|
| 79 | - */ |
|
| 80 | - public function getProvider($providerClosure) { |
|
| 81 | - $provider = $providerClosure(); |
|
| 82 | - if ($provider instanceof IProvider) { |
|
| 83 | - $provider = new ProviderV1Adapter($provider); |
|
| 84 | - } |
|
| 85 | - return $provider; |
|
| 86 | - } |
|
| 76 | + /** |
|
| 77 | + * @param callable $providerClosure |
|
| 78 | + * @return IProviderV2 |
|
| 79 | + */ |
|
| 80 | + public function getProvider($providerClosure) { |
|
| 81 | + $provider = $providerClosure(); |
|
| 82 | + if ($provider instanceof IProvider) { |
|
| 83 | + $provider = new ProviderV1Adapter($provider); |
|
| 84 | + } |
|
| 85 | + return $provider; |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -46,272 +46,272 @@ |
||
| 46 | 46 | |
| 47 | 47 | class Migrator { |
| 48 | 48 | |
| 49 | - /** @var \Doctrine\DBAL\Connection */ |
|
| 50 | - protected $connection; |
|
| 51 | - |
|
| 52 | - /** @var ISecureRandom */ |
|
| 53 | - private $random; |
|
| 54 | - |
|
| 55 | - /** @var IConfig */ |
|
| 56 | - protected $config; |
|
| 57 | - |
|
| 58 | - /** @var EventDispatcherInterface */ |
|
| 59 | - private $dispatcher; |
|
| 60 | - |
|
| 61 | - /** @var bool */ |
|
| 62 | - private $noEmit = false; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param \Doctrine\DBAL\Connection $connection |
|
| 66 | - * @param ISecureRandom $random |
|
| 67 | - * @param IConfig $config |
|
| 68 | - * @param EventDispatcherInterface $dispatcher |
|
| 69 | - */ |
|
| 70 | - public function __construct(\Doctrine\DBAL\Connection $connection, |
|
| 71 | - ISecureRandom $random, |
|
| 72 | - IConfig $config, |
|
| 73 | - EventDispatcherInterface $dispatcher = null) { |
|
| 74 | - $this->connection = $connection; |
|
| 75 | - $this->random = $random; |
|
| 76 | - $this->config = $config; |
|
| 77 | - $this->dispatcher = $dispatcher; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
| 82 | - */ |
|
| 83 | - public function migrate(Schema $targetSchema) { |
|
| 84 | - $this->noEmit = true; |
|
| 85 | - $this->applySchema($targetSchema); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - public function generateChangeScript(Schema $targetSchema) { |
|
| 93 | - $schemaDiff = $this->getDiff($targetSchema, $this->connection); |
|
| 94 | - |
|
| 95 | - $script = ''; |
|
| 96 | - $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); |
|
| 97 | - foreach ($sqls as $sql) { |
|
| 98 | - $script .= $this->convertStatementToScript($sql); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return $script; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @param Schema $targetSchema |
|
| 106 | - * @throws \OC\DB\MigrationException |
|
| 107 | - */ |
|
| 108 | - public function checkMigrate(Schema $targetSchema) { |
|
| 109 | - $this->noEmit = true; |
|
| 110 | - /**@var \Doctrine\DBAL\Schema\Table[] $tables */ |
|
| 111 | - $tables = $targetSchema->getTables(); |
|
| 112 | - $filterExpression = $this->getFilterExpression(); |
|
| 113 | - $this->connection->getConfiguration()-> |
|
| 114 | - setFilterSchemaAssetsExpression($filterExpression); |
|
| 115 | - $existingTables = $this->connection->getSchemaManager()->listTableNames(); |
|
| 116 | - |
|
| 117 | - $step = 0; |
|
| 118 | - foreach ($tables as $table) { |
|
| 119 | - if (strpos($table->getName(), '.')) { |
|
| 120 | - list(, $tableName) = explode('.', $table->getName()); |
|
| 121 | - } else { |
|
| 122 | - $tableName = $table->getName(); |
|
| 123 | - } |
|
| 124 | - $this->emitCheckStep($tableName, $step++, count($tables)); |
|
| 125 | - // don't need to check for new tables |
|
| 126 | - if (array_search($tableName, $existingTables) !== false) { |
|
| 127 | - $this->checkTableMigrate($table); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Create a unique name for the temporary table |
|
| 134 | - * |
|
| 135 | - * @param string $name |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - protected function generateTemporaryTableName($name) { |
|
| 139 | - return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Check the migration of a table on a copy so we can detect errors before messing with the real table |
|
| 144 | - * |
|
| 145 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
| 146 | - * @throws \OC\DB\MigrationException |
|
| 147 | - */ |
|
| 148 | - protected function checkTableMigrate(Table $table) { |
|
| 149 | - $name = $table->getName(); |
|
| 150 | - $tmpName = $this->generateTemporaryTableName($name); |
|
| 151 | - |
|
| 152 | - $this->copyTable($name, $tmpName); |
|
| 153 | - |
|
| 154 | - //create the migration schema for the temporary table |
|
| 155 | - $tmpTable = $this->renameTableSchema($table, $tmpName); |
|
| 156 | - $schemaConfig = new SchemaConfig(); |
|
| 157 | - $schemaConfig->setName($this->connection->getDatabase()); |
|
| 158 | - $schema = new Schema(array($tmpTable), array(), $schemaConfig); |
|
| 159 | - |
|
| 160 | - try { |
|
| 161 | - $this->applySchema($schema); |
|
| 162 | - $this->dropTable($tmpName); |
|
| 163 | - } catch (DBALException $e) { |
|
| 164 | - // pgsql needs to commit it's failed transaction before doing anything else |
|
| 165 | - if ($this->connection->isTransactionActive()) { |
|
| 166 | - $this->connection->commit(); |
|
| 167 | - } |
|
| 168 | - $this->dropTable($tmpName); |
|
| 169 | - throw new MigrationException($table->getName(), $e->getMessage()); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
| 175 | - * @param string $newName |
|
| 176 | - * @return \Doctrine\DBAL\Schema\Table |
|
| 177 | - */ |
|
| 178 | - protected function renameTableSchema(Table $table, $newName) { |
|
| 179 | - /** |
|
| 180 | - * @var \Doctrine\DBAL\Schema\Index[] $indexes |
|
| 181 | - */ |
|
| 182 | - $indexes = $table->getIndexes(); |
|
| 183 | - $newIndexes = array(); |
|
| 184 | - foreach ($indexes as $index) { |
|
| 185 | - if ($index->isPrimary()) { |
|
| 186 | - // do not rename primary key |
|
| 187 | - $indexName = $index->getName(); |
|
| 188 | - } else { |
|
| 189 | - // avoid conflicts in index names |
|
| 190 | - $indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER); |
|
| 191 | - } |
|
| 192 | - $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary()); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // foreign keys are not supported so we just set it to an empty array |
|
| 196 | - return new Table($newName, $table->getColumns(), $newIndexes, array(), 0, $table->getOptions()); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - public function createSchema() { |
|
| 200 | - $filterExpression = $this->getFilterExpression(); |
|
| 201 | - $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
| 202 | - return $this->connection->getSchemaManager()->createSchema(); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param Schema $targetSchema |
|
| 207 | - * @param \Doctrine\DBAL\Connection $connection |
|
| 208 | - * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
| 209 | - * @throws DBALException |
|
| 210 | - */ |
|
| 211 | - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
| 212 | - // adjust varchar columns with a length higher then getVarcharMaxLength to clob |
|
| 213 | - foreach ($targetSchema->getTables() as $table) { |
|
| 214 | - foreach ($table->getColumns() as $column) { |
|
| 215 | - if ($column->getType() instanceof StringType) { |
|
| 216 | - if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) { |
|
| 217 | - $column->setType(Type::getType('text')); |
|
| 218 | - $column->setLength(null); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $filterExpression = $this->getFilterExpression(); |
|
| 225 | - $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
| 226 | - $sourceSchema = $connection->getSchemaManager()->createSchema(); |
|
| 227 | - |
|
| 228 | - // remove tables we don't know about |
|
| 229 | - /** @var $table \Doctrine\DBAL\Schema\Table */ |
|
| 230 | - foreach ($sourceSchema->getTables() as $table) { |
|
| 231 | - if (!$targetSchema->hasTable($table->getName())) { |
|
| 232 | - $sourceSchema->dropTable($table->getName()); |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - // remove sequences we don't know about |
|
| 236 | - foreach ($sourceSchema->getSequences() as $table) { |
|
| 237 | - if (!$targetSchema->hasSequence($table->getName())) { |
|
| 238 | - $sourceSchema->dropSequence($table->getName()); |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - $comparator = new Comparator(); |
|
| 243 | - return $comparator->compare($sourceSchema, $targetSchema); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
| 248 | - * @param \Doctrine\DBAL\Connection $connection |
|
| 249 | - */ |
|
| 250 | - protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { |
|
| 251 | - if (is_null($connection)) { |
|
| 252 | - $connection = $this->connection; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $schemaDiff = $this->getDiff($targetSchema, $connection); |
|
| 256 | - |
|
| 257 | - $connection->beginTransaction(); |
|
| 258 | - $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); |
|
| 259 | - $step = 0; |
|
| 260 | - foreach ($sqls as $sql) { |
|
| 261 | - $this->emit($sql, $step++, count($sqls)); |
|
| 262 | - $connection->query($sql); |
|
| 263 | - } |
|
| 264 | - $connection->commit(); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @param string $sourceName |
|
| 269 | - * @param string $targetName |
|
| 270 | - */ |
|
| 271 | - protected function copyTable($sourceName, $targetName) { |
|
| 272 | - $quotedSource = $this->connection->quoteIdentifier($sourceName); |
|
| 273 | - $quotedTarget = $this->connection->quoteIdentifier($targetName); |
|
| 274 | - |
|
| 275 | - $this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')'); |
|
| 276 | - $this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @param string $name |
|
| 281 | - */ |
|
| 282 | - protected function dropTable($name) { |
|
| 283 | - $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name)); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @param $statement |
|
| 288 | - * @return string |
|
| 289 | - */ |
|
| 290 | - protected function convertStatementToScript($statement) { |
|
| 291 | - $script = $statement . ';'; |
|
| 292 | - $script .= PHP_EOL; |
|
| 293 | - $script .= PHP_EOL; |
|
| 294 | - return $script; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - protected function getFilterExpression() { |
|
| 298 | - return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - protected function emit($sql, $step, $max) { |
|
| 302 | - if ($this->noEmit) { |
|
| 303 | - return; |
|
| 304 | - } |
|
| 305 | - if(is_null($this->dispatcher)) { |
|
| 306 | - return; |
|
| 307 | - } |
|
| 308 | - $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step+1, $max])); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - private function emitCheckStep($tableName, $step, $max) { |
|
| 312 | - if(is_null($this->dispatcher)) { |
|
| 313 | - return; |
|
| 314 | - } |
|
| 315 | - $this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step+1, $max])); |
|
| 316 | - } |
|
| 49 | + /** @var \Doctrine\DBAL\Connection */ |
|
| 50 | + protected $connection; |
|
| 51 | + |
|
| 52 | + /** @var ISecureRandom */ |
|
| 53 | + private $random; |
|
| 54 | + |
|
| 55 | + /** @var IConfig */ |
|
| 56 | + protected $config; |
|
| 57 | + |
|
| 58 | + /** @var EventDispatcherInterface */ |
|
| 59 | + private $dispatcher; |
|
| 60 | + |
|
| 61 | + /** @var bool */ |
|
| 62 | + private $noEmit = false; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param \Doctrine\DBAL\Connection $connection |
|
| 66 | + * @param ISecureRandom $random |
|
| 67 | + * @param IConfig $config |
|
| 68 | + * @param EventDispatcherInterface $dispatcher |
|
| 69 | + */ |
|
| 70 | + public function __construct(\Doctrine\DBAL\Connection $connection, |
|
| 71 | + ISecureRandom $random, |
|
| 72 | + IConfig $config, |
|
| 73 | + EventDispatcherInterface $dispatcher = null) { |
|
| 74 | + $this->connection = $connection; |
|
| 75 | + $this->random = $random; |
|
| 76 | + $this->config = $config; |
|
| 77 | + $this->dispatcher = $dispatcher; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
| 82 | + */ |
|
| 83 | + public function migrate(Schema $targetSchema) { |
|
| 84 | + $this->noEmit = true; |
|
| 85 | + $this->applySchema($targetSchema); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + public function generateChangeScript(Schema $targetSchema) { |
|
| 93 | + $schemaDiff = $this->getDiff($targetSchema, $this->connection); |
|
| 94 | + |
|
| 95 | + $script = ''; |
|
| 96 | + $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); |
|
| 97 | + foreach ($sqls as $sql) { |
|
| 98 | + $script .= $this->convertStatementToScript($sql); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return $script; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @param Schema $targetSchema |
|
| 106 | + * @throws \OC\DB\MigrationException |
|
| 107 | + */ |
|
| 108 | + public function checkMigrate(Schema $targetSchema) { |
|
| 109 | + $this->noEmit = true; |
|
| 110 | + /**@var \Doctrine\DBAL\Schema\Table[] $tables */ |
|
| 111 | + $tables = $targetSchema->getTables(); |
|
| 112 | + $filterExpression = $this->getFilterExpression(); |
|
| 113 | + $this->connection->getConfiguration()-> |
|
| 114 | + setFilterSchemaAssetsExpression($filterExpression); |
|
| 115 | + $existingTables = $this->connection->getSchemaManager()->listTableNames(); |
|
| 116 | + |
|
| 117 | + $step = 0; |
|
| 118 | + foreach ($tables as $table) { |
|
| 119 | + if (strpos($table->getName(), '.')) { |
|
| 120 | + list(, $tableName) = explode('.', $table->getName()); |
|
| 121 | + } else { |
|
| 122 | + $tableName = $table->getName(); |
|
| 123 | + } |
|
| 124 | + $this->emitCheckStep($tableName, $step++, count($tables)); |
|
| 125 | + // don't need to check for new tables |
|
| 126 | + if (array_search($tableName, $existingTables) !== false) { |
|
| 127 | + $this->checkTableMigrate($table); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Create a unique name for the temporary table |
|
| 134 | + * |
|
| 135 | + * @param string $name |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + protected function generateTemporaryTableName($name) { |
|
| 139 | + return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Check the migration of a table on a copy so we can detect errors before messing with the real table |
|
| 144 | + * |
|
| 145 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
| 146 | + * @throws \OC\DB\MigrationException |
|
| 147 | + */ |
|
| 148 | + protected function checkTableMigrate(Table $table) { |
|
| 149 | + $name = $table->getName(); |
|
| 150 | + $tmpName = $this->generateTemporaryTableName($name); |
|
| 151 | + |
|
| 152 | + $this->copyTable($name, $tmpName); |
|
| 153 | + |
|
| 154 | + //create the migration schema for the temporary table |
|
| 155 | + $tmpTable = $this->renameTableSchema($table, $tmpName); |
|
| 156 | + $schemaConfig = new SchemaConfig(); |
|
| 157 | + $schemaConfig->setName($this->connection->getDatabase()); |
|
| 158 | + $schema = new Schema(array($tmpTable), array(), $schemaConfig); |
|
| 159 | + |
|
| 160 | + try { |
|
| 161 | + $this->applySchema($schema); |
|
| 162 | + $this->dropTable($tmpName); |
|
| 163 | + } catch (DBALException $e) { |
|
| 164 | + // pgsql needs to commit it's failed transaction before doing anything else |
|
| 165 | + if ($this->connection->isTransactionActive()) { |
|
| 166 | + $this->connection->commit(); |
|
| 167 | + } |
|
| 168 | + $this->dropTable($tmpName); |
|
| 169 | + throw new MigrationException($table->getName(), $e->getMessage()); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
| 175 | + * @param string $newName |
|
| 176 | + * @return \Doctrine\DBAL\Schema\Table |
|
| 177 | + */ |
|
| 178 | + protected function renameTableSchema(Table $table, $newName) { |
|
| 179 | + /** |
|
| 180 | + * @var \Doctrine\DBAL\Schema\Index[] $indexes |
|
| 181 | + */ |
|
| 182 | + $indexes = $table->getIndexes(); |
|
| 183 | + $newIndexes = array(); |
|
| 184 | + foreach ($indexes as $index) { |
|
| 185 | + if ($index->isPrimary()) { |
|
| 186 | + // do not rename primary key |
|
| 187 | + $indexName = $index->getName(); |
|
| 188 | + } else { |
|
| 189 | + // avoid conflicts in index names |
|
| 190 | + $indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER); |
|
| 191 | + } |
|
| 192 | + $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary()); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // foreign keys are not supported so we just set it to an empty array |
|
| 196 | + return new Table($newName, $table->getColumns(), $newIndexes, array(), 0, $table->getOptions()); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + public function createSchema() { |
|
| 200 | + $filterExpression = $this->getFilterExpression(); |
|
| 201 | + $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
| 202 | + return $this->connection->getSchemaManager()->createSchema(); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param Schema $targetSchema |
|
| 207 | + * @param \Doctrine\DBAL\Connection $connection |
|
| 208 | + * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
| 209 | + * @throws DBALException |
|
| 210 | + */ |
|
| 211 | + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
| 212 | + // adjust varchar columns with a length higher then getVarcharMaxLength to clob |
|
| 213 | + foreach ($targetSchema->getTables() as $table) { |
|
| 214 | + foreach ($table->getColumns() as $column) { |
|
| 215 | + if ($column->getType() instanceof StringType) { |
|
| 216 | + if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) { |
|
| 217 | + $column->setType(Type::getType('text')); |
|
| 218 | + $column->setLength(null); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $filterExpression = $this->getFilterExpression(); |
|
| 225 | + $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
| 226 | + $sourceSchema = $connection->getSchemaManager()->createSchema(); |
|
| 227 | + |
|
| 228 | + // remove tables we don't know about |
|
| 229 | + /** @var $table \Doctrine\DBAL\Schema\Table */ |
|
| 230 | + foreach ($sourceSchema->getTables() as $table) { |
|
| 231 | + if (!$targetSchema->hasTable($table->getName())) { |
|
| 232 | + $sourceSchema->dropTable($table->getName()); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + // remove sequences we don't know about |
|
| 236 | + foreach ($sourceSchema->getSequences() as $table) { |
|
| 237 | + if (!$targetSchema->hasSequence($table->getName())) { |
|
| 238 | + $sourceSchema->dropSequence($table->getName()); |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + $comparator = new Comparator(); |
|
| 243 | + return $comparator->compare($sourceSchema, $targetSchema); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
| 248 | + * @param \Doctrine\DBAL\Connection $connection |
|
| 249 | + */ |
|
| 250 | + protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { |
|
| 251 | + if (is_null($connection)) { |
|
| 252 | + $connection = $this->connection; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $schemaDiff = $this->getDiff($targetSchema, $connection); |
|
| 256 | + |
|
| 257 | + $connection->beginTransaction(); |
|
| 258 | + $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); |
|
| 259 | + $step = 0; |
|
| 260 | + foreach ($sqls as $sql) { |
|
| 261 | + $this->emit($sql, $step++, count($sqls)); |
|
| 262 | + $connection->query($sql); |
|
| 263 | + } |
|
| 264 | + $connection->commit(); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @param string $sourceName |
|
| 269 | + * @param string $targetName |
|
| 270 | + */ |
|
| 271 | + protected function copyTable($sourceName, $targetName) { |
|
| 272 | + $quotedSource = $this->connection->quoteIdentifier($sourceName); |
|
| 273 | + $quotedTarget = $this->connection->quoteIdentifier($targetName); |
|
| 274 | + |
|
| 275 | + $this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')'); |
|
| 276 | + $this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @param string $name |
|
| 281 | + */ |
|
| 282 | + protected function dropTable($name) { |
|
| 283 | + $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name)); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @param $statement |
|
| 288 | + * @return string |
|
| 289 | + */ |
|
| 290 | + protected function convertStatementToScript($statement) { |
|
| 291 | + $script = $statement . ';'; |
|
| 292 | + $script .= PHP_EOL; |
|
| 293 | + $script .= PHP_EOL; |
|
| 294 | + return $script; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + protected function getFilterExpression() { |
|
| 298 | + return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + protected function emit($sql, $step, $max) { |
|
| 302 | + if ($this->noEmit) { |
|
| 303 | + return; |
|
| 304 | + } |
|
| 305 | + if(is_null($this->dispatcher)) { |
|
| 306 | + return; |
|
| 307 | + } |
|
| 308 | + $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step+1, $max])); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + private function emitCheckStep($tableName, $step, $max) { |
|
| 312 | + if(is_null($this->dispatcher)) { |
|
| 313 | + return; |
|
| 314 | + } |
|
| 315 | + $this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step+1, $max])); |
|
| 316 | + } |
|
| 317 | 317 | } |