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

View File

@ -8,8 +8,13 @@ defined('_JEXEC') or die;
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);
}
@ -24,7 +29,7 @@ class ClubsControllerUser extends AbstractClubsController
'user'=>array('required'=>true, 'name'=>'Benutzername', 'filter'=>'cmd'),
'pwd'=>array('required'=>false, 'name'=>'Passwort', 'filter'=>'string', 'setter'=>'setPassword'),
'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'),
'city'=>array('required'=>true, 'name'=>'Stadt', 'filter'=>'string'),
'mail'=>array('required'=>true, 'name'=>'E-Mail', 'filter'=>'string'),
@ -37,13 +42,13 @@ class ClubsControllerUser extends AbstractClubsController
* {@inheritDoc}
* @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;
// TODO Auto-generated method stub
if(isset($values['pwd']))
if(isset($values['pwd']) && strlen($values['pwd']) > 0)
{
$pwd = $values['pwd'];
$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');
return false;
@ -97,7 +102,15 @@ class ClubsControllerUser extends AbstractClubsController
public function applyData($obj, $values)
{
// 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')
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);
$q->update(self::tableName)->set('password=' . $q->q($this->password))->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
// $dbo = Factory::getDbo();
// $q = $dbo->getQuery(true);
// $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; ?>
</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>