diff options
author | Maxence Lange <maxence@pontapreta.net> | 2017-02-09 00:31:06 +0300 |
---|---|---|
committer | Maxence Lange <maxence@pontapreta.net> | 2017-02-09 00:31:06 +0300 |
commit | 1b6695910252dee39838df31ef07eaa272d54c05 (patch) | |
tree | 852ba0e322d09478b342fe3906145721590b45cb /lib | |
parent | 8929d60a1c2cb37fa07c288bdb0d6a63eed11587 (diff) |
Code Style.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AppInfo/Application.php | 143 | ||||
-rw-r--r-- | lib/Controller/OrcidController.php | 163 | ||||
-rw-r--r-- | lib/Controller/SettingsController.php | 194 | ||||
-rw-r--r-- | lib/Service/ConfigService.php | 202 | ||||
-rw-r--r-- | lib/Service/MiscService.php | 36 | ||||
-rw-r--r-- | lib/admin.php | 31 | ||||
-rw-r--r-- | lib/personal.php | 31 |
7 files changed, 441 insertions, 359 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 100e72d..8e12a84 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -1,6 +1,6 @@ <?php - /** + * * Orcid - based on user_orcid from Lars Naesbye Christensen * * This file is licensed under the Affero General Public License version 3 or @@ -25,6 +25,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ + namespace OCA\Orcid\AppInfo; use \OCA\Orcid\Controller\SettingsController; @@ -34,65 +35,91 @@ use \OCA\Orcid\Service\MiscService; use OCP\AppFramework\App; use OCP\Util; -class Application extends App -{ +class Application extends App { + + /** + * + * @param array $params + */ + public function __construct(array $params = array()) { + parent::__construct('orcid', $params); + $container = $this->getContainer(); + + /** + * Controllers + */ + $container->registerService( + 'MiscService', function ($c) { + return new MiscService($c->query('Logger'), $c->query('AppName')); + } + ); + + $container->registerService( + 'ConfigService', function ($c) { + return new ConfigService( + $c->query('AppName'), $c->query('CoreConfig'), $c->query('UserId'), + $c->query('MiscService') + ); + } + ); + + $container->registerService( + 'SettingsController', function ($c) { + return new SettingsController( + $c->query('AppName'), $c->query('Request'), $c->query('ConfigService'), + $c->query('MiscService') + ); + } + ); + + $container->registerService( + 'OrcidController', function ($c) { + return new OrcidController( + $c->query('AppName'), $c->query('Request'), $c->query('ConfigService'), + $c->query('MiscService') + ); + } + ); + + /** + * Core + */ + $container->registerService( + 'Logger', function ($c) { + return $c->query('ServerContainer') + ->getLogger(); + } + ); + $container->registerService( + 'CoreConfig', function ($c) { + return $c->query('ServerContainer') + ->getConfig(); + } + ); + + $container->registerService( + 'UserId', function ($c) { + $user = $c->query('ServerContainer') + ->getUserSession() + ->getUser(); - /** - * - * @param array $params - */ - public function __construct(array $params = array()) - { - parent::__construct('orcid', $params); - $container = $this->getContainer(); - - /** - * Controllers - */ - $container->registerService('MiscService', function ($c) { - return new MiscService($c->query('Logger'), $c->query('AppName')); - }); - - $container->registerService('ConfigService', function ($c) { - return new ConfigService($c->query('AppName'), $c->query('CoreConfig'), $c->query('UserId'), $c->query('MiscService')); - }); - - $container->registerService('SettingsController', function ($c) { - return new SettingsController($c->query('AppName'), $c->query('Request'), $c->query('ConfigService'), $c->query('MiscService')); - }); - - $container->registerService('OrcidController', function ($c) { - return new OrcidController($c->query('AppName'), $c->query('Request'), $c->query('ConfigService'), $c->query('MiscService')); - }); - - /** - * Core - */ - $container->registerService('Logger', function ($c) { - return $c->query('ServerContainer') - ->getLogger(); - }); - $container->registerService('CoreConfig', function ($c) { - return $c->query('ServerContainer') - ->getConfig(); - }); - - $container->registerService('UserId', function ($c) { - $user = $c->query('ServerContainer') - ->getUserSession() - ->getUser(); - return is_null($user) ? '' : $user->getUID(); - }); - } + return is_null($user) ? '' : $user->getUID(); + } + ); + } - public function registerSettingsAdmin() - { - \OCP\App::registerAdmin($this->getContainer()->query('AppName'), 'lib/admin'); - } + public function registerSettingsAdmin() { + \OCP\App::registerAdmin( + $this->getContainer() + ->query('AppName'), 'lib/admin' + ); + } - public function registerSettingsPersonal() - { - \OCP\App::registerPersonal($this->getContainer()->query('AppName'), 'lib/personal'); - } + public function registerSettingsPersonal() { + \OCP\App::registerPersonal( + $this->getContainer() + ->query('AppName'), 'lib/personal' + ); + } } diff --git a/lib/Controller/OrcidController.php b/lib/Controller/OrcidController.php index 7598141..2fa0509 100644 --- a/lib/Controller/OrcidController.php +++ b/lib/Controller/OrcidController.php @@ -1,6 +1,6 @@ <?php - /** + * * Orcid - based on user_orcid from Lars Naesbye Christensen * * This file is licensed under the Affero General Public License version 3 or @@ -25,6 +25,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ + namespace OCA\Orcid\Controller; use \OCA\Orcid\Service\ConfigService; @@ -32,83 +33,87 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\TemplateResponse; use OCP\IRequest; -class OrcidController extends Controller -{ - - private $configService; - - private $miscService; - - public function __construct($appName, IRequest $request, ConfigService $configService, $miscService) - { - parent::__construct($appName, $request); - $this->configService = $configService; - $this->miscService = $miscService; - } - - public static function generateOrcidUrl() - { - $redirectURL = \OC::$server->getURlGenerator()->linkToRouteAbsolute('orcid.orcid.OrcidCode'); - return $redirectURL; - } - - /** - * @NoCSRFRequired - * @NoAdminRequired - */ - public function AboutOrcid() - { - return new TemplateResponse($this->appName, 'about', [], 'blank'); - } - - /** - * @NoCSRFRequired - * @NoAdminRequired - */ - public function OrcidCode() - { - $code = $_GET['code']; - // $this->miscService->log('Received code: ' . $code); - - $clientAppID = trim($this->configService->getAppValue('clientAppID')); - $clientSecret = trim($this->configService->getAppValue('clientSecret')); - - $redirectURL = self::generateOrcidUrl(); - - $url = "https://orcid.org/oauth/token"; - $content = "client_id=" . $clientAppID . "&" . "client_secret=" . $clientSecret . "&" . "grant_type=authorization_code&" . "code=" . $code . "&" . "redirect_uri=" . $redirectURL; - - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_HEADER, false); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, $content); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); - curl_setopt($curl, CURLOPT_UNRESTRICTED_AUTH, TRUE); - - $json_response = curl_exec($curl); - $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); - curl_close($curl); - if ($status === 0 || $status >= 300 || $json_response === null || $json_response === false) { - // $this->miscService->log('ERROR: bad ws response. ' . $json_response); - return false; - } else { - $response = json_decode($json_response, true); - } - - // $this->miscService->log('Got token: ' . serialize($response)); - - if (! empty($response) && ! empty($response['orcid'])) { - $this->configService->setUserValue('orcid', $response['orcid']); - $this->configService->setUserValue('access_token', $response['access_token']); - return new TemplateResponse($this->appName, 'thanks', [], 'blank'); - } else { - return false; - } - - return true; - } +class OrcidController extends Controller { + + private $configService; + + private $miscService; + + public function __construct( + $appName, IRequest $request, ConfigService $configService, $miscService + ) { + parent::__construct($appName, $request); + $this->configService = $configService; + $this->miscService = $miscService; + } + + public static function generateOrcidUrl() { + $redirectURL = \OC::$server->getURlGenerator() + ->linkToRouteAbsolute('orcid.orcid.OrcidCode'); + + return $redirectURL; + } + + /** + * @NoCSRFRequired + * @NoAdminRequired + */ + public function AboutOrcid() { + return new TemplateResponse($this->appName, 'about', [], 'blank'); + } + + /** + * @NoCSRFRequired + * @NoAdminRequired + */ + public function OrcidCode() { + $code = $_GET['code']; + // $this->miscService->log('Received code: ' . $code); + + $clientAppID = trim($this->configService->getAppValue('clientAppID')); + $clientSecret = trim($this->configService->getAppValue('clientSecret')); + + $redirectURL = self::generateOrcidUrl(); + + $url = "https://orcid.org/oauth/token"; + $content = "client_id=" . $clientAppID . "&" . "client_secret=" . $clientSecret . "&" + . "grant_type=authorization_code&" . "code=" . $code . "&" . "redirect_uri=" + . $redirectURL; + + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_HEADER, false); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $content); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl, CURLOPT_UNRESTRICTED_AUTH, true); + + $json_response = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + if ($status === 0 || $status >= 300 || $json_response === null + || $json_response === false + ) { + // $this->miscService->log('ERROR: bad ws response. ' . $json_response); + return false; + } else { + $response = json_decode($json_response, true); + } + + // $this->miscService->log('Got token: ' . serialize($response)); + + if (!empty($response) && !empty($response['orcid'])) { + $this->configService->setUserValue('orcid', $response['orcid']); + $this->configService->setUserValue('access_token', $response['access_token']); + + return new TemplateResponse($this->appName, 'thanks', [], 'blank'); + } else { + return false; + } + + return true; + } } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index fc04cea..f52cb00 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -1,6 +1,6 @@ <?php - /** + * * Orcid - based on user_orcid from Lars Naesbye Christensen * * This file is licensed under the Affero General Public License version 3 or @@ -25,6 +25,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ + namespace OCA\Orcid\Controller; use \OCA\Orcid\Service\ConfigService; @@ -33,102 +34,97 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\TemplateResponse; use OCP\IRequest; -class SettingsController extends Controller -{ - - private $configService; - - private $miscService; - - public function __construct($appName, IRequest $request, ConfigService $configService, $miscService) - { - parent::__construct($appName, $request); - $this->configService = $configService; - $this->miscService = $miscService; - } - - // - // Admin - // - - /** - * @NoCSRFRequired - */ - public function admin() - { - return new TemplateResponse($this->appName, 'settings.admin', [], 'blank'); - } - - public function getOrcidInfo() - { - $params = [ - 'clientAppID' => $this->configService->getAppValue('clientAppID'), - 'clientSecret' => $this->configService->getAppValue('clientSecret'), - 'redirUrl' => OrcidController::generateOrcidUrl() - ]; - - return $params; - } - - public function setOrcidInfo($client_app_id, $client_secret) - { - $this->configService->setAppValue('clientAppID', $client_app_id); - $this->configService->setAppValue('clientSecret', $client_secret); - - return $this->getOrcidInfo(); - } - - // - // Personal - // - - /** - * @NoCSRFRequired - * @NoAdminRequired - */ - public function personal() - { - // 'orcid_token' => $this->configService->getUserValue($this->userId, 'orcid_token') - $params = [ - 'orcid' => $this->configService->getUserValue('orcid') - ]; - - return new TemplateResponse($this->appName, 'settings.personal', $params, 'blank'); - } - - /** - * @NoAdminRequired - */ - public function getClient() - { - $redirectURL = OrcidController::generateOrcidUrl(); - - // 'clientSecret' => $this->configService->getAppValue('clientSecret'), - $params = [ - 'clientAppID' => $this->configService->getAppValue('clientAppID'), - 'redirectURL' => $redirectURL - ]; - - return $params; - } - - /** - * @NoAdminRequired - */ - public function getOrcid() - { - $params = [ - 'orcid' => $this->configService->getUserValue('orcid') - ]; - return $params; - } - - /** - * @NoAdminRequired - */ - public function setOrcid() - { - $this->configService->setUserValue('orcid', $_POST['orcid']); - return $this->getOrcid(); - } +class SettingsController extends Controller { + + private $configService; + + private $miscService; + + public function __construct( + $appName, IRequest $request, ConfigService $configService, $miscService + ) { + parent::__construct($appName, $request); + $this->configService = $configService; + $this->miscService = $miscService; + } + + // + // Admin + // + + /** + * @NoCSRFRequired + */ + public function admin() { + return new TemplateResponse($this->appName, 'settings.admin', [], 'blank'); + } + + public function getOrcidInfo() { + $params = [ + 'clientAppID' => $this->configService->getAppValue('clientAppID'), + 'clientSecret' => $this->configService->getAppValue('clientSecret'), + 'redirUrl' => OrcidController::generateOrcidUrl() + ]; + + return $params; + } + + public function setOrcidInfo($client_app_id, $client_secret) { + $this->configService->setAppValue('clientAppID', $client_app_id); + $this->configService->setAppValue('clientSecret', $client_secret); + + return $this->getOrcidInfo(); + } + + // + // Personal + // + + /** + * @NoCSRFRequired + * @NoAdminRequired + */ + public function personal() { + // 'orcid_token' => $this->configService->getUserValue($this->userId, 'orcid_token') + $params = [ + 'orcid' => $this->configService->getUserValue('orcid') + ]; + + return new TemplateResponse($this->appName, 'settings.personal', $params, 'blank'); + } + + /** + * @NoAdminRequired + */ + public function getClient() { + $redirectURL = OrcidController::generateOrcidUrl(); + + // 'clientSecret' => $this->configService->getAppValue('clientSecret'), + $params = [ + 'clientAppID' => $this->configService->getAppValue('clientAppID'), + 'redirectURL' => $redirectURL + ]; + + return $params; + } + + /** + * @NoAdminRequired + */ + public function getOrcid() { + $params = [ + 'orcid' => $this->configService->getUserValue('orcid') + ]; + + return $params; + } + + /** + * @NoAdminRequired + */ + public function setOrcid() { + $this->configService->setUserValue('orcid', $_POST['orcid']); + + return $this->getOrcid(); + } }
\ No newline at end of file diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 35b92c9..3438f32 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -1,6 +1,6 @@ <?php - /** + * * Orcid - based on user_orcid from Lars Naesbye Christensen * * This file is licensed under the Affero General Public License version 3 or @@ -25,108 +25,110 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ + namespace OCA\Orcid\Service; use \OCA\Orcid\Controller\SettingsController; use OCP\IConfig; -class ConfigService -{ - - private $defaults = [ - 'configured' => '0' - ]; - - private $appName; - - private $config; - - private $miscService; - - public function __construct($appName, IConfig $config, $userId, $miscService) - { - $this->appName = $appName; - $this->config = $config; - $this->userId = $userId; - $this->miscService = $miscService; - } - - /** - * Get a value by key - * - * @param string $key - * @return string - */ - public function getAppValue($key) - { - $defaultValue = null; - if (array_key_exists($key, $this->defaults)) - $defaultValue = $this->defaults[$key]; - return $this->config->getAppValue($this->appName, $key, $defaultValue); - } - - /** - * Set a value by key - * - * @param string $key - * @param string $value - * @return string - */ - public function setAppValue($key, $value) - { - return $this->config->setAppValue($this->appName, $key, $value); - } - - /** - * remove a key - * - * @param string $key - * @return string - */ - public function deleteAppValue($key) - { - return $this->config->deleteAppValue($this->appName, $key); - } - - /** - * Get a user value by key - * - * @param string $userId - * @param string $key - * @return string - */ - public function getUserValue($key) - { - return $this->config->getUserValue($this->userId, $this->appName, $key); - } - - /** - * Set a user value by key - * - * @param string $userId - * @param string $key - * @param string $value - * @return string - */ - public function setUserValue($key, $value) - { - return $this->config->setUserValue($this->userId, $this->appName, $key, $value); - } - - /** - * return the cloud version. - * if $complete is true, return a string x.y.z - * - * @param boolean $complete - * @return string|integer - */ - public function getCloudVersion($complete = false) - { - $ver = \OCP\Util::getVersion(); - - if ($complete) - return implode('.', $ver); - - return $ver[0]; - } +class ConfigService { + + private $defaults = [ + 'configured' => '0' + ]; + + private $appName; + + private $config; + + private $miscService; + + public function __construct($appName, IConfig $config, $userId, $miscService) { + $this->appName = $appName; + $this->config = $config; + $this->userId = $userId; + $this->miscService = $miscService; + } + + /** + * Get a value by key + * + * @param string $key + * + * @return string + */ + public function getAppValue($key) { + $defaultValue = null; + if (array_key_exists($key, $this->defaults)) { + $defaultValue = $this->defaults[$key]; + } + + return $this->config->getAppValue($this->appName, $key, $defaultValue); + } + + /** + * Set a value by key + * + * @param string $key + * @param string $value + * + * @return string + */ + public function setAppValue($key, $value) { + return $this->config->setAppValue($this->appName, $key, $value); + } + + /** + * remove a key + * + * @param string $key + * + * @return string + */ + public function deleteAppValue($key) { + return $this->config->deleteAppValue($this->appName, $key); + } + + /** + * Get a user value by key + * + * @param string $userId + * @param string $key + * + * @return string + */ + public function getUserValue($key) { + return $this->config->getUserValue($this->userId, $this->appName, $key); + } + + /** + * Set a user value by key + * + * @param string $userId + * @param string $key + * @param string $value + * + * @return string + */ + public function setUserValue($key, $value) { + return $this->config->setUserValue($this->userId, $this->appName, $key, $value); + } + + /** + * return the cloud version. + * if $complete is true, return a string x.y.z + * + * @param boolean $complete + * + * @return string|integer + */ + public function getCloudVersion($complete = false) { + $ver = \OCP\Util::getVersion(); + + if ($complete) { + return implode('.', $ver); + } + + return $ver[0]; + } } diff --git a/lib/Service/MiscService.php b/lib/Service/MiscService.php index 8a3d01d..c8bbb5c 100644 --- a/lib/Service/MiscService.php +++ b/lib/Service/MiscService.php @@ -1,6 +1,6 @@ <?php - /** + * * Orcid - based on user_orcid from Lars Naesbye Christensen * * This file is licensed under the Affero General Public License version 3 or @@ -25,30 +25,28 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ + namespace OCA\Orcid\Service; use OCP\ILogger; -class MiscService -{ +class MiscService { + + private $logger; - private $logger; + private $appName; - private $appName; + public function __construct(ILogger $logger, $appName) { + $this->logger = $logger; + $this->appName = $appName; + } - public function __construct(ILogger $logger, $appName) - { - $this->logger = $logger; - $this->appName = $appName; - } + public function log($message, $level = 2) { + $data = array( + 'app' => $this->appName, + 'level' => $level + ); - public function log($message, $level = 2) - { - $data = array( - 'app' => $this->appName, - 'level' => $level - ); - - $this->logger->log($level, $message, $data); - } + $this->logger->log($level, $message, $data); + } }
\ No newline at end of file diff --git a/lib/admin.php b/lib/admin.php index 0878ae2..8693215 100644 --- a/lib/admin.php +++ b/lib/admin.php @@ -1,10 +1,37 @@ <?php +/** + * + * Orcid - based on user_orcid from Lars Naesbye Christensen + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Lars Naesbye Christensen, DeIC + * @author Maxence Lange <maxence@pontapreta.net> + * @copyright 2017 + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + namespace OCA\Orcid; $app = new \OCA\Orcid\AppInfo\Application(); $response = $app->getContainer() - ->query('SettingsController') - ->admin(); + ->query('SettingsController') + ->admin(); return $response->render(); diff --git a/lib/personal.php b/lib/personal.php index 8acef7e..26f29af 100644 --- a/lib/personal.php +++ b/lib/personal.php @@ -1,11 +1,38 @@ <?php +/** + * + * Orcid - based on user_orcid from Lars Naesbye Christensen + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Lars Naesbye Christensen, DeIC + * @author Maxence Lange <maxence@pontapreta.net> + * @copyright 2017 + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + namespace OCA\Orcid; $app = new \OCA\Orcid\AppInfo\Application(); $response = $app->getContainer() - ->query('SettingsController') - ->personal(); + ->query('SettingsController') + ->personal(); return $response->render(); |