Conditions | 15 |
Paths | 8 |
Total Lines | 57 |
Code Lines | 30 |
Lines | 4 |
Ratio | 7.02 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php namespace EvolutionCMS\Legacy; |
||
28 | public function checkPermissions() |
||
|
|||
29 | { |
||
30 | |||
31 | global $udperms_allowroot; |
||
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) { |
|
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( |
||
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 |
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: