Passed
Push — master ( 700b0f...aafb0a )
by Björn
18:25 queued 10s
created

AclController   F

Complexity

Total Complexity 76

Size/Duplication

Total Lines 861
Duplicated Lines 40.07 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 54.61%

Importance

Changes 0
Metric Value
wmc 76
lcom 1
cbo 15
dl 345
loc 861
ccs 219
cts 401
cp 0.5461
rs 2.059
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
B onDispatch() 0 126 1
A indexAction() 0 12 1
A acllistAction() 0 4 1
B acldataAction() 0 51 9
A rolesAction() 32 32 2
A resourcesAction() 31 31 2
B addaclAction() 0 59 6
B editaclAction() 0 76 8
B deleteaclAction() 0 46 6
A addroleAction() 38 38 4
B editroleAction() 51 51 6
B deleteroleAction() 48 48 6
A addresourceAction() 38 38 4
B editresourceAction() 52 52 6
B deleteresourceAction() 47 47 6
A reinitACLCache() 0 7 2
A getAclTable() 0 8 2
A getAclroleTable() 8 8 2
A getAclresourceTable() 0 8 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
        return parent::onDispatch($e);
181
    }
182
183
    /**
184
     * list acl
185
     * @return \Zend\View\Model\ViewModel
186
     */
187 1
    public function indexAction()
188
    {
189 1
        return new ViewModel(
190
            array(
191 1
                'acldata' => $this->getAclTable()->fetchAll(),
192 1
                'acltable' => $this->getAclTable(),
193 1
                'roles' => $this->getAclroleTable()->fetchAll()->toArray(),
194 1
                'resources' => $this->getAclresourceTable()->fetchAll()->toArray(),
195 1
                'form' => new AclmatrixForm(),
196
            )
197
        );
198
    }
199
200
    /**
201
     * list acl in a table
202
     * @return \Zend\View\Model\ViewModel
203
     */ 
204 1
    public function acllistAction()
205
    {
206 1
        return $this->indexAction();
207
    }
208
209
    /**
210
     * list acl in json
211
     * @return mixed|\Zend\Http\Response
212
     */
213 2
    public function acldataAction()
214
    {
215 2
        if ( $this->isXHR() ) {
216 1
            $roles = $this->getAclroleTable()->fetchAll()->toArray();
217 1
            $resources = $this->getAclresourceTable()->fetchAll()->toArray();
218 1
            $acls = array();
219 1
            foreach ($resources as $resource) {
220 1
                $resourceacl = [];
0 ignored issues
show
Unused Code introduced by
$resourceacl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
221 1
                foreach ($roles as $role) {
222 1
                    $aclstate = $this->getAclTable()
223 1
                        ->getAclByRoleResource($role['aclroles_id'], $resource['aclresources_id']);
224 1
                    $acls[] = array( 
225 1
                        'acl_id' => ($aclstate && !empty($aclstate->acl_id) ? ($aclstate->acl_id) : ''), 
226 1
                        'roleslug' => $role['roleslug'], 
227 1
                        'resourceslug' => $resource['resourceslug'], 
228 1
                        'status' => ($aclstate && !empty($aclstate->state) ? ($aclstate->state) : 'allow')
229
                    );
230
                }
231
            }
232
            $datatablesData = array(
233 1
                'tableid' => 'acltable',
234 1
                'data' => $acls
235
            );
236 1
            $oController = $this;
237 1
            $datatablesData['data'] = array_map(
238 1
                function ($row) use ($oController) {
239
                    $actions = '<div class="button-group tiny btn-group btn-group-xs">'.
240 1
                        (empty($row["acl_id"]) ? 
241
                            '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
242
                                'admin/acledit',
243
                                array('action'=>'addacl', 'acl_id' => '')
244
                            ).'"><span class="fa fa-pencil"></span> '.$oController->translate("add acl").'</a>'
245
                            :
246 1
                            '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
247 1
                                'admin/acledit',
248 1
                                array('action'=>'editacl', 'acl_id' => $row["acl_id"])
249 1
                            ).'"><span class="fa fa-pencil"></span> '.$oController->translate("edit").'</a>'.
250 1
                            '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
251 1
                                'admin/acledit',
252 1
                                array('action'=>'deleteacl', 'acl_id' => $row["acl_id"])
253 1
                            ).'"><span class="fa fa-trash-o"></span> '.$oController->translate("delete").'</a>'
254
                        ).                            
255 1
                    '</div>';
256 1
                    $row["_actions_"] = $actions;
257 1
                    return $row;
258 1
                }, $datatablesData['data'] 
