Skip to content

Commit

Permalink
feat: support custom role claims
Browse files Browse the repository at this point in the history
* feat: add non-vocabulary tao role type support

* refactor: rename

* chore: copyright year

Signed-off-by: Nikita Pimenov <[email protected]>

---------

Signed-off-by: Nikita Pimenov <[email protected]>
  • Loading branch information
pnal authored Jan 20, 2025
1 parent 9858265 commit db40dac
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Role/Factory/RoleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OAT\Library\Lti1p3Core\Role\Type\InstitutionRole;
use OAT\Library\Lti1p3Core\Role\Type\LtiSystemRole;
use OAT\Library\Lti1p3Core\Role\Type\SystemRole;
use OAT\Library\Lti1p3Core\Role\Type\CustomRole;

class RoleFactory
{
Expand All @@ -36,18 +37,26 @@ class RoleFactory
*/
public static function create(string $name): RoleInterface
{
if (strpos($name, SystemRole::getNameSpace()) === 0) {
if (str_starts_with($name, SystemRole::getNameSpace())) {
return new SystemRole($name);
}

if (strpos($name, InstitutionRole::getNameSpace()) === 0) {
if (str_starts_with($name, InstitutionRole::getNameSpace())) {
return new InstitutionRole($name);
}

if (strpos($name, LtiSystemRole::getNameSpace()) === 0) {
if (str_starts_with($name, LtiSystemRole::getNameSpace())) {
return new LtiSystemRole($name);
}

if (str_starts_with($name, ContextRole::getNameSpace())) {
return new ContextRole($name);
}

if (filter_var($name, FILTER_VALIDATE_URL)) {
return new CustomRole($name);
}

return new ContextRole($name);
}
}
1 change: 1 addition & 0 deletions src/Role/RoleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface RoleInterface
public const TYPE_INSTITUTION = 'institution';
public const TYPE_CONTEXT = 'context';
public const TYPE_LTI_SYSTEM = 'lti-system';
public const TYPE_CUSTOM_TAO = 'non-lti-custom';

public const NAMESPACE_SYSTEM = 'http://purl.imsglobal.org/vocab/lis/v2/system/person';
public const NAMESPACE_INSTITUTION = 'http://purl.imsglobal.org/vocab/lis/v2/institution/person';
Expand Down
62 changes: 62 additions & 0 deletions src/Role/Type/CustomRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2025 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace OAT\Library\Lti1p3Core\Role\Type;

use OAT\Library\Lti1p3Core\Role\AbstractRole;

/**
* @see https://www.imsglobal.org/node/162741#roles-claim
*/
class CustomRole extends AbstractRole
{
public static function getType(): string
{
return static::TYPE_CUSTOM_TAO;
}

public static function getNameSpace(): string
{
return '';
}

public function getSubName(): ?string
{
$exp = explode('#', $this->name);
return end($exp);
}

public function isCore(): bool
{
return false;
}

protected function isValid(): bool
{
return count(explode('#', $this->name)) === 2;
}

protected function getMap(): array
{
return [];
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Role/Factory/RoleFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OAT\Library\Lti1p3Core\Role\Type\InstitutionRole;
use OAT\Library\Lti1p3Core\Role\Type\LtiSystemRole;
use OAT\Library\Lti1p3Core\Role\Type\SystemRole;
use OAT\Library\Lti1p3Core\Role\Type\CustomRole;
use PHPUnit\Framework\TestCase;

class RoleFactoryTest extends TestCase
Expand Down Expand Up @@ -92,6 +93,16 @@ public function testCreateLtiSystemRole(): void
$this->assertTrue($result->isCore());
}

public function testCreateTaoRole(): void
{
$result = RoleFactory::create('http://www.tao.lu/Ontologies/TAOLTI.rdf#ContentTranslator');

$this->assertInstanceOf(CustomRole::class, $result);
$this->assertEquals('http://www.tao.lu/Ontologies/TAOLTI.rdf#ContentTranslator', $result->getName());
$this->assertEquals('ContentTranslator', $result->getSubName());
$this->assertFalse($result->isCore());
}

public function testCreateFailure(): void
{
$this->expectException(LtiExceptionInterface::class);
Expand Down

0 comments on commit db40dac

Please sign in to comment.