Skip to content

Commit

Permalink
Merge pull request #85 from oat-sa/release-3.4.0
Browse files Browse the repository at this point in the history
Release 3.4.0
  • Loading branch information
antoinerobin authored Oct 13, 2017
2 parents 7ba190d + 90bf024 commit 4bf7a44
Show file tree
Hide file tree
Showing 12 changed files with 648 additions and 27 deletions.
3 changes: 3 additions & 0 deletions config/default/LtiUserService.conf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return new \oat\taoLti\models\classes\user\OntologyLtiUserService();
2 changes: 1 addition & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
'label' => 'LTI library',
'description' => 'TAO LTI library and helpers',
'license' => 'GPL-2.0',
'version' => '3.3.1',
'version' => '3.4.0',
'author' => 'Open Assessment Technologies SA',
'requires' => array(
'tao' => '>=10.8.0'
Expand Down
23 changes: 7 additions & 16 deletions models/classes/LtiRestApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace oat\taoLti\models\classes;

use oat\taoLti\models\classes\user\LtiUserService;

class LtiRestApiService extends \tao_models_classes_Service
{
protected function getRootClass()
Expand All @@ -43,8 +45,6 @@ protected function getClassService()
*/
public function getUserId($id, $key)
{
$class = new \core_kernel_classes_Class(CLASS_LTI_USER);

$dataStore = new \tao_models_classes_oauth_DataStore();
try {
/** @var \core_kernel_classes_Resource $consumerResource */
Expand All @@ -53,25 +53,16 @@ public function getUserId($id, $key)
throw new \common_exception_NotFound($e->getMessage());
}

$instances = $class->searchInstances(array(
PROPERTY_USER_LTIKEY => $id,
PROPERTY_USER_LTICONSUMER => $consumerResource
), array(
'like' => false
));

if (count($instances) > 1) {
throw new \common_Exception('Multiple user accounts found for user key: ' . $id);
}
/** @var LtiUserService $service */
$service = $this->getServiceLocator()->get(LtiUserService::SERVICE_ID);
$userIdentifier = $service->getUserIdentifier($id, $consumerResource);

/** @var \core_kernel_classes_Resource $ltiUser */
$ltiUser = count($instances) == 1 ? current($instances) : null;
if (!$ltiUser) {
if (is_null($userIdentifier)) {
return null;
}

return array (
'id' => $ltiUser->getUri()
'id' => $userIdentifier
);
}
}
10 changes: 8 additions & 2 deletions models/classes/class.LtiAuthAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

use oat\taoLti\models\classes\LtiMessages\LtiErrorMessage;
use oat\taoLti\models\classes\user\LtiUserService;

/**
* Authentication adapter interface to be implemented by authentication methods
Expand All @@ -29,8 +30,11 @@
* @package taoLti
*/
class taoLti_models_classes_LtiAuthAdapter
implements common_user_auth_Adapter
implements common_user_auth_Adapter, \Zend\ServiceManager\ServiceLocatorAwareInterface
{

use \Zend\ServiceManager\ServiceLocatorAwareTrait;

/**
*
* @var common_http_Request
Expand All @@ -56,7 +60,9 @@ public function authenticate() {
try {
$service->validate($this->request);
$ltiLaunchData = taoLti_models_classes_LtiLaunchData::fromRequest($this->request);
return new taoLti_models_classes_LtiUser($ltiLaunchData);
/** @var LtiUserService $userService */
$userService = $this->getServiceLocator()->get(LtiUserService::SERVICE_ID);
return $userService->findOrSpawnUser($ltiLaunchData);
} catch (common_http_InvalidSignatureException $e) {
throw new taoLti_models_classes_LtiException('Invalid LTI signature', LtiErrorMessage::ERROR_UNAUTHORIZED);
}
Expand Down
19 changes: 19 additions & 0 deletions models/classes/class.LtiLaunchData.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class taoLti_models_classes_LtiLaunchData
* @var array
*/
private $customParams;

/**
* @var core_kernel_classes_Resource
*/
private $ltiConsumer;


/**
Expand Down Expand Up @@ -206,6 +211,20 @@ public function getToolConsumerName() {
? $this->getVariable(self::TOOL_CONSUMER_INSTANCE_DESCRIPTION)
: null;
}

/**
* @return core_kernel_classes_Resource
* @throws tao_models_classes_oauth_Exception
*/
public function getLtiConsumer()
{
if(is_null($this->ltiConsumer)){
$dataStore = new tao_models_classes_oauth_DataStore();
$this->ltiConsumer = $dataStore->findOauthConsumerResource($this->getOauthKey());
}

return $this->ltiConsumer;
}

/**
* Return the returnUrl to the tool consumer
Expand Down
12 changes: 7 additions & 5 deletions models/classes/class.LtiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function __construct() {
*/
public function startLtiSession(common_http_Request $request) {
$adapter = new taoLti_models_classes_LtiAuthAdapter($request);
$this->getServiceLocator()->propagate($adapter);
$user = $adapter->authenticate();
$session = new taoLti_models_classes_TaoLtiSession($user);
common_session_SessionManager::startSession($session);
Expand Down Expand Up @@ -83,13 +84,14 @@ public function getCredential($key) {
*
* @access public
* @author Joel Bout, <joel@taotesting.com>
* @return core_kernel_classes_Resource resource of LtiConsumer
* @param taoLti_models_classes_LtiLaunchData $launchData
* @return core_kernel_classes_Resource resource of LtiConsumer
* @throws tao_models_classes_oauth_Exception thrown if no Consumer found for key
* @deprecated use taoLti_models_classes_LtiLaunchData::getLtiConsumer instead
*/
public function getLtiConsumerResource($launchData)
{
$dataStore = new tao_models_classes_oauth_DataStore();
return $dataStore->findOauthConsumerResource($launchData->getOauthKey());
return $launchData->getLtiConsumer();
}

/**
Expand Down Expand Up @@ -119,7 +121,7 @@ public function findUser(taoLti_models_classes_LtiLaunchData $ltiContext) {
$class = new core_kernel_classes_Class(CLASS_LTI_USER);
$instances = $class->searchInstances(array(
PROPERTY_USER_LTIKEY => $ltiContext->getUserID(),
PROPERTY_USER_LTICONSUMER => $this->getLtiConsumerResource($ltiContext)
PROPERTY_USER_LTICONSUMER => $ltiContext->getLtiConsumer()
), array(
'like' => false
));
Expand All @@ -144,7 +146,7 @@ public function spawnUser(taoLti_models_classes_LtiLaunchData $ltiContext) {

$props = array(
PROPERTY_USER_LTIKEY => $ltiContext->getUserID(),
PROPERTY_USER_LTICONSUMER => $this->getLtiConsumerResource($ltiContext),
PROPERTY_USER_LTICONSUMER => $ltiContext->getLtiConsumer(),
/*
PROPERTY_USER_UILG => $lang,
PROPERTY_USER_DEFLG => $lang,
Expand Down
6 changes: 4 additions & 2 deletions models/classes/class.TaoLtiSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*
*/

use oat\taoLti\models\classes\user\LtiUser;

/**
* The TAO layer ontop of the LtiSession
*
Expand All @@ -36,7 +38,7 @@ class taoLti_models_classes_TaoLtiSession extends common_session_DefaultSession
*/
private $ltiLink = null;

public function __construct(taoLti_models_classes_LtiUser $user)
public function __construct(LtiUser $user)
{
parent::__construct($user);
}
Expand Down Expand Up @@ -81,7 +83,7 @@ public function getLtiLinkResource()
{
if (is_null($this->ltiLink)) {
$class = new core_kernel_classes_Class(CLASS_LTI_INCOMINGLINK);
$consumer = taoLti_models_classes_LtiService::singleton()->getLtiConsumerResource($this->getLaunchData());
$consumer = $this->getLaunchData()->getLtiConsumer();
// search for existing resource
$instances = $class->searchInstances(array(
PROPERTY_LTI_LINK_ID => $this->getLaunchData()->getResourceLinkID(),
Expand Down
132 changes: 132 additions & 0 deletions models/classes/user/KvLtiUserService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?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) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
*
*/

namespace oat\taoLti\models\classes\user;


/**
* Key value implementation of the lti user service
*
* @access public
* @author Antoine Robin, <antoine@taotesting.com>
* @package taoLti
*/
class KvLtiUserService extends LtiUserService
{


const OPTION_PERSISTENCE = 'persistence';

const LTI_USER = 'lti_ku_';

/**
* @var \common_persistence_KeyValuePersistence
*/
private $persistence;

/**
* @return \common_persistence_KeyValuePersistence|\common_persistence_Persistence
*/
protected function getPersistence()
{
if (is_null($this->persistence)) {
$persistenceOption = $this->getOption(self::OPTION_PERSISTENCE);
$this->persistence = (is_object($persistenceOption))
? $persistenceOption
: \common_persistence_KeyValuePersistence::getPersistence($persistenceOption);
}
return $this->persistence;
}


/**
* @inheritdoc
*/
public function findUser(\taoLti_models_classes_LtiLaunchData $ltiContext)
{
$ltiConsumer = $ltiContext->getLtiConsumer();
$data = $this->getPersistence()->get(self::LTI_USER . $ltiContext->getUserID() . $ltiConsumer->getUri());
if ($data === false) {
return null;
}

$ltiUser = LtiUser::unserialize($data);

return $ltiUser;
}

/**
* @inheritdoc
*/
public function getUserIdentifier($userId, $consumer)
{
$data = $this->getPersistence()->get(self::LTI_USER . $userId . $consumer);
if ($data === false) {
return null;
}
return $userId;
}


/**
* @inheritdoc
*/
public function spawnUser(\taoLti_models_classes_LtiLaunchData $ltiContext)
{

$firstname = '';
$lastname = '';
$email = '';
$label = '';

if ($ltiContext->hasLaunchLanguage()) {
$launchLanguage = $ltiContext->getLaunchLanguage();
$uiLanguage = \taoLti_models_classes_LtiUtils::mapCode2InterfaceLanguage($launchLanguage);
} else {
$uiLanguage = DEFAULT_LANG;
}


if ($ltiContext->hasVariable(\taoLti_models_classes_LtiLaunchData::LIS_PERSON_NAME_FULL)) {
$label = $ltiContext->getUserFullName();
}

if ($ltiContext->hasVariable(\taoLti_models_classes_LtiLaunchData::LIS_PERSON_NAME_GIVEN)) {
$firstname = $ltiContext->getUserGivenName();
}
if ($ltiContext->hasVariable(\taoLti_models_classes_LtiLaunchData::LIS_PERSON_NAME_FAMILY)) {
$lastname = $ltiContext->getUserFamilyName();
}
if ($ltiContext->hasVariable(\taoLti_models_classes_LtiLaunchData::LIS_PERSON_CONTACT_EMAIL_PRIMARY)) {
$email = $ltiContext->getUserEmail();;
}

$roles = $this->determineTaoRoles($ltiContext);

$userId = $ltiContext->getUserID();

$ltiUser = new LtiUser($ltiContext, $userId, $roles, $uiLanguage, $firstname, $lastname, $email, $label);
$ltiConsumer = $ltiContext->getLtiConsumer();
$this->getPersistence()->set(self::LTI_USER . $userId . $ltiConsumer->getUri(), json_encode($ltiUser));

return $ltiUser;
}
}
Loading

0 comments on commit 4bf7a44

Please sign in to comment.