PermissionHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Smartobject;
2
3
/**
4
 *
5
 * Module: SmartObject
6
 * Author: The SmartFactory <www.smartfactory.ca>
7
 * Credits: Mithrandir
8
 * Licence: GNU
9
 */
10
11
use XoopsDatabase;
12
use XoopsModules\Smartobject;
13
14
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
15
16
/**
17
 * Class SmartobjectPermissionHandler
18
 * @package XoopsModules\Smartobject
19
 */
20
class PermissionHandler extends \XoopsObjectHandler
21
{
22
    public $handler;
23
24
    /**
25
     * SmartobjectPermissionHandler constructor.
26
     * @param \XoopsDatabase $handler
27
     */
28
    public function __construct($handler)
29
    {
30
        $this->handler = $handler;
31
    }
32
33
    /*
34
     * Returns permissions for a certain type
35
     *
36
     * @param string $type "global", "forum" or "topic" (should perhaps have "post" as well - but I don't know)
37
     * @param int    $id   id of the item (forum, topic or possibly post) to get permissions for
38
     *
39
     * @return array
40
     */
41
    /**
42
     * @param        $gperm_name
43
     * @param  null  $id
44
     * @return array
45
     */
46
    public function getGrantedGroups($gperm_name, $id = null)
47
    {
48
        static $groups;
49
50
        if (!isset($groups[$gperm_name]) || (null !== $id && !isset($groups[$gperm_name][$id]))) {
51
            $smartModule = $this->handler->getModuleInfo();
52
            //Get group permissions handler
53
            $gpermHandler = xoops_getHandler('groupperm');
54
55
            //Get groups allowed for an item id
56
            $allowedgroups            = $gpermHandler->getGroupIds($gperm_name, $id, $smartModule->getVar('mid'));
57
            $groups[$gperm_name][$id] = $allowedgroups;
58
        }
59
60
        //Return the permission array
61
        return isset($groups[$gperm_name][$id]) ? $groups[$gperm_name][$id] : [];
62
    }
63
64
    /**
65
     * @param        $item_ids_array
66
     * @param  bool  $gperm_name
67
     * @return array
68
     */
69
    public function getGrantedGroupsForIds($item_ids_array, $gperm_name = false)
70
    {
71
        static $groups;
72
73
        if ($gperm_name) {
74
            if (isset($groups[$gperm_name])) {
75
                return $groups[$gperm_name];
76
            }
77
        } else {
78
            // if !$gperm_name then we will fetch all permissions in the module so we don't need them again
79
            return $groups;
80
        }
81
82
        $smartModule = $this->handler->getModuleInfo();
83
84
        $criteria = new \CriteriaCompo();
85
        $criteria->add(new \Criteria('gperm_modid', $smartModule->getVar('mid')));
86
87
        if ($gperm_name) {
88
            $criteria->add(new \Criteria('gperm_name', $gperm_name));
89
        }
90
91
        //Get group permissions handler
92
        $gpermHandler = xoops_getHandler('groupperm');
93
94
        $permissionsObj = $gpermHandler->getObjects($criteria);
95
96
        foreach ($permissionsObj as $permissionObj) {
97
            $groups[$permissionObj->getVar('gperm_name')][$permissionObj->getVar('gperm_itemid')][] = $permissionObj->getVar('gperm_groupid');
98
        }
99
100
        //Return the permission array
101
        if ($gperm_name) {
102
            return isset($groups[$gperm_name]) ? $groups[$gperm_name] : [];
103
        } else {
104
            return isset($groups) ? $groups : [];
105
        }
106
    }
107
108
    /*
109
     * Returns permissions for a certain type
110
     *
111
     * @param string $type "global", "forum" or "topic" (should perhaps have "post" as well - but I don't know)
112
     * @param int    $id   id of the item (forum, topic or possibly post) to get permissions for
113
     *
114
     * @return array
115
     */
116
    /**
117
     * @param        $gperm_name
118
     * @param  null  $id
119
     * @return array
120
     */
121
    public function getGrantedItems($gperm_name, $id = null)
122
    {
123
        global $xoopsUser;
124
        static $permissions;
125
126
        if (!isset($permissions[$gperm_name]) || (null !== $id && !isset($permissions[$gperm_name][$id]))) {
127
            $smartModule = $this->handler->getModuleInfo();
128
129
            if (is_object($smartModule)) {
130
131
                //Get group permissions handler
132
                $gpermHandler = xoops_getHandler('groupperm');
133
134
                //Get user's groups
135
                $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : [XOOPS_GROUP_ANONYMOUS];
136
137
                //Get all allowed item ids in this module and for this user's groups
138
                $userpermissions          = $gpermHandler->getItemIds($gperm_name, $groups, $smartModule->getVar('mid'));
139
                $permissions[$gperm_name] = $userpermissions;
140
            }
141
        }
142
143
        //Return the permission array
144
        return isset($permissions[$gperm_name]) ? $permissions[$gperm_name] : [];
145
    }
146
147
    /**
148
     * @param $id
149
     */
150
    public function storeAllPermissionsForId($id)
151
    {
152
        foreach ($this->handler->getPermissions() as $permission) {
153
            $this->saveItem_Permissions($_POST[$permission['perm_name']], $id, $permission['perm_name']);
154
        }
155
    }
156
157
    /**
158
     * Saves permissions for the selected item
159
     *
160
     *  saveItem_Permissions()
161
     *
162
     * @param  array  $groups    : group with granted permission
163
     * @param  int    $itemid    categoryID on which we are setting permissions for Categories and Forums
164
     * @param  string $perm_name : name of the permission
165
     * @return bool   : TRUE if the no errors occured
166
     */
167
168
    public function saveItem_Permissions($groups, $itemid, $perm_name)
169
    {
170
        $smartModule = $this->handler->getModuleInfo();
171
172
        $result       = true;
173
        $module_id    = $smartModule->getVar('mid');
174
        $gpermHandler = xoops_getHandler('groupperm');
175
176
        // First, if the permissions are already there, delete them
177
        $gpermHandler->deleteByModule($module_id, $perm_name, $itemid);
178
        //echo "itemid: $itemid - perm: $perm_name - modid: $module_id";
179
        //exit;
180
        // Save the new permissions
181
182
        if (count($groups) > 0) {
183
            foreach ($groups as $group_id) {
184
                $gpermHandler->addRight($perm_name, $itemid, $group_id, $module_id);
185
            }
186
        }
187
188
        return $result;
189
    }
190
191
    /**
192
     * Delete all permission for a specific item
193
     *
194
     *  deletePermissions()
195
     *
196
     * @param  integer $itemid : id of the item for which to delete the permissions
197
     * @param          $gperm_name
198
     * @return bool:   TRUE if the no errors occured
0 ignored issues
show
Documentation introduced by
The doc-type bool: could not be parsed: Unknown type name "bool:" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
199
     */
200
    public function deletePermissions($itemid, $gperm_name)
201
    {
202
        global $xoopsModule;
203
204
        $smartModule = smartsection_getModuleInfo();
205
206
        $result       = true;
207
        $module_id    = $smartModule->getVar('mid');
208
        $gpermHandler = xoops_getHandler('groupperm');
209
210
        $gpermHandler->deleteByModule($module_id, $gperm_name, $itemid);
211
212
        return $result;
213
    }
214
215
    /**
216
     * Checks if the user has access to a specific permission on a given object
217
     *
218
     * @param  string $gperm_name   name of the permission to test
219
     * @param  int    $gperm_itemid id of the object to check
220
     * @return boolean: TRUE if user has access, FALSE if not
0 ignored issues
show
Documentation introduced by
The doc-type boolean: could not be parsed: Unknown type name "boolean:" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
221
     **/
222
    public function accessGranted($gperm_name, $gperm_itemid)
223
    {
224
        global $xoopsUser;
225
226
        $gperm_groupid = is_object($xoopsUser) ? $xoopsUser->getGroups() : [XOOPS_GROUP_ANONYMOUS];
227
        $smartModule   = $this->handler->getModuleInfo();
228
        $gperm_modid   = $smartModule->getVar('mid');
229
230
        //Get group permissions handler
231
        $gpermHandler = xoops_getHandler('groupperm');
232
233
        return $gpermHandler->checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid);
234
    }
235
}
236