AlumniPermissionHandler   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getGrantedGroupsById() 0 23 3
B getGrantedItems() 0 30 4
A isGranted() 0 12 4
A saveItemPermissions() 0 17 3
A deletePermissions() 0 8 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 34 and the first side effect is on line 29.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
use Xoops\Core\Kernel\XoopsObjectHandler;
13
use Xoops\Core\Kernel\Criteria;
14
use Xoops\Core\Kernel\CriteriaCompo;
15
16
/**
17
 *  Alumni class
18
 *
19
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
20
 * @license         GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
21
 * @package         Class
22
 * @subpackage      Handlers
23
 * @since           1.0
24
 * @author          trabis <[email protected]>
25
 * @author          The SmartFactory <www.smartfactory.ca>
26
 * @version         $Id$
27
 */
28
29
include_once dirname(__DIR__) . '/include/common.php';
30
31
/**
32
 * Class AlumniPermissionHandler
33
 */
34
class AlumniPermissionHandler extends XoopsObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
35
{
36
    /**
37
     * @var Alumni
38
     * @access public
39
     */
40
    public $alumni = null;
41
42
    /**
43
     * constructor
44
     */
45
    public function __construct()
46
    {
47
        $this->alumni = Alumni::getInstance();
48
        $this->db2    = \Xoops::getInstance()->db();
49
    }
50
51
    /**
52
     * Returns permissions for a certain type
53
     *
54
     * @param string $gperm_name "global", "forum" or "topic" (should perhaps have "post" as well - but I don't know)
55
     * @param int    $id         id of the item (forum, topic or possibly post) to get permissions for
56
     *
57
     * @return array
58
     */
59
    public function getGrantedGroupsById($gperm_name, $id)
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
60
    {
61
        static $items;
62
        if (isset($items[$gperm_name][$id])) {
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
63
            return $items[$gperm_name][$id];
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
64
        }
65
        $groups   = [];
66
        $criteria = new CriteriaCompo();
67
        $criteria->add(new Criteria('gperm_modid', $this->alumni->getModule()->getVar('mid')));
68
        $criteria->add(new Criteria('gperm_name', $gperm_name));
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
69
        $criteria->add(new Criteria('gperm_itemid', $id));
70
        //Instead of calling groupperm handler and get objects, we will save some memory and do it our way
71
        $qb = $this->db2->createXoopsQueryBuilder();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $qb. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
72
        $qb->select('gperm_groupid')->fromPrefix('group_permission', '');
73
        $criteria->renderQb($qb);
74
        $result = $qb->execute();
75
76
        while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
77
            $groups[$myrow['gperm_groupid']] = $myrow['gperm_groupid'];
78
        }
79
        $items[$gperm_name][$id] = $groups;
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
80
        return $groups;
81
    }
82
83
    /**
84
     * Returns permissions for a certain type
85
     *
86
     * @param string $gperm_name "global", "forum" or "topic" (should perhaps have "post" as well - but I don't know)
87
     *
88
     * @return array
89
     */
90
    public function getGrantedItems($gperm_name)
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
91
    {
92
        static $items;
93
        if (isset($items[$gperm_name])) {
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
94
            return $items[$gperm_name];
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
95
        }
96
        $ret = [];
97
        //Instead of calling groupperm handler and get objects, we will save some memory and do it our way
98
        $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
99
        $criteria->add(new Criteria('gperm_modid', $this->alumni->getModule()->getVar('mid')));
100
101
        //Get user's groups
102
        $groups    = \Xoops::getInstance()->getUserGroups();
103
        $criteria2 = new CriteriaCompo();
104
        foreach ($groups as $gid) {
105
            $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
106
        }
107
        $criteria->add($criteria2);
108
109
        $qb = $this->db2->createXoopsQueryBuilder();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $qb. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
110
        $qb->select('gperm_itemid')->fromPrefix('group_permission', '');
111
        $criteria->renderQb($qb);
112
        $result = $qb->execute();
113
114
        while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
115
            $ret[$myrow['gperm_itemid']] = $myrow['gperm_itemid'];
116
        }
117
        $items[$gperm_name] = $ret;
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
118
        return $ret;
119
    }
120
121
    /**
122
     * isGranted
123
     *
124
     * @param string $gperm_name permission name
125
     * @param int    $id         item id
126
     *
127
     * @return bool
128
     */
129
    public function isGranted($gperm_name, $id)
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
130
    {
131
        if (!$id) {
132
            return false;
133
        }
134
        $permissions = $this->getGrantedItems($gperm_name);
0 ignored issues
show
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
135
        if (!empty($permissions) && isset($permissions[$id])) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return !empty($permissio...set($permissions[$id]);.
Loading history...
136
            return true;
137
        } else {
138
            return false;
139
        }
140
    }
141
142
    /**
143
     * Saves permissions for the selected category
144
     *  saveCategory_Permissions()
145
     *
146
     * @param array   $groups    group with granted permission
147
     * @param integer $itemid    itemid on which we are setting permissions for Categories and Forums
148
     * @param string  $perm_name name of the permission
149
     *
150
     * @return boolean : TRUE if the no errors occured
151
     *
152
     * @todo is this used anywhere?
153
     */
154
    public function saveItemPermissions($groups, $itemid, $perm_name)
0 ignored issues
show
Coding Style introduced by
function saveItemPermissions() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
$perm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
155
    {
156
        $xoops         = Xoops::getInstance();
157
        $result        = true;
158
        $module_id     = $this->alumni->getModule()->getVar('mid');
0 ignored issues
show
Coding Style introduced by
$module_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
159
        $gperm_handler = $xoops->getHandlerGroupPermission();
0 ignored issues
show
Coding Style introduced by
$gperm_handler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
160
        // First, if the permissions are already there, delete them
161
        $gperm_handler->deleteByModule($module_id, $perm_name, $itemid);
0 ignored issues
show
Coding Style introduced by
$gperm_handler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
162
        // Save the new permissions
163
        if (count($groups) > 0) {
164
            foreach ($groups as $group_id) {
0 ignored issues
show
Coding Style introduced by
$group_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
165
                echo $group_id . '-';
0 ignored issues
show
Coding Style introduced by
$group_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
166
                echo $gperm_handler->addRight($perm_name, $itemid, $group_id, $module_id);
0 ignored issues
show
Coding Style introduced by
$gperm_handler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
167
            }
168
        }
169
        return $result;
170
    }
171
172
    /**
173
     * Delete all permission for a specific item
174
     *  deletePermissions()
175
     *
176
     * @param integer $itemid     id of the item for which to delete the permissions
177
     * @param string  $gperm_name permission name
178
     *
179
     * @return boolean : TRUE if the no errors occured
180
     */
181
    public function deletePermissions($itemid, $gperm_name)
0 ignored issues
show
Coding Style introduced by
function deletePermissions() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
$gperm_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
182
    {
183
        $xoops         = Xoops::getInstance();
184
        $result        = true;
185
        $gperm_handler = $xoops->getHandlerGroupPermission();
0 ignored issues
show
Coding Style introduced by
$gperm_handler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
186
        $gperm_handler->deleteByModule($this->alumni->getModule()->getVar('mid'), $gperm_name, $itemid);
0 ignored issues
show
Coding Style introduced by
$gperm_handler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
187
        return $result;
188
    }
189
}
190