Completed
Pull Request — develop (#716)
by Agel_Nash
11:45 queued 04:51
created

Permissions::checkPermissions()   C

Complexity

Conditions 15
Paths 8

Size

Total Lines 57
Code Lines 30

Duplication

Lines 4
Ratio 7.02 %

Importance

Changes 0
Metric Value
cc 15
eloc 30
nc 8
nop 0
dl 4
loc 57
rs 6.5498
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace EvolutionCMS\Legacy;
2
3
/**
4
 * @class: udperms
5
 */
6
class Permissions
7
{
8
    /**
9
     * @var int
10
     */
11
    public $user;
12
    /**
13
     * @var int
14
     */
15
    public $document;
16
    /**
17
     * @var int
18
     */
19
    public $role;
20
    /**
21
     * @var bool
22
     */
23
    public $duplicateDoc = false;
24
25
    /**
26
     * @return bool
27
     */
28
    public function checkPermissions()
0 ignored issues
show
Coding Style introduced by
checkPermissions uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
29
    {
30
31
        global $udperms_allowroot;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
32
        $modx = evolutionCMS();
33
34
        $tblsc = $modx->getFullTableName('site_content');
35
        $tbldg = $modx->getFullTableName('document_groups');
36
        $tbldgn = $modx->getFullTableName('documentgroup_names');
37
38
        $document = $this->document;
39
        $role = $this->role;
40
41
        if ($role == 1) {
42
            return true;  // administrator - grant all document permissions
43
        }
44
45
        if ($modx->config['use_udperms'] == 0 || $modx->config['use_udperms'] == "" || !isset($modx->config['use_udperms'])) {
46
            return true; // permissions aren't in use
47
        }
48
49
        $parent = $modx->db->getValue($modx->db->select('parent', $tblsc, "id='{$this->document}'"));
50 View Code Duplication
        if ($document == 0 && $parent == null && $udperms_allowroot == 1) {
51
            return true;
52
        } // User is allowed to create new document in root
53 View Code Duplication
        if (($this->duplicateDoc == true || $document == 0) && $parent == 0 && $udperms_allowroot == 0) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
54
            return false; // deny duplicate || create new document at root if Allow Root is No
55
        }
56
57
        // get document groups for current user
58
        $docgrp = empty($_SESSION['mgrDocgroups']) ? '' : implode(' || dg.document_group = ',
59
            $_SESSION['mgrDocgroups']);
60
61
        /* Note:
62
            A document is flagged as private whenever the document group that it
63
            belongs to is assigned or links to a user group. In other words if
64
            the document is assigned to a document group that is not yet linked
65
            to a user group then that document will be made public. Documents that
66
            are private to the manager users will not be private to web users if the
67
            document group is not assigned to a web user group and visa versa.
68
         */
69
        $permissionsok = false;  // set permissions to false
70
71
        $rs = $modx->db->select(
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $rs. 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
            'count(DISTINCT sc.id)',
73
            "{$tblsc} AS sc 
74
				LEFT JOIN {$tbldg} AS dg on dg.document = sc.id 
75
				LEFT JOIN {$tbldgn} dgn ON dgn.id = dg.document_group",
76
            "sc.id='{$this->document}' AND (" . (empty($docgrp) ? '' : "dg.document_group = " . $docgrp . " ||") . " sc.privatemgr = 0)"
77
        );
78
        $limit = $modx->db->getValue($rs);
79
        if ($limit == 1) {
80
            $permissionsok = true;
81
        }
82
83
        return $permissionsok;
84
    }
85
}
86