Skip to content

Commit

Permalink
pkp#229 Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed Apr 15, 2023
1 parent b8b0205 commit 0623ad8
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 248 deletions.
42 changes: 16 additions & 26 deletions OrcidProfileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace APP\plugins\generic\orcidProfile;


use APP\core\Application;
use APP\core\Request;
use APP\facades\Repo;
Expand All @@ -25,7 +24,6 @@
use Carbon\Carbon;
use Exception;
use PKP\core\Core;
use PKP\plugins\Hook;
use PKP\plugins\PluginRegistry;
use PKP\security\authorization\PKPSiteAccessPolicy;
use PKP\security\authorization\UserRequiredPolicy;
Expand All @@ -39,8 +37,6 @@ class OrcidProfileHandler extends Handler
private bool $isSandBox;
private OrcidProfilePlugin $plugin;



public function __construct()
{
$request = Application::get()->getRequest();
Expand Down Expand Up @@ -88,7 +84,6 @@ public function authorize($request, &$args, $roleAssignments)
public function orcidAuthorize($args, $request)
{
$context = $request->getContext();
$op = $request->getRequestedOp();
$contextId = ($context == null) ? \PKP\core\PKPApplication::CONTEXT_ID_NONE : $context->getId();
$httpClient = Application::get()->getHttpClient();

Expand Down Expand Up @@ -156,17 +151,17 @@ public function orcidAuthorize($args, $request)

// Suppress errors for nonexistent array indexes
echo '
<html><body><script type="text/javascript">
opener.document.getElementById("givenName").value = ' . json_encode(@$profileJson['name']['given-names']['value']) . ';
opener.document.getElementById("familyName").value = ' . json_encode(@$profileJson['name']['family-name']['value']) . ';
opener.document.getElementById("email").value = ' . json_encode(@$profileJson['emails']['email'][0]['email']) . ';
opener.document.getElementById("country").value = ' . json_encode(@$profileJson['addresses']['address'][0]['country']['value']) . ';
opener.document.getElementById("affiliation").value = ' . json_encode(@$employmentJson['employment-summary'][0]['organization']['name']) . ';
opener.document.getElementById("orcid").value = ' . json_encode($orcidUri) . ';
opener.document.getElementById("connect-orcid-button").style.display = "none";
window.close();
</script></body></html>
';
<html><body><script type="text/javascript">
opener.document.getElementById("givenName").value = ' . json_encode(@$profileJson['name']['given-names']['value']) . ';
opener.document.getElementById("familyName").value = ' . json_encode(@$profileJson['name']['family-name']['value']) . ';
opener.document.getElementById("email").value = ' . json_encode(@$profileJson['emails']['email'][0]['email']) . ';
opener.document.getElementById("country").value = ' . json_encode(@$profileJson['addresses']['address'][0]['country']['value']) . ';
opener.document.getElementById("affiliation").value = ' . json_encode(@$employmentJson['employment-summary'][0]['organization']['name']) . ';
opener.document.getElementById("orcid").value = ' . json_encode($orcidUri) . ';
opener.document.getElementById("connect-orcid-button").style.display = "none";
window.close();
</script></body></html>
';
break;
case 'profile':

Expand All @@ -177,11 +172,11 @@ public function orcidAuthorize($args, $request)

// Reload the public profile tab (incl. form)
echo '
<html><body><script type="text/javascript">
opener.$("#profileTabs").tabs("load", 3);
window.close();
</script></body></html>
';
<html><body><script type="text/javascript">
opener.$("#profileTabs").tabs("load", 3);
window.close();
</script></body></html>
';
break;
default:
throw new Exception('Invalid target');
Expand Down Expand Up @@ -333,12 +328,9 @@ public function orcidVerify($args, $request)
'verifySuccess' => true,
'orcidIcon' => $this->plugin->getIcon()
]);


} catch (\GuzzleHttp\Exception\ClientException $exception) {
$this->plugin->logInfo("Publication fail:".$exception->getMessage());
$templateMgr->assign('orcidAPIError',$exception->getMessage());

}
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
Expand Down Expand Up @@ -376,6 +368,4 @@ public function about($args, $request)
$templateMgr->assign('isMemberApi', $this->plugin->isMemberApiEnabled($contextId));
$templateMgr->display($this->plugin->getTemplateResource('orcidAbout.tpl'));
}


}
52 changes: 21 additions & 31 deletions classes/OrcidValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,27 @@
use APP\plugins\generic\orcidProfile\OrcidProfilePlugin;

