Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.77% covered (danger)
12.77%
6 / 47
16.67% covered (danger)
16.67%
2 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
common_session_BasicSession
12.77% covered (danger)
12.77%
6 / 47
16.67% covered (danger)
16.67%
2 / 12
474.75
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserUri
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserPropertyValues
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserLabel
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
 getUserRoles
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getDataLanguage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInterfaceLanguage
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getTimeZone
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 refresh
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 setServiceLocator
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getContexts
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
2.50
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2017-2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22/**
23 * Represents a Session on Generis.
24 *
25 * @access private
26 * @author Joel Bout, <joel@taotesting.com>
27 * @package generis
28
29 */
30
31use oat\generis\model\GenerisRdf;
32use oat\generis\model\OntologyRdfs;
33use oat\oatbox\user\User;
34use oat\oatbox\Refreshable;
35use Zend\ServiceManager\ServiceLocatorAwareInterface;
36use Zend\ServiceManager\ServiceLocatorAwareTrait;
37use oat\oatbox\user\UserLanguageServiceInterface;
38use Zend\ServiceManager\ServiceLocatorInterface;
39use oat\oatbox\session\SessionContext;
40
41class common_session_BasicSession implements common_session_Session, ServiceLocatorAwareInterface
42{
43    use ServiceLocatorAwareTrait {
44        setServiceLocator as protected setOriginalServiceLocator;
45    }
46
47    /**
48     * @var common_user_User
49     */
50    private $user;
51
52    /**
53     * @var SessionContext[]
54     */
55    private $contexts;
56
57    /**
58     * @param SessionContext[] $context
59     */
60    public function __construct(User $user, array $contexts = [])
61    {
62        $this->user = $user;
63        $this->contexts = $contexts;
64    }
65
66    public function getUser()
67    {
68        return $this->user;
69    }
70
71    /**
72     * {@inheritDoc}
73     * @see common_session_Session::getUserUri()
74     */
75    public function getUserUri()
76    {
77        return $this->user->getIdentifier();
78    }
79
80    /**
81     * @param string $property
82     * @return mixed
83     */
84    public function getUserPropertyValues($property)
85    {
86        return $this->user->getPropertyValues($property);
87    }
88
89    /**
90     * (non-PHPdoc)
91     * @see common_session_Session::getUserLabel()
92     */
93    public function getUserLabel()
94    {
95        $label = '';
96        $first = $this->user->getPropertyValues(GenerisRdf::PROPERTY_USER_FIRSTNAME);
97        $label .= empty($first) ? '' : current($first);
98        $last = $this->user->getPropertyValues(GenerisRdf::PROPERTY_USER_LASTNAME);
99        $label .= empty($last) ? '' : ' ' . current($last);
100        $label = trim($label);
101        if (empty($label)) {
102            $login = $this->user->getPropertyValues(GenerisRdf::PROPERTY_USER_LOGIN);
103            if (!empty($login)) {
104                $label = current($login);
105            }
106        }
107        if (empty($label)) {
108            $rdflabel = $this->user->getPropertyValues(OntologyRdfs::RDFS_LABEL);
109            $label =  empty($rdflabel) ? __('user') : current($rdflabel);
110        }
111        return $label;
112    }
113
114    /**
115     * {@inheritDoc}
116     * @see common_session_Session::getUserRoles()
117     */
118    public function getUserRoles()
119    {
120        $returnValue = [];
121        // We use a Depth First Search approach to flatten the Roles Graph.
122        foreach ($this->user->getPropertyValues(GenerisRdf::PROPERTY_USER_ROLES) as $roleUri) {
123            $returnValue[$roleUri] = $roleUri;
124            $role = new core_kernel_classes_Resource($roleUri);
125            foreach (core_kernel_users_Service::singleton()->getIncludedRoles($role) as $incRole) {
126                $returnValue[$incRole->getUri()] = $incRole->getUri();
127            }
128        }
129        return $returnValue;
130    }
131
132    /**
133     * @return string language code (e.g. 'en-US')
134     */
135    public function getDataLanguage()
136    {
137        return $this->getServiceLocator()->get(UserLanguageServiceInterface::class)->getDataLanguage($this->getUser());
138    }
139
140    /**
141     * @return string language code (e.g. 'en-US')
142     */
143    public function getInterfaceLanguage()
144    {
145        /** @var PHPSession $session */
146        $session = PHPSession::singleton();
147
148        /** @var UserLanguageServiceInterface $userLanguageService */
149        $userLanguageService = $this->getServiceLocator()->get(UserLanguageServiceInterface::class);
150
151        if ($session->hasAttribute('overrideInterfaceLanguage')) {
152            $userLanguageService->setCustomInterfaceLanguage($session->getAttribute('overrideInterfaceLanguage'));
153        } else {
154            // Just to be sure the custom interface language is removed when the session attribute is gone
155            $userLanguageService->setCustomInterfaceLanguage(null);
156        }
157
158        return $userLanguageService->getInterfaceLanguage($this->getUser());
159    }
160
161    /**
162     * (non-PHPdoc)
163     * @see common_session_Session::getTimeZone()
164     */
165    public function getTimeZone()
166    {
167        $tzs = $this->user->getPropertyValues(GenerisRdf::PROPERTY_USER_TIMEZONE);
168        $tz = empty($tzs) ? '' : (string)current($tzs);
169        return empty($tz) ? TIME_ZONE : $tz;
170    }
171
172    public function refresh()
173    {
174        if ($this->user instanceof Refreshable) {
175            $this->user->refresh();
176        }
177    }
178
179    /**
180     * {@inheritDoc}
181     * @see \Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator()
182     * propagate to user
183     */
184    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
185    {
186        if ($this->user instanceof ServiceLocatorAwareInterface) {
187            $this->user->setServiceLocator($serviceLocator);
188        }
189        return $this->setOriginalServiceLocator($serviceLocator);
190    }
191
192    public function getContexts(string $class = null): array
193    {
194        $contexts = $this->contexts;
195        if ($class != null) {
196            $contexts = array_filter($contexts, function ($element) use ($class) {
197                return $element instanceof $class;
198            });
199        }
200        return $contexts;
201    }
202}