Skip to content

Commit

Permalink
#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 Jan 8, 2025
1 parent 7b40613 commit 5a5905d
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 75 deletions.
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 @@ -35,14 +35,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
{
$this->invitation->changeInvitationUserIdUsingUserEmail();

Expand All @@ -56,14 +56,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 @@ -76,7 +76,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 @@ -98,16 +98,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 @@ -126,8 +126,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 @@ -143,10 +143,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 @@ -170,7 +170,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 @@ -29,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,
public ?array $inviteStagePayload = null,
public ?bool $shouldUseInviteData = null,
Expand Down Expand Up @@ -158,10 +164,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',
],
'shouldUseInviteData' => [
new ProhibitedIncludingNull($validationContext === ValidationContext::VALIDATION_CONTEXT_REFINE||$validationContext === ValidationContext::VALIDATION_CONTEXT_POPULATE),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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(),
];
}

Expand All @@ -90,4 +91,4 @@ protected function createNewUserFromPayload(UserRoleAssignmentInvitePayload $pay

return $newUser;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function toArray(Request $request)
$existingUser = null;
$newUser = null;

if ($this->getPayload()->shouldUseInviteData) {
$payload = UserRoleAssignmentInvitePayload::fromArray($this->getPayload()->inviteStagePayload);
if ($payload->shouldUseInviteData) {
$payload = UserRoleAssignmentInvitePayload::fromArray($payload->inviteStagePayload);

$newUser = $this->createNewUserFromPayload($payload);
} else {
Expand All @@ -45,7 +45,13 @@ public function toArray(Request $request)

// Return specific fields from the UserRoleAssignmentInvite
return array_merge($baseData, [
'orcid' => $payload->userOrcid,
'orcid' => $payload->orcid,
'orcidAccessDenied' => $payload->orcidAccessDenied,
'orcidAccessExpiresOn' => $payload->orcidAccessExpiresOn,
'orcidAccessScope' => $payload->orcidAccessScope,
'orcidAccessToken' => $payload->orcidAccessToken,
'orcidIsVerified' => $payload->orcidIsVerified,
'orcidRefreshToken' => $payload->orcidRefreshToken,
'givenName' => $payload->givenName,
'familyName' => $payload->familyName,
'affiliation' => $payload->affiliation,
Expand All @@ -59,4 +65,4 @@ public function toArray(Request $request)
'newUser' => $this->transformUser($newUser),
]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,39 @@ public function toArray(Request $request)
$existingUser = $this->getExistingUser();
$newUser = null;

$payload = $this->getPayload();

if (!isset($existingUser)) {
$newUser = new User();

$newUser->setAffiliation($this->getPayload()->affiliation, null);
$newUser->setFamilyName($this->getPayload()->familyName, null);
$newUser->setGivenName($this->getPayload()->givenName, null);
$newUser->setCountry($this->getPayload()->userCountry);
$newUser->setUsername($this->getPayload()->username);
$newUser->setEmail($this->getPayload()->sendEmailAddress);
$newUser->setAffiliation($payload->affiliation, null);
$newUser->setFamilyName($payload->familyName, null);
$newUser->setGivenName($payload->givenName, null);
$newUser->setCountry($payload->userCountry);
$newUser->setUsername($payload->username);
$newUser->setEmail($payload->sendEmailAddress);
}

// Return specific fields from the UserRoleAssignmentInvite
return array_merge($baseData, [
'orcid' => $this->getPayload()->userOrcid,
'givenName' => $this->getPayload()->givenName,
'familyName' => $this->getPayload()->familyName,
'affiliation' => $this->getPayload()->affiliation,
'country' => $this->getPayload()->userCountry,
'emailSubject' => $this->getPayload()->emailSubject,
'emailBody' => $this->getPayload()->emailBody,
'userGroupsToAdd' => $this->transformUserGroups($this->getPayload()->userGroupsToAdd),
'username' => $this->getPayload()->username,
'sendEmailAddress' => $this->getPayload()->sendEmailAddress,
'orcid' => $payload->orcid,
'orcidAccessDenied' => $payload->orcidAccessDenied,
'orcidAccessExpiresOn' => $payload->orcidAccessExpiresOn,
'orcidAccessScope' => $payload->orcidAccessScope,
'orcidAccessToken' => $payload->orcidAccessToken,
'orcidIsVerified' => $payload->orcidIsVerified,
'orcidRefreshToken' => $payload->orcidRefreshToken,
'givenName' => $payload->givenName,
'familyName' => $payload->familyName,
'affiliation' => $payload->affiliation,
'country' => $payload->userCountry,
'emailSubject' => $payload->emailSubject,
'emailBody' => $payload->emailBody,
'userGroupsToAdd' => $this->transformUserGroups($payload->userGroupsToAdd),
'username' => $payload->username,
'sendEmailAddress' => $payload->sendEmailAddress,
'existingUser' => $this->transformUser($this->getExistingUser()),
'newUser' => $this->transformUser($newUser),
]);
}
}
}
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 5a5905d

Please sign in to comment.