summaryrefslogtreecommitdiffstats
path: root/source/Command/User/Edit.php
blob: 72467ee880d780a8cb43872308c3a2ecf86d63bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2014-08-13 
 */

/**
 * Class Command_User_Edit
 */
class Command_User_Edit extends Command_User_AbstractCommand
{
    /**
     * @var array
     */
    private $inputChannels;

    /**
     * @var string
     */
    private $inputName;

    /**
     * @var string
     */
    private $inputPassword;

    /**
     * @var string
     */
    private $inputRole;

    /**
     * @var int
     */
    private $inputId;

    /**
     * @throws Exception
     */
    public function execute()
    {
        reset($this->users);

        $lines = $this->file->read();
        $content = array();
        $contentAfterCurrentUser = array();
        $string = new String();

        $foundCurrentUserContent = false;
        $linePrefixToSearchFor = '$users[' . $this->inputId . '][';

        foreach ($lines as $line) {
            if ($string->startsWith($line, $linePrefixToSearchFor)) {
                $foundCurrentUserContent = true;
            } else {
                if ($foundCurrentUserContent) {
                    $contentAfterCurrentUser[] = $line;
                } else {
                    $content[] = $line;
                }
            }
        }

        $content[] = '$users[' . $this->inputId . '][\'userRole\'] = ' . $this->roles[$this->inputRole] . ';';
        $content[] = '$users[' . $this->inputId . '][\'userName\'] = \'' . $this->inputName . '\';';
        $content[] = '$users[' . $this->inputId . '][\'password\'] = \'' . $this->inputPassword . '\';';
        $content[] = '$users[' . $this->inputId . '][\'channels\'] = array(' . implode(',', $this->inputChannels) . ');';

        foreach ($contentAfterCurrentUser as $line) {
            $content[] = $line;
        }

        $this->file->write($content);
    }

    /**
     * @return array
     */
    public function getUsage()
    {
        return array(
            'user_id=<id> name="<name>" password="<password>" role=<id> channels="<id>[,<id>[...]]"',
            '   available channels: ' . implode(',', array_keys($this->channels)),
            '   available roles: ' . implode(',', array_keys($this->roles)),
            '   available users: ' . implode(',', array_keys($this->users))
        );
    }

    /**
     * @throws Exception
     */
    public function verify()
    {
        if ($this->input->getNumberOfArguments() !== 5) {
            throw new Exception(
                'invalid number of arguments provided'
            );
        }

        $channels = explode(',', $this->input->getParameterValue('channels', ''));
        $name = $this->input->getParameterValue('name');
        $password = $this->input->getParameterValue('password');
        $role = $this->input->getParameterValue('role');
        $userId = $this->input->getParameterValue('user_id');
        $validIds = array_keys($this->users);

        if (is_null($name)) {
            throw new Exception(
                'no name provided'
            );
        }

        if (is_null($role)) {
            throw new Exception(
                'no role provided'
            );
        } else {
            if (!isset($this->roles[$role])) {
                throw new Exception(
                    'invalid role "' . $role . '" provided'
                );
            }
        }

        if (is_null($password)) {
            throw new Exception(
                'no password provided'
            );
        }

        if (empty($channels)) {
            throw new Exception(
                'no channels provided'
            );
        }

        foreach ($channels as $channel) {
            if (!isset($this->channels[$channel])) {
                throw new Exception(
                    'invalid channel "' . $channel . '" provided'
                );
            }
        }

        if (($userId === 0)) {
            throw new Exception(
                'no id provided'
            );
        } else if (!isset($validIds[$userId])) {
            throw new Exception(
                'invalid id provided'
            );
        }

        $this->inputChannels = $channels;
        $this->inputName = $name;
        $this->inputPassword = $password;
        $this->inputRole = $role;
        $this->inputId = $userId;
    }
}