class OrcidValidator {
public $plugin;
/**
* OrcidValidator constructor.
* @param OrcidProfilePlugin $plugin
*/
function __construct(&$plugin) {
$this->plugin =& $plugin;
}
/**
* OrcidValidator constructor.
*/
public function __construct(public OrcidProfilePlugin $plugin) {
}

/**
* @param string $str
* @return bool
*/
public function validateClientId($str) {
$valid = false;
if (preg_match('/^APP-[\da-zA-Z]{16}|(\d{4}-){3,}\d{3}[\dX]/', $str) == 1) {
$valid = true;
}
return $valid;
}

/**
* @param string $str
* @return bool
*/
public function validateClientSecret($str) {
$valid = false;
if (preg_match('/^(\d|-|[a-f]){36,64}/', $str) == 1) {
$valid = true;
}
return $valid;
}
/**
* @param string $str
* @return bool
*/
public function validateClientId(?string $str): bool
{
return (bool) preg_match('/^APP-[\da-zA-Z]{16}|(\d{4}-){3,}\d{3}[\dX]/', (string) $str);
}

/**
* @param string $str
* @return bool
*/
public function validateClientSecret(?string $str): bool
{
return (bool) preg_match('/^(\d|-|[a-f]){36,64}/', (string) $str);
}
}
227 changes: 111 additions & 116 deletions classes/form/OrcidProfileSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,123 +21,118 @@
use APP\plugins\generic\orcidProfile\classes\OrcidValidator;
use APP\plugins\generic\orcidProfile\OrcidProfilePlugin;
use APP\template\TemplateManager;
use PKP\plugins\Plugin;

class OrcidProfileSettingsForm extends Form {

const CONFIG_VARS = array(
'orcidProfileAPIPath' => 'string',
'orcidClientId' => 'string',
'orcidClientSecret' => 'string',
'sendMailToAuthorsOnPublication' => 'bool',
'logLevel' => 'string',
'isSandBox' => 'bool',
'country' => 'string',
'city' => 'string'

);
/** @var $contextId int */
var $contextId;

/** @var $plugin object */
var $plugin;

var $validator;

/**
* Constructor
* @param OrcidProfilePlugin $plugin
* @param int $contextId
*/
function __construct($plugin, $contextId) {
$this->contextId = $contextId;
$this->plugin = $plugin;
$orcidValidator = new OrcidValidator($plugin);
$this->validator = $orcidValidator;
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));

if (!$this->plugin->isGloballyConfigured()) {
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'orcidProfileAPIPath', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidAPIPathRequired'));
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'orcidClientId', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidClientId.error', function ($clientId) {
return $this->validator->validateClientId($clientId);
}));
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'orcidClientSecret', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidClientSecret.error', function ($clientSecret) {
return $this->validator->validateClientSecret($clientSecret);
}));
}

}

/**
* Initialize form data.
*/
function initData() {
$contextId = $this->contextId;
$plugin =& $this->plugin;
$this->_data = array();
foreach (self::CONFIG_VARS as $configVar => $type) {
$this->_data[$configVar] = $plugin->getSetting($contextId, $configVar);
}
}

/**
* Assign form data to user-submitted data.
*/
function readInputData() {
$this->readUserVars(array_keys(self::CONFIG_VARS));
}

/**
* Fetch the form.
* @copydoc Form::fetch()
*/
function fetch($request, $template = null, $display = false) {
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('globallyConfigured', $this->plugin->isGloballyConfigured());
$templateMgr->assign('pluginName', $this->plugin->getName());
$templateMgr->assign('applicationName', Application::get()->getName());
return parent::fetch($request, $template, $display);
}

/**
* @copydoc Form::execute()
*/
function execute(...$functionArgs) {
$plugin =& $this->plugin;
$contextId = $this->contextId;
foreach (self::CONFIG_VARS as $configVar => $type) {
if ($configVar === 'orcidProfileAPIPath') {
$plugin->updateSetting($contextId, $configVar, trim($this->getData($configVar), "\"\';"), $type);
} else {
$plugin->updateSetting($contextId, $configVar, $this->getData($configVar), $type);
}
}
if (strpos($this->getData("orcidProfileAPIPath"), "sandbox.orcid.org") == true) {
$plugin->updateSetting($contextId, "isSandBox", true, "bool");
}

parent::execute(...$functionArgs);
}