259
            );
260 1
            return $this->getResponse()->setContent(json_encode($datatablesData));
261
        }
262 1
        return $this->redirect()->toRoute('admin/acledit', array());
263
    }
264
265
    /**
266
     * list roles
267
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
268
     */
269 1 View Code Duplication
    public function rolesAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
    {
271
272 1
        if ( $this->isXHR() ) {
273
            $datatablesData = array('data' => $this->getAclroleTable()->fetchAll()->toArray());
274
            $oController = $this;
275
            $datatablesData['data'] = array_map(
276
                function ($row) use ($oController) {
277
                    $actions = '<div class="button-group tiny btn-group btn-group-xs">'.
278
                        '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
279
                            'admin/acledit',
280
                            array('action'=>'editrole', 'acl_id' => $row["aclroles_id"])
281
                        ).'"><span class="fa fa-pencil"></span> '.$oController->translate("edit").'</a>'.
282
                        '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
283
                            'admin/acledit',
284
                            array('action'=>'deleterole', 'acl_id' => $row["aclroles_id"])
285
                        ).'"><span class="fa fa-trash-o"></span> '.$oController->translate("delete").'</a>'.
286
                    '</div>';
287
                    $row["_actions_"] = $actions;
288
                    return $row;
289
                }, $datatablesData['data'] 
290
            );
291
            return $this->getResponse()->setContent(json_encode($datatablesData));
292
        }
293 1
        return new ViewModel(
294
            array(
295 1
                'acldata' => $this->getAclTable()->fetchAll(),
296 1
                'roles' => $this->getAclroleTable()->fetchAll(),
297 1
                'resources' => $this->getAclresourceTable()->fetchAll(),
298
            )
299
        );
300
    }
301
302
    /**
303
     * list resources
304
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
305
     */
306 1 View Code Duplication
    public function resourcesAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
    {
308 1
        if ( $this->isXHR() ) {
309
            $datatablesData = array('data' => $this->getAclresourceTable()->fetchAll()->toArray());
310
            $oController = $this;
311
            $datatablesData['data'] = array_map(
312
                function ($row) use ($oController) {
313
                    $actions = '<div class="button-group tiny btn-group btn-group-xs">'.
314
                    '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
315
                        'admin/acledit',
316
                        array('action'=>'editresource', 'acl_id' => $row["aclresources_id"])
317
                    ).'"><span class="fa fa-pencil"></span> '.$oController->translate("edit").'</a>'.
318
                    '<a class="button btn btn-default tiny btn-xs btn-clean btn-cta-xhr cta-xhr-modal" href="'.$oController->url()->fromRoute(
319
                        'admin/acledit',
320
                        array('action'=>'deleteresource', 'acl_id' => $row["aclresources_id"])
321
                    ).'"><span class="fa fa-trash-o"></span> '.$oController->translate("delete").'</a>'.
322
                    '</div>';
323
                    $row["_actions_"] = $actions;
324
                    return $row;
325
                }, $datatablesData['data'] 
326
            );
327
            return $this->getResponse()->setContent(json_encode($datatablesData));
328
        }
329 1
        return new ViewModel(
330
            array(
331 1
            'acldata' => $this->getAclTable()->fetchAll(),
332 1
            'roles' => $this->getAclroleTable()->fetchAll(),
333 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
334
            )
335
        );
