GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PermissionsProvider::applyPermissions()   D
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 34
Code Lines 24

Duplication

Lines 5
Ratio 14.71 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 5
loc 34
rs 4.909
c 1
b 0
f 0
cc 9
eloc 24
nc 10
nop 1
1
<?php
2
namespace Kunstmaan\Skylab\Provider;
3
4
use Cilex\Application;
5
use Kunstmaan\Skylab\Entity\PermissionDefinition;
6
7
/**
8
 * PermissionsProvider
9
 */
10
class PermissionsProvider extends AbstractProvider
11
{
12
13
    /**
14
     * Registers services on the given app.
15
     *
16
     * @param Application $app An Application instance
17
     */
18
    public function register(Application $app)
19
    {
20
        $app['permission'] = $this;
21
        $this->app = $app;
22
    }
23
24
    /**
25
     * @param string $groupName The group name
26
     */
27
    public function createGroupIfNeeded($groupName)
28
    {
29
        if (PHP_OS == "Darwin") {
30
            $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName);
31
            $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName . ' RealName ' . $groupName);
32
            $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName . " name " . $groupName);
33
            $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName . ' passwd "*"');
34
            $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName . ' PrimaryGroupID 20');
35
        } else {
36
            if (!$this->isGroup($groupName)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isGroup($groupName) of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
37
                $this->processProvider->executeSudoCommand('addgroup ' . $groupName);
38
            }
39
        }
40
    }
41
42
    /**
43
     * @param string $groupName The group name
44
     *
45
     * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
46
     */
47
    private function isGroup($groupName)
48
    {
49
        if (PHP_OS == "Darwin") {
50
            return $this->processProvider->executeSudoCommand('dscl . -list /groups | grep ^' . $groupName . '$', true);
51
        } else {
52
            return $this->processProvider->executeSudoCommand('cat /etc/group | egrep ^' . $groupName . ':', true);
53
        }
54
    }
55
56
    /**
57
     * @param string $userName  The user name
58
     * @param string $groupName The group name
59
     */
60
    public function createUserIfNeeded($userName, $groupName)
