Completed
Pull Request — develop (#716)
by Agel_Nash
06:39
created

Permissions   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 80
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 4
loc 80
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C checkPermissions() 4 57 15

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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