summaryrefslogtreecommitdiff
path: root/src/UserModule.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/UserModule.php')
-rw-r--r--src/UserModule.php61
1 files changed, 25 insertions, 36 deletions
diff --git a/src/UserModule.php b/src/UserModule.php
index fcab5a8..c230976 100644
--- a/src/UserModule.php
+++ b/src/UserModule.php
@@ -2,50 +2,37 @@
namespace PHPIAC\Modules;
+use PHPIAC\Connection;
use PHPIAC\Module\Module;
use PHPIAC\Module\State;
-use phpseclib3\Net\SSH2;
class UserModule extends Module
{
- /**
- * UserModule constructor.
- *
- * @param string $username
- * @param array $options
- * @param string $state
- */
- public function __construct(
- private string $username,
- private array $options = [],
- private string $state = State::PRESENT
- ) {
- $this->options = array_replace([
- 'append' => false,
- 'create_home' => true,
- 'groups' => [],
- 'shell' => '/bin/bash',
- ], $options);
- }
+ protected string $username;
+ protected string $password;
+
+ protected bool $append = false;
+ protected bool $createHome = true;
+ protected array $groups = [];
+ protected string $shell = '/bin/bash';
+ protected string $state = State::PRESENT;
/**
* @inheritDoc
*/
public function checkState(): bool
{
- global $ssh;
- /**@var SSH2 $ssh*/
- $ssh->enablePTY();
+ Connection::enablePty();
- $ssh->exec("cat /etc/passwd | grep $this->username:");
- $hasUser = $ssh->read();
+ Connection::exec("cat /etc/passwd | grep $this->username:");
+ $hasUser = Connection::read();
$state = match ($this->state) {
State::PRESENT => str_starts_with($hasUser, "$this->username:"),
State::ABSENT => empty($hasUser),
};
- $ssh->disablePTY();
+ Connection::disablePty();
return $state;
}
@@ -55,16 +42,18 @@ class UserModule extends Module
*/
public function getCommands(): array
{
- return match ($this->state) {
- State::PRESENT => [
+ if ($this->state === State::PRESENT) {
+ Connection::exec(implode(PHP_EOL, [
"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",
- ],
- };
+ " --shell " . $this->shell .
+ ($this->createHome ? '' : ' --no-create-home'),
+ "sudo usermod -" . ($this->append ? 'a' : '') . "G " . implode(',', $this->groups) . " $this->username"
+ ]));
+ }
+ else if ($this->state === State::ABSENT) {
+ Connection::exec("sudo userdel $this->username");
+ }
+
+ return [];
}
}