-
Notifications
You must be signed in to change notification settings - Fork 4
CFM-121_ORCID_Source_Plugin_2 #312
Merged
Ioannis
merged 1 commit into
COmanage:develop
from
Ioannis:CFM-121_ORCID_Source_Plugin_2
May 6, 2025
+262
−168
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
app/plugins/OrcidSource/src/Controller/ApiV2Controller.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| <?php | ||
| /** | ||
| * COmanage Registry Api Sources API v2 Controller | ||
| * | ||
| * Portions licensed to the University Corporation for Advanced Internet | ||
| * Development, Inc. ("UCAID") under one or more contributor license agreements. | ||
| * See the NOTICE file distributed with this work for additional information | ||
| * regarding copyright ownership. | ||
| * | ||
| * UCAID licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at: | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * @link https://www.internet2.edu/comanage COmanage Project | ||
| * @package registry-plugins | ||
| * @since COmanage Registry v5.2.0 | ||
| * @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OrcidSource\Controller; | ||
|
|
||
| use Cake\Datasource\Exception\RecordNotFoundException; | ||
| use Cake\Event\EventInterface; | ||
| use Cake\ORM\TableRegistry; | ||
| use Cake\Utility\Hash; | ||
| use OrcidSource\Lib\Enum\OrcidSourceScopeEnum; | ||
| use \App\Controller\StandardApiController; | ||
| use \App\Lib\Enum\HttpStatusCodesEnum; | ||
|
|
||
| class ApiV2Controller extends StandardApiController { | ||
| protected $orcidSources; | ||
| protected $orcidTokens; | ||
|
|
||
| /** | ||
| * Calculate the CO ID associated with the request. | ||
| * | ||
| * @since COmanage Registry v5.2.0 | ||
| * @return int CO ID, or null if no CO contextwas found | ||
| */ | ||
|
|
||
| public function calculateRequestedCOID(): ?int { | ||
| return (int)$this->request->getParam('coId') ?? null; | ||
| } | ||
|
|
||
|
|
||
| public function initialize(): void { | ||
| parent::initialize(); | ||
| $this->OrcidSources = TableRegistry::getTableLocator()->get('OrcidSource.OrcidSources'); | ||
| $this->OrcidTokens = TableRegistry::getTableLocator()->get('OrcidSource.OrcidTokens'); | ||
| } | ||
|
|
||
| /** | ||
| * Calculate authorization for the current request. | ||
| * | ||
| * @since COmanage Registry v5.2.0 | ||
| * @return bool True if the current request is permitted, false otherwise | ||
| */ | ||
|
|
||
| public function calculatePermission(): bool { | ||
| $authUser = $this->RegistryAuth->getAuthenticatedUser(); | ||
| $coid = $this->calculateRequestedCOID(); | ||
|
|
||
| return $authUser !== null | ||
| && $this->RegistryAuth->isApiUser() | ||
| && ($this->RegistryAuth->isPlatformAdmin() || $this->RegistryAuth->isCoAdmin($coid)); | ||
| } | ||
|
|
||
| /** | ||
| * Handle a Get SOR Person Role request. | ||
| * | ||
| * @param string $orcid | ||
| * @param int $coId | ||
| * @since COmanage Registry v5.2.0 | ||
| */ | ||
|
|
||
| public function get(string $orcid, int $coId) { | ||
| try { | ||
| $orcidSourcesRecords = $this->OrcidSources | ||
| ->find() | ||
| ->contain([ | ||
| 'Servers.Oauth2Servers' => function ($q) { | ||
| return $q->where(["LOWER(Oauth2Servers.url) LIKE" => '%orcid%']); | ||
| }, | ||
| 'ExternalIdentitySources', | ||
| ]) | ||
| ->innerJoinWith('Servers.Oauth2Servers', function ($q) { | ||
| return $q->where([ | ||
| "LOWER(Oauth2Servers.url) LIKE" => '%orcid%' | ||
| ]); | ||
| }) | ||
| ->innerJoinWith('ExternalIdentitySources') | ||
| ->where([ | ||
| 'Servers.plugin' => 'CoreServer.Oauth2Servers', | ||
| 'ExternalIdentitySources.co_id' => $coId, | ||
| 'ExternalIdentitySources.plugin' => 'OrcidSource.OrcidSources' | ||
| ]) | ||
| ->disableHydration() | ||
| ->all() | ||
| ->toArray(); | ||
|
|
||
| // Extract OrcidSource IDs | ||
| $orcid_source_ids = Hash::extract($orcidSourcesRecords, '{n}.id'); | ||
|
|
||
| // Find token records from the database | ||
| $tokens = $this->OrcidTokens->find() | ||
| ->where([ | ||
| 'OrcidTokens.orcid_identifier' => $orcid, | ||
| 'OrcidTokens.orcid_source_id IN' => $orcid_source_ids | ||
| ]) | ||
| ->disableHydration() | ||
| ->all() | ||
| ->toArray(); | ||
|
|
||
| $columnsToDecrypt = [ | ||
| 'access_token', | ||
| 'id_token', | ||
| 'refresh_token' | ||
| ]; | ||
|
|
||
| if (count($tokens) === 0) { | ||
| throw new RecordNotFoundException(__d('orcid_source', 'error.param.notfound', [__d('orcid_source', 'information.orcid_source.identifier')])); | ||
| } | ||
|
|
||
| foreach ($tokens as $idx => $token) { | ||
| $orcidSourceIndex = array_search($token['orcid_source_id'], $orcid_source_ids); | ||
| $tokens[$idx]['scopes'] = $this->getOauth2ServerScopes( | ||
| $orcidSourcesRecords[$orcidSourceIndex]['server'], | ||
| $orcidSourcesRecords[$orcidSourceIndex] | ||
| ); | ||
| foreach ($columnsToDecrypt as $column) { | ||
| $value = $token[$column] ?? null; | ||
| $tokens[$idx][$column] = !empty($value) ? $this->OrcidTokens->getUnencrypted($value) : ''; | ||
| } | ||
| } | ||
|
|
||
| // Return data in structured format | ||
| $this->set('orcid_tokens', $tokens); | ||
| $this->set('vv_model_name', 'OrcidTokens'); | ||
| $this->set('vv_table_name', 'orcid_tokens'); | ||
| } | ||
| catch(RecordNotFoundException $e) { | ||
| // Return 404 | ||
| $this->response = $this->response->withStatus( | ||
| HttpStatusCodesEnum::HTTP_NOT_FOUND, | ||
| $e->getMessage() | ||
| ); | ||
| $this->autoRender = false; | ||
| return; | ||
| } | ||
| catch(\Exception $e) { | ||
| // Return 400 | ||
| $this->response = $this->response->withStatus( | ||
| HttpStatusCodesEnum::HTTP_BAD_REQUEST, | ||
| $e->getMessage() | ||
| ); | ||
| $this->autoRender = false; | ||
| return; | ||
| } | ||
|
|
||
| // Let the view render | ||
| $this->render('/Standard/api/v2/json/index'); | ||
| } | ||
|
|
||
| /** | ||
| * Indicate whether this Controller will handle some or all authnz. | ||
| * | ||
| * @since COmanage Registry v5.2.0 | ||
| * @param EventInterface $event Cake event, ie: from beforeFilter | ||
| * @return string "no", "open", "authz", or "yes" | ||
| */ | ||
|
|
||
| public function willHandleAuth(\Cake\Event\EventInterface $event): string { | ||
| // We always take over authz | ||
| return 'authz'; | ||
| } | ||
|
|
||
| /** | ||
| * Get the scopes | ||
| * | ||
| * @param array $server Server Record | ||
| * @param array $orcidSource OrcidSource record | ||
| * | ||
| * @return string List of scopes | ||
| * @since COmanage Registry v5.2.0 | ||
| */ | ||
|
|
||
| public function getOauth2ServerScopes(array $server, array $orcidSource): string | ||
| { | ||
| if(is_bool($orcidSource['scope_inherit']) && $orcidSource['scope_inherit']) { | ||
| $Oauth2ServersTable = TableRegistry::getTableLocator()->get('Oauth2Servers'); | ||
| $oauth2Server = $Oauth2ServersTable->find() | ||
| ->select(['scope']) | ||
| ->where(['server_id' => $server['id']]) | ||
| ->first(); | ||
|
|
||
| if ($oauth2Server && !empty($oauth2Server->scope)) { | ||
| return $oauth2Server->scope; | ||
| } | ||
| } | ||
|
|
||
| return OrcidSourceScopeEnum::DEFAULT_SCOPE; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.