Passed
Branch ops-updates (277b44)
by Björn
05:09
created

AclController   F

Complexity

Total Complexity 76

Size/Duplication

Total Lines 873
Duplicated Lines 0 %

Test Coverage

Coverage 56.15%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 516
c 1
b 0
f 0
dl 0
loc 873
ccs 219
cts 390
cp 0.5615
rs 2.32
wmc 76

19 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 9 1
B deleteaclAction() 0 45 6
B editroleAction() 0 50 6
B deleteroleAction() 0 47 6
B editresourceAction() 0 51 6
A addresourceAction() 0 37 4
A getAclroleTable() 0 7 2
A acllistAction() 0 3 1
A reinitACLCache() 0 6 2
A rolesAction() 0 29 2
A addroleAction() 0 37 4
A resourcesAction() 0 28 2
B addaclAction() 0 58 6
B editaclAction() 0 75 8
B deleteresourceAction() 0 46 6
A getAclTable() 0 7 2
A getAclresourceTable() 0 7 2
B onDispatch() 0 140 1
B acldataAction() 0 50 9

How to fix   Complexity   

Complex Class

Complex classes like AclController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AclController, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * BB's Zend Framework 2 Components
4
 * 
5
 * AdminModule
6
 *
7
 * @package   [MyApplication]
8
 * @package   BB's Zend Framework 2 Components
9
 * @package   AdminModule
10
 * @author    Björn Bartels <[email protected]>
11
 * @link      https://gitlab.bjoernbartels.earth/groups/zf2
12
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
13
 * @copyright copyright (c) 2016 Björn Bartels <[email protected]>
14
 */
