Skip to content

Commit

Permalink
pkp#9661 Allow Journal Managers to invite users to adopt a role - ORCiD
Browse files Browse the repository at this point in the history
  • Loading branch information
ewhanson committed Dec 20, 2024
1 parent 5373947 commit 0439d88
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 57 deletions.
6 changes: 6 additions & 0 deletions api/v1/_i18n/I18nController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use PKP\core\PKPBaseController;
use PKP\core\PKPRequest;
use PKP\facades\Locale;

class I18nController extends PKPBaseController
Expand All @@ -41,6 +42,11 @@ public function getRouteGroupMiddleware(): array
return [];
}

public function authorize(PKPRequest $request, array &$args, array $roleAssignments): bool
{
return true;
}

/**
* @copydoc \PKP\core\PKPBaseController::getGroupRoutes()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ public function __construct($action, $locales)
'isRequired' => false,
'isMultilingual' => true,
'size' => 'large',
'value' => ''
'value' => '',
]))
->addField(new FieldText('affiliation', [
'label' => __('user.affiliation'),
'description' => __('acceptInvitation.userDetailsForm.affiliation.description'),
'isMultilingual' => true,
'isRequired' => false,
'size' => 'large',
'value' => '',

]))
->addField(new FieldSelect('userCountry', [
Expand Down
29 changes: 16 additions & 13 deletions classes/components/forms/invitation/UserDetailsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PKP\components\forms\FieldHTML;
use PKP\components\forms\FieldText;
use PKP\components\forms\FormComponent;
use PKP\orcid\OrcidManager;

class UserDetailsForm extends FormComponent
{
Expand All @@ -43,20 +44,22 @@ public function __construct(string $action, array $locales)
'description' => __('invitation.email.description'),
'isRequired' => true,
'size' => 'large',
]));
if (OrcidManager::isEnabled()) {
$this->addField(new FieldHTML('orcid', [
'label' => __('user.orcid'),
'description' => __('invitation.orcid.description'),
'isRequired' => false,
'size' => 'large',
]));
}
$this->addField(new FieldText('givenName', [
'label' => __('user.givenName'),
'description' => __('invitation.givenName.description'),
'isRequired' => false,
'isMultilingual' => true,
'size' => 'large',
]))
->addField(new FieldHTML('orcid', [
'label' => __('user.orcid'),
'description' => __('invitation.orcid.description'),
'isRequired' => false,
'size' => 'large',
]))
->addField(new FieldText('givenName', [
'label' => __('user.givenName'),
'description' => __('invitation.givenName.description'),
'isRequired' => false,
'isMultilingual' => true,
'size' => 'large',
]))
->addField(new FieldText('familyName', [
'label' => __('user.familyName'),
'description' => __('invitation.familyName.description'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@

class UserRoleAssignmentReceiveController extends ReceiveInvitationController
{
public function __construct(public UserRoleAssignmentInvite $invitation)
public function __construct(public UserRoleAssignmentInvite $invitation)
{
}

/**
* @inheritDoc
*/
public function authorize(PKPBaseController $controller, PKPRequest $request, array &$args, array $roleAssignments): bool
public function authorize(PKPBaseController $controller, PKPRequest $request, array &$args, array $roleAssignments): bool
{
$user = $this->invitation->getExistingUser();
if (!isset($user)) {
Expand All @@ -55,14 +55,14 @@ public function authorize(PKPBaseController $controller, PKPRequest $request, ar

$controller->addPolicy(new UserRequiredPolicy($request));
}

return true;
}

/**
* @inheritDoc
*/
public function decline(Request $illuminateRequest): JsonResponse
public function decline(Request $illuminateRequest): JsonResponse
{
$this->invitation->decline();

Expand All @@ -75,7 +75,7 @@ public function decline(Request $illuminateRequest): JsonResponse
/**
* @inheritDoc
*/
public function finalize(Request $illuminateRequest): JsonResponse
public function finalize(Request $illuminateRequest): JsonResponse
{
if (!$this->invitation->validate([], ValidationContext::VALIDATION_CONTEXT_FINALIZE)) {
return response()->json([
Expand All @@ -97,16 +97,16 @@ public function finalize(Request $illuminateRequest): JsonResponse
$user->setCountry($this->invitation->getPayload()->userCountry);
$user->setAffiliation($this->invitation->getPayload()->affiliation, null);

$user->setOrcid($this->invitation->getPayload()->userOrcid);
$user->setVerifiedOrcidOAuthData($this->invitation->getPayload()->toArray());

$user->setDateRegistered(Core::getCurrentDate());
$user->setInlineHelp(1); // default new users to having inline help visible.
$user->setPassword($this->invitation->getPayload()->password);

Repo::user()->add($user);
} else {
if (empty($user->getOrcid()) && isset($this->invitation->getPayload()->userOrcid)) {
$user->setOrcid($this->invitation->getPayload()->userOrcid);
if (empty($user->getOrcid()) && isset($this->invitation->getPayload()->orcid)) {
$user->setVerifiedOrcidOAuthData($this->invitation->getPayload()->toArray());
Repo::user()->edit($user);
}
}
Expand All @@ -125,8 +125,8 @@ public function finalize(Request $illuminateRequest): JsonResponse
$userGroupHelper->userGroupId,
$effectiveDateStart,
$userGroupHelper->dateEnd,
isset($userGroupHelper->masthead) && $userGroupHelper->masthead
? UserUserGroupMastheadStatus::STATUS_ON
isset($userGroupHelper->masthead) && $userGroupHelper->masthead
? UserUserGroupMastheadStatus::STATUS_ON
: UserUserGroupMastheadStatus::STATUS_OFF
);
}
Expand All @@ -142,10 +142,10 @@ public function finalize(Request $illuminateRequest): JsonResponse
/**
* @inheritDoc
*/
public function receive(Request $illuminateRequest): JsonResponse
public function receive(Request $illuminateRequest): JsonResponse
{
return response()->json(
(new UserRoleAssignmentInviteResource($this->invitation))->toArray($illuminateRequest),
(new UserRoleAssignmentInviteResource($this->invitation))->toArray($illuminateRequest),
Response::HTTP_OK
);
}
Expand All @@ -169,7 +169,7 @@ public function refine(Request $illuminateRequest): JsonResponse
$this->invitation->updatePayload(ValidationContext::VALIDATION_CONTEXT_REFINE);

return response()->json(
(new UserRoleAssignmentInviteResource($this->invitation))->toArray($illuminateRequest),
(new UserRoleAssignmentInviteResource($this->invitation))->toArray($illuminateRequest),
Response::HTTP_OK
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use PKP\invitation\invitations\userRoleAssignment\rules\AddUserGroupRule;
use PKP\invitation\invitations\userRoleAssignment\rules\AllowedKeysRule;
use PKP\invitation\invitations\userRoleAssignment\rules\NotNullIfPresent;
use PKP\invitation\invitations\userRoleAssignment\rules\PrimaryLocaleRequired;
use PKP\invitation\invitations\userRoleAssignment\rules\ProhibitedIncludingNull;
use PKP\invitation\invitations\userRoleAssignment\rules\UserGroupExistsRule;
use PKP\invitation\invitations\userRoleAssignment\rules\UsernameExistsRule;
Expand All @@ -30,17 +29,23 @@
class UserRoleAssignmentInvitePayload extends InvitePayload
{
public function __construct(
public ?string $userOrcid = null,
public ?array $givenName = null,
public ?array $familyName = null,
public ?array $affiliation = null,
public ?string $orcid = null,
public ?string $orcidAccessDenied = null,
public ?string $orcidAccessExpiresOn = null,
public ?string $orcidAccessScope = null,
public ?string $orcidAccessToken = null,
public ?bool $orcidIsVerified = null,
public ?string $orcidRefreshToken = null,
public ?array $givenName = null,
public ?array $familyName = null,
public ?array $affiliation = null,
public ?string $userCountry = null,
public ?string $username = null,
public ?string $password = null,
public ?string $emailSubject = null,
public ?string $emailBody = null,
public ?array $userGroupsToAdd = null,
public ?bool $passwordHashed = null,
public ?array $userGroupsToAdd = null,
public ?bool $passwordHashed = null,
public ?string $sendEmailAddress = null,
)
{
Expand Down Expand Up @@ -157,10 +162,40 @@ public function getValidationRules(UserRoleAssignmentInvite $invitation, Validat
],
'userGroupsToAdd.*.masthead' => 'required|bool',
'userGroupsToAdd.*.dateStart' => 'required|date',
'userOrcid' => [
Rule::when(in_array($validationContext, [ValidationContext::VALIDATION_CONTEXT_INVITE, ValidationContext::VALIDATION_CONTEXT_FINALIZE]), ['nullable']),
// FIXME: A duplication of existing rules in user schema. Can they be reused?
'orcid' => [
'nullable',
'orcid'
],
'orcidAccessDenied' => [
'nullable',
'string',
'max:255',
],
'orcidAccessExpiresOn' => [
'nullable',
'string',
'max:255',
],
'orcidAccessScope' => [
'nullable',
'string',
'max:255',
],
'orcidAccessToken' => [
'nullable',
'string',
'max:255',
],
'orcidIsVerified' => [
'nullable',
'boolean',
],
'orcidRefreshToken' => [
'nullable',
'string',
'max:255',
],
];

return $validationRules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,24 @@ public function toArray(Request $request)
$newUser->setAffiliation($this->getPayload()->affiliation, null);
$newUser->setFamilyName($this->getPayload()->familyName, null);
$newUser->setGivenName($this->getPayload()->givenName, null);
$newUser->setCountry($this->getPayload()->country);
$newUser->setCountry($this->getPayload()->userCountry);
$newUser->setUsername($this->getPayload()->username);
$newUser->setEmail($this->getPayload()->sendEmailAddress);
}

// Return specific fields from the UserRoleAssignmentInvite
return array_merge($invitationData, [
'orcid' => $this->getPayload()->orcid,
'orcidAccessDenied' => $this->getPayload()->orcidAccessDenied,
'orcidAccessExpiresOn' => $this->getPayload()->orcidAccessExpiresOn,
'orcidAccessScope' => $this->getPayload()->orcidAccessScope,
'orcidAccessToken' => $this->getPayload()->orcidAccessToken,
'orcidIsVerified' => $this->getPayload()->orcidIsVerified,
'orcidRefreshToken' => $this->getPayload()->orcidRefreshToken,
'givenName' => $this->getPayload()->givenName,
'familyName' => $this->getPayload()->familyName,
'affiliation' => $this->getPayload()->affiliation,
'country' => $this->getPayload()->country,
'country' => $this->getPayload()->userCountry,
'emailSubject' => $this->getPayload()->emailSubject,
'emailBody' => $this->getPayload()->emailBody,
'userGroupsToAdd' => $this->transformUserGroups($this->getPayload()->userGroupsToAdd),
Expand Down Expand Up @@ -101,7 +107,8 @@ protected function transformUser(?User $user): ?array
'givenName' => $user->getGivenName(null),
'country' => $user->getCountry(),
'affiliation' => $user->getAffiliation(null),
'orcid' => $user->getOrcid()
'orcid' => $user->getOrcid(),
'orcidIsVerified' => $user->hasVerifiedOrcid(),
];
}
}
}
14 changes: 9 additions & 5 deletions classes/invitation/stepTypes/AcceptInvitationStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PKP\invitation\sections\Form;
use PKP\invitation\sections\Sections;
use PKP\invitation\steps\Step;
use PKP\orcid\OrcidManager;
use PKP\user\User;

class AcceptInvitationStep extends InvitationStepTypes
Expand All @@ -33,17 +34,18 @@ public function getSteps(?Invitation $invitation, Context $context, ?User $user)

switch ($user) {
case !null:
if(!$user->getData('orcidAccessToken')) {
if(!$user->hasVerifiedOrcid() && OrcidManager::isEnabled($context)) {
$steps[] = $this->verifyOrcidStep();
$steps[] = $this->acceptInvitationReviewStep($context);
}
break;
default:
$steps[] = $this->verifyOrcidStep();
if (OrcidManager::isEnabled($context)) {
$steps[] = $this->verifyOrcidStep();
}
$steps[] = $this->userAccountDetailsStep();
$steps[] = $this->userDetailsStep($context);
$steps[] = $this->acceptInvitationReviewStep($context);
}
$steps[] = $this->acceptInvitationReviewStep($context);
return $steps;
}

Expand All @@ -62,7 +64,9 @@ private function verifyOrcidStep(): \stdClass
$sections->addSection(
null,
[
'validateFields' => ['userOrcid']
'validateFields' => ['orcid', 'orcidIsVerified', 'orcidAccessDenied', 'orcidAccessToken', 'orcidAccessScope', 'orcidRefreshToken', 'orcidAccessExpiresOn'],
'orcidUrl' => OrcidManager::getOrcidUrl(),
'orcidOAuthUrl' => OrcidManager::buildOAuthUrl('authorizeOrcid', ['targetOp' => 'invitation']),
]
);
$step = new Step(
Expand Down
Loading

0 comments on commit 0439d88

Please sign in to comment.