Bearbeiten des Benutzers im Backend implementiert

This commit is contained in:
Christian Wolf 2019-04-16 14:47:23 +02:00
parent 8aaf1bf057
commit 604ac0e84f
5 changed files with 267 additions and 2 deletions

View File

@ -0,0 +1,108 @@
<?php
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
// No direct access.
defined('_JEXEC') or die;
class ClubsControllerUser extends BaseController
{
function new()
{}
function change()
{
$app = Factory::getApplication();
$input = $app->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);
}
}

View File

@ -1,11 +1,16 @@
<?php <?php
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Factory; use Joomla\CMS\Factory;
// No direct access. // No direct access.
defined('_JEXEC') or die; defined('_JEXEC') or die;
class UserInvalidException extends Exception
{}
class PasswordInvalidException extends Exception
{}
class ClubsUser class ClubsUser
{ {
protected $dbo; protected $dbo;
@ -104,6 +109,14 @@ class ClubsUser
*/ */
public function setUser(string $user) public function setUser(string $user)
{ {
if($this->id === 'new')
$valid = self::isUserNameFree($user);
else
$valid = self::isUserNameFree($user, $this->id);
if(! $valid)
throw new UserInvalidException();
$this->user = $user; $this->user = $user;
} }
@ -112,6 +125,9 @@ class ClubsUser
*/ */
public function setPassword(string $password) public function setPassword(string $password)
{ {
if(! $this->checkPassword($password))
throw new PasswordInvalidException();
$this->hash = password_hash($password, PASSWORD_DEFAULT); $this->hash = password_hash($password, PASSWORD_DEFAULT);
} }
@ -326,4 +342,34 @@ class ClubsUser
$this->dbo->setQuery($q); $this->dbo->setQuery($q);
$this->dbo->execute(); $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;
}
} }

View File

@ -0,0 +1,57 @@
<?php
// No direct access.
defined('_JEXEC') or die;
?>
<form method="post" action="<?php echo $this->address; ?>">
<input type='hidden' name='id' value='<?php echo $this->user->getId(); ?>'>
<table>
<tr>
<td>Username</td>
<td><input type='text' name='user' value='<?php echo htmlentities($this->user->getUser()); ?>'></td>
</tr>
<tr>
<td>Passwort</td>
<td><input type='password' name='pwd' value=''></td>
</tr>
<tr>
<td>Passwort wiederholen</td>
<td><input type='password' name='pwd-confirm' value=''></td>
</tr>
<tr>
<td>Bürgerlicher Name</td>
<td><input type='text' name='name' value='<?php echo htmlentities($this->user->getName()); ?>'></td>
</tr>
<tr>
<td>Adresse</td>
<td>
<textarea rows="4" name='address'><?php echo (htmlentities($this->user->getAddress())); ?></textarea>
</tr>
<tr>
<td>Stadt</td>
<td><input type='text' name='city' value='<?php echo htmlentities($this->user->getCity()); ?>'></td>
</tr>
<tr>
<td>E-Mail</td>
<td><input type='text' name='mail' value='<?php echo htmlentities($this->user->getMail()); ?>'></td>
</tr>
<tr>
<td>Telefon</td>
<td><input type='text' name='phone' value='<?php echo htmlentities($this->user->getPhone()); ?>'></td>
</tr>
<tr>
<td>Handy</td>
<td><input type='text' name='mobile' value='<?php echo htmlentities($this->user->getMobile()); ?>'></td>
</tr>
<?php if(! $this->isNew): ?>
<tr>
<td>ID</td>
<td><?php echo $this->user->getId(); ?></td>
</tr>
<?php endif; ?>
</table>
<input type='submit' value='Speichern'>
</form>

View File

@ -0,0 +1,54 @@
<?php
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
// No direct access.
defined('_JEXEC') or die;
class ClubsViewUser extends HtmlView
{
public function display($tpl = null)
{
$input = Factory::getApplication()->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);
}
}

View File

@ -23,7 +23,7 @@ defined('_JEXEC') or die;
<td></td> <td></td>
<td><a href='<?php echo $link; ?>'><?php echo htmlentities($user->getName()); ?></a></td> <td><a href='<?php echo $link; ?>'><?php echo htmlentities($user->getName()); ?></a></td>
<td><?php echo htmlentities($user->getCity()); ?></td> <td><?php echo htmlentities($user->getCity()); ?></td>
<td><?php echo htmlentities($user->getMail()); ?></td> <td><a href='mailto:<?php echo htmlentities($user->getMail()); ?>'><?php echo htmlentities($user->getMail()); ?></a></td>
<td><?php echo htmlentities($user->getId()); ?></td> <td><?php echo htmlentities($user->getId()); ?></td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>