15
16
namespace Admin\Controller;
17
18
use Admin\Model\Acl;
19
use Admin\Model\Aclresource;
20
use Admin\Model\Aclrole;
21
use Admin\Model\AclTable;
22
use Admin\Model\AclresourceTable;
23
use Admin\Model\AclroleTable;
24
use Admin\Form\AclForm;
25
use Admin\Form\AclmatrixForm;
26
use Admin\Form\AclresourceForm;
27
use Admin\Form\AclroleForm;
28
use Application\Controller\BaseActionController;
29
use Zend\Mvc\MvcEvent;
30
use Zend\View\Model\ViewModel;
31
32
class AclController extends BaseActionController
33
{
34
	
35
	/**
36
	 * @var array|\Admin\Model\AclTable
37
	 */
38
	protected $AclTable;
39
	
40
	/**
41
	 * @var array|\Admin\Model\AclroleTable
42
	 */
43
	protected $AclroleTable;
44
	
45
	/**
46
	 * @var array|\Admin\Model\AclresourceTable
47
	 */
48
	protected $AclresourceTable;
49
50
    /**
51
     * initialize titles and toolbar items
52
     * 
53
     * {@inheritDoc}
54
     * @see \Zend\Mvc\Controller\AbstractActionController::onDispatch()
55
     */
56
    public function onDispatch(\Zend\Mvc\MvcEvent $e)
57
    {
58
        $this->setToolbarItems(
59
            array(
60
                "index" => array(
61
                    array(
62
                        'label' => 'add role',
63
                        'icon' => 'plus',
64
                        'class' => 'button btn btn-default small btn-sm btn-cta-xhr cta-xhr-modal',
65
                        'route' => 'admin/acledit',
66
                        'action' => 'addrole',
67
                        'resource' => 'mvc:user',
68
                    ),
69
                    array(
70
                        'label' => 'manage roles',
71
                        'icon' => 'user',
72
                        'class' => 'button btn btn-default small btn-sm btn-cta',
73
                        'route' => 'admin/acledit',
74
                        'action' => 'roles',
75
                        'resource' => 'mvc:user',
76
                    ),
77
                    array(
78
                        'label' => "",
79
                        'class' => 'btn btn-none small btn-sm',
80
                        'uri' => "#",
81
                        'active' => false,
82
                    ),
83
                    array(
84
                        'label' => 'add resource',
85
                        'icon' => 'plus',
86
                        'class' => 'button btn btn-default small btn-sm btn-cta-xhr cta-xhr-modal',
87
                        'route' => 'admin/acledit',
88
                        'action' => 'addresource',
89
                        'resource' => 'mvc:user',
90
                    ),
91
                    array(
92
                        'label' => 'manage resources',
93
                        'icon' => 'list-alt',
94
                        'class' => 'button btn btn-default small btn-sm btn-cta',
95
                        'route' => 'admin/acledit',
96
                        'action' => 'roles',
97
                        'resource' => 'mvc:user',
98
                    ),
99
                  ),
100
               "roles" => array(
101
                    array(
102
                        'label' => 'add role',
103
                        'icon' => 'plus',
104
                        'class' => 'button btn btn-default small btn-sm btn-cta-xhr cta-xhr-modal',
105
                        'route' => 'admin/acledit',
106
                        'action' => 'addrole',
107
                        'resource' => 'mvc:user',
108
                    ),
109
                    array(
110
                        'label' => "",
111
                        'class' => 'btn btn-none small btn-sm',
112
                        'uri' => "#",
113
                        'active' => false,
114
                    ),
115
                    array(
116
                        'label' => 'ACL',
117
                        'icon' => 'asterisk',
118
                        'class' => 'button btn btn-default small btn-sm btn-cta',
119
                        'route' => 'admin/acledit',
120
                        'resource' => 'mvc:user',
121
                    ),
122
                    array(
123
                        'label' => 'manage resources',
124
                        'icon' => 'list-alt',
125
                        'class' => 'button btn btn-default small btn-sm btn-cta',
126
                        'route' => 'admin/acledit',
127
                        'action' => 'resources',
128
                        'resource' => 'mvc:user',
129
                    ),
130
               ),
131
               "resources" => array(
132
                    array(
133
                        'label' => 'add resource',
134
                        'icon' => 'plus',
135
                        'class' => 'button btn btn-default small btn-sm btn-cta-xhr cta-xhr-modal',
136
                        'route' => 'admin/acledit',
137
                        'action' => 'addresource',
138
                        'resource' => 'mvc:user',
139
                    ),
140
                    array(
141
                        'label' => "",
142
                        'class' => 'btn btn-none small btn-sm',
143
                        'uri' => "#",
144
                        'active' => false,
145
                    ),
146
                    array(
147
                        'label' => 'ACL',
148
                        'icon' => 'asterisk',
149
                        'class' => 'button btn btn-default small btn-sm btn-cta',
150
                        'route' => 'admin/acledit',
151
                        'resource' => 'mvc:user',
152
                    ),
153
                    array(
154
                        'label' => 'manage roles',
155
                        'icon' => 'user',
156
                        'class' => 'button btn btn-default small btn-sm btn-cta',
157
                        'route' => 'admin/acledit',
158
                        'action' => 'roles',
159
                        'resource' => 'mvc:user',
160
                    ),
161
               ),
162
            )
163
        );
164
        $this->setActionTitles(
165
            /*array(
166
                'index' => $this->translate("manage permissions"),
167
                'roles' => $this->translate("manage roles"),
168
                'resources' => $this->translate("manage resources"),
169
                'addacl' => $this->translate("add permission"),
170
                'addrole' => $this->translate("add role"),
171
                'addresource' => $this->translate("add resource"),
172
                'editacl' => $this->translate("edit acl"),
173
                'editrole' => $this->translate("edit role"),
174
                'editresource' => $this->translate("edit resource"),
175
                'deleteacl' => $this->translate("delete acl"),
176
                'deleterole' => $this->translate("delete role"),
177
                'deleteresource' => $this->translate("delete resource"),
178
            )
179
        );*/
180
        array(
181
            'index' => ("manage permissions"),
182
            'roles' => ("manage roles"),
183
            'resources' => ("manage resources"),
184
            'addacl' => ("add permission"),
185
            'addrole' => ("add role"),
186
            'addresource' => ("add resource"),
187
            'editacl' => ("edit acl"),
188
            'editrole' => ("edit role"),
189
            'editresource' => ("edit resource"),
190
            'deleteacl' => ("delete acl"),
191
            'deleterole' => ("delete role"),
192
            'deleteresource' => ("delete resource"),
193
        )
194
    );
195
        return parent::onDispatch($e);
196
    }
197
198
    /**
199
     * list acl
200
     * @return \Zend\View\Model\ViewModel
201
     */
202 1
    public function indexAction()
203
    {
204 1
        return new ViewModel(
205
            array(
206 1
                'acldata' => $this->getAclTable()->fetchAll(),
207 1
                'acltable' => $this->getAclTable(),
208 1
                'roles' => $this->getAclroleTable()->fetchAll()->toArray(),
209 1
                'resources' => $this->getAclresourceTable()->fetchAll()->toArray(),
210 1
                'form' => new AclmatrixForm(),
211
            )
212
        );
213
    }
214
215
    /**
216
     * list acl in a table
217
     * @return \Zend\View\Model\ViewModel
218
     */ 
219 1
    public function acllistAction()
220
    {
221 1
        return $this->indexAction();
222
    }
223
224
    /**
225
     * list acl in json
226
     * @return mixed|\Zend\Http\Response
227
     */
228 2
    public function acldataAction()
229
    {
230 2
        if ( $this->isXHR() ) {
231 1
            $roles = $this->getAclroleTable()->fetchAll()->toArray();
232 1
            $resources = $this->getAclresourceTable()->fetchAll()->toArray();
233 1
            $acls = array();
234 1
            foreach ($resources as $resource) {
235 1
                $resourceacl = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $resourceacl is dead and can be removed.
Loading history...
236 1
                foreach ($roles as $role) {
237 1
                    $aclstate = $this->getAclTable()
238 1
                        ->getAclByRoleResource($role['aclroles_id'], $resource['aclresources_id']);
239 1
                    $acls[] = array( 
240 1
                        'acl_id' => ($aclstate && !empty($aclstate->acl_id) ? ($aclstate->acl_id) : ''), 
241 1
                        'roleslug' => $role['roleslug'], 
242 1
                        'resourceslug' => $resource['resourceslug'], 
243 1
                        'status' => ($aclstate && !empty($aclstate->state) ? ($aclstate->state) : 'allow')
244
                    );
245
                }
246
            }
247
            $datatablesData = array(
248 1
                'tableid' => 'acltable',
249 1
                'data' => $acls
250
            );
251 1
            $oController = $this;
252 1
            $datatablesData['data'] = array_map(
253 1
                function ($row) use ($oController) {
254
                    $actions = '<div class="button-group tiny btn-group btn-group-xs">'.
255 1
                        (empty($row["acl_id"]) ? 
256
                            '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
257
                                'admin/acledit',
258
                                array('action'=>'addacl', 'acl_id' => '')
259
                            ).'"><span class="fa fa-pencil"></span> '.$oController->translate("add acl").'</a>'
260
                            :
261 1
                            '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
262 1
                                'admin/acledit',
263 1
                                array('action'=>'editacl', 'acl_id' => $row["acl_id"])
264 1
                            ).'"><span class="fa fa-pencil"></span> '.$oController->translate("edit").'</a>'.
265 1
                            '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
266 1
                                'admin/acledit',
267 1
                                array('action'=>'deleteacl', 'acl_id' => $row["acl_id"])
268 1
                            ).'"><span class="fa fa-trash-o"></span> '.$oController->translate("delete").'</a>'
269
                        ).                            
270 1
                    '</div>';
271 1
                    $row["_actions_"] = $actions;
272 1
                    return $row;
273 1
                }, $datatablesData['data'] 
274
            );