336
    }
337
338
    /**
339
     * add acl entry
340
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
341
     */
342 1
    public function addaclAction()
343
    {
344 1
        $tmplVars = $this->getTemplateVars( 
345
            array(
346 1
            'acldata' => $this->getAclTable()->fetchAll(),
347 1
            'roles' => $this->getAclroleTable()->fetchAll(),
348 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
349
            'showForm' => true,
350
            )
351
        );
352 1
        $form = new AclForm();
353
354 1
        $roles = $this->getAclroleTable()->fetchAll()->toArray();
355 1
        $valueoptions = array();
356 1
        foreach ($roles as $role) {
357 1
            $valueoptions[$role["aclroles_id"]] = $role["rolename"];
358
        }
359
        /**
360
         * @var \Zend\Form\Element\Select $selectRoles
361
         */
362 1
        $selectRoles = $form->get('aclroles_id');
363 1
        $selectRoles->setValueOptions($valueoptions);
364
        
365 1
        $resources = $this->getAclresourceTable()->fetchAll()->toArray();
366 1
        $valueoptions = array();
367 1
        foreach ($resources as $resource) {
368 1
            $valueoptions[$resource["aclresources_id"]] = $resource["resourcename"];
369
        }
370
        /**
371
         * @var \Zend\Form\Element\Select $selectResources
372
         */
373 1
        $selectResources = $form->get('aclresources_id');
374 1
        $selectResources->setValueOptions($valueoptions);
375
        
376 1
        $request = $this->getRequest();
377 1
        $Acl = new Acl();
378 1
        if ($request->isPost()) {
379
            $form->setInputFilter($Acl->getInputFilter());
380
            $form->setData($request->getPost());
381
382
            if ($form->isValid()) {
383
                $Acl->exchangeArray($form->getData());
384
                $this->getAclTable()->saveAcl($Acl);
385
                
386
                $this->reinitACLCache();
387
                
388
                // Redirect to list of Acl
389
                $this->flashMessenger()->addSuccessMessage($this->translate("permission has been saved"));
390
                if ( $this->isXHR() ) {
391
                    $tmplVars["showForm"] = false;
392
                } else {
393
                    return $this->redirect()->toRoute('admin/acledit', array());
394
                }
395
            }
396
            $tmplVars["acl"] = $Acl;
397
        }
398 1
        $tmplVars["form"] = $form;
399 1
        return new ViewModel($tmplVars);
400
    }
401
402
    /**
403
     * edit acl entry
404
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
405
     */
406 3
    public function editaclAction()
