From 604ac0e84f778b8225fa92271630853038af9449 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Tue, 16 Apr 2019 14:47:23 +0200 Subject: [PATCH] Bearbeiten des Benutzers im Backend implementiert --- src/admin/controllers/user.php | 108 +++++++++++++++++++++++++ src/admin/mymodels/user.php | 48 ++++++++++- src/admin/views/user/tmpl/default.php | 57 +++++++++++++ src/admin/views/user/view.html.php | 54 +++++++++++++ src/admin/views/users/tmpl/default.php | 2 +- 5 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 src/admin/controllers/user.php create mode 100644 src/admin/views/user/tmpl/default.php create mode 100644 src/admin/views/user/view.html.php diff --git a/src/admin/controllers/user.php b/src/admin/controllers/user.php new file mode 100644 index 0000000..a6310e4 --- /dev/null +++ b/src/admin/controllers/user.php @@ -0,0 +1,108 @@ +input; + $id = (int) $input->post->getInt('id'); + $u = ClubsUser::loadUser((int) $id); + + // Fetch the posted data + $user = $input->post->getCmd('user'); + $pwd = $input->post->getCmd('pwd'); + $pwdConfirm = $input->post->getCmd('pwd-confirm'); + $name = $input->post->getString('name'); + $address = $input->post->getString('address'); + $city = $input->post->getString('city'); + $mail = $input->post->getString('mail'); + $phone = $input->post->getString('phone'); + $mobile = $input->post->getString('mobile'); + + // Check the input data + $error = false; + + if(isset($user)) + { + if(! $this->checkUserName(trim($user), $id)) + { + $app->enqueueMessage('Username ' . $user . ' ist nicht gültig.', 'error'); + $error = true; + } + + $u->setUser($user); + } + + if(isset($pwd)) + { + if(trim($pwd) != trim($pwdConfirm)) + { + $app->enqueueMessage('Die Passwörter stimmen nicht überein.', 'error'); + $error = true; + } + + if(! empty(trim($pwd))) + { + if(! $u->checkPassword(trim($pwd))) + { + $app->enqueueMessage('Das Passwort ist nicht zulässig.', 'error'); + $error = true; + } + + $u->setPassword(trim($pwd)); + } + } + + // Check existence of the other fields + $fields = array('name'=>'Bürgerlicher Name', 'address'=>'Adresse', 'city'=>"Stadt", 'mail'=>'E-Mail'); + foreach ($fields as $f => $fname) + { + $fvalue = $$f; + if(! isset($fvalue) || strlen(trim($fvalue)) == 0) + { + $app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error'); + $error = true; + } + } + + $u->setName($name); + $u->setAddress($address); + $u->setCity($city); + $u->setMail($mail); + $u->setPhone($phone); + $u->setMobile($mobile); + + if($error) + { + $data = array(); + foreach(array('user', 'name', 'address', 'city', 'mail', 'phone', 'mobile') as $i) + $data[$i] = $$i; + + $urldata = urlencode(json_encode($data)); + $this->setRedirect(Route::_('index.php?option=com_clubs&view=user&id=' . $id . '&data=' . $urldata, false)); + return; + } + + // Do the actual work + $u->save(); + $this->setRedirect(Route::_('index.php?option=com_clubs&view=users', false)); + } + + private function checkUserName($username, $id = -1) + { + return ClubsUser::isUserNameFree($username, $id); + } + +} diff --git a/src/admin/mymodels/user.php b/src/admin/mymodels/user.php index 2ad9733..c6d64a1 100644 --- a/src/admin/mymodels/user.php +++ b/src/admin/mymodels/user.php @@ -1,11 +1,16 @@ id === 'new') + $valid = self::isUserNameFree($user); + else + $valid = self::isUserNameFree($user, $this->id); + + if(! $valid) + throw new UserInvalidException(); + $this->user = $user; } @@ -112,6 +125,9 @@ class ClubsUser */ public function setPassword(string $password) { + if(! $this->checkPassword($password)) + throw new PasswordInvalidException(); + $this->hash = password_hash($password, PASSWORD_DEFAULT); } @@ -326,4 +342,34 @@ class ClubsUser $this->dbo->setQuery($q); $this->dbo->execute(); } + + public static function isUserNameFree($username, int $id = -1) + { + $db = Factory::getDbo(); + $q = $db->getQuery(true); + $q->select('COUNT(*)')->from('#__club_users') + ->where('id <> ' . (int) $id) + ->where('user = ' . $q->q($username)); + $db->setQuery($q); + $db->execute(); + $row = $db->loadRow(); + return $row[0] == 0; + } + + public function checkPassword($pwd) + { + if(strlen($pwd) < 6) + return false; + + if(preg_match_all('/[A-Z]/', $pwd) === false) + return false; + + if(preg_match_all('/[a-z]/', $pwd) === false) + return false; + + if(preg_match_all('/[0-9]/', $pwd) === false) + return false; + + return true; + } } diff --git a/src/admin/views/user/tmpl/default.php b/src/admin/views/user/tmpl/default.php new file mode 100644 index 0000000..8f4c13b --- /dev/null +++ b/src/admin/views/user/tmpl/default.php @@ -0,0 +1,57 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + isNew): ?> + + + + + +
Username
Passwort
Passwort wiederholen
Bürgerlicher Name
Adresse + +
Stadt
E-Mail
Telefon
Handy
IDuser->getId(); ?>
+ + +
diff --git a/src/admin/views/user/view.html.php b/src/admin/views/user/view.html.php new file mode 100644 index 0000000..a0d5dc5 --- /dev/null +++ b/src/admin/views/user/view.html.php @@ -0,0 +1,54 @@ +input; + $id = $input->get->get('id'); + + if($id === 'new') + { + $this->address = Route::_('index.php?option-com_clubs&task=user.new'); + $this->user = ClubsUser::createUser(); + $this->isNew = true; + } + else if(is_numeric($id)) + { + $this->address = Route::_('index.php?option=com_clubs&task=user.change'); + $this->user = ClubsUser::loadUser((int) $id); + $this->isNew = false; + } + else + throw new Exception('Need a user id.'); + + if($input->get->get('data', null, 'json') != null) + { + // Restore previous data + $dataurl = $input->get->get('data', null, 'json'); + $data = json_decode($dataurl, true); + + $this->user->setUser($data['user']); + $this->user->setName($data['name']); + $this->user->setAddress($data['address']); + $this->user->setCity($data['city']); + $this->user->setMail($data['mail']); + $this->user->setPhone($data['phone']); + $this->user->setMobile($data['mobile']); + + } + + ToolbarHelper::title('Club-Management'); + parent::display($tpl); + } + +} diff --git a/src/admin/views/users/tmpl/default.php b/src/admin/views/users/tmpl/default.php index fcd060c..6209998 100644 --- a/src/admin/views/users/tmpl/default.php +++ b/src/admin/views/users/tmpl/default.php @@ -23,7 +23,7 @@ defined('_JEXEC') or die; getName()); ?> getCity()); ?> - getMail()); ?> + getMail()); ?> getId()); ?>