Completed
Pull Request — master (#174)
by Vladimir
03:51
created

AdminController::landingAction()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 14
nc 3
nop 1
1
<?php
2
3
/**
4
 * @todo Configure the AdminController to be behind a Symfony firewall
5
 */
6
class AdminController extends HTMLController
7
{
8
    public function listAction()
9
    {
10
        $rolesToDisplay = Role::getLeaderRoles();
11
        $roles = array();
12
13
        foreach ($rolesToDisplay as $role) {
14
            $roleMembers = $role->getUsers();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Model as the method getUsers() does only exist in the following sub-classes of Model: Role. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
15
16
            if (count($roleMembers) > 0) {
17
                $roles[] = array(
18
                    "role"    => $role,
19
                    "members" => $roleMembers
20
                );
21
            }
22
        }
23
24
        return array("role_sections" => $roles);
25
    }
26
27
    public function landingAction(Player $me)
28
    {
29
        if (!$me->isValid()) {
30
            throw new ForbiddenException('Please log in to view this page.');
31
        }
32
33
        // @todo Model editing should be a generic permission
34
        $canViewModelEditor = true;
35
        $canViewPageEditor = $this->isEditorFor(Page::class, $me);
36
        $canViewRoleEditor = $this->isEditorFor(Role::class, $me);
37
        $canViewVisitLog   = $me->hasPermission(Permission::VIEW_VISITOR_LOG);
38
39
        if (!$canViewPageEditor && !$canViewRoleEditor && !$canViewVisitLog) {
40
            throw new ForbiddenException('Contact a site administrator if you feel you should have access to this page.');
41
        }
42
43
        return [
44
            'canViewPageEditor' => $canViewPageEditor,
45
            'canViewRoleEditor' => $canViewRoleEditor,
46
            'canViewModelEditor' => $canViewModelEditor,
47
            'canViewVisitLog' => $canViewVisitLog,
48
        ];
49
    }
50
51 View Code Duplication
    public function pageListAction(Player $me)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        if (!$me->isValid()) {
54
            throw new ForbiddenException('Please log in to view this page.');
55
        }
56
57
        if (!$this->isEditorFor(Page::class, $me)) {
58
            throw new ForbiddenException('Contact a site administrator if you feel you should have access to this page.');
59
        }
60
61
        $pages = Page::getQueryBuilder()
62
            ->where('status')->notEquals('deleted')
63
            ->getModels(true)
64
        ;
65
66
        return [
67
            'pages' => $pages,
68
            'canCreate' => $me->hasPermission(Page::CREATE_PERMISSION),
69
            'canEdit' => $me->hasPermission(Page::EDIT_PERMISSION),
70
            'canDelete' => $me->hasPermission(Page::SOFT_DELETE_PERMISSION),
71
            'canWipe' => $me->hasPermission(Page::HARD_DELETE_PERMISSION),
72
        ];
73
    }
74
75 View Code Duplication
    public function roleListAction(Player $me)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        if (!$me->isValid()) {
78
            throw new ForbiddenException('Please log in to view this page.');
79
        }
80
81
        if (!$this->isEditorFor(Role::class, $me)) {
82
            throw new ForbiddenException('Contact a site administrator if you feel you should have access to this page.');
83
        }
84
85
        $roles = Role::getQueryBuilder()
86
            ->sortBy('display_order')
87
            ->getModels($fast = true)
88
        ;
89
90
        return [
91
            'roles' => $roles,
92
            'canCreate' => $me->hasPermission(Role::CREATE_PERMISSION),
93
            'canEdit' => $me->hasPermission(Role::EDIT_PERMISSION),
94
            'canDelete' => $me->hasPermission(Role::SOFT_DELETE_PERMISSION),
95
            'canWipe' => $me->hasPermission(Role::HARD_DELETE_PERMISSION),
96
        ];
97
    }
98
99
    public function wipeAction(Player $me)
100
    {
101
        $canViewThisPage = false;
102
        $wipeable = array('Ban', 'Map', 'Match', 'News', 'NewsCategory', 'Page', 'Server', 'Team');
103
        $models   = array();
104
105
        foreach ($wipeable as $type) {
106
            if (!$me->hasPermission($type::HARD_DELETE_PERMISSION)) {
107
                continue;
108
            }
109
110
            $canViewThisPage = true;
111
            $models = array_merge($models, $type::getQueryBuilder()
112
                ->where('status')->equals('deleted')
113
                ->getModels());
114
        }
115
116
        // Permission checking
117
        if (!$me->isValid()) {
118
            throw new ForbiddenException("Please log in to view this page.");
119
        }
120
        if (!$canViewThisPage) {
121
            throw new ForbiddenException("Contact a site administrator if you feel you should have access to this page.");
122
        }
123
124
        return array('models' => $models);
125
    }
126
127
    private function isEditorFor($className, Player $me)
128
    {
129
        $permissionConstants = [
130
            'CREATE_PERMISSION',
131
            'EDIT_PERMISSION',
132
            'SOFT_DELETE_PERMISSION',
133
            'HARD_DELETE_PERMISSION',
134
        ];
135
136
        $reflector = new ReflectionClass($className);
137
138
        foreach ($permissionConstants as $permission) {
139
            $permissionName = $reflector->getConstant($permission);
140
141
            if ($me->hasPermission($permissionName)) {
142
                return true;
143
            }
144
        }
145
146
        return false;
147
    }
148
}
149