407
    {
408 3
        $tmplVars = $this->getTemplateVars( 
409
            array(
410 3
            'acldata' => $this->getAclTable()->fetchAll(),
411 3
            'roles' => $this->getAclroleTable()->fetchAll(),
412 3
            'resources' => $this->getAclresourceTable()->fetchAll(),
413
            'showForm' => true,
414
            )
415
        );
416 3
        $id = (int) $this->params()->fromRoute('acl_id', 0);
417 3
        if (!$id) {
418 1
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
419 1
            return $this->redirect()->toRoute(
420 1
                'admin/acledit', array(
421 1
                'action' => 'addacl'
422
                )
423
            );
424
        }
425
        try {
426 2
            $Acl = $this->getAclTable()->getAcl($id);
427 1
        } catch (\Exception $e) {
428 1
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
429 1
            return $this->redirect()->toRoute('admin/acledit', array());
430
        }
431
432 1
        $form  = new AclForm();
433 1
        $form->bind($Acl);
434
435 1
        $roles = $this->getAclroleTable()->fetchAll()->toArray();
436 1
        $valueoptions = array();
437 1
        foreach ($roles as $role) {
438 1
            $valueoptions[$role["aclroles_id"]] = $role["rolename"];
439
        }
440
        /**
441
         * @var \Zend\Form\Element\Select $selectRoles
442
         */
443 1
        $selectRoles = $form->get('aclroles_id');
444 1
        $selectRoles->setValueOptions($valueoptions);
445
        
446 1
        $resources = $this->getAclresourceTable()->fetchAll()->toArray();
447 1
        $valueoptions = array();
448 1
        foreach ($resources as $resource) {
449 1
            $valueoptions[$resource["aclresources_id"]] = $resource["resourcename"];
450
        }
451
        /**
452
         * @var \Zend\Form\Element\Select $selectResources
453
         */
454 1
        $selectResources = $form->get('aclresources_id');
455 1
        $selectResources->setValueOptions($valueoptions);
456
        
457 1
        $request = $this->getRequest();
458 1
        if ($request->isPost()) {
459
            $form->setInputFilter($Acl->getInputFilter());
460
            $form->setData($request->getPost());
461
462
            if ($form->isValid()) {
463
                $this->getAclTable()->saveAcl($Acl);
464
465
                $this->reinitACLCache();
466
                
467
                // Redirect to list of Acl
468
                $this->flashMessenger()->addSuccessMessage($this->translate("permission has been saved"));
469
                if ( $this->isXHR() ) {
470
                    $tmplVars["showForm"] = false;
471
                } else {
472
                    return $this->redirect()->toRoute('admin/acledit', array());
473
                }
474
            }
475
        } else {
476 1
            $form->bind($Acl);
477
        }
478 1
        $tmplVars["acl_id"] = $id;
479 1
        $tmplVars["form"] = $form;
480 1
        return new ViewModel($tmplVars);
481
    }
482
483
    /**
484
     * delete acl entry
485
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
486
     */
487 3
    public function deleteaclAction()
488
    {
489 3
        $tmplVars = $this->getTemplateVars( 
490
            array(
491 3
            'acldata' => $this->getAclTable()->fetchAll(),
492 3
            'roles' => $this->getAclroleTable()->fetchAll(),
493 3
            'resources' => $this->getAclresourceTable()->fetchAll(),
494
            'showForm' => true,
495
            )
496
        );
497 3
        $id = (int) $this->params()->fromRoute('acl_id', 0);
498 3
        if (!$id) {
499 1
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
500 1
            return $this->redirect()->toRoute('admin/acledit', array());
501
        }
502
503 2
        $tmplVars["acl_id"] = $id;
504
        try {
505 2
            $Acl = $this->getAclTable()->getAcl($id);
506 1
        } catch (\Exception $e) {
507 1
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
508 1
            return $this->redirect()->toRoute('admin/acledit', array());
509
        }
510 1
        $tmplVars["acl"] = $Acl;
511 1
        $request = $this->getRequest();
512 1
        if ($request->isPost()) {
513
            $del = $request->getPost('del', 'No');
514
515
            if ($del == 'Yes') {
516
                $id = (int) $request->getPost('id');
517
                $this->getAclTable()->deleteAcl($id);
518
                $this->flashMessenger()->addSuccessMessage($this->translate("permission has been deleted"));
519
                
520
                $this->reinitACLCache();
521
            }
522
523
            // Redirect to list of albums
524
            if ( $this->isXHR() ) {
525
                $tmplVars["showForm"] = false;
526
            } else {
527
                return $this->redirect()->toRoute('admin/acledit', array());
528
            }
529
        }
530
531 1
        return new ViewModel($tmplVars);
532
    }
533
534
    /**
535
     * add role item
536
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
537
     */
538 1 View Code Duplication
    public function addroleAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
