Issues (26)

src/AppBundle/Services/LtiToolProvider.php (6 issues)

1
<?php
2
3
namespace AppBundle\Services;
4
5
use FOS\UserBundle\Doctrine\UserManager;
6
use IMSGlobal\LTI\ToolProvider;
7
use IMSGlobal\LTI\ToolProvider\DataConnector;
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
use Symfony\Component\HttpFoundation\Session\Session;
10
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
11
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
12
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
14
15
/**
16
 * Since this app is used as a lti tool provider, this service offers lti integration
17
 * @see https://www.edu-apps.org/code.html
18
 * @see https://github.com/IMSGlobal/LTI-Tool-Provider-Library-PHP
19
 * @see https://github.com/IMSGlobal/LTI-Tool-Provider-Library-PHP/wiki
20
 * Class LtiToolProvider
21
 * @package AppBundle\Services
22
 */
23
class LtiToolProvider extends ToolProvider\ToolProvider
24
{
25
    private $userManager;
26
    private $tokenStorage;
27
    private $session;
28
    private $eventDispatcher;
29
    private $request;
30
31
    /**
32
     * @return mixed
33
     */
34
    public function getRequest()
35
    {
36
        return $this->request;
37
    }
38
39
    /**
40
     * @param mixed $request
41
     */
42
    public function setRequest($request)
43
    {
44
        $this->request = $request;
45
    }
46
47
    public function __construct(UserManager $userManager, TokenStorage $tokenStorage, Session $session, EventDispatcher $eventDispatcher)
48
    {
49
50
        $dsn = sprintf('%s:host=%s;dbname=%s', getenv('DB_TYPE'), getenv('DB_HOST'), getenv('DB_NAME'));
51
        $db = new \PDO($dsn, getenv('DB_USER'), getenv('DB_PWD'));
52
        $dataConnector = DataConnector\DataConnector::getDataConnector('', $db);
53
        parent::__construct($dataConnector);
54
        $this->userManager = $userManager;
55
        $this->tokenStorage = $tokenStorage;
56
        $this->session = $session;
57
        $this->eventDispatcher = $eventDispatcher;
58
    }
59
60
    //todo: role mapping between e-media and LMS
61
    function onLaunch()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
    {
63
        if (!$userName = $_POST['user_id']) {
64
            return false;
65
        }
66
        if (!$user = $this->userManager->findUserByUsername($userName)) {
67
            $user = $this->userManager->createUser();
68
            $user->setFirstName($_POST['lis_person_name_given']);
69
            $user->setLastName($_POST['lis_person_name_family']);
70
            $user->setUsername($userName);
71
            $user->setEmail($_POST['lis_person_contact_email_primary']);
72
            $user->setPlainPassword($userName);
73
            $user->setEnabled(true);
74
            $this->userManager->updateUser($user);
75
        }
76
        $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
77
        $this->tokenStorage->setToken($token);
78
        $this->session->set('_security_main', serialize($token));
79
        // Fire the login event manually
80
        $event = new InteractiveLoginEvent($this->getRequest(), $token);
81
        $this->eventDispatcher->dispatch("security.interactive_login", $event);
82
83
84
        return $user;
85
    }
86
    // Insert code here to handle incoming launches - use the user, context
87
    // and resourceLink properties to access the current user, context and resource link.
88
89
90
    function onContentItem()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
    {
92
        die(__FUNCTION__);
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
93
94
        // Insert code here to handle incoming content-item requests - use the user and context
95
        // properties to access the current user and context.
96
97
    }
98
99
    function onRegister()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100
    {
101
        die(__FUNCTION__);
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
102
103
        // Insert code here to handle incoming registration requests - use the user
104
        // property of the $tool_provider parameter to access the current user.
105
106
    }
107
108
    function onError()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
109
    {
110
        throw new AccessDeniedException();
111
        // Insert code here to handle errors on incoming connections - do not expect
112
        // the user, context and resourceLink properties to be populated but check the reason
113
        // property for the cause of the error.  Return TRUE if the error was fully
114
        // handled by this method.
115
116
    }
117
118
}