275 1
            return $this->getResponse()->setContent(json_encode($datatablesData));
276
        }
277 1
        return $this->redirect()->toRoute('admin/acledit', array());
278
    }
279
280
    /**
281
     * list roles
282
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
283
     */
284 1
    public function rolesAction()
285
    {
286
287 1
        if ( $this->isXHR() ) {
288
            $datatablesData = array('data' => $this->getAclroleTable()->fetchAll()->toArray());
289
            $oController = $this;
290
            $datatablesData['data'] = array_map(
291
                function ($row) use ($oController) {
292
                    $actions = '<div class="button-group tiny btn-group btn-group-xs">'.
293
                        '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
294
                            'admin/acledit',
295
                            array('action'=>'editrole', 'acl_id' => $row["aclroles_id"])
296
                        ).'"><span class="fa fa-pencil"></span> '.$oController->translate("edit").'</a>'.
297
                        '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
298
                            'admin/acledit',
299
                            array('action'=>'deleterole', 'acl_id' => $row["aclroles_id"])
300
                        ).'"><span class="fa fa-trash-o"></span> '.$oController->translate("delete").'</a>'.
301
                    '</div>';
302
                    $row["_actions_"] = $actions;
303
                    return $row;
304
                }, $datatablesData['data'] 
305
            );