539
    {
540 1
        $tmplVars = $this->getTemplateVars( 
541
            array(
542 1
            'acldata' => $this->getAclTable()->fetchAll(),
543 1
            'roles' => $this->getAclroleTable()->fetchAll(),
544 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
545
            'showForm' => true,
546
            )
547
        );
548 1
        $form = new AclroleForm();
549
550 1
        $request = $this->getRequest();
551 1
        $Aclrole = new Aclrole();
552 1
        if ($request->isPost()) {
553
            $form->setInputFilter($Aclrole->getInputFilter());
554
            $form->setData($request->getPost());
555
556
            if ($form->isValid()) {
557
                $Aclrole->exchangeArray($form->getData());
558
                $this->getAclroleTable()->saveAclrole($Aclrole);
559
                
560
                $this->reinitACLCache();
561
                
562
                // Redirect to list of Acl
563
                $this->flashMessenger()->addSuccessMessage($this->translate("role has been saved"));
564
                if ( $this->isXHR() ) {
565
                    $tmplVars["showForm"] = false;
566
                } else {
567
                    return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
568
                }
569
            }
570
            $tmplVars["acl"] = $Aclrole;
571
        }
572 1
        $tmplVars["form"] = $form;
573 1
        $tmplVars["roles"] = $this->getAclroleTable()->fetchAll();
574 1
        return new ViewModel($tmplVars);
575
    }
576
577
    /**
578
     * edit role item
579
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
580
     */
581 1 View Code Duplication
    public function editroleAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
582
    {
583 1
        $tmplVars = $this->getTemplateVars( 
584
            array(
585 1
            'acldata' => $this->getAclTable()->fetchAll(),
586 1
            'roles' => $this->getAclroleTable()->fetchAll(),
587 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
588
            'showForm' => true,
589
            )
590
        );
591 1
        $id = (int) $this->params()->fromRoute('acl_id', 0);
592 1
        if (!$id) {
593
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
594
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
595
        }
596
        try {
597 1
            $Aclrole = $this->getAclroleTable()->getAclrole($id);
598
        } catch (\Exception $e) {
599
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
600
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
601
        }
602
603 1
        $form  = new AclroleForm();
604 1
        $form->bind($Aclrole);
605
606 1
        $request = $this->getRequest();
607 1
        if ($request->isPost()) {
608
            $form->setInputFilter($Aclrole->getInputFilter());
609
            $form->setData($request->getPost());
610
611
            if ($form->isValid()) {
612
                $this->getAclroleTable()->saveAclrole($Aclrole);
613
614
                $this->reinitACLCache();
615
                
616
                // Redirect to list of Acl
617
                $this->flashMessenger()->addSuccessMessage($this->translate("role has been saved"));
618
                if ( $this->isXHR() ) {
619
                    $tmplVars["showForm"] = false;
620
                } else {
621
                    return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
622
                }
623
            }
624
        } else {
625 1
            $form->bind($Aclrole);
626
        }
627 1
        $tmplVars["acl_id"] = $id;
628 1
        $tmplVars["form"] = $form;
629 1
        $tmplVars["roles"] = $this->getAclroleTable()->fetchAll();
630 1
        return new ViewModel($tmplVars);
631
    }
632
633
    /**
634
     * delete role item
635
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
636
     */
637 1 View Code Duplication
    public function deleteroleAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
638
    {
639 1
        $tmplVars = $this->getTemplateVars( 
640
            array(
641 1
            'acldata' => $this->getAclTable()->fetchAll(),
642 1
            'roles' => $this->getAclroleTable()->fetchAll(),
643 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
644
            'showForm' => true,
645
            )
646
        );
647 1
        $id = (int) $this->params()->fromRoute('acl_id', 0);
648 1
        if (!$id) {
649
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
650
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
651
        }
652
653 1
        $tmplVars["acl_id"] = $id;
654
        try {
655 1
            $Aclrole = $this->getAclroleTable()->getAclrole($id);
656
        } catch (\Exception $e) {
657
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
658
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
659
        }
660 1
        $tmplVars["aclrole"] = $Aclrole;
661 1
        $request = $this->getRequest();
662 1
        if ($request->isPost()) {
663
            $del = $request->getPost('del', '');
664
665
            if (!empty($del)) {
666
                $id = (int) $request->getPost('id');
667
                $this->getAclroleTable()->deleteAclrole($id);
668
                $this->flashMessenger()->addSuccessMessage($this->translate("role has been deleted"));
669
            
670
                $this->reinitACLCache();
671
            }
672
673
            // Redirect to list of albums
674
            if ( $this->isXHR() ) {
675
                $tmplVars["showForm"] = false;
676
            } else {
677
                return $this->redirect()->toRoute('admin/acledit', array('action' => 'roles'));
678
            }
679
            
680
        }
681
682 1
        $tmplVars["roles"] = $this->getAclroleTable()->fetchAll();
683 1
        return new ViewModel($tmplVars);
684
    }
