Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 82
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_actions_Roles
0.00% covered (danger)
0.00%
0 / 82
0.00% covered (danger)
0.00%
0 / 10
420
0.00% covered (danger)
0.00%
0 / 1
 index
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 editRole
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
42
 assignUsers
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 delete
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 getUsers
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 editRoleClass
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getRootClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getClassService
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getUserService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDeleteRoleService
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg
19 *                         (under the project TAO & TAO2);
20 *               2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung
21 *                         (under the project TAO-TRANSFER);
22 *               2009-2012 (update and modification) Public Research Centre Henri Tudor
23 *                         (under the project TAO-SUSTAIN & TAO-DEV);
24 *               2013-2018 (original work) Open Assessment Technologies SA;
25 *
26 */
27
28use oat\generis\model\GenerisRdf;
29use oat\generis\model\OntologyRdfs;
30use oat\tao\model\accessControl\Service\DeleteRoleService;
31use oat\tao\model\TaoOntology;
32use oat\tao\model\exceptions\UserErrorException;
33use oat\generis\model\OntologyAwareTrait;
34use tao_helpers_form_FormContainer as FormContainer;
35
36/**
37 * Role Controller provide actions performed from url resolution
38 *
39 * @author Bertrand Chevrier, <taosupport@tudor.lu>
40 * @package taoGroups
41
42 * @license GPLv2  http://www.opensource.org/licenses/gpl-2.0.php
43 */
44class tao_actions_Roles extends tao_actions_RdfController
45{
46    use OntologyAwareTrait;
47
48    protected $authoringService = null;
49    protected $forbidden = [];
50
51    /**
52     * index:
53     */
54    public function index()
55    {
56        $this->defaultData();
57
58        $this->removeSessionAttribute('uri');
59        $this->removeSessionAttribute('classUri');
60
61        $this->setView('roles/index.tpl');
62    }
63
64    /**
65     * Edit a group instance
66     * @return void
67     */
68    public function editRole()
69    {
70        $this->defaultData();
71
72        $clazz = $this->getCurrentClass();
73        $role = $this->getCurrentInstance();
74
75        $isWritable = $role->isWritable();
76
77        $formContainer = new tao_actions_form_Role(
78            $clazz,
79            $role,
80            [
81                FormContainer::CSRF_PROTECTION_OPTION => true,
82                FormContainer::IS_DISABLED => !$isWritable,
83            ]
84        );
85
86        $myForm = $formContainer->getForm();
87
88        if ($isWritable && $myForm->isSubmited() && $myForm->isValid()) {
89            $formValues = $myForm->getValues();
90            $roleService = tao_models_classes_RoleService::singleton();
91            $includedRolesProperty = $this->getProperty(GenerisRdf::PROPERTY_ROLE_INCLUDESROLE);
92
93            // We have to make the difference between the old list
94            // of included roles and the new ones.
95            $oldIncludedRolesUris = $role->getPropertyValues($includedRolesProperty);
96            $newIncludedRolesUris = $formValues[GenerisRdf::PROPERTY_ROLE_INCLUDESROLE];
97            $removeIncludedRolesUris = array_diff($oldIncludedRolesUris, $newIncludedRolesUris);
98            $addIncludedRolesUris = array_diff($newIncludedRolesUris, $oldIncludedRolesUris);
99
100            // Make the changes according to the detected differences.
101            foreach ($removeIncludedRolesUris as $rU) {
102                $r = $this->getResource($rU);
103                $roleService->unincludeRole($role, $r);
104            }
105
106            foreach ($addIncludedRolesUris as $aU) {
107                $r = $this->getResource($aU);
108                $roleService->includeRole($role, $r);
109            }
110
111            // Let's deal with other properties the usual way.
112            unset($formValues[$includedRolesProperty->getUri()]);
113
114            $binder = new tao_models_classes_dataBinding_GenerisFormDataBinder($role);
115            $role = $binder->bind($myForm->getValues());
116
117            core_kernel_users_Cache::removeIncludedRoles($role); // flush cache for this role.
118
119            $this->setData('selectNode', tao_helpers_Uri::encode($role->getUri()));
120            $this->setData('message', __('Role saved'));
121            $this->setData('reload', true);
122        }
123
124        $this->setData('uri', tao_helpers_Uri::encode($role->getUri()));
125        $this->setData('classUri', tao_helpers_Uri::encode($clazz->getUri()));
126        $this->setData('formTitle', 'Edit Role');
127        $this->setData('myForm', $myForm->render());
128        $this->setView('roles/form.tpl');
129    }
130
131    public function assignUsers()
132    {
133        $this->defaultData();
134
135        $role = $this->getCurrentInstance();
136        $prop = $this->getProperty(GenerisRdf::PROPERTY_USER_ROLES);
137        $tree = tao_helpers_form_GenerisTreeForm::buildReverseTree($role, $prop);
138        $tree->setData('title', __('Assign User to role'));
139        $tree->setData('dataUrl', _url('getUsers'));
140        $this->setData('userTree', $tree->render());
141        $this->setView('roles/assignUsers.tpl');
142    }
143
144    /**
145     * Delete a group or a group class
146     * @throws UserErrorException
147     * @throws common_exception_BadRequest
148     * @throws common_exception_Error
149     * @throws common_exception_MissingParameter
150     * @return void
151     */
152    public function delete()
153    {
154        try {
155            if (!$this->isXmlHttpRequest()) {
156                throw new common_exception_BadRequest('wrong request mode');
157            }
158
159            if (!$this->hasRequestParameter('uri')) {
160                throw new common_exception_BadRequest('Missing uri parameter');
161            }
162
163            $this->getDeleteRoleService()->delete($this->getCurrentInstance());
164
165            $deleted = true;
166            $message = null;
167        } catch (Throwable $exception) {
168            $deleted = false;
169            $message = $exception->getMessage();
170        }
171
172        $this->returnJson(
173            [
174                'deleted' => $deleted,
175                'success' => $deleted,
176                'message' => $message
177            ]
178        );
179    }
180
181    /**
182     * @throws common_exception_BadRequest
183     * @throws common_exception_Error
184     */
185    public function getUsers()
186    {
187        if (!$this->isXmlHttpRequest()) {
188            throw new common_exception_BadRequest('wrong request mode');
189        } else {
190            $this->returnJson($this->getUserService()->toTree($this->getClass(TaoOntology::CLASS_URI_TAO_USER), []));
191        }
192    }
193
194    /**
195     * @throws common_ext_ExtensionException
196     */
197    public function editRoleClass()
198    {
199        $this->defaultData();
200
201        $this->removeSessionAttribute('uri');
202        $this->index();
203    }
204
205    /**
206     * get the main class
207     * @return \core_kernel_classes_Class
208     */
209    protected function getRootClass()
210    {
211        return $this->getClassService()->getRoleClass();
212    }
213
214    /**
215     * @return tao_models_classes_RoleService
216     */
217    protected function getClassService()
218    {
219        if (!$this->service) {
220            $this->service = tao_models_classes_RoleService::singleton();
221        }
222        return $this->service;
223    }
224
225    /**
226     * @return tao_models_classes_UserService
227     */
228    protected function getUserService()
229    {
230        return $this->getServiceLocator()->get(tao_models_classes_UserService::SERVICE_ID);
231    }
232
233    private function getDeleteRoleService(): DeleteRoleService
234    {
235        return $this->getPsrContainer()
236            ->get(DeleteRoleService::class)
237            ->withForbiddenRoles($this->forbidden);
238    }
239}