306
            return $this->getResponse()->setContent(json_encode($datatablesData));
307
        }
308 1
        return new ViewModel(
309
            array(
310 1
                'acldata' => $this->getAclTable()->fetchAll(),
311 1
                'roles' => $this->getAclroleTable()->fetchAll(),
312 1
                'resources' => $this->getAclresourceTable()->fetchAll(),
313
            )
314
        );
315
    }
316
317
    /**
318
     * list resources
319
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
320
     */
321 1
    public function resourcesAction()
322
    {
323 1
        if ( $this->isXHR() ) {
324
            $datatablesData = array('data' => $this->getAclresourceTable()->fetchAll()->toArray());
325
            $oController = $this;
326
            $datatablesData['data'] = array_map(
327
                function ($row) use ($oController) {
328
                    $actions = '<div class="button-group tiny btn-group btn-group-xs">'.
329
                    '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
330
                        'admin/acledit',
331
                        array('action'=>'editresource', 'acl_id' => $row["aclresources_id"])
332
                    ).'"><span class="fa fa-pencil"></span> '.$oController->translate("edit").'</a>'.
333
                    '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
334
                        'admin/acledit',
335
                        array('action'=>'deleteresource', 'acl_id' => $row["aclresources_id"])
336
                    ).'"><span class="fa fa-trash-o"></span> '.$oController->translate("delete").'</a>'.
337
                    '</div>';
338
                    $row["_actions_"] = $actions;
339
                    return $row;
340
                }, $datatablesData['data'] 
341
            );
342
            return $this->getResponse()->setContent(json_encode($datatablesData));
343
        }
344 1
        return new ViewModel(
345
            array(
346 1
            'acldata' => $this->getAclTable()->fetchAll(),
347 1
            'roles' => $this->getAclroleTable()->fetchAll(),
348 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
349
            )
350
        );
351
    }
352
353
    /**
354
     * add acl entry
355
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
356
     */
357 1
    public function addaclAction()