685
686
    /**
687
     * add resource item
688
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
689
     */
690 1 View Code Duplication
    public function addresourceAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
691
    {
692 1
        $tmplVars = $this->getTemplateVars( 
693
            array(
694 1
            'acldata' => $this->getAclTable()->fetchAll(),
695 1
            'roles' => $this->getAclroleTable()->fetchAll(),
696 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
697
            'showForm' => true,
698
            )
699
        );
700 1
        $form = new AclresourceForm();
701
702 1
        $request = $this->getRequest();
703 1
        $Aclresource = new Aclresource();
704 1
        if ($request->isPost()) {
705
            $form->setInputFilter($Aclresource->getInputFilter());
706
            $form->setData($request->getPost());
707
708
            if ($form->isValid()) {
709
                $Aclresource->exchangeArray($form->getData());
710
                $this->getAclresourceTable()->saveAclresource($Aclresource);
711
                
712
                $this->reinitACLCache();
713
                
714
                // Redirect to list of Acl
715
                $this->flashMessenger()->addSuccessMessage($this->translate("resource has been saved"));
716
                if ( $this->isXHR() ) {
717
                    $tmplVars["showForm"] = false;
718
                } else {
719
                    return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
720
                }
721
            }
722
            $tmplVars["aclresource"] = $Aclresource;
723
        }
724 1
        $tmplVars["form"] = $form;
725 1
        $tmplVars["resources"] = $this->getAclresourceTable()->fetchAll();
726 1
        return new ViewModel($tmplVars);
727
    }
728
729
    /**
730
     * edit resource item
731
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
732
     */
733 1 View Code Duplication
    public function editresourceAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
734
    {
735 1
        $tmplVars = $this->getTemplateVars( 
736
            array(
737 1
            'acldata' => $this->getAclTable()->fetchAll(),
738 1
            'roles' => $this->getAclroleTable()->fetchAll(),
739 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
740
            'showForm' => true,
741
            )
742
        );
743 1
        $id = (int) $this->params()->fromRoute('acl_id', 0);
744 1
        if (!$id) {
745
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
746
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
747
        }
748
        try {
749 1
            $Aclresource = $this->getAclresourceTable()->getAclresource($id);
750
        } catch (\Exception $e) {
751
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
752
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
753
        }
754
755 1
        $form  = new AclresourceForm();
756 1
        $form->bind($Aclresource);
757
758 1
        $request = $this->getRequest();
759 1
        if ($request->isPost()) {
760
            $form->setInputFilter($Aclresource->getInputFilter());
761
            $form->setData($request->getPost());
762
763
            if ($form->isValid()) {
764
                $this->getAclresourceTable()->saveAclresource($Aclresource);
765
766
                $this->reinitACLCache();
767
                
768
                // Redirect to list of Acl
769
                $this->flashMessenger()->addSuccessMessage($this->translate("resource has been saved"));
770
                if ( $this->isXHR() ) {
771
                    $tmplVars["showForm"] = false;
772
                } else {
773
                    return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
774
                }
775
            }
776
        } else {
777 1
            $form->bind($Aclresource);
778
        }
779 1
        $tmplVars["acl_id"] = $id;
780 1
        $tmplVars["aclresource"] = $Aclresource;
781 1
        $tmplVars["form"] = $form;
782 1
        $tmplVars["resources"] = $this->getAclresourceTable()->fetchAll();
783 1
        return new ViewModel($tmplVars);
784
    }
