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-2024 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-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    4.0.0
29 */
30
31class AclForm extends Form
32{
33
34    /**
35     * Acl object
36     * @var ?Acl
37     */
38    protected ?Acl $acl = null;
39
40    /**
41     * AclRole role objects
42     * @var array
43     */
44    protected array $roles = [];
45
46    /**
47     * Acl strict flag
48     * @var bool
49     */
50    protected bool $aclStrict = false;
51
52    /**
53     * Acl flag to manage how to display/manage fields
54     * @var array
55     */
56    protected array $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): AclForm
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): AclForm
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): AclForm
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): AclForm
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  bool $strict
115     * @return AclForm
116     */
117    public function setAclStrict(bool $strict): AclForm
118    {
119        $this->aclStrict = $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(string $displayPermission, string $modifyPermission): AclForm
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 bool
142     */
143    public function isAclStrict(): bool
144    {
145        return $this->aclStrict;
146    }
147    /**
148     * Get field permissions
149     *
150     * @return array
151     */
152    public function getPermissions(): array
153    {
154        return $this->permissions;
155    }
156
157    /**
158     * Render the form object
159     *
160     * @param  int     $depth
161     * @param  ?string $indent
162     * @param  bool    $inner
163     * @return string|null
164     */
165    public function render(int $depth = 0, ?string $indent = null, bool $inner = false): string|null
166    {
167        foreach ($this->fieldsets as $fieldset) {
168            foreach ($fieldset->getAllFields() as $field) {
169                $fieldName   = $field->getName();
170                if ($this->acl->hasResource($fieldName)) {
171                    $viewDenied = ($this->aclStrict) ?
172                        $this->acl->isDeniedMultiStrict($this->roles, $fieldName, $this->permissions['display']) :
173                        $this->acl->isDeniedMulti($this->roles, $fieldName, $this->permissions['display']);
174
175                    if ($viewDenied) {
176                        unset($fieldset[$fieldName]);
177                    } else {
178                        $modifyDenied = ($this->aclStrict) ?
179                            $this->acl->isDeniedMultiStrict($this->roles, $fieldName, $this->permissions['modify']) :
180                            $this->acl->isDeniedMulti($this->roles, $fieldName, $this->permissions['modify']);
181                        if ($modifyDenied) {
182                            $field->setReadonly(true);
183                        }
184                    }
185                }
186            }
187        }
188
189        return parent::render($depth, $indent, $inner);
190    }
191
192}