@@ -11,188 +11,188 @@ |
||
| 11 | 11 | use Zend\View\Model\ViewModel; |
| 12 | 12 | |
| 13 | 13 | class UserController extends AbstractActionController { |
| 14 | - private $htpasswdService = NULL; |
|
| 15 | - private $userService = NULL; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Check Login |
|
| 19 | - * |
|
| 20 | - * @see \Zend\Mvc\Controller\AbstractActionController::onDispatch() |
|
| 21 | - */ |
|
| 22 | - public function onDispatch(\Zend\Mvc\MvcEvent $e) { |
|
| 23 | - $username = $this->getUserService()->getCurrentUser(); |
|
| 24 | - |
|
| 25 | - if (false === $this->getUserService()->isUserAllowedToManageUsers($username)) { |
|
| 26 | - $this->getResponse()->setStatusCode(401); |
|
| 27 | - |
|
| 28 | - return; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - return parent::onDispatch($e); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - public function indexAction() { |
|
| 35 | - $htpasswdService = $this->getHtpasswdService(); |
|
| 36 | - $userList = $htpasswdService->getUserList(); |
|
| 37 | - |
|
| 38 | - $result = array(); |
|
| 39 | - foreach ($userList as $username => $pass) { |
|
| 40 | - |
|
| 41 | - $result [] = array( |
|
| 42 | - 'username' => $username, |
|
| 43 | - 'paswd' => $pass, |
|
| 44 | - 'isAdmin' => $this->getUserService()->isUserAllowedToManageUsers($username), |
|
| 45 | - 'isDeletable' => $this->getUserService()->isUserDeleteable($username) |
|
| 46 | - ); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $userService = $this->getServiceLocator()->get('HtpasswdManager\Service\UserService'); |
|
| 50 | - |
|
| 51 | - $model = new ViewModel (array( |
|
| 52 | - 'userList' => $result, |
|
| 53 | - 'currentUser' => $userService->getCurrentUser() |
|
| 54 | - )); |
|
| 55 | - |
|
| 56 | - return $model; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function deleteAction() { |
|
| 60 | - $user = $this->params('user'); |
|
| 61 | - $messages = array(); |
|
| 62 | - |
|
| 63 | - if (true === $this->getUserService()->isUserDeleteable($user)) { |
|
| 64 | - $this->getHtpasswdService()->deleteUser($user); |
|
| 65 | - $messages [] = "Der Benutzer '{$user}' wurde gelöscht."; |
|
| 66 | - } else { |
|
| 67 | - $messages [] = "Der Benutzer '{$user}' steht auf einer Sperrliste und kann nicht gelöscht werden."; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $model = $this->indexAction(); |
|
| 71 | - $model->setTemplate('htpasswd-manager/user/index'); |
|
| 72 | - $model->setVariable('messages', $messages); |
|
| 73 | - |
|
| 74 | - return $model; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function editAction() { |
|
| 78 | - $user = $this->params('user'); |
|
| 79 | - $messages = array(); |
|
| 80 | - $htpasswd = $this->getHtpasswdService(); |
|
| 81 | - |
|
| 82 | - $model = new ViewModel (); |
|
| 83 | - $model->setVariable('user', $user); |
|
| 84 | - |
|
| 85 | - $post = $this->getRequest()->getPost(); |
|
| 86 | - if (!isset ($post ['password'])) { |
|
| 87 | - // Formular initialisieren |
|
| 88 | - if (false === $htpasswd->userExists($user)) { |
|
| 89 | - $this->redirect()->toRoute('htpasswdmanager', array( |
|
| 90 | - 'action' => 'index' |
|
| 91 | - )); |
|
| 92 | - } |
|
| 93 | - } else { |
|
| 94 | - // Formular speichern |
|
| 95 | - $password = $post ['password']; |
|
| 96 | - $htpasswd->updateUser($user, $password); |
|
| 97 | - $messages [] = "Passwort für '{$user}' wurde aktualisiert."; |
|
| 98 | - |
|
| 99 | - $model = $this->indexAction(); |
|
| 100 | - $model->setTemplate('htpasswd-manager/user/index'); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $model->setVariable('messages', $messages); |
|
| 104 | - |
|
| 105 | - return $model; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function addAction() { |
|
| 109 | - $username = $this->getRequest()->getPost('username', ''); |
|
| 110 | - $password = $this->getRequest()->getPost('password', ''); |
|
| 111 | - $messages = array(); |
|
| 112 | - $model = new ViewModel (); |
|
| 113 | - |
|
| 114 | - $post = $this->getRequest()->getPost(); |
|
| 115 | - if (!isset ($post ['username'])) { |
|
| 116 | - return new ViewModel (array( |
|
| 117 | - 'username' => $username, |
|
| 118 | - 'password' => $password, |
|
| 119 | - 'messages' => $messages |
|
| 120 | - )); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $model->setVariable('username', $username); |
|
| 124 | - $uValid = $this->isUsernameValid($username); |
|
| 125 | - if (true !== $uValid) { |
|
| 126 | - $messages [] = $uValid; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $model->setVariable('password', $password); |
|
| 130 | - $pValid = $this->isPasswordValid($password); |
|
| 131 | - if (true !== $pValid) { |
|
| 132 | - $messages [] = $pValid; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - if (true === $uValid && true === $pValid) { |
|
| 136 | - if (true === $this->getHtpasswdService()->userExists($username)) { |
|
| 137 | - $this->getHtpasswdService()->updateUser($username, $password); |
|
| 138 | - $messages [] = "Benutzer '{$username}' wurde aktualisiert."; |
|
| 139 | - } else { |
|
| 140 | - $this->getHtpasswdService()->addUser($username, $password); |
|
| 141 | - $messages [] = "Benutzer '{$username}' wurde hinzugefügt."; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $model = $this->indexAction(); |
|
| 145 | - $model->setTemplate('htpasswd-manager/user/index'); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $model->setVariable('messages', $messages); |
|
| 149 | - |
|
| 150 | - return $model; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Returns true if valid, of not it returns a string with information about the reason |
|
| 155 | - * |
|
| 156 | - * @param string $username |
|
| 157 | - * @return boolean string |
|
| 158 | - */ |
|
| 159 | - private function isUsernameValid($username) { |
|
| 160 | - if (strlen($username) <= 2) |
|
| 161 | - return "Benutzername ist zu kurz."; |
|
| 162 | - else if (preg_match('~[a-zäöo][a-zäöu_0-9-]+~i', $username) !== 1) |
|
| 163 | - return "Benutzername enthält ungültige Zeichen"; |
|
| 164 | - else if (strpos($username, ' ') !== false) |
|
| 165 | - return "Leerzeichen sind im Benutzernamen nicht erlaubt"; |
|
| 166 | - |
|
| 167 | - return true; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Returns true if valid, of not it returns a string with information about the reason |
|
| 172 | - * |
|
| 173 | - * @param string $password |
|
| 174 | - * @return boolean string |
|
| 175 | - */ |
|
| 176 | - private function isPasswordValid($password) { |
|
| 177 | - return true; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - private function getHtpasswdService() { |
|
| 181 | - if ($this->htpasswdService === NULL) { |
|
| 182 | - $sl = $this->getServiceLocator(); |
|
| 183 | - $this->htpasswdService = $sl->get('HtpasswdManager\Service\HtpasswdService'); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - return $this->htpasswdService; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - private function getUserService() { |
|
| 190 | - if ($this->userService === NULL) { |
|
| 191 | - $sl = $this->getServiceLocator(); |
|
| 192 | - $this->userService = $sl->get('HtpasswdManager\Service\UserService'); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return $this->userService; |
|
| 196 | - } |
|
| 14 | + private $htpasswdService = NULL; |
|
| 15 | + private $userService = NULL; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Check Login |
|
| 19 | + * |
|
| 20 | + * @see \Zend\Mvc\Controller\AbstractActionController::onDispatch() |
|
| 21 | + */ |
|
| 22 | + public function onDispatch(\Zend\Mvc\MvcEvent $e) { |
|
| 23 | + $username = $this->getUserService()->getCurrentUser(); |
|
| 24 | + |
|
| 25 | + if (false === $this->getUserService()->isUserAllowedToManageUsers($username)) { |
|
| 26 | + $this->getResponse()->setStatusCode(401); |
|
| 27 | + |
|
| 28 | + return; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + return parent::onDispatch($e); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + public function indexAction() { |
|
| 35 | + $htpasswdService = $this->getHtpasswdService(); |
|
| 36 | + $userList = $htpasswdService->getUserList(); |
|
| 37 | + |
|
| 38 | + $result = array(); |
|
| 39 | + foreach ($userList as $username => $pass) { |
|
| 40 | + |
|
| 41 | + $result [] = array( |
|
| 42 | + 'username' => $username, |
|
| 43 | + 'paswd' => $pass, |
|
| 44 | + 'isAdmin' => $this->getUserService()->isUserAllowedToManageUsers($username), |
|
| 45 | + 'isDeletable' => $this->getUserService()->isUserDeleteable($username) |
|
| 46 | + ); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $userService = $this->getServiceLocator()->get('HtpasswdManager\Service\UserService'); |
|
| 50 | + |
|
| 51 | + $model = new ViewModel (array( |
|
| 52 | + 'userList' => $result, |
|
| 53 | + 'currentUser' => $userService->getCurrentUser() |
|
| 54 | + )); |
|
| 55 | + |
|
| 56 | + return $model; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function deleteAction() { |
|
| 60 | + $user = $this->params('user'); |
|
| 61 | + $messages = array(); |
|
| 62 | + |
|
| 63 | + if (true === $this->getUserService()->isUserDeleteable($user)) { |
|
| 64 | + $this->getHtpasswdService()->deleteUser($user); |
|
| 65 | + $messages [] = "Der Benutzer '{$user}' wurde gelöscht."; |
|
| 66 | + } else { |
|
| 67 | + $messages [] = "Der Benutzer '{$user}' steht auf einer Sperrliste und kann nicht gelöscht werden."; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $model = $this->indexAction(); |
|
| 71 | + $model->setTemplate('htpasswd-manager/user/index'); |
|
| 72 | + $model->setVariable('messages', $messages); |
|
| 73 | + |
|
| 74 | + return $model; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function editAction() { |
|
| 78 | + $user = $this->params('user'); |
|
| 79 | + $messages = array(); |
|
| 80 | + $htpasswd = $this->getHtpasswdService(); |
|
| 81 | + |
|
| 82 | + $model = new ViewModel (); |
|
| 83 | + $model->setVariable('user', $user); |
|
| 84 | + |
|
| 85 | + $post = $this->getRequest()->getPost(); |
|
| 86 | + if (!isset ($post ['password'])) { |
|
| 87 | + // Formular initialisieren |
|
| 88 | + if (false === $htpasswd->userExists($user)) { |
|
| 89 | + $this->redirect()->toRoute('htpasswdmanager', array( |
|
| 90 | + 'action' => 'index' |
|
| 91 | + )); |
|
| 92 | + } |
|
| 93 | + } else { |
|
| 94 | + // Formular speichern |
|
| 95 | + $password = $post ['password']; |
|
| 96 | + $htpasswd->updateUser($user, $password); |
|
| 97 | + $messages [] = "Passwort für '{$user}' wurde aktualisiert."; |
|
| 98 | + |
|
| 99 | + $model = $this->indexAction(); |
|
| 100 | + $model->setTemplate('htpasswd-manager/user/index'); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $model->setVariable('messages', $messages); |
|
| 104 | + |
|
| 105 | + return $model; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function addAction() { |
|
| 109 | + $username = $this->getRequest()->getPost('username', ''); |
|
| 110 | + $password = $this->getRequest()->getPost('password', ''); |
|
| 111 | + $messages = array(); |
|
| 112 | + $model = new ViewModel (); |
|
| 113 | + |
|
| 114 | + $post = $this->getRequest()->getPost(); |
|
| 115 | + if (!isset ($post ['username'])) { |
|
| 116 | + return new ViewModel (array( |
|
| 117 | + 'username' => $username, |
|
| 118 | + 'password' => $password, |
|
| 119 | + 'messages' => $messages |
|
| 120 | + )); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $model->setVariable('username', $username); |
|
| 124 | + $uValid = $this->isUsernameValid($username); |
|
| 125 | + if (true !== $uValid) { |
|
| 126 | + $messages [] = $uValid; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $model->setVariable('password', $password); |
|
| 130 | + $pValid = $this->isPasswordValid($password); |
|
| 131 | + if (true !== $pValid) { |
|
| 132 | + $messages [] = $pValid; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + if (true === $uValid && true === $pValid) { |
|
| 136 | + if (true === $this->getHtpasswdService()->userExists($username)) { |
|
| 137 | + $this->getHtpasswdService()->updateUser($username, $password); |
|
| 138 | + $messages [] = "Benutzer '{$username}' wurde aktualisiert."; |
|
| 139 | + } else { |
|
| 140 | + $this->getHtpasswdService()->addUser($username, $password); |
|
| 141 | + $messages [] = "Benutzer '{$username}' wurde hinzugefügt."; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $model = $this->indexAction(); |
|
| 145 | + $model->setTemplate('htpasswd-manager/user/index'); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $model->setVariable('messages', $messages); |
|
| 149 | + |
|
| 150 | + return $model; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Returns true if valid, of not it returns a string with information about the reason |
|
| 155 | + * |
|
| 156 | + * @param string $username |
|
| 157 | + * @return boolean string |
|
| 158 | + */ |
|
| 159 | + private function isUsernameValid($username) { |
|
| 160 | + if (strlen($username) <= 2) |
|
| 161 | + return "Benutzername ist zu kurz."; |
|
| 162 | + else if (preg_match('~[a-zäöo][a-zäöu_0-9-]+~i', $username) !== 1) |
|
| 163 | + return "Benutzername enthält ungültige Zeichen"; |
|
| 164 | + else if (strpos($username, ' ') !== false) |
|
| 165 | + return "Leerzeichen sind im Benutzernamen nicht erlaubt"; |
|
| 166 | + |
|
| 167 | + return true; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Returns true if valid, of not it returns a string with information about the reason |
|
| 172 | + * |
|
| 173 | + * @param string $password |
|
| 174 | + * @return boolean string |
|
| 175 | + */ |
|
| 176 | + private function isPasswordValid($password) { |
|
| 177 | + return true; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + private function getHtpasswdService() { |
|
| 181 | + if ($this->htpasswdService === NULL) { |
|
| 182 | + $sl = $this->getServiceLocator(); |
|
| 183 | + $this->htpasswdService = $sl->get('HtpasswdManager\Service\HtpasswdService'); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + return $this->htpasswdService; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + private function getUserService() { |
|
| 190 | + if ($this->userService === NULL) { |
|
| 191 | + $sl = $this->getServiceLocator(); |
|
| 192 | + $this->userService = $sl->get('HtpasswdManager\Service\UserService'); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return $this->userService; |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | 198 | } |
@@ -5,150 +5,150 @@ |
||
| 5 | 5 | |
| 6 | 6 | class HtpasswdServiceTest extends \PHPUnit_Framework_TestCase { |
| 7 | 7 | |
| 8 | - protected $testDataDir; |
|
| 8 | + protected $testDataDir; |
|
| 9 | 9 | |
| 10 | - public function setUp() { |
|
| 11 | - $this->testDataDir = __DIR__ . '/TestData'; |
|
| 10 | + public function setUp() { |
|
| 11 | + $this->testDataDir = __DIR__ . '/TestData'; |
|
| 12 | 12 | |
| 13 | - parent::setUp(); |
|
| 14 | - } |
|
| 13 | + parent::setUp(); |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | 16 | |
| 17 | - public function testCreateFileIfNotExistant() { |
|
| 18 | - $testFile = $this->testDataDir . '/notExistant'; |
|
| 17 | + public function testCreateFileIfNotExistant() { |
|
| 18 | + $testFile = $this->testDataDir . '/notExistant'; |
|
| 19 | 19 | |
| 20 | - if (true === file_exists($testFile)) { |
|
| 21 | - unlink($testFile); |
|
| 22 | - } |
|
| 20 | + if (true === file_exists($testFile)) { |
|
| 21 | + unlink($testFile); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - $this->assertFileNotExists($testFile); |
|
| 25 | - $this->assertFileExists($testFile); |
|
| 24 | + $this->assertFileNotExists($testFile); |
|
| 25 | + $this->assertFileExists($testFile); |
|
| 26 | 26 | |
| 27 | - // Cleanup |
|
| 28 | - unlink($testFile); |
|
| 29 | - } |
|
| 27 | + // Cleanup |
|
| 28 | + unlink($testFile); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - public function testGetUserList_Empty() { |
|
| 32 | - $testFile = $this->testDataDir . '/htaccess_UserServiceTest_empty'; |
|
| 31 | + public function testGetUserList_Empty() { |
|
| 32 | + $testFile = $this->testDataDir . '/htaccess_UserServiceTest_empty'; |
|
| 33 | 33 | |
| 34 | - if (true === file_exists($testFile)) { |
|
| 35 | - unlink($testFile); |
|
| 36 | - } |
|
| 37 | - $this->assertFileNotExists($testFile); |
|
| 38 | - touch($testFile); |
|
| 39 | - $this->assertFileExists($testFile); |
|
| 34 | + if (true === file_exists($testFile)) { |
|
| 35 | + unlink($testFile); |
|
| 36 | + } |
|
| 37 | + $this->assertFileNotExists($testFile); |
|
| 38 | + touch($testFile); |
|
| 39 | + $this->assertFileExists($testFile); |
|
| 40 | 40 | |
| 41 | - $service = new HtpasswdService($testFile); |
|
| 41 | + $service = new HtpasswdService($testFile); |
|
| 42 | 42 | |
| 43 | - $userList = $service->getUserList(); |
|
| 44 | - $this->assertEquals($expected = [ ], $userList, "UserList"); |
|
| 43 | + $userList = $service->getUserList(); |
|
| 44 | + $this->assertEquals($expected = [ ], $userList, "UserList"); |
|
| 45 | 45 | |
| 46 | - // Cleanup |
|
| 47 | - unlink($testFile); |
|
| 48 | - } |
|
| 46 | + // Cleanup |
|
| 47 | + unlink($testFile); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function testGetUserList_Filled() { |
|
| 51 | - $testFile = $this->testDataDir . '/htaccess_UserServiceTest'; |
|
| 52 | - $this->assertFileExists($testFile); |
|
| 50 | + public function testGetUserList_Filled() { |
|
| 51 | + $testFile = $this->testDataDir . '/htaccess_UserServiceTest'; |
|
| 52 | + $this->assertFileExists($testFile); |
|
| 53 | 53 | |
| 54 | - $service = new HtpasswdService($testFile); |
|
| 54 | + $service = new HtpasswdService($testFile); |
|
| 55 | 55 | |
| 56 | - $userList = $service->getUserList(); |
|
| 57 | - $this->assertEquals( |
|
| 58 | - $expected = [ 'steven' => '$apr1$UF1l/6iv$tT3IJUH.QL82HAraXetNo0', |
|
| 59 | - 'blub' => '$apr1$jVosh5nC$q6KJ5EZNU6thOPETMQw8O/' |
|
| 60 | - ], $userList, "UserList"); |
|
| 61 | - } |
|
| 56 | + $userList = $service->getUserList(); |
|
| 57 | + $this->assertEquals( |
|
| 58 | + $expected = [ 'steven' => '$apr1$UF1l/6iv$tT3IJUH.QL82HAraXetNo0', |
|
| 59 | + 'blub' => '$apr1$jVosh5nC$q6KJ5EZNU6thOPETMQw8O/' |
|
| 60 | + ], $userList, "UserList"); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function testAddUser() { |
|
| 64 | - $testFileTemplate = $this->testDataDir . '/htaccess_UserServiceTest'; |
|
| 65 | - $testFile = $this->testDataDir . '/htaccess_UserServiceTest_addUser'; |
|
| 66 | - $this->assertFileExists($testFileTemplate); |
|
| 63 | + public function testAddUser() { |
|
| 64 | + $testFileTemplate = $this->testDataDir . '/htaccess_UserServiceTest'; |
|
| 65 | + $testFile = $this->testDataDir . '/htaccess_UserServiceTest_addUser'; |
|
| 66 | + $this->assertFileExists($testFileTemplate); |
|
| 67 | 67 | |
| 68 | - if (true === file_exists($testFile)) { |
|
| 69 | - unlink($testFile); |
|
| 70 | - } |
|
| 71 | - $this->assertFileNotExists($testFile); |
|
| 68 | + if (true === file_exists($testFile)) { |
|
| 69 | + unlink($testFile); |
|
| 70 | + } |
|
| 71 | + $this->assertFileNotExists($testFile); |
|
| 72 | 72 | |
| 73 | - copy($testFileTemplate, $testFile); |
|
| 74 | - $this->assertFileExists($testFile); |
|
| 73 | + copy($testFileTemplate, $testFile); |
|
| 74 | + $this->assertFileExists($testFile); |
|
| 75 | 75 | |
| 76 | - $service = new HtpasswdService($testFile); |
|
| 77 | - $userList = $service->getUserList(); |
|
| 78 | - $this->assertCount(2, $userList); |
|
| 79 | - $this->assertEquals(2, $this->getNumberOfLines($testFile), "Number of Lines"); |
|
| 76 | + $service = new HtpasswdService($testFile); |
|
| 77 | + $userList = $service->getUserList(); |
|
| 78 | + $this->assertCount(2, $userList); |
|
| 79 | + $this->assertEquals(2, $this->getNumberOfLines($testFile), "Number of Lines"); |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - $service->addUser('test', 'test'); |
|
| 82 | + $service->addUser('test', 'test'); |
|
| 83 | 83 | |
| 84 | - $userList = $service->getUserList(); |
|
| 85 | - $this->assertCount(3, $userList); |
|
| 86 | - $this->assertEquals($expected = [ 'steven', 'blub', 'test' ], |
|
| 87 | - array_keys($userList)); |
|
| 88 | - $this->assertEquals(3, $this->getNumberOfLines($testFile), "Number of Lines"); |
|
| 84 | + $userList = $service->getUserList(); |
|
| 85 | + $this->assertCount(3, $userList); |
|
| 86 | + $this->assertEquals($expected = [ 'steven', 'blub', 'test' ], |
|
| 87 | + array_keys($userList)); |
|
| 88 | + $this->assertEquals(3, $this->getNumberOfLines($testFile), "Number of Lines"); |
|
| 89 | 89 | |
| 90 | 90 | |
| 91 | - // Cleanup |
|
| 92 | - unlink($testFile); |
|
| 93 | - } |
|
| 91 | + // Cleanup |
|
| 92 | + unlink($testFile); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | |
| 96 | - public function testdeleteUser() { |
|
| 97 | - $testFileTemplate = $this->testDataDir . '/htaccess_UserServiceTest'; |
|
| 98 | - $testFile = $this->testDataDir . '/htaccess_UserServiceTest_addUser'; |
|
| 99 | - $this->assertFileExists($testFileTemplate); |
|
| 96 | + public function testdeleteUser() { |
|
| 97 | + $testFileTemplate = $this->testDataDir . '/htaccess_UserServiceTest'; |
|
| 98 | + $testFile = $this->testDataDir . '/htaccess_UserServiceTest_addUser'; |
|
| 99 | + $this->assertFileExists($testFileTemplate); |
|
| 100 | 100 | |
| 101 | - if (true === file_exists($testFile)) { |
|
| 102 | - unlink($testFile); |
|
| 103 | - } |
|
| 104 | - $this->assertFileNotExists($testFile); |
|
| 101 | + if (true === file_exists($testFile)) { |
|
| 102 | + unlink($testFile); |
|
| 103 | + } |
|
| 104 | + $this->assertFileNotExists($testFile); |
|
| 105 | 105 | |
| 106 | - copy($testFileTemplate, $testFile); |
|
| 107 | - $this->assertFileExists($testFile); |
|
| 106 | + copy($testFileTemplate, $testFile); |
|
| 107 | + $this->assertFileExists($testFile); |
|
| 108 | 108 | |
| 109 | - $service = new HtpasswdService($testFile); |
|
| 110 | - $userList = $service->getUserList(); |
|
| 111 | - $this->assertCount(2, $userList); |
|
| 112 | - $this->assertEquals(2, $this->getNumberOfLines($testFile), "Number of Lines"); |
|
| 109 | + $service = new HtpasswdService($testFile); |
|
| 110 | + $userList = $service->getUserList(); |
|
| 111 | + $this->assertCount(2, $userList); |
|
| 112 | + $this->assertEquals(2, $this->getNumberOfLines($testFile), "Number of Lines"); |
|
| 113 | 113 | |
| 114 | 114 | |
| 115 | - $service->deleteUser('steven'); |
|
| 116 | - $userList = $service->getUserList(); |
|
| 117 | - $this->assertCount(1, $userList); |
|
| 118 | - $this->assertEquals($expected = [ 'blub' ], |
|
| 119 | - array_keys($userList)); |
|
| 120 | - $this->assertEquals(1, $this->getNumberOfLines($testFile), "Number of Lines"); |
|
| 115 | + $service->deleteUser('steven'); |
|
| 116 | + $userList = $service->getUserList(); |
|
| 117 | + $this->assertCount(1, $userList); |
|
| 118 | + $this->assertEquals($expected = [ 'blub' ], |
|
| 119 | + array_keys($userList)); |
|
| 120 | + $this->assertEquals(1, $this->getNumberOfLines($testFile), "Number of Lines"); |
|
| 121 | 121 | |
| 122 | 122 | |
| 123 | - // Cleanup |
|
| 124 | - unlink($testFile); |
|
| 125 | - } |
|
| 123 | + // Cleanup |
|
| 124 | + unlink($testFile); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - public function testUserExists() { |
|
| 128 | - $testFile = $this->testDataDir . '/htaccess_UserServiceTest'; |
|
| 129 | - $this->assertFileExists($testFile); |
|
| 127 | + public function testUserExists() { |
|
| 128 | + $testFile = $this->testDataDir . '/htaccess_UserServiceTest'; |
|
| 129 | + $this->assertFileExists($testFile); |
|
| 130 | 130 | |
| 131 | - $service = new HtpasswdService($testFile); |
|
| 131 | + $service = new HtpasswdService($testFile); |
|
| 132 | 132 | |
| 133 | - $this->assertTrue($service->userExists('steven'), 'User should exist'); |
|
| 134 | - $this->assertTrue($service->userExists('blub'), 'User should exist'); |
|
| 135 | - $this->assertFalse($service->userExists(''), "User '' should not exist"); |
|
| 136 | - $this->assertFalse($service->userExists('notExistant'), "User 'notExistant' should not exist"); |
|
| 137 | - $this->assertFalse($service->userExists('not Existant'), "User 'not Existant' should not exist"); |
|
| 138 | - } |
|
| 133 | + $this->assertTrue($service->userExists('steven'), 'User should exist'); |
|
| 134 | + $this->assertTrue($service->userExists('blub'), 'User should exist'); |
|
| 135 | + $this->assertFalse($service->userExists(''), "User '' should not exist"); |
|
| 136 | + $this->assertFalse($service->userExists('notExistant'), "User 'notExistant' should not exist"); |
|
| 137 | + $this->assertFalse($service->userExists('not Existant'), "User 'not Existant' should not exist"); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - protected function getNumberOfLines($filename) { |
|
| 141 | - $cont = file_get_contents($filename); |
|
| 142 | - $lines = explode("\n", $cont); |
|
| 140 | + protected function getNumberOfLines($filename) { |
|
| 141 | + $cont = file_get_contents($filename); |
|
| 142 | + $lines = explode("\n", $cont); |
|
| 143 | 143 | |
| 144 | - // Remove emtpy lines |
|
| 145 | - foreach ($lines as $i => $line) { |
|
| 146 | - if (empty(trim($line))) { |
|
| 147 | - unset($lines[$i]); |
|
| 148 | - } |
|
| 149 | - } |
|
| 144 | + // Remove emtpy lines |
|
| 145 | + foreach ($lines as $i => $line) { |
|
| 146 | + if (empty(trim($line))) { |
|
| 147 | + unset($lines[$i]); |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - return count($lines); |
|
| 152 | - } |
|
| 151 | + return count($lines); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | 154 | } |
| 155 | 155 | \ No newline at end of file |