From d4d5ae3bb6566311b2d42cf888e463b62f6cf0dc Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 19 Apr 2021 11:00:50 +0200 Subject: Initial commit --- src/UserModule.php | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/UserModule.php (limited to 'src/UserModule.php') diff --git a/src/UserModule.php b/src/UserModule.php new file mode 100644 index 0000000..fcab5a8 --- /dev/null +++ b/src/UserModule.php @@ -0,0 +1,70 @@ +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->username:"); + $hasUser = $ssh->read(); + + $state = match ($this->state) { + State::PRESENT => str_starts_with($hasUser, "$this->username:"), + State::ABSENT => empty($hasUser), + }; + + $ssh->disablePTY(); + + return $state; + } + + /** + * @inheritDoc + */ + public function getCommands(): array + { + return match ($this->state) { + State::PRESENT => [ + "sudo adduser $this->username --quiet" . + " --shell " . $this->options['shell'] . + ($this->options['create_home'] ? '' : ' --no-create-home'), + "sudo usermod -" . ($this->options['append'] ? 'a' : '') . "G " . implode(',', $this->options['groups']) . " $this->username" + ], + State::ABSENT => [ + "sudo userdel $this->username", + ], + }; + } +} -- cgit v1.2.3