diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 73785c6..c5c9309 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -26,8 +26,7 @@ namespace OCA\Registration\AppInfo; use OCP\AppFramework\App; class Application extends App { - - public function __construct(array $urlParams = array()) { + public function __construct(array $urlParams = []) { parent::__construct('registration', $urlParams); $container = $this->getContainer(); @@ -39,5 +38,4 @@ class Application extends App { $container->registerCapability(\OCA\Registration\Capabilities::class); } } - } diff --git a/lib/Capabilities.php b/lib/Capabilities.php index 8234245..2403247 100644 --- a/lib/Capabilities.php +++ b/lib/Capabilities.php @@ -23,7 +23,6 @@ namespace OCA\Registration; -use OCP\Capabilities\ICapability; use OCP\Capabilities\IPublicCapability; use OCP\IURLGenerator; @@ -47,5 +46,4 @@ class Capabilities implements IPublicCapability { ] ]; } - } diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index c11732d..c7847e9 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -47,9 +47,9 @@ class ApiController extends OCSController { /** @var Defaults */ private $defaults; - const REGISTRATION_STATUS_COMPLETE = 0; - const REGISTRATION_STATUS_PENDING = 1; - const REGISTRATION_STATUS_EXISTING = 2; + public const REGISTRATION_STATUS_COMPLETE = 0; + public const REGISTRATION_STATUS_PENDING = 1; + public const REGISTRATION_STATUS_EXISTING = 2; public function __construct($appName, IRequest $request, @@ -80,7 +80,7 @@ class ApiController extends OCSController { $this->registrationService->validateDisplayname($displayname); $this->registrationService->validateUsername($username); } catch (RegistrationException $e) { - throw CoreBridge::createException('OCSBadRequestException', $e->getMessage()); + throw CoreBridge::createException('OCSBadRequestException', $e->getMessage()); } $data = [ 'username' => $username, @@ -103,7 +103,7 @@ class ApiController extends OCSController { /** @var Registration $registration */ $registration = $this->registrationService->getRegistrationForSecret($clientSecret); } catch (DoesNotExistException $e) { - throw CoreBridge::createException('OCSNotFoundException', 'No pending registration.'); + throw CoreBridge::createException('OCSNotFoundException', 'No pending registration.'); } if (!$registration->getEmailConfirmed()) { @@ -148,7 +148,7 @@ class ApiController extends OCSController { try { $secret = null; $registration = $this->registrationService->validateEmail($email); - if($registration === true) { + if ($registration === true) { $this->registrationService->validateDisplayname($displayname); $this->registrationService->validateUsername($username); $registration = $this->registrationService->createRegistration($email, $username, $password, $displayname); @@ -168,12 +168,12 @@ class ApiController extends OCSController { $data['message'] = $this->l10n->t('Your registration is pending. Please confirm your email address.'); $data['registrationStatus'] = self::REGISTRATION_STATUS_PENDING; - if($secret !== null) { + if ($secret !== null) { $data['secret'] = $secret; } return new DataResponse($data, Http::STATUS_OK); } catch (RegistrationException $exception) { - throw CoreBridge::createException('OCSException', $exception->getMessage(), $exception->getCode()); + throw CoreBridge::createException('OCSException', $exception->getMessage(), $exception->getCode()); } } } diff --git a/lib/Controller/RegisterController.php b/lib/Controller/RegisterController.php index a401325..dfe82bb 100644 --- a/lib/Controller/RegisterController.php +++ b/lib/Controller/RegisterController.php @@ -42,7 +42,7 @@ class RegisterController extends Controller { IURLGenerator $urlgenerator, RegistrationService $registrationService, MailService $mailService - ){ + ) { parent::__construct($appName, $request); $this->l10n = $l10n; $this->urlgenerator = $urlgenerator; @@ -59,10 +59,10 @@ class RegisterController extends Controller { * @return TemplateResponse */ public function askEmail($errormsg, $entered) { - $params = array( + $params = [ 'errormsg' => $errormsg ? $errormsg : $this->request->getParam('errormsg'), 'entered' => $entered ? $entered : $this->request->getParam('entered') - ); + ]; return new TemplateResponse('registration', 'register', $params, 'guest'); } @@ -82,26 +82,26 @@ class RegisterController extends Controller { } try { $reg = $this->registrationService->validateEmail($email); - if ( $reg === true ) { + if ($reg === true) { $registration = $this->registrationService->createRegistration($email); $this->mailService->sendTokenByMail($registration); } else { $this->registrationService->generateNewToken($reg); $this->mailService->sendTokenByMail($reg); - return new TemplateResponse('registration', 'message', array('msg' => + return new TemplateResponse('registration', 'message', ['msg' => $this->l10n->t('There is already a pending registration with this email, a new verification email has been sent to the address.') - ), 'guest'); + ], 'guest'); } } catch (RegistrationException $e) { - return new TemplateResponse('registration', 'message', array('msg' => + return new TemplateResponse('registration', 'message', ['msg' => $e->getMessage().'
'.$e->getHint() - ), 'guest'); + ], 'guest'); } - return new TemplateResponse('registration', 'message', array('msg' => + return new TemplateResponse('registration', 'message', ['msg' => $this->l10n->t('Verification email successfully sent.') - ), 'guest'); + ], 'guest'); } /** @@ -130,7 +130,6 @@ class RegisterController extends Controller { } catch (RegistrationException $exception) { return $this->renderError($exception->getMessage(), $exception->getHint()); } - } /** @@ -152,8 +151,8 @@ class RegisterController extends Controller { return new TemplateResponse('registration', 'form', [ 'email' => $registration->getEmail(), - 'entered_data' => array('user' => $username), - 'errormsgs' => array($exception->getMessage()), + 'entered_data' => ['user' => $username], + 'errormsgs' => [$exception->getMessage()], 'token' => $token ], 'guest'); } @@ -166,18 +165,17 @@ class RegisterController extends Controller { return new TemplateResponse( 'registration', 'message', - array('msg' => $this->l10n->t("Your account has been successfully created, but it still needs approval from an administrator.")), + ['msg' => $this->l10n->t("Your account has been successfully created, but it still needs approval from an administrator.")], 'guest'); } } private function renderError($error, $hint="") { - return new TemplateResponse('', 'error', array( - 'errors' => array(array( + return new TemplateResponse('', 'error', [ + 'errors' => [[ 'error' => $error, 'hint' => $hint - )) - ), 'error'); + ]] + ], 'error'); } - } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 0252317..a487cf5 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -32,7 +32,7 @@ class SettingsController extends Controller { /** @var string */ protected $appName; - public function __construct($appName, IRequest $request, IL10N $l10n, IConfig $config, IGroupManager $groupmanager){ + public function __construct($appName, IRequest $request, IL10N $l10n, IConfig $config, IGroupManager $groupmanager) { parent::__construct($appName, $request); $this->l10n = $l10n; $this->config = $config; @@ -47,14 +47,14 @@ class SettingsController extends Controller { * * @param string $registered_user_group all newly registered user will be put in this group * @param string $allowed_domains Registrations are only allowed for E-Mailadresses with these domains - * @param bool $admin_approval_required newly registered users have to be validated by an admin + * @param bool $admin_approval_required newly registered users have to be validated by an admin * @return DataResponse */ public function admin($registered_user_group, $allowed_domains, $admin_approval_required) { // handle domains - if ( ( $allowed_domains==='' ) || ( $allowed_domains === NULL ) ){ + if (($allowed_domains==='') || ($allowed_domains === null)) { $this->config->deleteAppValue($this->appName, 'allowed_domains'); - }else{ + } else { $this->config->setAppValue($this->appName, 'allowed_domains', $allowed_domains); } @@ -63,34 +63,34 @@ class SettingsController extends Controller { // handle groups $groups = $this->groupmanager->search(''); - $group_id_list = array(); - foreach ( $groups as $group ) { + $group_id_list = []; + foreach ($groups as $group) { $group_id_list[] = $group->getGid(); } - if ( $registered_user_group === 'none' ) { + if ($registered_user_group === 'none') { $this->config->deleteAppValue($this->appName, 'registered_user_group'); - return new DataResponse(array( - 'data' => array( + return new DataResponse([ + 'data' => [ 'message' => (string) $this->l10n->t('Saved'), - ), + ], 'status' => 'success' - )); - } else if ( in_array($registered_user_group, $group_id_list) ) { + ]); + } elseif (in_array($registered_user_group, $group_id_list)) { $this->config->setAppValue($this->appName, 'registered_user_group', $registered_user_group); - return new DataResponse(array( - 'data' => array( + return new DataResponse([ + 'data' => [ 'message' => (string) $this->l10n->t('Saved'), - ), + ], 'status' => 'success' - )); + ]); } else { - return new DataResponse(array( - 'data' => array( + return new DataResponse([ + 'data' => [ 'message' => (string) $this->l10n->t('No such group'), - ), + ], 'status' => 'error' - ), Http::STATUS_NOT_FOUND); + ], Http::STATUS_NOT_FOUND); } } /** @@ -102,7 +102,7 @@ class SettingsController extends Controller { // handle groups $groups = $this->groupmanager->search(''); $group_id_list = []; - foreach ( $groups as $group ) { + foreach ($groups as $group) { $group_id_list[] = $group->getGid(); } $current_value = $this->config->getAppValue($this->appName, 'registered_user_group', 'none'); diff --git a/lib/Db/Registration.php b/lib/Db/Registration.php index 9d8473f..f9899b4 100644 --- a/lib/Db/Registration.php +++ b/lib/Db/Registration.php @@ -26,7 +26,6 @@ namespace OCA\Registration\Db; use OCP\AppFramework\Db\Entity; class Registration extends Entity { - public $id; protected $email; protected $username; @@ -40,5 +39,4 @@ class Registration extends Entity { public function __construct() { $this->addType('emailConfirmed', 'boolean'); } - -} \ No newline at end of file +} diff --git a/lib/Db/RegistrationMapper.php b/lib/Db/RegistrationMapper.php index 80b6308..6f8fcb9 100644 --- a/lib/Db/RegistrationMapper.php +++ b/lib/Db/RegistrationMapper.php @@ -97,5 +97,4 @@ class RegistrationMapper extends Mapper { //FIXME eqivalent to ISecureRandom::CHAR_HUMAN_READABLE introduced in https://github.com/nextcloud/server/commit/f2a2b34e4639e88f8d948a388a51f010212b42a3 but not supported in ownCloud yet. We'll just use the string for now then switch to constants when supported. $registration->setClientSecret($token); } - } diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index c7ad07f..cf15dc7 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -68,7 +68,7 @@ class MailService { * @throws RegistrationException */ public function validateEmail($email) { - if ( !$this->mailer->validateMailAddress($email) ) { + if (!$this->mailer->validateMailAddress($email)) { throw new RegistrationException($this->l10n->t('The email address you entered is not valid')); } } @@ -79,7 +79,7 @@ class MailService { * @throws RegistrationException */ public function sendTokenByMail(Registration $registration) { - $link = $this->urlGenerator->linkToRoute('registration.register.verifyToken', array('token' => $registration->getToken())); + $link = $this->urlGenerator->linkToRoute('registration.register.verifyToken', ['token' => $registration->getToken()]); $link = $this->urlGenerator->getAbsoluteURL($link); $template_var = [ 'link' => $link, @@ -99,7 +99,7 @@ class MailService { $message->setPlainBody($plaintext_part); $message->setHtmlBody($html_part); $failed_recipients = $this->mailer->send($message); - if ( !empty($failed_recipients) ) { + if (!empty($failed_recipients)) { throw new RegistrationException($this->l10n->t('A problem occurred sending email, please contact your administrator.')); } } @@ -125,10 +125,10 @@ class MailService { } } - $to_arr = array(); - foreach ( $admin_users as $au ) { + $to_arr = []; + foreach ($admin_users as $au) { $au_email = $au->getEMailAddress(); - if ( $au_email && $au->isEnabled()) { + if ($au_email && $au->isEnabled()) { $to_arr[$au_email] = $au->getDisplayName(); } } @@ -147,7 +147,7 @@ class MailService { * @throws \Exception */ private function sendNewUserNotifEmail(array $to, $username, $userIsEnabled) { - if ( $this->config->getAppValue('core', 'vendor', '') === 'nextcloud' ) { + if ($this->config->getAppValue('core', 'vendor', '') === 'nextcloud') { $link = $this->urlGenerator->linkToRouteAbsolute('settings.Users.usersList'); } else { $link = $this->urlGenerator->linkToRouteAbsolute('user_management.users'); @@ -182,8 +182,8 @@ class MailService { $message->setPlainBody($plaintext_part); $message->setHtmlBody($html_part); $failed_recipients = $this->mailer->send($message); - if ( !empty($failed_recipients) ) + if (!empty($failed_recipients)) { throw new RegistrationException('Failed recipients: '.print_r($failed_recipients, true)); + } } - } diff --git a/lib/Service/RegistrationException.php b/lib/Service/RegistrationException.php index b3070d1..deabe6c 100644 --- a/lib/Service/RegistrationException.php +++ b/lib/Service/RegistrationException.php @@ -24,7 +24,6 @@ namespace OCA\Registration\Service; class RegistrationException extends \Exception { - protected $hint; /** @@ -45,5 +44,4 @@ class RegistrationException extends \Exception { public function getHint() { return $this->hint; } - -} \ No newline at end of file +} diff --git a/lib/Service/RegistrationService.php b/lib/Service/RegistrationService.php index 7a0e3fc..592801b 100644 --- a/lib/Service/RegistrationService.php +++ b/lib/Service/RegistrationService.php @@ -87,7 +87,7 @@ class RegistrationService { public function __construct($appName, MailService $mailService, IL10N $l10n, IURLGenerator $urlGenerator, RegistrationMapper $registrationMapper, IUserManager $userManager, IConfig $config, IGroupManager $groupManager, Defaults $defaults, - ISecureRandom $random, IUserSession $us, IRequest $request, ILogger $logger, ISession $session, IProvider $tokenProvider, ICrypto $crypto){ + ISecureRandom $random, IUserSession $us, IRequest $request, ILogger $logger, ISession $session, IProvider $tokenProvider, ICrypto $crypto) { $this->appName = $appName; $this->mailService = $mailService; $this->l10n = $l10n; @@ -134,13 +134,14 @@ class RegistrationService { $registration->setEmail($email); $registration->setUsername($username); $registration->setDisplayname($displayname); - if($password !== "") { + if ($password !== "") { $password = $this->crypto->encrypt($password); $registration->setPassword($password); } $this->registrationMapper->generateNewToken($registration); - if ( $password !== '' && $username !== '' ) + if ($password !== '' && $username !== '') { $this->registrationMapper->generateClientSecret($registration); + } $this->registrationMapper->insert($registration); return $registration; } @@ -151,15 +152,15 @@ class RegistrationService { * @throws RegistrationException */ public function validateEmail($email) { - $this->mailService->validateEmail($email); // check for pending registrations try { return $this->registrationMapper->find($email);//if not found DB will throw a exception - } catch (DoesNotExistException $e) {} + } catch (DoesNotExistException $e) { + } - if ( $this->userManager->getByEmail($email) ) { + if ($this->userManager->getByEmail($email)) { throw new RegistrationException( $this->l10n->t('A user has already taken this email, maybe you already have an account?'), $this->l10n->t('You can log in now.', [$this->urlGenerator->getAbsoluteURL('/')]) @@ -182,7 +183,7 @@ class RegistrationService { * @throws RegistrationException */ public function validateDisplayname($displayname) { - if($displayname === "") { + if ($displayname === "") { throw new RegistrationException($this->l10n->t('Please provide a valid display name.')); } } @@ -192,11 +193,11 @@ class RegistrationService { * @throws RegistrationException */ public function validateUsername($username) { - if($username === "") { + if ($username === "") { throw new RegistrationException($this->l10n->t('Please provide a valid user name.')); } - if($this->registrationMapper->usernameIsPending($username) || $this->userManager->get($username) !== null) { + if ($this->registrationMapper->usernameIsPending($username) || $this->userManager->get($username) !== null) { throw new RegistrationException($this->l10n->t('The username you have chosen already exists.')); } } @@ -209,7 +210,7 @@ class RegistrationService { */ public function checkAllowedDomains($email) { $allowed_domains = $this->config->getAppValue($this->appName, 'allowed_domains', ''); - if ( $allowed_domains !== '' ) { + if ($allowed_domains !== '') { $allowed_domains = explode(';', $allowed_domains); $allowed = false; foreach ($allowed_domains as $domain) { @@ -257,7 +258,7 @@ class RegistrationService { * @throws RegistrationException|\InvalidTokenException */ public function createAccount(Registration &$registration, $username = null, $password = null) { - if($password === null && $registration->getPassword() === null) { + if ($password === null && $registration->getPassword() === null) { $generatedPassword = $this->generateRandomDeviceToken(); $registration->setPassword($this->crypto->encrypt($generatedPassword)); } @@ -266,7 +267,7 @@ class RegistrationService { $username = $registration->getUsername(); } - if($registration->getPassword() !== null) { + if ($registration->getPassword() !== null) { $password = $this->crypto->decrypt($registration->getPassword()); } @@ -293,9 +294,9 @@ class RegistrationService { // Add user to group $registered_user_group = $this->config->getAppValue($this->appName, 'registered_user_group', 'none'); - if ( $registered_user_group !== 'none' ) { + if ($registered_user_group !== 'none') { $group = $this->groupManager->get($registered_user_group); - if ( $group === null ) { + if ($group === null) { // This might happen if $registered_user_group is deleted after setting the value // Here I choose to log error instead of stopping the user to register $this->logger->error("You specified newly registered users be added to '$registered_user_group' group, but it does not exist."); @@ -318,7 +319,7 @@ class RegistrationService { // with client secret implies registered via API // without client secret implies registered via form // if registered via API, the registration request will be deleted in apicontroller::status - if($registration->getClientSecret() === null) { + if ($registration->getClientSecret() === null) { $res = $this->registrationMapper->delete($registration); if ($res === false) { throw new RegistrationException($this->l10n->t('Failed to delete pending registration request')); @@ -417,7 +418,7 @@ class RegistrationService { if ($decrypt) { $password = $this->crypto->decrypt($password); } - if ( method_exists($this->usersession, 'createSessionToken') ) { + if (method_exists($this->usersession, 'createSessionToken')) { $this->usersession->login($username, $password); $this->usersession->createSessionToken($this->request, $userId, $username, $password); return new RedirectResponse($this->urlGenerator->linkTo('', 'index.php')); @@ -427,14 +428,13 @@ class RegistrationService { $logintoken = $this->random->generate(32); $this->config->setUserValue($userId, 'login_token', $logintoken, time()); \OC_User::setMagicInCookie($userId, $logintoken); - \OC_Util::redirectToDefaultPage(); + \OC_Util::redirectToDefaultPage(); } // Render message in case redirect failed return new TemplateResponse('registration', 'message', ['msg' => $this->l10n->t('Your account has been successfully created, you can log in now.', [$this->urlGenerator->getAbsoluteURL('/')])] , 'guest' ); - } /** @@ -451,5 +451,4 @@ class RegistrationService { } } } - } diff --git a/lib/Settings/RegistrationSettings.php b/lib/Settings/RegistrationSettings.php index 92a9363..bbb36fb 100644 --- a/lib/Settings/RegistrationSettings.php +++ b/lib/Settings/RegistrationSettings.php @@ -1,7 +1,7 @@ getSection(); } } - -?> diff --git a/lib/Util/CoreBridge.php b/lib/Util/CoreBridge.php index 5fc2124..3875dd5 100644 --- a/lib/Util/CoreBridge.php +++ b/lib/Util/CoreBridge.php @@ -2,80 +2,77 @@ namespace OCA\Registration\Util; -class CoreBridge -{ - /** - * This function maps an exception class to the available exception class of the core - * in order to provide cross-core and cross-version compatibility. - * - * @param string $className - * @return string - * @throws \LogicException - */ - public static function exceptionClass($className) - { - static $classes = [ - 'OCSException' => [ - 'OCP\AppFramework\OCS\OCSException', - 'OC\OCS\Exception', - ], - 'OCSBadRequestException' => [ - 'OCP\AppFramework\OCS\OCSBadRequestException', - 'OC\OCS\Exception', - ], - 'OCSNotFoundException' => [ - 'OCP\AppFramework\OCS\OCSNotFoundException', - 'OC\OCS\Exception', - ], - 'DoesNotExistException' => [ - 'OCP\AppFramework\Db\DoesNotExistException', - 'OCP\AppFramework\Db\DoesNotExistException', - ], - ]; +class CoreBridge { + /** + * This function maps an exception class to the available exception class of the core + * in order to provide cross-core and cross-version compatibility. + * + * @param string $className + * @return string + * @throws \LogicException + */ + public static function exceptionClass($className) { + static $classes = [ + 'OCSException' => [ + 'OCP\AppFramework\OCS\OCSException', + 'OC\OCS\Exception', + ], + 'OCSBadRequestException' => [ + 'OCP\AppFramework\OCS\OCSBadRequestException', + 'OC\OCS\Exception', + ], + 'OCSNotFoundException' => [ + 'OCP\AppFramework\OCS\OCSNotFoundException', + 'OC\OCS\Exception', + ], + 'DoesNotExistException' => [ + 'OCP\AppFramework\Db\DoesNotExistException', + 'OCP\AppFramework\Db\DoesNotExistException', + ], + ]; - if (!array_key_exists($className, $classes)) { - throw new \LogicException('No valid exception class found'); - } + 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; - } - } + foreach ($classes[$className] as $class) { + if (class_exists($class)) { + return $class; + } + } - throw new \LogicException('No valid exception class found'); - } + 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); + /** + * @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(); + $reflection = new \ReflectionClass($exceptionClassName); + $params = $reflection->getConstructor()->getParameters(); - if ($params[0]->getClass() && ($params[0]->getClass()->getName() === 'OC\OCS\Result' || $params[0]->getClass()->getName() === 'OC_OCS_Result')) { - $subClass = $params[0]->getClass()->getName(); - return new $exceptionClassName(new $subClass($message, $code)); - } + if ($params[0]->getClass() && ($params[0]->getClass()->getName() === 'OC\OCS\Result' || $params[0]->getClass()->getName() === 'OC_OCS_Result')) { + $subClass = $params[0]->getClass()->getName(); + return new $exceptionClassName(new $subClass($message, $code)); + } - if (count($params) >= 2) { - if ($params[1]->getClass() && $params[1]->getClass()->getName() === 'Exception') { - return new $exceptionClassName($message); - } + if (count($params) >= 2) { + if ($params[1]->getClass() && $params[1]->getClass()->getName() === 'Exception') { + return new $exceptionClassName($message); + } - return new $exceptionClassName($message, $code); - } + return new $exceptionClassName($message, $code); + } - if ($exceptionClassName === 'OCP\AppFramework\OCS\OCSNotFoundException') { - return new $exceptionClassName($message); - } + if ($exceptionClassName === 'OCP\AppFramework\OCS\OCSNotFoundException') { + return new $exceptionClassName($message); + } - return new $exceptionClassName(); - } + return new $exceptionClassName(); + } } diff --git a/templates/admin.php b/templates/admin.php index e778d93..faf6ff4 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -8,7 +8,7 @@ script('registration', 'settings'); > + >

diff --git a/templates/domains.php b/templates/domains.php index 08cb3dd..111ed9e 100644 --- a/templates/domains.php +++ b/templates/domains.php @@ -4,7 +4,7 @@