public function _checkPrerequisites() {
$messages = array();

$clientId = $this->getData('orcidClientId');
if (!$this->validator->validateClientId($clientId)) {
$messages[] = __('plugins.generic.orcidProfile.manager.settings.orcidClientId.error');
}
$clientSecret = $this->getData('orcidClientSecret');
if (!$this->validator->validateClientSecret($clientSecret)) {
$messages[] = __('plugins.generic.orcidProfile.manager.settings.orcidClientSecret.error');
}
if (strlen($clientId) == 0 or strlen($clientSecret) == 0) {
$this->plugin->setEnabled(false);
}
return $messages;
}


public const CONFIG_VARS = [
'orcidProfileAPIPath' => 'string',
'orcidClientId' => 'string',
'orcidClientSecret' => 'string',
'sendMailToAuthorsOnPublication' => 'bool',
'logLevel' => 'string',
'isSandBox' => 'bool',
'country' => 'string',
'city' => 'string'
];

/** @var $contextId int */
public $contextId;

/** @var $plugin object */
public $plugin;

public $validator;

/**
* Constructor
* @param OrcidProfilePlugin $plugin
* @param int $contextId
*/
public function __construct($plugin, $contextId) {
$this->contextId = $contextId;
$this->plugin = $plugin;
$orcidValidator = new OrcidValidator($plugin);
$this->validator = $orcidValidator;
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));

if (!$this->plugin->isGloballyConfigured()) {
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'orcidProfileAPIPath', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidAPIPathRequired'));
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'orcidClientId', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidClientId.error', function ($clientId) {
return $this->validator->validateClientId($clientId);
}));
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'orcidClientSecret', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidClientSecret.error', function ($clientSecret) {
return $this->validator->validateClientSecret($clientSecret);
}));
}
}

/**
* Initialize form data.
*/
public function initData() {
$contextId = $this->contextId;
$plugin =& $this->plugin;
$this->_data = [];
foreach (self::CONFIG_VARS as $configVar => $type) {
$this->_data[$configVar] = $plugin->getSetting($contextId, $configVar);
}
}

/**
* Assign form data to user-submitted data.
*/
public function readInputData() {
$this->readUserVars(array_keys(self::CONFIG_VARS));
}

/**
* Fetch the form.
* @copydoc Form::fetch()
*/
public function fetch($request, $template = null, $display = false) {
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('globallyConfigured', $this->plugin->isGloballyConfigured());
$templateMgr->assign('pluginName', $this->plugin->getName());
$templateMgr->assign('applicationName', Application::get()->getName());
return parent::fetch($request, $template, $display);
}

/**
* @copydoc Form::execute()
*/
public function execute(...$functionArgs) {
$plugin =& $this->plugin;
$contextId = $this->contextId;
foreach (self::CONFIG_VARS as $configVar => $type) {
if ($configVar === 'orcidProfileAPIPath') {
$plugin->updateSetting($contextId, $configVar, trim($this->getData($configVar), "\"\';"), $type);
} else {
$plugin->updateSetting($contextId, $configVar, $this->getData($configVar), $type);
}
}
if (strpos($this->getData("orcidProfileAPIPath"), "sandbox.orcid.org") == true) {
$plugin->updateSetting($contextId, "isSandBox", true, "bool");
}

parent::execute(...$functionArgs);
}

public function _checkPrerequisites() {
$messages = [];

$clientId = $this->getData('orcidClientId');
if (!$this->validator->validateClientId($clientId)) {
$messages[] = __('plugins.generic.orcidProfile.manager.settings.orcidClientId.error');
}
$clientSecret = $this->getData('orcidClientSecret');
if (!$this->validator->validateClientSecret($clientSecret)) {
$messages[] = __('plugins.generic.orcidProfile.manager.settings.orcidClientSecret.error');
}
if (strlen($clientId) == 0 or strlen($clientSecret) == 0) {
$this->plugin->setEnabled(false);
}
return $messages;
}
}

Loading

0 comments on commit 0623ad8

Please sign in to comment.