Made user class work with abstract classes

This commit is contained in:
Christian Wolf 2019-05-17 13:14:39 +02:00
parent 17e737b64c
commit 03df8c8f1a
4 changed files with 47 additions and 22 deletions

View File

@ -34,7 +34,7 @@ abstract class AbstractClubsController extends BaseController
$this->filterPreCheck($values); $this->filterPreCheck($values);
// Check the input data // Check the input data
$error = ! $this->checkDataIsValid($values, true); $error = ! $this->checkDataIsValid($values, true, Null);
$view = $this->getNameOfView(); $view = $this->getNameOfView();
@ -66,7 +66,7 @@ abstract class AbstractClubsController extends BaseController
$this->filterPreCheck($values); $this->filterPreCheck($values);
// Check the input data // Check the input data
$error = ! $this->checkDataIsValid($values, false); $error = ! $this->checkDataIsValid($values, false, $obj);
$view = $this->getNameOfView(); $view = $this->getNameOfView();
@ -101,7 +101,7 @@ abstract class AbstractClubsController extends BaseController
protected function filterPreCheck(&$values){} protected function filterPreCheck(&$values){}
protected function checkDataIsValid($values, bool $isNew) protected function checkDataIsValid($values, bool $isNew, $obj)
{ {
$error = false; $error = false;
// Check existence of the required fields // Check existence of the required fields
@ -147,7 +147,10 @@ abstract class AbstractClubsController extends BaseController
$data = array(); $data = array();
foreach($this->getDataMapping() as $m => $i) foreach($this->getDataMapping() as $m => $i)
{
if(isset($values[$m]))
$data[$m] = $values[$m]; $data[$m] = $values[$m];
}
return urlencode(json_encode($data)); return urlencode(json_encode($data));
} }
@ -156,7 +159,12 @@ abstract class AbstractClubsController extends BaseController
public function applyData($obj, $values) public function applyData($obj, $values)
{ {
foreach($this->getDataMapping() as $m => $v) $this->applyDataToObject($obj, $values, $this->getDataMapping());
}
protected function applyDataToObject($obj, $values, $mapping)
{
foreach($mapping as $m => $v)
{ {
$functionName = $this->getSetterMethodName($m, $v); $functionName = $this->getSetterMethodName($m, $v);
@ -165,14 +173,14 @@ abstract class AbstractClubsController extends BaseController
continue; continue;
} }
$value = (isset($values[$m])) ? $values[$m] : null; $value = (isset($values[$m]) && strlen($values[$m]) > 0) ? $values[$m] : null;
$obj->$functionName($value); $obj->$functionName($value);
} }
} }
private function getSetterMethodName($m, $options) private function getSetterMethodName($m, $options)
{ {
if(isset($options['setter'])) if(array_key_exists('setter', $options))
return $options['setter']; return $options['setter'];
$firstChar = substr($m, 0, 1); $firstChar = substr($m, 0, 1);

View File

@ -8,8 +8,13 @@ defined('_JEXEC') or die;
class ClubsControllerUser extends AbstractClubsController class ClubsControllerUser extends AbstractClubsController
{ {
private function checkUserName($username, $id = -1) private function checkUserName($username, $obj = null)
{ {
$id = -1;
if(!is_null($obj))
$id = $obj->getId();
return ClubsUser::isUserNameFree($username, $id); return ClubsUser::isUserNameFree($username, $id);
} }
@ -24,7 +29,7 @@ class ClubsControllerUser extends AbstractClubsController
'user'=>array('required'=>true, 'name'=>'Benutzername', 'filter'=>'cmd'), 'user'=>array('required'=>true, 'name'=>'Benutzername', 'filter'=>'cmd'),
'pwd'=>array('required'=>false, 'name'=>'Passwort', 'filter'=>'string', 'setter'=>'setPassword'), 'pwd'=>array('required'=>false, 'name'=>'Passwort', 'filter'=>'string', 'setter'=>'setPassword'),
'pwdConfirm'=>array('required'=>false, 'name'=>'Passwortwiederholung', 'filter'=>'string', 'setter'=>null), 'pwdConfirm'=>array('required'=>false, 'name'=>'Passwortwiederholung', 'filter'=>'string', 'setter'=>null),
'name'=>array('required'=>true, 'name'=>'Benutzername', 'filter'=>'string'), 'name'=>array('required'=>true, 'name'=>'Bürgerlicher Name', 'filter'=>'string'),
'address'=>array('required'=>true, 'name'=>'Adresse', 'filter'=>'string'), 'address'=>array('required'=>true, 'name'=>'Adresse', 'filter'=>'string'),
'city'=>array('required'=>true, 'name'=>'Stadt', 'filter'=>'string'), 'city'=>array('required'=>true, 'name'=>'Stadt', 'filter'=>'string'),
'mail'=>array('required'=>true, 'name'=>'E-Mail', 'filter'=>'string'), 'mail'=>array('required'=>true, 'name'=>'E-Mail', 'filter'=>'string'),
@ -37,13 +42,13 @@ class ClubsControllerUser extends AbstractClubsController
* {@inheritDoc} * {@inheritDoc}
* @see AbstractClubsController::checkData() * @see AbstractClubsController::checkData()
*/ */
protected function checkDataIsValid($values, $isNew) protected function checkDataIsValid($values, $isNew, $obj)
{ {
if(! parent::checkDataIsValid($values, $isNew)) if(! parent::checkDataIsValid($values, $isNew, $obj))
return false; return false;
// TODO Auto-generated method stub // TODO Auto-generated method stub
if(isset($values['pwd'])) if(isset($values['pwd']) && strlen($values['pwd']) > 0)
{ {
$pwd = $values['pwd']; $pwd = $values['pwd'];
$pwdConfirm = $values['pwdConfirm']; $pwdConfirm = $values['pwdConfirm'];
@ -70,7 +75,7 @@ class ClubsControllerUser extends AbstractClubsController
} }
} }
if(! $this->checkUserName(trim($values['user']))) if(! $this->checkUserName(trim($values['user']), $obj))
{ {
Factory::getApplication()->enqueueMessage('Username ' . $$values['user'] . ' ist nicht gültig.', 'error'); Factory::getApplication()->enqueueMessage('Username ' . $$values['user'] . ' ist nicht gültig.', 'error');
return false; return false;
@ -97,7 +102,15 @@ class ClubsControllerUser extends AbstractClubsController
public function applyData($obj, $values) public function applyData($obj, $values)
{ {
// TODO Auto-generated method stub // TODO Auto-generated method stub
parent::applyData($obj, $values); $mapping = $this->getDataMapping();
if(strlen($values['pwd']) == 0)
{
unset($values['pwd']);
unset($mapping['pwd']);
}
$this->applyDataToObject($obj, $values, $mapping);
} }

View File

@ -233,16 +233,20 @@ class ClubsUser extends AbstractClubsModel
if($this->id === 'new') if($this->id === 'new')
return; return;
if(password_needs_rehash($this->password, PASSWORD_DEFAULT)) if(password_needs_rehash($this->password, PASSWORD_DEFAULT) || true)
{ {
$this->password = password_hash($password, PASSWORD_DEFAULT); $copy = ClubsUser::loadUser($this->id);
$copy->password = password_hash($password, PASSWORD_DEFAULT);
$copy->save();
$dbo = Factory::getDbo(); // $this->password = password_hash($password, PASSWORD_DEFAULT);
$q = $dbo->getQuery(true); // $dbo = Factory::getDbo();
$q->update(self::tableName)->set('password=' . $q->q($this->password))->where('id=' . (int) $this->id);
$dbo->setQuery($q); // $q = $dbo->getQuery(true);
$dbo->execute(); // $q->update(self::tableName)->set('password=' . $q->q($this->password))->where('id=' . (int) $this->id);
// $dbo->setQuery($q);
// $dbo->execute();
} }
} }

View File

@ -25,4 +25,4 @@ defined('_JEXEC') or die;
<?php endforeach; ?> <?php endforeach; ?>
</table> </table>
<div><a href='<?php echo Route::_('index.php?option=com_clubs&view=position&id=new'); ?>'>Neues Angebot anlegen</a></div> <div><a href='<?php echo Route::_('index.php?option=com_clubs&view=position&id=new'); ?>'>Neuen Posten anlegen</a></div>