Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
AclForm
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
9 / 9
17
100.00% covered (success)
100.00%
1 / 1
 setAcl
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setRole
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addRole
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addRoles
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setAclStrict
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setPermissions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isAclStrict
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPermissions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Form;
15
16use Pop\Acl\Acl;
17use Pop\Acl\AclRole;
18use ReturnTypeWillChange;
19
20/**
21 * ACL Form class
22 *
23 * @category   Pop
24 * @package    Pop\Form
25 * @author     Nick Sagona, III <dev@nolainteractive.com>
26 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    3.6.0
29 */
30
31class AclForm extends Form
32{
33
34    /**
35     * Acl object
36     * @var Acl
37     */
38    protected $acl = null;
39
40    /**
41     * AclRole role objects
42     * @var array
43     */
44    protected $roles = [];
45
46    /**
47     * Acl strict flag
48     * @var boolean
49     */
50    protected $aclStrict = false;
51
52    /**
53     * Acl flag to manage how to display/manage fields
54     * @var array
55     */
56    protected $permissions = [
57        'display' => 'view', // permission to display a field
58        'modify'  => 'edit'  // permission to modify a field
59    ];
60
61    /**
62     * Set the Acl object
63     *
64     * @param  Acl     $acl
65     * @return AclForm
66     */
67    public function setAcl(Acl $acl = null)
68    {
69        $this->acl = $acl;
70        return $this;
71    }
72
73    /**
74     * Set a AclRole object (alias method)
75     *
76     * @param  AclRole $role
77     * @return AclForm
78     */
79    public function setRole(AclRole $role = null)
80    {
81        $this->roles[$role->getName()] = $role;
82        return $this;
83    }
84
85    /**
86     * Add a AclRole object
87     *
88     * @param  AclRole $role
89     * @return AclForm
90     */
91    public function addRole(AclRole $role = null)
92    {
93        return $this->setRole($role);
94    }
95
96    /**
97     * Add AclRole objects
98     *
99     * @param  array $roles
100     * @return AclForm
101     */
102    public function addRoles(array $roles)
103    {
104        foreach ($roles as $role) {
105            $this->setRole($role);
106        }
107
108        return $this;
109    }
110
111    /**
112     * Set the Acl object as strict evaluation
113     *
114     * @param  boolean $strict
115     * @return AclForm
116     */
117    public function setAclStrict($strict)
118    {
119        $this->aclStrict = (bool)$strict;
120        return $this;
121    }
122
123    /**
124     * Set the Acl field permissions
125     *
126     * @param  string $displayPermission
127     * @param  string $modifyPermission
128     * @return AclForm
129     */
130    public function setPermissions($displayPermission, $modifyPermission)
131    {
132        $this->permissions['display'] = $displayPermission;
133        $this->permissions['modify']  = $modifyPermission;
134
135        return $this;
136    }
137
138    /**
139     * Is the Acl object set to strict evaluation
140     *
141     * @return boolean
142     */
143    public function isAclStrict()
144    {
145        return $this->aclStrict;
146    }
147    /**
148     * Get field permissions
149     *
150     * @return array
151     */
152    public function getPermissions()
153    {
154        return $this->permissions;
155    }
156
157    /**
158     * Render the form object
159     *
160     * @param  int     $depth
161     * @param  string  $indent
162     * @param  boolean $inner
163     * @return mixed
164     */
165    #[\ReturnTypeWillChange]
166    public function render($depth = 0, $indent = null, $inner = false)
167    {
168        foreach ($this->fieldsets as $fieldset) {
169            foreach ($fieldset->getAllFields() as $field) {
170                $fieldName   = $field->getName();
171                if ($this->acl->hasResource($fieldName)) {
172                    $viewDenied = ($this->aclStrict) ?
173                        $this->acl->isDeniedManyStrict($this->roles, $fieldName, $this->permissions['display']) :
174                        $this->acl->isDeniedMany($this->roles, $fieldName, $this->permissions['display']);
175
176                    if ($viewDenied) {
177                        unset($fieldset[$fieldName]);
178                    } else {
179                        $modifyDenied = ($this->aclStrict) ?
180                            $this->acl->isDeniedManyStrict($this->roles, $fieldName, $this->permissions['modify']) :
181                            $this->acl->isDeniedMany($this->roles, $fieldName, $this->permissions['modify']);
182                        if ($modifyDenied) {
183                            $field->setReadonly(true);
184                        }
185                    }
186                }
187            }
188        }
189
190        return parent::render($depth, $indent, $inner);
191    }
192
193}