diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | .travis.yml | 108 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | appinfo/info.xml | 8 | ||||
-rw-r--r-- | capabilities.php | 2 | ||||
-rw-r--r-- | composer.json | 5 | ||||
-rw-r--r-- | controller/apicontroller.php | 19 | ||||
-rw-r--r-- | service/registrationservice.php | 10 | ||||
-rw-r--r-- | templates/admin.php | 2 | ||||
-rw-r--r-- | tests/scripts/app-check-code.sh | 9 | ||||
-rwxr-xr-x | tests/scripts/core-database.sh | 23 | ||||
-rw-r--r-- | tests/scripts/core-download.sh | 18 | ||||
-rwxr-xr-x | tests/scripts/core-setup.sh | 20 | ||||
-rwxr-xr-x | tests/scripts/lint-appinfo.sh | 13 | ||||
-rwxr-xr-x | tests/scripts/lint-php.sh | 11 | ||||
-rwxr-xr-x | tests/scripts/phpunit.sh | 12 | ||||
-rw-r--r-- | tests/unit/controller/ApiControllerTest.php | 78 | ||||
-rw-r--r-- | util/corebridge.php | 77 |
18 files changed, 307 insertions, 113 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f62603 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# due to the different Travis-CI PHP versions we MUST NOT lock to specific versions +composer.lock +vendor diff --git a/.travis.yml b/.travis.yml index e1d1ca4..d3913c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,22 @@ -sudo: required +sudo: false language: php php: - 5.6 - 7.0 + - 7.1 + - 7.2 + +cache: + directories: + - $HOME/.composer/cache/files + +services: + - mysql + - postgresql addons: apt: packages: - - mysql-server-5.6 - - mysql-client-core-5.6 - - mysql-client-5.6 - php5-pgsql - libxml2-utils @@ -17,71 +24,62 @@ env: global: - PHP_COVERAGE=FALSE matrix: - - CORE_FORK_BRANCH=nc_master DB=sqlite - - CORE_FORK_BRANCH=nc_stable11 DB=sqlite - - CORE_FORK_BRANCH=nc_stable12 DB=sqlite - - CORE_FORK_BRANCH=oc_master DB=sqlite - - CORE_FORK_BRANCH=oc_stable10 DB=sqlite - - CORE_FORK_BRANCH=nc_master DB=mysql - - CORE_FORK_BRANCH=nc_stable11 DB=mysql - - CORE_FORK_BRANCH=nc_stable12 DB=mysql - - CORE_FORK_BRANCH=oc_master DB=mysql - - CORE_FORK_BRANCH=oc_stable10 DB=mysql - -matrix: - allow_failures: - - php: 7.0 - fast_finish: true + - CORE_TYPE=nextcloud CORE_BRANCH=master DB=sqlite + - CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=sqlite + - CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=sqlite -branches: - only: - - master + - CORE_TYPE=nextcloud CORE_BRANCH=master DB=mysql + - CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=mysql + - CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=mysql -before_install: - - php --info + - CORE_TYPE=nextcloud CORE_BRANCH=master DB=pgsql + - CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=pgsql + - CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=pgsql - # Set up DB - - if [[ "$DB" == 'pgsql' ]]; then createuser -U travis -s oc_autotest; fi - - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e 'create database oc_autotest;'; fi - - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e "CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY '';"; fi - - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e "GRANT ALL ON oc_autotest.* TO 'oc_autotest'@'localhost';"; fi + - CORE_TYPE=owncloud CORE_BRANCH=master DB=sqlite + - CORE_TYPE=owncloud CORE_BRANCH=stable10 DB=sqlite - - cd .. - - if [[ $(echo $CORE_FORK_BRANCH | awk -F '_' '{print $1}') == 'oc' ]]; then git clone https://github.com/owncloud/core.git --recursive --depth 1 -b $(echo $CORE_FORK_BRANCH | awk -F '_' '{print $2}') core; fi - - if [[ $(echo $CORE_FORK_BRANCH | awk -F '_' '{print $1}') == 'nc' ]]; then git clone https://github.com/nextcloud/server.git --recursive --depth 1 -b $(echo $CORE_FORK_BRANCH | awk -F '_' '{print $2}') core; fi - - mv registration core/apps/ + - CORE_TYPE=owncloud CORE_BRANCH=master DB=mysql + - CORE_TYPE=owncloud CORE_BRANCH=stable10 DB=mysql -before_script: - # Set up core - - php -f core/occ maintenance:install --database-name oc_autotest --database-user oc_autotest --admin-user admin --admin-pass admin --database $DB --database-pass='' + - CORE_TYPE=owncloud CORE_BRANCH=master DB=pgsql + - CORE_TYPE=owncloud CORE_BRANCH=stable10 DB=pgsql - # Set up app - - php -f core/occ app:enable registration - # Enable app twice to check occ errors of registered commands - - php -f core/occ app:enable registration - - cd core/apps/registration +matrix: + fast_finish: true + exclude: + # Nextcloud 11/12 are not compatible with PHP 7.2 + - php: 7.2 + env: CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=mysql + - php: 7.2 + env: CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=mysql + - php: 7.2 + env: CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=sqlite + - php: 7.2 + env: CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=sqlite + - php: 7.2 + env: CORE_TYPE=nextcloud CORE_BRANCH=stable11 DB=pgsql + - php: 7.2 + env: CORE_TYPE=nextcloud CORE_BRANCH=stable12 DB=pgsql +before_install: # XDebug is only needed if we report coverage -> speeds up other builds - if [[ "$PHP_COVERAGE" = "FALSE" ]]; then phpenv config-rm xdebug.ini; fi +install: + - . ./tests/scripts/core-download.sh + - . ${BUILD_APP_MODULE_DIR}/tests/scripts/core-database.sh + - . ${BUILD_APP_MODULE_DIR}/tests/scripts/core-setup.sh + script: # Check info.xml schema validity - - wget https://apps.nextcloud.com/schema/apps/info.xsd - - xmllint appinfo/info.xml --schema info.xsd --noout - - rm info.xsd + - . ${BUILD_APP_MODULE_DIR}/tests/scripts/lint-appinfo.sh # Check PHP syntax errors - #- find . -name \*.php -not -path './vendor/*' -exec php -l "{}" \; - - # Run server's app code checker - # TODO: enable once table renames are possible - # - php ../../occ app:check-code mail + - . ${BUILD_APP_MODULE_DIR}/tests/scripts/lint-php.sh + # Run app code checker but do not fail, as we have to use private API for now + - . ${BUILD_APP_MODULE_DIR}/tests/scripts/app-check-code.sh || true # Run PHP tests - - cd tests - - phpunit -c phpunit.xml - - # Test packaging - #- cd .. - #- if [[ "$PACKAGE" = "TRUE" ]]; then make appstore; fi + - . ${BUILD_APP_MODULE_DIR}/tests/scripts/phpunit.sh @@ -1,3 +1,5 @@ +[![Build Status](https://travis-ci.org/buffcode/registration.svg?branch=master)](https://travis-ci.org/buffcode/registration) + # Registration This app allows users to register a new account. diff --git a/appinfo/info.xml b/appinfo/info.xml index 9f35631..4127aee 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -3,10 +3,13 @@ xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd"> <id>registration</id> <name>Registration</name> + <summary>This app allows users to self-register a new account using their e-mail address.</summary> <description>User registration</description> + <version>0.3.1</version> <licence>agpl</licence> <author mail="pellaeon@cnmc.tw" homepage="https://nyllep.wordpress.com/about-2">Pellaeon Lin</author> - <version>0.3.1</version> + <category>security</category> + <category>tools</category> <website>https://github.com/pellaeon/registration</website> <bugs>https://github.com/pellaeon/registration/issues</bugs> <repository>https://github.com/pellaeon/registration</repository> @@ -18,7 +21,4 @@ <owncloud min-version="9.0" max-version="11"/> <nextcloud min-version="9" max-version="13" /> </dependencies> - <category>auth</category> - <category>tools</category> - <ocsid>170608</ocsid> </info> diff --git a/capabilities.php b/capabilities.php index a97f56e..8234245 100644 --- a/capabilities.php +++ b/capabilities.php @@ -48,4 +48,4 @@ class Capabilities implements IPublicCapability { ]; } -}
\ No newline at end of file +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4a5539e --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require-dev": { + "phpunit/phpunit": "^5.5" + } +} diff --git a/controller/apicontroller.php b/controller/apicontroller.php index a00a85b..07eb43b 100644 --- a/controller/apicontroller.php +++ b/controller/apicontroller.php @@ -27,12 +27,10 @@ use OCA\Registration\Db\Registration; use OCA\Registration\Service\MailService; use OCA\Registration\Service\RegistrationException; use OCA\Registration\Service\RegistrationService; +use OCA\Registration\Util\CoreBridge; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; -use OCP\AppFramework\OCS\OCSBadRequestException; -use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCSController; -use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\Http\DataResponse; use OCP\Defaults; use OCP\IL10N; @@ -73,7 +71,7 @@ class ApiController extends OCSController { * @param string $username * @param string $displayname * @param string $email - * @throws OCSException + * @throws \Exception * @return DataResponse */ public function validate($username, $displayname, $email) { @@ -82,7 +80,7 @@ class ApiController extends OCSController { $this->registrationService->validateDisplayname($displayname); $this->registrationService->validateUsername($username); } catch (RegistrationException $e) { - throw new OCSBadRequestException($e->getMessage()); + throw CoreBridge::createException('OCSBadRequestException', $e->getMessage()); } $data = [ 'username' => $username, @@ -97,7 +95,7 @@ class ApiController extends OCSController { * @AnonRateThrottle(limit=10, period=1) * * @param string $clientSecret - * @throws OCSException + * @throws \Exception * @return DataResponse */ public function status($clientSecret) { @@ -105,7 +103,7 @@ class ApiController extends OCSController { /** @var Registration $registration */ $registration = $this->registrationService->getRegistrationForSecret($clientSecret); } catch (DoesNotExistException $e) { - throw new OCSNotFoundException('No pending registration.'); + throw CoreBridge::createException('OCSNotFoundException', 'No pending registration.'); } if (!$registration->getEmailConfirmed()) { @@ -142,7 +140,7 @@ class ApiController extends OCSController { * @param string $displayname * @param string $email * @param string $password - * @throws OCSException + * @throws \Exception * @return DataResponse */ public function register($username, $displayname, $email, $password) { @@ -175,8 +173,7 @@ class ApiController extends OCSController { } return new DataResponse($data, Http::STATUS_OK); } catch (RegistrationException $exception) { - throw new OCSException($exception->getMessage(), $exception->getCode()); + throw CoreBridge::createException('OCSException', $exception->getMessage(), $exception->getCode()); } } - -}
\ No newline at end of file +} diff --git a/service/registrationservice.php b/service/registrationservice.php index 20f4e8c..26509ae 100644 --- a/service/registrationservice.php +++ b/service/registrationservice.php @@ -49,8 +49,6 @@ use \OCP\IGroupManager; use \OCP\IL10N; use \OCP\IConfig; use \OCP\Security\ISecureRandom; -use \OC_User; -use \OC_Util; class RegistrationService { @@ -298,7 +296,7 @@ class RegistrationService { // disable user if this is requested by config $admin_approval_required = $this->config->getAppValue($this->appName, 'admin_approval_required', "no"); - if ($admin_approval_required == "yes") { + if ($admin_approval_required === "yes") { $user->setEnabled(false); } @@ -406,13 +404,13 @@ class RegistrationService { $this->usersession->login($username, $password); $this->usersession->createSessionToken($this->request, $userId, $username, $password); return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index')); - } elseif (OC_User::login($username, $password)) { + } elseif (\OC_User::login($username, $password)) { $this->cleanupLoginTokens($userId); // FIXME unsetMagicInCookie will fail from session already closed, so now we always remember $logintoken = $this->random->generate(32); $this->config->setUserValue($userId, 'login_token', $logintoken, time()); - OC_User::setMagicInCookie($userId, $logintoken); - OC_Util::redirectToDefaultPage(); + \OC_User::setMagicInCookie($userId, $logintoken); + \OC_Util::redirectToDefaultPage(); } // Render message in case redirect failed return new TemplateResponse('registration', 'message', diff --git a/templates/admin.php b/templates/admin.php index 8641035..e778d93 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -25,7 +25,7 @@ foreach ( $_['groups'] as $group ) { <p> <label for="admin_approval_required"><?php p($l->t('Require admin approval?')); ?> - <input type="checkbox" id="admin_approval_required" name="admin_approval_required" <?php if($_['approval_required'] == "yes" ) echo " checked"; ?>> + <input type="checkbox" id="admin_approval_required" name="admin_approval_required" <?php if($_['approval_required'] === "yes" ) echo " checked"; ?>> </label> </p> </form> diff --git a/tests/scripts/app-check-code.sh b/tests/scripts/app-check-code.sh new file mode 100644 index 0000000..8261e61 --- /dev/null +++ b/tests/scripts/app-check-code.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:app.check-code" + +set -e + +php -f ${BUILD_CORE_DIR}/occ app:check-code registration + +echo "travis_fold:end:app.check-code" diff --git a/tests/scripts/core-database.sh b/tests/scripts/core-database.sh new file mode 100755 index 0000000..9bc9fca --- /dev/null +++ b/tests/scripts/core-database.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:core.database.setup" + +set -e + +# MySQL +if [[ "${DB}" == 'mysql' ]] +then + mysql -u root -e 'CREATE DATABASE oc_autotest;' + mysql -u root -e "CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY '';" + mysql -u root -e "GRANT ALL ON oc_autotest.* TO 'oc_autotest'@'localhost';" +fi + +# Postgres +if [[ "${DB}" == 'pgsql' ]] +then + psql -U postgres -c 'CREATE DATABASE oc_autotest;' + psql -U postgres -c "CREATE USER oc_autotest WITH PASSWORD '';" + psql -U postgres -c 'GRANT ALL PRIVILEGES ON DATABASE oc_autotest TO oc_autotest;' +fi + +echo "travis_fold:end:core.database.setup" diff --git a/tests/scripts/core-download.sh b/tests/scripts/core-download.sh new file mode 100644 index 0000000..945b387 --- /dev/null +++ b/tests/scripts/core-download.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:core.download" + +set -e + +# export some generic paths +export BUILD_ROOT_DIR=$(dirname `pwd`) +export BUILD_CORE_DIR="${BUILD_ROOT_DIR}/core" +export BUILD_APPS_DIR="${BUILD_CORE_DIR}/apps" +export BUILD_APP_MODULE_DIR="${BUILD_APPS_DIR}/registration" + +if [[ ${CORE_TYPE} == 'owncloud' ]]; then git clone https://github.com/owncloud/core.git --recursive --depth 1 -b ${CORE_BRANCH} ${BUILD_CORE_DIR}; fi +if [[ ${CORE_TYPE} == 'nextcloud' ]]; then git clone https://github.com/nextcloud/server.git --recursive --depth 1 -b ${CORE_BRANCH} ${BUILD_CORE_DIR}; fi + +mv ${BUILD_ROOT_DIR}/registration ${BUILD_APPS_DIR} + +echo "travis_fold:end:core.download" diff --git a/tests/scripts/core-setup.sh b/tests/scripts/core-setup.sh new file mode 100755 index 0000000..5481f53 --- /dev/null +++ b/tests/scripts/core-setup.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:core.setup" + +set -e + +cd ${BUILD_CORE_DIR} + +if [[ ${CORE_TYPE} == 'owncloud' ]]; then composer install -o --prefer-dist --no-suggest --no-interaction; fi + +# Set up core +php -f occ maintenance:install --database-name oc_autotest --database-user oc_autotest --admin-user admin --admin-pass admin --database ${DB} --database-pass='' + +# Set up app +php -f occ app:enable registration + +# Enable app twice to check occ errors of registered commands +php -f occ app:enable registration + +echo "travis_fold:end:core.setup" diff --git a/tests/scripts/lint-appinfo.sh b/tests/scripts/lint-appinfo.sh new file mode 100755 index 0000000..89fb3c1 --- /dev/null +++ b/tests/scripts/lint-appinfo.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:lint.appinfo" + +set -e + +cd ${BUILD_APP_MODULE_DIR} + +wget -nv https://apps.nextcloud.com/schema/apps/info.xsd +xmllint appinfo/info.xml --schema info.xsd --noout +rm info.xsd + +echo "travis_fold:end:lint.appinfo" diff --git a/tests/scripts/lint-php.sh b/tests/scripts/lint-php.sh new file mode 100755 index 0000000..afa2a6e --- /dev/null +++ b/tests/scripts/lint-php.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:lint.php" + +set -e + +cd ${BUILD_APP_MODULE_DIR} + +find . -name '*.php' -type f -not -path './vendor/*' -print0 | xargs --no-run-if-empty -0 -n1 -P8 php -l -d display_errors -d display_startup_errors 1>/dev/null + +echo "travis_fold:end:lint.php" diff --git a/tests/scripts/phpunit.sh b/tests/scripts/phpunit.sh new file mode 100755 index 0000000..0f92356 --- /dev/null +++ b/tests/scripts/phpunit.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +echo "travis_fold:start:phpunit" + +set -e + +cd ${BUILD_APP_MODULE_DIR} + +composer install --prefer-dist --no-interaction -o +vendor/bin/phpunit -c tests/phpunit.xml + +echo "travis_fold:end:phpunit" diff --git a/tests/unit/controller/ApiControllerTest.php b/tests/unit/controller/ApiControllerTest.php index c466e10..0db35a4 100644 --- a/tests/unit/controller/ApiControllerTest.php +++ b/tests/unit/controller/ApiControllerTest.php @@ -14,11 +14,9 @@ namespace OCA\Registration\Controller; use OCA\Registration\Db\Registration; use OCA\Registration\Service\MailService; use OCA\Registration\Service\RegistrationService; -use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Registration\Util\CoreBridge; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\OCS\OCSException; -use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\Defaults; use OCP\IL10N; use OCP\IRequest; @@ -80,58 +78,58 @@ class ApiControllerTest extends TestCase { $this->assertEquals($expected, $actual); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSException - * @expectedExceptionCode 999 - */ public function testValidateFailEmail() { + $exception = CoreBridge::createException('OCSException', '', 999); + + $this->expectException(get_class($exception)); + $this->registrationService ->expects($this->once()) ->method('validateEmail') - ->willThrowException(new OCSException('', 999)); + ->willThrowException($exception); + $this->controller->validate('user1', 'user test', 'test@example.com'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSException - * @expectedExceptionCode 999 - */ public function testValidateFailDisplayname() { - $this->registrationService + $exception = CoreBridge::createException('OCSException', '', 999); + + $this->expectException(get_class($exception)); + + $this->registrationService ->expects($this->once()) ->method('validateDisplayname') - ->willThrowException(new OCSException('', 999)); + ->willThrowException($exception); + $this->controller->validate('user1', 'user test', 'test@example.com'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSException - * @expectedExceptionCode 999 - */ public function testValidateFailUsername() { - $this->registrationService + $exception = CoreBridge::createException('OCSException', '', 999); + + $this->expectException(get_class($exception)); + + $this->registrationService ->expects($this->once()) ->method('validateUsername') - ->willThrowException(new OCSException('', 999)); + ->willThrowException($exception); + $this->controller->validate('user1', 'user test', 'test@example.com'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionCode 404 - */ public function testStatusNoRegistration() { + $exception = CoreBridge::createException('OCSNotFoundException', '', 404); + + $this->expectException(get_class($exception)); + $this->registrationService ->method('getRegistrationForSecret') ->with('L2qdLAtrJTx499ErjwkwnZqGmLdm3Acp') - ->willThrowException(new DoesNotExistException('')); + ->willThrowException($exception); + $this->controller->status('L2qdLAtrJTx499ErjwkwnZqGmLdm3Acp'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSException - * @expectedExceptionCode 403 - */ public function testStatusPendingRegistration() { $registration = new Registration(); $registration->setEmailConfirmed(false); @@ -139,7 +137,18 @@ class ApiControllerTest extends TestCase { ->method('getRegistrationForSecret') ->with('L2qdLAtrJTx499ErjwkwnZqGmLdm3Acp') ->willReturn($registration); + $actual = $this->controller->status('L2qdLAtrJTx499ErjwkwnZqGmLdm3Acp'); + + $expected = new DataResponse( + [ + 'registrationStatus' => 1, + 'message' => $this->l10n->t('Your registration is pending. Please confirm your email address.'), + ], + Http::STATUS_OK + ); + + $this->assertEquals($expected, $actual); } public function testStatusConfirmedRegistration() { @@ -163,12 +172,11 @@ class ApiControllerTest extends TestCase { ->expects($this->once()) ->method('generateAppPassword'); $actual = $this->controller->status('mysecret'); - $expected = new DataResponse([]); + $expected = new DataResponse([ + 'appPassword' => null, + 'cloudUrl' => $this->defaults->getBaseUrl(), + 'registrationStatus' => 0, + ]); $this->assertEquals($expected, $actual); } - - public function testStatusConfirmedRegistrationWithSecret() { - - } - } diff --git a/util/corebridge.php b/util/corebridge.php new file mode 100644 index 0000000..2eee5dd --- /dev/null +++ b/util/corebridge.php @@ -0,0 +1,77 @@ +<?php + +namespace OCA\Registration\Util; + +class CoreBridge +{ + /** + * @param string $className + * @return string + * @throws \LogicException + */ + public static function exceptionClass($className) + { + static $classes = [ + 'OCSException' => [ + 'nextcloud' => 'OCP\AppFramework\OCS\OCSException', + 'owncloud' => 'OC\OCS\Exception', + ], + 'OCSBadRequestException' => [ + 'nextcloud' => 'OCP\AppFramework\OCS\OCSBadRequestException', + 'owncloud' => 'OC\OCS\Exception', + ], + 'OCSNotFoundException' => [ + 'nextcloud' => 'OCP\AppFramework\OCS\OCSNotFoundException', + 'owncloud' => 'OC\OCS\Exception', + ], + 'DoesNotExistException' => [ + 'nextcloud' => 'OCP\AppFramework\Db\DoesNotExistException', + 'owncloud' => 'OCP\AppFramework\Db\DoesNotExistException', + ], + ]; + + if (!array_key_exists($className, $classes)) { + throw new \LogicException('No valid exception class found'); + } + + foreach ($classes[$className] as $class) { + if (class_exists($class)) { + return $class; + } + } + + throw new \LogicException('No valid exception class found'); + } + + /** + * @param string $className + * @param null|string $message + * @param null|int $code + * @return \Exception + */ + public static function createException($className, $message = null, $code = null) + { + $exceptionClassName = self::exceptionClass($className); + + $reflection = new \ReflectionClass($exceptionClassName); + $params = $reflection->getConstructor()->getParameters(); + + if ($params[0]->getClass() && $params[0]->getClass()->getName() === 'OC\OCS\Result') { + return new $exceptionClassName(new \OC\OCS\Result($message, $code)); + } + + if (count($params) >= 2) { + if ($params[1]->getClass() && $params[1]->getClass()->getName() === 'Exception') { + return new $exceptionClassName($message); + } + + return new $exceptionClassName($message, $code); + } + + if ($exceptionClassName === 'OCP\AppFramework\OCS\OCSNotFoundException') { + return new $exceptionClassName($message); + } + + return new $exceptionClassName(); + } +} |