61
    {
62
        if (!$this->isUser($userName)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isUser($userName) of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
63
            if (PHP_OS == "Darwin") {
64
                $maxid = $this->processProvider->executeSudoCommand("dscl . list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1");
65
                $maxid = $maxid + 1;
66
                $this->processProvider->executeSudoCommand('dscl . create /Users/' . $userName);
67 View Code Duplication
                if (file_exists("/usr/local/bin/bash")){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
                    $this->processProvider->executeSudoCommand('dscl . create /Users/' . $userName . ' UserShell /usr/local/bin/bash');
69
                } else {
70
                    $this->processProvider->executeSudoCommand('dscl . create /Users/' . $userName . ' UserShell /bin/bash');
71
                }
72
                $this->processProvider->executeSudoCommand('dscl . create /Users/' . $userName . ' NFSHomeDirectory ' . $this->app["config"]["projects"]["path"] . "/" . $userName);
73
                $this->processProvider->executeSudoCommand('dscl . create /Users/' . $userName . ' PrimaryGroupID 20');
74
                $this->processProvider->executeSudoCommand('dscl . create /Users/' . $userName . ' UniqueID ' . $maxid);
75
                $this->processProvider->executeSudoCommand('dscl . append /Groups/' . $groupName . ' GroupMembership ' . $userName);
76
                $this->processProvider->executeSudoCommand('defaults write /Library/Preferences/com.apple.loginwindow HiddenUsersList -array-add ' . $userName);
77
            } else {
78
                $this->processProvider->executeSudoCommand('adduser --firstuid 1000 --lastuid 1999 --disabled-password --system --quiet --ingroup ' . $groupName . ' --home "'.$this->app["config"]["projects"]["path"] . "/" . $userName . '" --no-create-home --shell /bin/bash ' . $userName);
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param string $userName The user name
85
     *
86
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
87
     */
88
    private function isUser($userName)
89
    {
90
        return $this->processProvider->executeSudoCommand('id ' . $userName, true);
91
    }
92
93
    /**
94
     * @param \ArrayObject $project The project
95
     */
96
    public function applyOwnership(\ArrayObject $project)
97
    {
98
		$permissions_sorted = new \ArrayObject($project["permissions"]);
99
		$permissions_sorted->ksort();
100
		/** @var PermissionDefinition $pd */
101
		foreach ($permissions_sorted as $pd) {
102
            $thePath = $this->fileSystemProvider->getProjectDirectory($project["name"]) . $pd->getPath();
103
            if (!$pd->getOwnership()) {
104
                $this->dialogProvider->logNotice("No ownership information for " . $thePath . ", do not chown");
105
                continue;
106
            }
107
            if (!file_exists($thePath)) {
108
                $this->dialogProvider->logNotice($thePath . " does not exist, do not chown");
109
                continue;
110
            }
111
            $dirContainsNFS = $this->processProvider->executeSudoCommand("mount | grep $thePath | cat");
112
            if (!empty($dirContainsNFS)) {
113
                $this->dialogProvider->logNotice($thePath . " is on an NFS share, do not chown");
114
                continue;
115
            }
116
            $owner = $this->projectConfigProvider->searchReplacer($pd->getOwnership(), $project);
117
            if (PHP_OS == "Darwin") {
118
                $owner = str_replace(".", ":", $owner);
119
            }
120
            $this->processProvider->executeSudoCommand('chown -f ' . $owner . ' ' . $thePath);
121
        }
122
    }
123
124
    /**
125
     * @param \ArrayObject $project The project
126
     */
127
    public function applyPermissions(\ArrayObject $project)
128
    {
129
        if ($this->app["config"]["develmode"] || !$this->processProvider->commandExists("setfacl")) {
130
            if (!file_exists($this->fileSystemProvider->getProjectDirectory($project["name"]))) {
131
                $this->dialogProvider->logNotice($this->fileSystemProvider->getProjectDirectory($project["name"]) . " does not exist, do not chmod");
132
            } else {
133
                $this->processProvider->executeSudoCommand('chmod -R 777 ' . $this->fileSystemProvider->getProjectDirectory($project["name"]));
134
            }
135
            if (!file_exists($this->fileSystemProvider->getProjectDirectory($project["name"]) . '/.ssh/')) {
136
                $this->dialogProvider->logNotice($this->fileSystemProvider->getProjectDirectory($project["name"]) . '/.ssh/' . " does not exist, do not chmod");
137
            } else {
138
                $this->processProvider->executeSudoCommand('chmod -R 700 ' . $this->fileSystemProvider->getProjectDirectory($project["name"]) . '/.ssh/');
139
            }
140
        } else {
141
            $permissions_sorted = new \ArrayObject($project["permissions"]);
142
            $permissions_sorted->ksort();
143
            /** @var PermissionDefinition $pd */
144
            foreach ($permissions_sorted as $pd) {
145
                $path = $this->fileSystemProvider->getProjectDirectory($project["name"]) . $pd->getPath();
146
                foreach ($pd->getAcl() as $acl) {
147
                    if (file_exists($path)) {
148
                        $this->processProvider->executeSudoCommand('setfacl ' . $this->projectConfigProvider->searchReplacer($acl, $project) . ' ' . $path, true);
149
                    } else {
150
                        $this->dialogProvider->logNotice($path . " does not exist, do not chmod");
151
                    }
152
                }
153
            }
154
        }
155 View Code Duplication
        if (PHP_OS == "Darwin") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
156
            $this->processProvider->executeSudoCommand('find ' . $this->fileSystemProvider->getProjectDirectory($project["name"]) . '/ -type d -exec chmod o+rx {} "\;"');
157
        } else {
158
            $this->processProvider->executeSudoCommand('find ' . $this->fileSystemProvider->getProjectDirectory($project["name"]) . '/ -type d -exec chmod o+rx {} \;');
159
        }
160
    }
161
162
    /**
163
     * @param string $userName The user name
164
     */
165
    public function killProcesses($userName)
166
    {
167
        $this->processProvider->executeSudoCommand("su - " . $userName . " -c 'kill -9 -1'", true);
168
    }
169
170
    /**
171
     * @param string $userName  The user name
172
     * @param string $groupName The group name
173
     */
174
    public function removeUser($userName, $groupName)
175
    {
176
        if ($this->isUser($userName)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isUser($userName) of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
177 View Code Duplication
            if (PHP_OS == "Darwin") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
178
                $this->processProvider->executeSudoCommand('dscl . delete /Users/' . $userName);
179
                $this->processProvider->executeSudoCommand('dscl . delete /Groups/' . $groupName);
180
            } else {
181
                $this->processProvider->executeSudoCommand('userdel ' . $userName);
182
            }
183
        }
184
    }
185
}
186