Completed
Push — master ( 0030fe...fe94fe )
by
unknown
01:54
created

DataObjectSortBaseClass::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
4
5
6
class DataObjectSortBaseClass extends Controller implements PermissionProvider
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...
7
{
8
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
        "show" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION'
10
    );
11
12
    /**
13
     * Permission for user management.
14
     *
15
     * @var string
16
     */
17
    const CAN_DO_STUFF = 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION';
18
19
    public function providePermissions()
20
    {
21
        return array(
22
            DataObjectSortBaseClass::CAN_DO_STUFF => array(
23
                'name' => _t(
24
                    'DataObjectSortBaseClass.PERMISSION_MANAGE_USERS_DESCRIPTION',
25
                    'Quick updates and edits'
26
                ),
27
                'help' => _t(
28
                    'DataObjectSortBaseClass.PERMISSION_MANAGE_USERS_HELP',
29
                    'Allows for certain data to be sorted, edited, etc... This is around quick edits'
30
                ),
31
                'category' => _t('DataObjectSortBaseClass.PERMISSIONS_CATEGORY', 'Miscellaneous'),
32
                'sort' => 100
33
            )
34
        );
35
    }
36
37
38
    public function init()
39
    {
40
        // Only administrators can run this method
41
        parent::init();
42
        if (! Permission::check("DATA_OBJECT_SORT_AND_EDIT_PERMISSION")) {
43
            return $this->permissionFailureStandard();
44
        }
45
    }
46
47
    public function show()
48
    {
49
        return array();
50
    }
51
52
53
    /**
54
     *
55
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
56
     */
57
    protected function SecureFieldToBeUpdated()
0 ignored issues
show
Coding Style introduced by
SecureFieldToBeUpdated uses the super-global variable $_POST 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...
58
    {
59
        if (isset($_POST["Field"])) {
60
            return addslashes($_POST["Field"]);
61
        }
62
        $field = $this->getRequest()->param("OtherID");
63
        if ($table = $this->SecureTableToBeUpdated()) {
64
            if ($tableObject = DataObject::get_one($table)) {
65
                if ($tableObject->hasDatabaseField($field)) {
66
                    return $field;
67
                } else {
68
                    user_error("$field does not exist on $table", E_USER_ERROR);
69
                }
70
            } else {
71
                user_error("there are no records in $table", E_USER_ERROR);
72
            }
73
        } else {
74
            user_error("there is no table specified", E_USER_ERROR);
75
        }
76
    }
77
78
    /**
79
     *
80
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
81
     */
82
    protected function SecureTableToBeUpdated()
0 ignored issues
show
Coding Style introduced by
SecureTableToBeUpdated uses the super-global variable $_POST 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...
83
    {
84
        if (isset($_POST["Table"])) {
85
            $table = addslashes($_POST["Table"]);
86
        } else {
87
            $table = $this->getRequest()->param("ID");
88
        }
89
        if (class_exists($table)) {
90
            return $table;
91
        } else {
92
            user_error("could not find record: $table", E_USER_ERROR);
93
        }
94
    }
95
96
97
    /**
98
     *
99
     * @return int
100
     */
101
    protected function SecureRecordToBeUpdated()
0 ignored issues
show
Coding Style introduced by
SecureRecordToBeUpdated uses the super-global variable $_POST 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...
Coding Style introduced by
SecureRecordToBeUpdated uses the super-global variable $_GET 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...
102
    {
103
        if (isset($_POST["Record"])) {
104
            return intval($_POST["Record"]);
105
        }
106
        if (isset($_GET["id"])) {
107
            $record = $_GET["id"];
108
            return intval($record);
109
        }
110
        return 0;
111
    }
112
113
114
    /**
115
     *
116
     *
117
     * @param  DataObject $obj       [description]
118
     * @param  string $fieldName     [description]
119
     * @return FormField
120
     */
121
    protected function getFormField($obj, $fieldName)
122
    {
123
        if (!self::$field) {
124
            self::$field  = $obj->dbObject($fieldName)->scaffoldFormField($obj->Title);
125
        }
126
        return self::$field;
127
    }
128
129
    /**
130
     *
131
     * @return string
132
     */
133
    protected function HumanReadableTableName()
134
    {
135
        return singleton($this->SecureTableToBeUpdated())->plural_name();
136
    }
137
138
    /**
139
     *
140
     * @return string
141
     */
142
    public function Link($action = null)
143
    {
144
        $link = Config::inst()->get($this->class, 'url_segment').'/';
145
        if ($action) {
146
            $link .= "$action/";
147
        }
148
        return $link;
149
    }
150
151
    public function permissionFailureStandard()
152
    {
153
        return Security::permissionFailure($this, _t('Security.PERMFAILURE', ' This page is secured and you need administrator rights to access it. Enter your credentials below and we will send you right along.'));
154
    }
155
}
156