User::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 18
cp 0
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * admin
5
 *
6
 * @category    Tollwerk
7
 * @package     Tollwerk\Admin
8
 * @subpackage  Tollwerk\Admin\Infrastructure\Shell
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Tollwerk\Admin\Infrastructure\Shell;
38
39
use Tollwerk\Admin\Infrastructure\App;
40
41
/**
42
 * User related commands
43
 *
44
 * @package    Tollwerk\Admin
45
 * @subpackage Tollwerk\Admin\Infrastructure
46
 */
47
class User extends AbstractCommand
48
{
49
    /**
50
     * Create a new system user
51
     *
52
     * @param string $user User name
53
     *
54
     * @return string Output
55
     */
56
    public static function create($user)
57
    {
58
        $user    = self::validateUser($user);
59
        $command = Binary::sudo('useradd');
60
        $command->addArg('--gid', App::getConfig('general.group'));
61
        $command->addArg('--shell', '/bin/false');
62
        $command->addArg('--base-dir', App::getConfig('general.basedir'));
63
        $command->addArg('--create-home');
64
        $command->addArg('--skel', dirname(__DIR__).DIRECTORY_SEPARATOR.'Skeleton');
65
        $command->addArg($user);
66
67
        try {
68
            return self::run($command);
69
        } catch (Exception $e) {
70
            // If the user already exists
71
            if ($e->getCode() == 9) {
72
                return '';
73
            }
74
            throw $e;
75
        }
76
    }
77
78
    /**
79
     * Validate a user name
80
     *
81
     * @param string $user User name
82
     *
83
     * @return string Validated user
84
     * @throws \RuntimeException If the user name is invalid
85
     */
86
    protected static function validateUser($user)
87
    {
88
        $user = trim($user);
89
90
        // If the user name is invalid
91
        if (!strlen($user) || !preg_match('%^[a-z][a-z0-9]{2,}$%', $user)) {
92
            throw  new \RuntimeException(sprintf('Invalid user name "%s"', $user), 1475514940);
93
        }
94
95
        return $user;
96
    }
97
98
    /**
99
     * Rename a system user
100
     *
101
     * @param string $olduser Old user name
102
     * @param string $newuser New user name
103
     */
104
    public static function rename($olduser, $newuser)
105
    {
106
        $olduser = self::validateUser($olduser);
107
        $newuser = self::validateUser($newuser);
108
        $command = Binary::sudo('usermod');
109
        $command->addArg('--home', App::getConfig('general.basedir').DIRECTORY_SEPARATOR.$newuser);
110
        $command->addArg('--move-home');
111
        $command->addArg('--login', $newuser);
112
        $command->addArg($olduser);
113
114
        return self::run($command);
115
    }
116
117
    /**
118
     * Delete a system user
119
     *
120
     * @param string $user User name
121
     */
122
    public static function delete($user)
123
    {
124
        $user    = self::validateUser($user);
125
        $command = Binary::sudo('userdel');
126
        $command->addArg($user);
127
128
        return self::run($command);
129
    }
130
131
    /**
132
     * Add a system user to a user group
133
     *
134
     * @param string $user  User name
135
     * @param string $group Group name
136
     */
137
    public static function addGroup($user, $group)
138
    {
139
        $user    = self::validateUser($user);
140
        $group   = self::validateUser($group);
141
        $command = Binary::sudo('usermod');
142
        $command->addArg('--append');
143
        $command->addArg('--groups', $group);
144
        $command->addArg($user);
145
146
        return self::run($command);
147
    }
148
149
    /**
150
     * Delete a system user from a user group
151
     *
152
     * @param string $user  User name
153
     * @param string $group Group name
154
     */
155
    public static function deleteGroup($user, $group)
156
    {
157
        $user  = self::validateUser($user);
158
        $group = self::validateUser($group);
159
160
        // Get the current user groups
161
        $command = Binary::sudo('id');
162
        $command->addArg('--groups');
163
        $command->addArg('--name');
164
        $command->addArg($user);
165
166
        $groups    = preg_split('%[^a-z]+%', self::run($command));
167
        $newgroups = array_diff($groups, [$group]);
168
169
        $command = Binary::sudo('usermod');
170
        $command->addArg('--groups', implode(',', $newgroups));
171
        $command->addArg($user);
172
173
        return self::run($command);
174
    }
175
}
176