Completed
Push — master ( ae0105...972fe6 )
by Joschi
04:33
created

User::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
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 © 2016 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 © 2016 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
     * @return string Output
54
     */
55
    public static function create($user)
56
    {
57
        $user = self::validateUser($user);
58
        $command = Binary::sudo('useradd');
59
        $command->addArg('--gid', App::getConfig('general.group'));
60
        $command->addArg('--shell', '/bin/false');
61
        $command->addArg('--base-dir', App::getConfig('general.basedir'));
62
        $command->addArg('--create-home');
63
        $command->addArg('--skel', dirname(__DIR__).DIRECTORY_SEPARATOR.'Skeleton');
64
        $command->addArg($user);
65
66
        try {
67
            return self::run($command);
68
        } catch (Exception $e) {
69
            // If the user already exists
70
            if ($e->getCode() == 9) {
71
                return '';
72
            }
73
            throw $e;
74
        }
75
    }
76
77
    /**
78
     * Validate a user name
79
     *
80
     * @param string $user User name
81
     * @return string Validated user
82
     * @throws \RuntimeException If the user name is invalid
83
     */
84
    protected static function validateUser($user)
85
    {
86
        $user = trim($user);
87
88
        // If the user name is invalid
89
        if (!strlen($user) || !preg_match('%^[a-z]+$%', $user)) {
90
            throw  new \RuntimeException(sprintf('Invalid user name "%s"', $user), 1475514940);
91
        }
92
        return $user;
93
    }
94
95
    /**
96
     * Rename a system user
97
     *
98
     * @param string $olduser Old user name
99
     * @param string $newuser New user name
100
     */
101
    public static function rename($olduser, $newuser)
102
    {
103
        $olduser = self::validateUser($olduser);
104
        $newuser = self::validateUser($newuser);
105
        $command = Binary::sudo('usermod');
106
        $command->addArg('--home', App::getConfig('general.basedir').DIRECTORY_SEPARATOR.$newuser);
107
        $command->addArg('--move-home');
108
        $command->addArg('--login', $newuser);
109
        $command->addArg($olduser);
110
111
        return self::run($command);
112
    }
113
114
    /**
115
     * Delete a system user
116
     *
117
     * @param string $user User name
118
     */
119
    public static function delete($user)
120
    {
121
        $user = self::validateUser($user);
122
        $command = Binary::sudo('userdel');
123
        $command->addArg($user);
124
125
        return self::run($command);
126
    }
127
128
    /**
129
     * Add a system user to a user group
130
     *
131
     * @param string $user User name
132
     * @param string $group Group name
133
     */
134
    public static function addGroup($user, $group)
135
    {
136
        $user = self::validateUser($user);
137
        $group = self::validateUser($group);
138
        $command = Binary::sudo('usermod');
139
        $command->addArg('--append');
140
        $command->addArg('--groups', $group);
141
        $command->addArg($user);
142
143
        return self::run($command);
144
    }
145
146
    /**
147
     * Delete a system user from a user group
148
     *
149
     * @param string $user User name
150
     * @param string $group Group name
151
     */
152
    public static function deleteGroup($user, $group)
153
    {
154
        $user = self::validateUser($user);
155
        $group = self::validateUser($group);
156
157
        // Get the current user groups
158
        $command = Binary::sudo('id');
159
        $command->addArg('--groups');
160
        $command->addArg('--name');
161
        $command->addArg($user);
162
163
        $groups = preg_split('%[^a-z]+%', self::run($command));
164
        $newgroups = array_diff($groups, [$group]);
165
166
        $command = Binary::sudo('usermod');
167
        $command->addArg('--groups', implode(',', $newgroups));
168
        $command->addArg($user);
169
170
        return self::run($command);
171
    }
172
}
173