Completed
Push — master ( 2f8aec...b781ca )
by Richard
16s
created

PermissionHandler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getGrantedGroupsById() 0 23 3
A getGrantedItems() 0 30 4
A saveItemPermissions() 0 17 3
A deletePermissions() 0 8 1
A isGranted() 0 11 4
A __construct() 0 4 1
1
<?php
2
3
namespace XoopsModules\Publisher;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
use PDO;
16
use Xoops;
17
use Xoops\Core\Kernel\Criteria;
18
use Xoops\Core\Kernel\CriteriaCompo;
19
use Xoops\Core\Kernel\XoopsObjectHandler;
20
21
/**
22
 *  Publisher class
23
 *
24
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
25
 * @license         GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26
 * @package         Class
27
 * @subpackage      Handlers
28
 * @since           1.0
29
 * @author          trabis <[email protected]>
30
 * @author          The SmartFactory <www.smartfactory.ca>
31
 * @version         $Id$
32
 */
33
require_once \dirname(__DIR__) . '/include/common.php';
34
35
/**
36
 * Class PermissionHandler
37
 * @package XoopsModules\Publisher
38
 */
39
class PermissionHandler extends XoopsObjectHandler
40
{
41
    /**
42
     * @var Helper
43
     * @access public
44
     */
45
    public $helper = null;
46
47
    /**
48
     * constructor
49
     */
50
    public function __construct()
51
    {
52
        $this->helper = Helper::getInstance();
53
        $this->db2 = Xoops::getInstance()->db();
54
    }
55
56
    /**
57
     * Returns permissions for a certain type
58
     *
59
     * @param string $gperm_name "global", "forum" or "topic" (should perhaps have "post" as well - but I don't know)
60
     * @param int    $id         id of the item (forum, topic or possibly post) to get permissions for
61
     */
62
    public function getGrantedGroupsById($gperm_name, $id): array
63
    {
64
        static $items;
65
        if (isset($items[$gperm_name][$id])) {
66
            return $items[$gperm_name][$id];
67
        }
68
        $groups = [];
69
        $criteria = new CriteriaCompo();
70
        $criteria->add(new Criteria('gperm_modid', $this->helper->getModule()->getVar('mid')));
0 ignored issues
show
Bug introduced by
It seems like $this->helper->getModule()->getVar('mid') can also be of type string[]; however, parameter $value of Xoops\Core\Kernel\Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

70
        $criteria->add(new Criteria('gperm_modid', /** @scrutinizer ignore-type */ $this->helper->getModule()->getVar('mid')));
Loading history...
71
        $criteria->add(new Criteria('gperm_name', $gperm_name));
72
        $criteria->add(new Criteria('gperm_itemid', $id));
73
        //Instead of calling groupperm handler and get objects, we will save some memory and do it our way
74
        $qb = $this->db2->createXoopsQueryBuilder();
75
        $qb->select('gperm_groupid')->fromPrefix('system_permission', '');
76
        $criteria->renderQb($qb);
77
        $result = $qb->execute();
78
79
        while (false !== ($myrow = $result->fetch(PDO::FETCH_ASSOC))) {
80
            $groups[$myrow['gperm_groupid']] = $myrow['gperm_groupid'];
81
        }
82
        $items[$gperm_name][$id] = $groups;
83
84
        return $groups;
85
    }
86
87
    /**
88
     * Returns permissions for a certain type
89
     *
90
     * @param string $gperm_name "global", "forum" or "topic" (should perhaps have "post" as well - but I don't know)
91
     */
92
    public function getGrantedItems($gperm_name): array
93
    {
94
        static $items;
95
        if (isset($items[$gperm_name])) {
96
            return $items[$gperm_name];
97
        }
98
        $ret = [];
99
        //Instead of calling groupperm handler and get objects, we will save some memory and do it our way
100
        $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
101
        $criteria->add(new Criteria('gperm_modid', $this->helper->getModule()->getVar('mid')));
0 ignored issues
show
Bug introduced by
It seems like $this->helper->getModule()->getVar('mid') can also be of type string[]; however, parameter $value of Xoops\Core\Kernel\Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

101
        $criteria->add(new Criteria('gperm_modid', /** @scrutinizer ignore-type */ $this->helper->getModule()->getVar('mid')));
Loading history...
102
103
        //Get user's groups
104
        $groups = Xoops::getInstance()->getUserGroups();
105
        $criteria2 = new CriteriaCompo();
106
        foreach ($groups as $gid) {
107
            $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
108
        }
109
        $criteria->add($criteria2);
110
111
        $qb = $this->db2->createXoopsQueryBuilder();
112
        $qb->select('gperm_itemid')->fromPrefix('system_permission', '');
113
        $criteria->renderQb($qb);
114
        $result = $qb->execute();
115
116
        while (false !== ($myrow = $result->fetch(PDO::FETCH_ASSOC))) {
117
            $ret[$myrow['gperm_itemid']] = $myrow['gperm_itemid'];
118
        }
119
        $items[$gperm_name] = $ret;
120
121
        return $ret;
122
    }
123
124
    /**
125
     * isGranted
126
     *
127
     * @param string $gperm_name permission name
128
     * @param int    $id         item id
129
     *
130
     * @return bool
131
     */
132
    public function isGranted($gperm_name, $id): ?bool
133
    {
134
        if (!$id) {
135
            return false;
136
        }
137
        $permissions = $this->getGrantedItems($gperm_name);
138
        if (!empty($permissions) && isset($permissions[$id])) {
139
            return true;
140
        }
141
142
        return false;
143
    }
144
145
    /**
146
     * Saves permissions for the selected category
147
     *  saveCategory_Permissions()
148
     *
149
     * @param array   $groups    group with granted permission
150
     * @param int $itemid    itemid on which we are setting permissions for Categories and Forums
151
     * @param string  $perm_name name of the permission
152
     *
153
     * @return bool : TRUE if the no errors occured
154
     *
155
     * @todo is this used anywhere?
156
     */
157
    public function saveItemPermissions($groups, $itemid, $perm_name): bool
158
    {
159
        $xoops = Xoops::getInstance();
160
        $result = true;
161
        $module_id = $this->helper->getModule()->getVar('mid');
162
        $gpermHandler = $xoops->getHandlerGroupPermission();
163
        // First, if the permissions are already there, delete them
164
        $gpermHandler->deleteByModule($module_id, $perm_name, $itemid);
165
        // Save the new permissions
166
        if (\count($groups) > 0) {
167
            foreach ($groups as $group_id) {
168
                echo $group_id . '-';
169
                echo $gpermHandler->addRight($perm_name, $itemid, $group_id, $module_id);
170
            }
171
        }
172
173
        return $result;
174
    }
175
176
    /**
177
     * Delete all permission for a specific item
178
     *  deletePermissions()
179
     *
180
     * @param int $itemid     id of the item for which to delete the permissions
181
     * @param string  $gperm_name permission name
182
     *
183
     * @return bool : TRUE if the no errors occured
184
     */
185
    public function deletePermissions($itemid, $gperm_name): bool
186
    {
187
        $xoops = Xoops::getInstance();
188
        $result = true;
189
        $gpermHandler = $xoops->getHandlerGroupPermission();
190
        $gpermHandler->deleteByModule($this->helper->getModule()->getVar('mid'), $gperm_name, $itemid);
191
192
        return $result;
193
    }
194
}
195