358
    {
359 1
        $tmplVars = $this->getTemplateVars( 
360
            array(
361 1
            'acldata' => $this->getAclTable()->fetchAll(),
362 1
            'roles' => $this->getAclroleTable()->fetchAll(),
363 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
364
            'showForm' => true,
365
            )
366
        );
367 1
        $form = new AclForm();
368
369 1
        $roles = $this->getAclroleTable()->fetchAll()->toArray();
370 1
        $valueoptions = array();
371 1
        foreach ($roles as $role) {
372 1
            $valueoptions[$role["aclroles_id"]] = $role["rolename"];
373
        }
374
        /**
375
         * @var \Zend\Form\Element\Select $selectRoles
376
         */
377 1
        $selectRoles = $form->get('aclroles_id');
378 1
        $selectRoles->setValueOptions($valueoptions);
379
        
380 1
        $resources = $this->getAclresourceTable()->fetchAll()->toArray();
381 1
        $valueoptions = array();
382 1
        foreach ($resources as $resource) {
383 1
            $valueoptions[$resource["aclresources_id"]] = $resource["resourcename"];
384
        }
385
        /**
386
         * @var \Zend\Form\Element\Select $selectResources
387
         */
388 1
        $selectResources = $form->get('aclresources_id');
389 1
        $selectResources->setValueOptions($valueoptions);
390
        
391 1
        $request = $this->getRequest();
392 1
        $Acl = new Acl();
393 1
        if ($request->isPost()) {
0 ignored issues
show
Bug introduced by
The method isPost() does not exist on Zend\Stdlib\RequestInterface. It seems like you code against a sub-type of Zend\Stdlib\RequestInterface such as Zend\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

393
        if ($request->/** @scrutinizer ignore-call */ isPost()) {
Loading history...
394
            $form->setInputFilter($Acl->getInputFilter());
395
            $form->setData($request->getPost());
0 ignored issues
show
Bug introduced by
The method getPost() does not exist on Zend\Stdlib\RequestInterface. It seems like you code against a sub-type of Zend\Stdlib\RequestInterface such as Zend\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

395
            $form->setData($request->/** @scrutinizer ignore-call */ getPost());
Loading history...
396
397
            if ($form->isValid()) {
398
                $Acl->exchangeArray($form->getData());
399
                $this->getAclTable()->saveAcl($Acl);
400
                
401
                $this->reinitACLCache();
402
                
403
                // Redirect to list of Acl
404
                $this->flashMessenger()->addSuccessMessage($this->translate("permission has been saved"));
405
                if ( $this->isXHR() ) {
406
                    $tmplVars["showForm"] = false;
407
                } else {
408
                    return $this->redirect()->toRoute('admin/acledit', array());
409
                }
410
            }
411
            $tmplVars["acl"] = $Acl;
412
        }
413 1
        $tmplVars["form"] = $form;
414 1
        return new ViewModel($tmplVars);
415
    }
416
417
    /**
418
     * edit acl entry
419
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
420
     */
421 3
    public function editaclAction()
422
    {
423 3
        $tmplVars = $this->getTemplateVars( 
424
            array(
425 3
            'acldata' => $this->getAclTable()->fetchAll(),
426 3
            'roles' => $this->getAclroleTable()->fetchAll(),
427 3
            'resources' => $this->getAclresourceTable()->fetchAll(),
428
            'showForm' => true,
429
            )
430
        );
431 3
        $id = (int) $this->params()->fromRoute('acl_id', 0);
432 3
        if (!$id) {
433 1
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
434 1
            return $this->redirect()->toRoute(
435 1
                'admin/acledit', array(
436 1
                'action' => 'addacl'
437
                )
438
            );
439
        }
440
        try {
441 2
            $Acl = $this->getAclTable()->getAcl($id);
442 1
        } catch (\Exception $e) {
443 1
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
444 1
            return $this->redirect()->toRoute('admin/acledit', array());
445
        }
446
447 1
        $form  = new AclForm();
448 1
        $form->bind($Acl);
449
450 1
        $roles = $this->getAclroleTable()->fetchAll()->toArray();
451 1
        $valueoptions = array();
452 1
        foreach ($roles as $role) {
453 1
            $valueoptions[$role["aclroles_id"]] = $role["rolename"];
454
        }
455
        /**
456
         * @var \Zend\Form\Element\Select $selectRoles
457
         */
458 1
        $selectRoles = $form->get('aclroles_id');
459 1
        $selectRoles->setValueOptions($valueoptions);
460
        
461 1
        $resources = $this->getAclresourceTable()->fetchAll()->toArray();
462 1
        $valueoptions = array();
463 1
        foreach ($resources as $resource) {
464 1
            $valueoptions[$resource["aclresources_id"]] = $resource["resourcename"];
465
        }
466
        /**
467
         * @var \Zend\Form\Element\Select $selectResources
468
         */
469 1
        $selectResources = $form->get('aclresources_id');
470 1
        $selectResources->setValueOptions($valueoptions);
471
        
472 1
        $request = $this->getRequest();
473 1
        if ($request->isPost()) {
474
            $form->setInputFilter($Acl->getInputFilter());
475
            $form->setData($request->getPost());
476
477
            if ($form->isValid()) {
478
                $this->getAclTable()->saveAcl($Acl);
479
480
                $this->reinitACLCache();
481
                
482
                // Redirect to list of Acl
483
                $this->flashMessenger()->addSuccessMessage($this->translate("permission has been saved"));
484
                if ( $this->isXHR() ) {
485
                    $tmplVars["showForm"] = false;
486
                } else {
487
                    return $this->redirect()->toRoute('admin/acledit', array());
488
                }
489
            }
490
        } else {
491 1
            $form->bind($Acl);
492
        }
493 1
        $tmplVars["acl_id"] = $id;
494 1
        $tmplVars["form"] = $form;
495 1
        return new ViewModel($tmplVars);
496
    }
497
498
    /**
499
     * delete acl entry
500
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
501
     */
502 3
    public function deleteaclAction()
503
    {
504 3
        $tmplVars = $this->getTemplateVars( 
505
            array(
506 3
            'acldata' => $this->getAclTable()->fetchAll(),
507 3
            'roles' => $this->getAclroleTable()->fetchAll(),
508 3
            'resources' => $this->getAclresourceTable()->fetchAll(),
509
            'showForm' => true,
510
            )
511
        );
512 3
        $id = (int) $this->params()->fromRoute('acl_id', 0);
513 3
        if (!$id) {
514 1
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
515 1
            return $this->redirect()->toRoute('admin/acledit', array());
516
        }
517
518 2
        $tmplVars["acl_id"] = $id;
519
        try {
520 2
            $Acl = $this->getAclTable()->getAcl($id);
521 1
        } catch (\Exception $e) {
522 1
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
523 1
            return $this->redirect()->toRoute('admin/acledit', array());
524
        }
525 1
        $tmplVars["acl"] = $Acl;
526 1
        $request = $this->getRequest();
527 1
        if ($request->isPost()) {
528
            $del = $request->getPost('del', 'No');
529
530
            if ($del == 'Yes') {
531
                $id = (int) $request->getPost('id');
532
                $this->getAclTable()->deleteAcl($id);
533
                $this->flashMessenger()->addSuccessMessage($this->translate("permission has been deleted"));
534
                
535
                $this->reinitACLCache();
536
            }
537
538
            // Redirect to list of albums
539
            if ( $this->isXHR() ) {
540
                $tmplVars["showForm"] = false;
541
            } else {
542
                return $this->redirect()->toRoute('admin/acledit', array());
543
            }
544
        }
545
546 1
        return new ViewModel($tmplVars);
547
    }
548
549
    /**
550
     * add role item
551
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
552
     */
553 1
    public function addroleAction()
554
    {
555 1
        $tmplVars = $this->getTemplateVars( 
556
            array(
557 1
            'acldata' => $this->getAclTable()->fetchAll(),
558 1
            'roles' => $this->getAclroleTable()->fetchAll(),
559 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
560
            'showForm' => true,
561
            )
562
        );
563 1
        $form = new AclroleForm();
564
565 1
        $request = $this->getRequest();
566 1
        $Aclrole = new Aclrole();
567 1
        if ($request->isPost()) {
568
            $form->setInputFilter($Aclrole->getInputFilter());
569
            $form->setData($request->getPost());
570
571
            if ($form->isValid()) {
572
                $Aclrole->exchangeArray($form->getData());
573
                $this->getAclroleTable()->saveAclrole($Aclrole);
574
                
575
                $this->reinitACLCache();
576
                
577
                // Redirect to list of Acl
578
                $this->flashMessenger()->addSuccessMessage($this->translate("role has been saved"));
579
                if ( $this->isXHR() ) {
580
                    $tmplVars["showForm"] = false;
581
                } else {
582
                    return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
583
                }
584
            }
585
            $tmplVars["acl"] = $Aclrole;
586
        }
587 1
        $tmplVars["form"] = $form;
588 1
        $tmplVars["roles"] = $this->getAclroleTable()->fetchAll();
589 1
        return new ViewModel($tmplVars);
590
    }
591
592
    /**
593
     * edit role item
594
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
595
     */
596 1
    public function editroleAction()
597
    {
598 1
        $tmplVars = $this->getTemplateVars( 
599
            array(
600 1
            'acldata' => $this->getAclTable()->fetchAll(),
601 1
            'roles' => $this->getAclroleTable()->fetchAll(),
602 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
603
            'showForm' => true,
604
            )
605
        );
606 1
        $id = (int) $this->params()->fromRoute('acl_id', 0);
607 1
        if (!$id) {
608
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
609
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
610
        }
611
        try {
612 1
            $Aclrole = $this->getAclroleTable()->getAclrole($id);
613
        } catch (\Exception $e) {
614
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
615
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
616
        }
617
618 1
        $form  = new AclroleForm();
619 1
        $form->bind($Aclrole);
620
621 1
        $request = $this->getRequest();
622 1
        if ($request->isPost()) {
623
            $form->setInputFilter($Aclrole->getInputFilter());
624
            $form->setData($request->getPost());
625
626
            if ($form->isValid()) {
627
                $this->getAclroleTable()->saveAclrole($Aclrole);
628
629
                $this->reinitACLCache();
630
                
631
                // Redirect to list of Acl
632
                $this->flashMessenger()->addSuccessMessage($this->translate("role has been saved"));
633
                if ( $this->isXHR() ) {
634
                    $tmplVars["showForm"] = false;
635
                } else {
636
                    return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
637
                }
638
            }
639
        } else {
640 1
            $form->bind($Aclrole);
641
        }
642 1
        $tmplVars["acl_id"] = $id;
643 1
        $tmplVars["form"] = $form;
644 1
        $tmplVars["roles"] = $this->getAclroleTable()->fetchAll();
645 1
        return new ViewModel($tmplVars);
646
    }
647
648
    /**
649
     * delete role item
650
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
651
     */
652 1
    public function deleteroleAction()
653
    {
654 1
        $tmplVars = $this->getTemplateVars( 
655
            array(
656 1
            'acldata' => $this->getAclTable()->fetchAll(),
657 1
            'roles' => $this->getAclroleTable()->fetchAll(),
658 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
659
            'showForm' => true,
660
            )
661
        );
662 1
        $id = (int) $this->params()->fromRoute('acl_id', 0);
663 1
        if (!$id) {
664
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
665
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
666
        }
667
668 1
        $tmplVars["acl_id"] = $id;
669
        try {
670 1
            $Aclrole = $this->getAclroleTable()->getAclrole($id);
671
        } catch (\Exception $e) {
672
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
673
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
674
        }
675 1
        $tmplVars["aclrole"] = $Aclrole;
676 1
        $request = $this->getRequest();
677 1
        if ($request->isPost()) {
678
            $del = $request->getPost('del', '');
679
680
            if (!empty($del)) {
681
                $id = (int) $request->getPost('id');
682
                $this->getAclroleTable()->deleteAclrole($id);
683
                $this->flashMessenger()->addSuccessMessage($this->translate("role has been deleted"));
684
            
685
                $this->reinitACLCache();
686
            }
687
688
            // Redirect to list of albums
689
            if ( $this->isXHR() ) {
690
                $tmplVars["showForm"] = false;
691
            } else {
692
                return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
693
            }
694
            
695
        }
696
697 1
        $tmplVars["roles"] = $this->getAclroleTable()->fetchAll();
698 1
        return new ViewModel($tmplVars);
699
    }
700
701
    /**
702
     * add resource item
703
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
704
     */
705 1
    public function addresourceAction()
706
    {
707 1
        $tmplVars = $this->getTemplateVars( 
708
            array(
709 1
            'acldata' => $this->getAclTable()->fetchAll(),
710 1
            'roles' => $this->getAclroleTable()->fetchAll(),
711 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
712
            'showForm' => true,
713
            )
714
        );
715 1
        $form = new AclresourceForm();
716
717 1
        $request = $this->getRequest();
718 1
        $Aclresource = new Aclresource();
719 1
        if ($request->isPost()) {
720
            $form->setInputFilter($Aclresource->getInputFilter());
721
            $form->setData($request->getPost());
722
723
            if ($form->isValid()) {
724
                $Aclresource->exchangeArray($form->getData());
725
                $this->getAclresourceTable()->saveAclresource($Aclresource);
726
                
727
                $this->reinitACLCache();
728
                
729
                // Redirect to list of Acl
730
                $this->flashMessenger()->addSuccessMessage($this->translate("resource has been saved"));
731
                if ( $this->isXHR() ) {
732
                    $tmplVars["showForm"] = false;
733
                } else {
734
                    return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
735
                }
736
            }
737
            $tmplVars["aclresource"] = $Aclresource;
738
        }
739 1
        $tmplVars["form"] = $form;
740 1
        $tmplVars["resources"] = $this->getAclresourceTable()->fetchAll();
741 1
        return new ViewModel($tmplVars);
742
    }
743
744
    /**
745
     * edit resource item
746
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
747
     */
748 1
    public function editresourceAction()
749
    {
750 1
        $tmplVars = $this->getTemplateVars( 
751
            array(
752 1
            'acldata' => $this->getAclTable()->fetchAll(),
753 1
            'roles' => $this->getAclroleTable()->fetchAll(),
754 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
755
            'showForm' => true,
756
            )
757
        );
758 1
        $id = (int) $this->params()->fromRoute('acl_id', 0);
759 1
        if (!$id) {
760
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
761
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
762
        }
763
        try {
764 1
            $Aclresource = $this->getAclresourceTable()->getAclresource($id);
765
        } catch (\Exception $e) {
766
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
767
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
768
        }
769
770 1
        $form  = new AclresourceForm();
771 1
        $form->bind($Aclresource);
772
773 1
        $request = $this->getRequest();
774 1
        if ($request->isPost()) {
775
            $form->setInputFilter($Aclresource->getInputFilter());
776
            $form->setData($request->getPost());
777
778
            if ($form->isValid()) {
779
                $this->getAclresourceTable()->saveAclresource($Aclresource);
780
781
                $this->reinitACLCache();
782
                
783
                // Redirect to list of Acl
784
                $this->flashMessenger()->addSuccessMessage($this->translate("resource has been saved"));
785
                if ( $this->isXHR() ) {
786
                    $tmplVars["showForm"] = false;
787
                } else {
788
                    return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
789
                }
790
            }
791
        } else {
792 1
            $form->bind($Aclresource);
793
        }
794 1
        $tmplVars["acl_id"] = $id;
795 1
        $tmplVars["aclresource"] = $Aclresource;
796 1
        $tmplVars["form"] = $form;
797 1
        $tmplVars["resources"] = $this->getAclresourceTable()->fetchAll();
798 1
        return new ViewModel($tmplVars);
799
    }
800
801
    /**
802
     * delete resource item
803
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
804
     */
805 1
    public function deleteresourceAction()
806
    {
807 1
        $tmplVars = $this->getTemplateVars( 
808
            array(
809 1
            'acldata' => $this->getAclTable()->fetchAll(),
810 1
            'roles' => $this->getAclroleTable()->fetchAll(),
811 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
812
            'showForm' => true,
813
            )
814
        );
815 1
        $id = (int) $this->params()->fromRoute('acl_id', 0);
816 1
        if (!$id) {
817
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
818
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
819
        }
820
821 1
        $tmplVars["acl_id"] = $id;
822
        try {
823 1
            $Aclresource = $this->getAclresourceTable()->getAclresource($id);
824
        } catch (\Exception $e) {
825
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
826
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
827
        }
828 1
        $tmplVars["aclresource"] = $Aclresource;
829 1
        $request = $this->getRequest();
830 1
        if ($request->isPost()) {
831
            $del = $request->getPost('del', '');
832
833
            if (!empty($del)) {
834
                $id = (int) $request->getPost('id');
835
                $this->getAclresourceTable()->deleteAclresource($id);
836
                $this->flashMessenger()->addSuccessMessage($this->translate("resource has been deleted"));
837
838
                $this->reinitACLCache();
839
            }
840
            
841
            // Redirect to list of albums
842
            if ( $this->isXHR() ) {
843
                $tmplVars["showForm"] = false;
844
            } else {
845
                return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
846
            }
847
        }
848
849 1
        $tmplVars["resources"] = $this->getAclresourceTable()->fetchAll();
850 1
        return new ViewModel($tmplVars);
851
    }
852
    
853
    /**
854
     * re-initialize, respectively clear ACL cache entries
855
     * @return self
856
     */
857
    private function reinitACLCache () {
858
        if ( \Application\Module::getService('CacheService') ) {
859
            \Application\Module::getService('CacheService')->removeItem('ACL');
860
            \Admin\Module::initACL($this->getServiceLocator());
861
        }
862
        return ($this);
863
    }
864
865
    
866
    // table getters
867
    
868
    /**
869
     * retrieve ACL entry table
870
     * @return array|\Admin\Model\AclTable
871
     */
872
    public function getAclTable()
873
    {
874
        if (!$this->AclTable) {
875
            $sm = $this->getServiceLocator();
876
            $this->AclTable = $sm->get('AdminAclTable');
877
        }
878
        return $this->AclTable;
879
    }
880
881
    /**
882
     * retrieve role item table
883
     * @return array|\Admin\Model\AclroleTable
884
     */
885
    public function getAclroleTable()
886
    {
887
        if (!$this->AclroleTable) {
888
            $sm = $this->getServiceLocator();
889
            $this->AclroleTable = $sm->get('AdminAclroleTable');
890
        }
891
        return $this->AclroleTable;
892
    }
893
894
    /**
895
     * retrieve resource item table
896
     * @return array|\Admin\Model\AclresourceTable
897
     */
898
    public function getAclresourceTable()
899
    {
900
        if (!$this->AclresourceTable) {
901
            $sm = $this->getServiceLocator();
902
            $this->AclresourceTable = $sm->get('AdminAclresourceTable');
903
        }
904
        return $this->AclresourceTable;
905
    }
906
    
907
}
908