Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SignedFormInstance
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 4
20
0.00% covered (danger)
0.00%
0 / 1
 initElements
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 addSignature
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getSignature
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getDataToSign
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace oat\tao\model\controller;
4
5use oat\oatbox\service\ServiceManager;
6use oat\tao\helpers\form\validators\ResourceSignatureValidator;
7use oat\tao\model\security\SignatureGenerator;
8use oat\tao\model\security\SignatureValidator;
9use tao_actions_form_Instance;
10use tao_helpers_form_FormFactory;
11
12class SignedFormInstance extends tao_actions_form_Instance
13{
14    public const SIGNATURE_ELEMENT_NAME = 'signature';
15
16    /**
17     * @return mixed|void
18     * @throws \common_Exception
19     */
20    protected function initElements()
21    {
22        parent::initElements();
23
24        $this->addSignature();
25    }
26
27    /**
28     * @throws \common_Exception
29     */
30    protected function addSignature()
31    {
32        $signature = tao_helpers_form_FormFactory::getElement(self::SIGNATURE_ELEMENT_NAME, 'Hidden');
33
34        $signature->setValue($this->getSignature());
35        $signature->addValidator(
36            new ResourceSignatureValidator(
37                new SignatureValidator(),
38                $this->getDataToSign()
39            )
40        );
41
42        $this->form->addElement($signature, true);
43    }
44
45    /**
46     * @return string
47     * @throws \oat\tao\model\metadata\exception\InconsistencyConfigException
48     */
49    protected function getSignature()
50    {
51        /** @var SignatureGenerator $signatureGenerator */
52        $signatureGenerator = ServiceManager::getServiceManager()->get(SignatureGenerator::class);
53
54        return $signatureGenerator->generate($this->getDataToSign());
55    }
56
57    protected function getDataToSign()
58    {
59        return $this->instance->getUri();
60    }
61}