options = array_replace([ 'append' => false, 'create_home' => true, 'groups' => [], 'shell' => '/bin/bash', ], $options); } /** * @inheritDoc */ public function checkState(): bool { global $ssh; /**@var SSH2 $ssh*/ $ssh->enablePTY(); $ssh->exec("cat /etc/passwd | grep $this->name:"); $hasUser = $ssh->read(); $state = match ($this->state) { State::PRESENT => str_starts_with($hasUser, "$this->name:"), State::ABSENT => empty($hasUser), }; $ssh->disablePTY(); return $state; } /** * @inheritDoc */ public function getCommands(): array { return match ($this->state) { State::PRESENT => [ "sudo adduser $this->name --quiet" . " --shell " . $this->options['shell'] . ($this->options['create_home'] ? '' : ' --no-create-home'), "sudo usermod -" . ($this->options['append'] ? 'a' : '') . "G " . implode(',', $this->options['groups']) . " $this->name" ], State::ABSENT => [ "sudo apt remove -y $this->package", ], }; } }