summaryrefslogtreecommitdiffstats
path: root/source/Command/User
diff options
context:
space:
mode:
Diffstat (limited to 'source/Command/User')
-rw-r--r--source/Command/User/AbstractCommand.php63
-rw-r--r--source/Command/User/Add.php124
-rw-r--r--source/Command/User/CommandInterface.php31
-rw-r--r--source/Command/User/Delete.php105
-rw-r--r--source/Command/User/Edit.php161
-rw-r--r--source/Command/User/List.php58
6 files changed, 542 insertions, 0 deletions
diff --git a/source/Command/User/AbstractCommand.php b/source/Command/User/AbstractCommand.php
new file mode 100644
index 0000000..00512c2
--- /dev/null
+++ b/source/Command/User/AbstractCommand.php
@@ -0,0 +1,63 @@
+<?php
+/**
+ * @author stev leibelt <artodeto@bazzline.net>
+ * @since 2014-08-14
+ */
+
+/**
+ * Class Command_User_AbstractCommand
+ */
+abstract class Command_User_AbstractCommand extends Command_AbstractCommand implements Command_User_CommandInterface
+{
+ /**
+ * @var array
+ */
+ protected $channels;
+
+ /**
+ * @var array
+ */
+ protected $roles;
+
+ /**
+ * @var File
+ */
+ protected $file;
+
+ /**
+ * @var array
+ */
+ protected $users;
+
+ /**
+ * @param array $channels
+ */
+ public function setChannels(array $channels)
+ {
+ $this->channels = $channels;
+ }
+
+ /**
+ * @param array $roles
+ */
+ public function setRoles(array $roles)
+ {
+ $this->roles = $roles;
+ }
+
+ /**
+ * @param \File $userFile
+ */
+ public function setUserFile(File $userFile)
+ {
+ $this->file = $userFile;
+ }
+
+ /**
+ * @param array $users
+ */
+ public function setUsers(array $users)
+ {
+ $this->users = $users;
+ }
+} \ No newline at end of file
diff --git a/source/Command/User/Add.php b/source/Command/User/Add.php
new file mode 100644
index 0000000..088feef
--- /dev/null
+++ b/source/Command/User/Add.php
@@ -0,0 +1,124 @@
+<?php
+/**
+ * @author stev leibelt <artodeto@bazzline.net>
+ * @since 2014-08-12
+ */
+
+/**
+ * Class Command_User_Add
+ */
+class Command_User_Add extends Command_User_AbstractCommand
+{
+ /**
+ * @var array
+ */
+ private $inputChannels;
+
+ /**
+ * @var string
+ */
+ private $inputName;
+
+ /**
+ * @var string
+ */
+ private $inputPassword;
+
+ /**
+ * @var string
+ */
+ private $inputRole;
+
+ /**
+ * @throws Exception
+ */
+ public function execute()
+ {
+ end($this->users);
+ $nextKey = (key($this->users) + 1);
+ reset($this->users);
+
+ $content = $this->file->read();
+
+ $content[] = '// added - ' . date('Y-m-d H:i:s');
+ $content[] = '$users[' . $nextKey . '] = array();';
+ $content[] = '$users[' . $nextKey . '][\'userRole\'] = ' . $this->roles[$this->inputRole] . ';';
+ $content[] = '$users[' . $nextKey . '][\'userName\'] = \'' . $this->inputName . '\';';
+ $content[] = '$users[' . $nextKey . '][\'password\'] = \'' . $this->inputPassword . '\';';
+ $content[] = '$users[' . $nextKey . '][\'channels\'] = array(' . implode(',', $this->inputChannels) . ');';
+
+ $this->file->write($content);
+ }
+
+ /**
+ * @return array
+ */
+ public function getUsage()
+ {
+ return array(
+ 'name="<name>" password="<password>" role=<id> channels="<id>[,<id>[...]]"',
+ ' available channels: ' . implode(',', array_keys($this->channels)),
+ ' available roles: ' . implode(',', array_keys($this->roles))
+ );
+ }
+
+ /**
+ * @throws Exception
+ */
+ public function verify()
+ {
+ if ($this->input->getNumberOfArguments() !== 4) {
+ 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');
+
+ if (is_null($name)) {
+ throw new Exception(
+ 'no name "' . $name . '" provided'
+ );
+ }
+
+ if (is_null($role)) {
+ throw new Exception(
+ 'no role "' . $role . '" provided'
+ );
+ } else {
+ if (!isset($this->roles[$role])) {
+ throw new Exception(
+ 'invalid role "' . $role . '" provided'
+ );
+ }
+ }
+
+ if (is_null($password)) {
+ throw new Exception(
+ 'no password "' . $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'
+ );
+ }
+ }
+
+ $this->inputChannels = $channels;
+ $this->inputName = $name;
+ $this->inputPassword = $password;
+ $this->inputRole = $role;
+ }
+}
diff --git a/source/Command/User/CommandInterface.php b/source/Command/User/CommandInterface.php
new file mode 100644
index 0000000..b8aa332
--- /dev/null
+++ b/source/Command/User/CommandInterface.php
@@ -0,0 +1,31 @@
+<?php
+/**
+ * @author stev leibelt <artodeto@bazzline.net>
+ * @since 2014-08-14
+ */
+
+/**
+ * Interface Command_User_CommandInterface
+ */
+interface Command_User_CommandInterface extends Command_CommandInterface
+{
+ /**
+ * @param array $channels
+ */
+ public function setChannels(array $channels);
+
+ /**
+ * @param array $roles
+ */
+ public function setRoles(array $roles);
+
+ /**
+ * @param \File $userFile
+ */
+ public function setUserFile(File $userFile);
+
+ /**
+ * @param array $users
+ */
+ public function setUsers(array $users);
+} \ No newline at end of file
diff --git a/source/Command/User/Delete.php b/source/Command/User/Delete.php
new file mode 100644
index 0000000..aeed24a
--- /dev/null
+++ b/source/Command/User/Delete.php
@@ -0,0 +1,105 @@
+<?php
+/**
+ * @author stev leibelt <artodeto@bazzline.net>
+ * @since 2014-08-12
+ */
+
+/**
+ * Class Command_User_Delete
+ */
+class Command_User_Delete extends Command_User_AbstractCommand
+{
+ /**
+ * @var int
+ */
+ private $inputId;
+
+ /**
+ * @throws Exception
+ */
+ public function execute()
+ {
+ reset($this->users);
+
+ $lines = $this->file->read();
+ $content = array();
+
+ foreach ($lines as $line) {
+ if ($line == '$users[0][\'channels\'] = array(0);') {
+ $content[] = $line;
+ $content[] = '';
+ break;
+ } else {
+ $content[] = $line;
+ }
+ }
+
+ unset($this->users[0]);
+
+ if (empty($this->users)) {
+ throw new Exception(
+ 'nothing to delete'
+ );
+ } else {
+ unset($this->users[$this->inputId]);
+ $idToUser = array_values($this->users);
+
+ if (!empty($idToUser)) {
+ foreach ($idToUser as $id => $user) {
+ ++$id; //we have to increase by one since we have to prevent overwriting the "0" user
+ $content[] = '// updated - ' . date('Y-m-d H:i:s');
+ $content[] = '$users[' . $id . '] = array();';
+ $content[] = '$users[' . $id . '][\'userRole\'] = ' . $this->roles[$user['userRole']] . ';';
+ $content[] = '$users[' . $id . '][\'userName\'] = \'' . $user['userName'] . '\';';
+ $content[] = '$users[' . $id . '][\'password\'] = \'' . $user['password'] . '\';';
+ $content[] = '$users[' . $id . '][\'channels\'] = array(' . implode(',', $user['channels']) . ');';
+ }
+ }
+
+ $this->file->write($content);
+ }
+ }
+
+ /**
+ * @return array
+ */
+ public function getUsage()
+ {
+ $users = $this->users;
+ unset($users[0]);
+
+ return array(
+ 'userid=<id>',
+ ' available users: ' . implode(',', array_keys($users))
+ );
+ }
+
+ /**
+ * @throws Exception
+ */
+ public function verify()
+ {
+ if ($this->input->getNumberOfArguments() !== 1) {
+ throw new Exception(
+ 'invalid number of arguments provided'
+ );
+ }
+
+ $validIds = array_keys($this->users);
+ $userId = $this->input->getParameterValue('user_id');
+
+ if (!isset($validIds[$userId])) {
+ throw new Exception(
+ 'invalid id "' . $userId . '" provided'
+ );
+ }
+
+ if ($userId === 0) {
+ throw new Exception(
+ 'you are not allowed to delete id "' . $userId . '"'
+ );
+ }
+
+ $this->inputId = $userId;
+ }
+}
diff --git a/source/Command/User/Edit.php b/source/Command/User/Edit.php
new file mode 100644
index 0000000..72467ee
--- /dev/null
+++ b/source/Command/User/Edit.php
@@ -0,0 +1,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;
+ }
+} \ No newline at end of file
diff --git a/source/Command/User/List.php b/source/Command/User/List.php
new file mode 100644
index 0000000..130e842
--- /dev/null
+++ b/source/Command/User/List.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * @author stev leibelt <artodeto@bazzline.net>
+ * @since 2014-08-12
+ */
+
+/**
+ * Class Command_User_List
+ */
+class Command_User_List extends Command_User_AbstractCommand
+{
+ /**
+ * @throws Exception
+ */
+ public function execute()
+ {
+ unset($this->users[0]);
+ //remove initial user since we are not allowed to remove this user
+ $numberOfUsers = count($this->users);
+
+ $this->output->addLine('number of users: ' . $numberOfUsers);
+
+ if ($numberOfUsers > 0) {
+ $this->output->addLine();
+ $this->output->addLine('id | name | role | channels');
+ $this->output->addLine('----------------');
+
+ foreach ($this->users as $id => $user) {
+ $this->output->addLine(
+ implode(
+ ' | ',
+ array(
+ $id,
+ $user['userName'],
+ $user['userRole'],
+ implode(',', $user['channels'])
+ )
+ )
+ );
+ }
+ }
+ }
+
+ /**
+ * @return array
+ */
+ public function getUsage()
+ {
+ return array();
+ }
+
+ /**
+ * @throws Exception
+ */
+ public function verify()
+ {
+ }
+}