785
786
    /**
787
     * delete resource item
788
     * @return mixed|\Zend\Http\Response|\Zend\View\Model\ViewModel
789
     */
790 1 View Code Duplication
    public function deleteresourceAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
791
    {
792 1
        $tmplVars = $this->getTemplateVars( 
793
            array(
794 1
            'acldata' => $this->getAclTable()->fetchAll(),
795 1
            'roles' => $this->getAclroleTable()->fetchAll(),
796 1
            'resources' => $this->getAclresourceTable()->fetchAll(),
797
            'showForm' => true,
798
            )
799
        );
800 1
        $id = (int) $this->params()->fromRoute('acl_id', 0);
801 1
        if (!$id) {
802
            $this->flashMessenger()->addWarningMessage($this->translate("missing parameters"));
803
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
804
        }
805
806 1
        $tmplVars["acl_id"] = $id;
807
        try {
808 1
            $Aclresource = $this->getAclresourceTable()->getAclresource($id);
809
        } catch (\Exception $e) {
810
            $this->flashMessenger()->addWarningMessage($this->translate("invalid parameters"));
811
            return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
812
        }
813 1
        $tmplVars["aclresource"] = $Aclresource;
814 1
        $request = $this->getRequest();
815 1
        if ($request->isPost()) {
816
            $del = $request->getPost('del', '');
817
818
            if (!empty($del)) {
819
                $id = (int) $request->getPost('id');
820
                $this->getAclresourceTable()->deleteAclresource($id);
821
                $this->flashMessenger()->addSuccessMessage($this->translate("resource has been deleted"));
822
823
                $this->reinitACLCache();
824
            }
825
            
826
            // Redirect to list of albums
827
            if ( $this->isXHR() ) {
828
                $tmplVars["showForm"] = false;
829
            } else {
830
                return $this->redirect()->toRoute('admin/acledit', array('action' => 'resources'));
831
            }
832
        }
833
834 1
        $tmplVars["resources"] = $this->getAclresourceTable()->fetchAll();
835 1
        return new ViewModel($tmplVars);
836
    }
837
    
838
    /**
839
     * re-initialize, respectively clear ACL cache entries
840
     * @return self
841
     */
842
    private function reinitACLCache () {
843
        if ( \Application\Module::getService('CacheService') ) {
844
            \Application\Module::getService('CacheService')->removeItem('ACL');
845
            \Admin\Module::initACL($this->getServiceLocator());
846
        }
847
        return ($this);
848
    }
849
850
    
851
    // table getters
852
    
853
    /**
854
     * retrieve ACL entry table
855
     * @return array|\Admin\Model\AclTable
856
     */
857
    public function getAclTable()
858
    {
859
        if (!$this->AclTable) {
860
            $sm = $this->getServiceLocator();
861
            $this->AclTable = $sm->get('AdminAclTable');
862
        }
863
        return $this->AclTable;
864
    }
865
866
    /**
867
     * retrieve role item table
868
     * @return array|\Admin\Model\AclroleTable
869
     */
870 View Code Duplication
    public function getAclroleTable()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
871
    {
872
        if (!$this->AclroleTable) {
873
            $sm = $this->getServiceLocator();
874
            $this->AclroleTable = $sm->get('AdminAclroleTable');
875
        }
876
        return $this->AclroleTable;
877
    }
878
879
    /**
880
     * retrieve resource item table
881
     * @return array|\Admin\Model\AclresourceTable
882
     */
883
    public function getAclresourceTable()
884
    {
885
        if (!$this->AclresourceTable) {
886
            $sm = $this->getServiceLocator();
887
            $this->AclresourceTable = $sm->get('AdminAclresourceTable');
888
        }
889
        return $this->AclresourceTable;
890
    }
891
    
892
}
893