Made user controller working mostly. Not everything is tested but seems good

This commit is contained in:
Christian Wolf 2019-06-05 16:02:36 +02:00
parent 16e7ed0bc0
commit fc85e6b322
8 changed files with 170 additions and 51 deletions

View File

@ -59,9 +59,9 @@ abstract class AbstractClubsController extends BaseController
try try
{ {
// Fetch the posted data // Fetch the posted data
$values = $this->loadData(); $values = $this->loadData($this->additionalData());
$this->filterRawCheck($values); $this->filterRaw($values);
// Check the input data // Check the input data
if( ! $this->requiredDataIsAvailable($values) ) if( ! $this->requiredDataIsAvailable($values) )
@ -70,7 +70,9 @@ abstract class AbstractClubsController extends BaseController
if( ! $this->rawDataIsValid($values) ) if( ! $this->rawDataIsValid($values) )
throw new DataParsingException(); throw new DataParsingException();
$obj->setValues($values, true); $obj->applyAndMergeValues($values, true);
$this->filterObject($obj, $values);
// Do some additional tests by the controller // Do some additional tests by the controller
if( ! $this->objectValid($obj) ) if( ! $this->objectValid($obj) )
@ -103,7 +105,12 @@ abstract class AbstractClubsController extends BaseController
} }
} }
protected function loadData() protected function additionalData()
{
return array();
}
protected function loadData($additionalData)
{ {
$values = array(); $values = array();
$factory = $this->getFactory(); $factory = $this->getFactory();
@ -114,10 +121,17 @@ abstract class AbstractClubsController extends BaseController
$values[$column->getAlias()] = $column->getFilter()->getFilteredValue($input, $column->getAlias()); $values[$column->getAlias()] = $column->getFilter()->getFilteredValue($input, $column->getAlias());
} }
foreach($additionalData as $k => $v)
{
$values[$k] = $v->getFilteredValue($input, $k);
}
return $values; return $values;
} }
protected function filterRawCheck(&$values){} protected function filterRaw(&$values){}
protected function filterObject($obj){}
protected function objectValid($obj) protected function objectValid($obj)
{ {

View File

@ -48,6 +48,27 @@ abstract class AbstractCommonClubsModel
$this->values = $values; $this->values = $values;
} }
public function applyAndMergeValues($values, $unpack = true)
{
$vals = $this->getValues();
if($unpack)
$vals = $this->packExternalReferencesAsKeys($vals);
foreach($this->getFactory()->getAttributes() as $column)
{
if(array_key_exists($column->getAlias(), $values))
{
$vals[$column->getAlias()] = $values[$column->getAlias()];
}
}
if($unpack)
$vals = $this->unpackExternalReferencesFromKeys($vals);
$this->setValues($vals, false);
}
protected function setValue($key, $value) protected function setValue($key, $value)
{ {
if(is_null($this->values)) if(is_null($this->values))
@ -134,6 +155,7 @@ abstract class AbstractCommonClubsModel
$vals[$alias] = $a->packValue($vals[$alias]); $vals[$alias] = $a->packValue($vals[$alias]);
} }
// XXX Joins
return $vals; return $vals;
} }
@ -144,7 +166,8 @@ abstract class AbstractCommonClubsModel
foreach($factory->getAttributes() as $a) foreach($factory->getAttributes() as $a)
{ {
$alias = $a->getAlias(); $alias = $a->getAlias();
$vals[$alias] = $a->unpackValue($vals[$alias]); if(isset($vals[$alias]))
$vals[$alias] = $a->unpackValue($vals[$alias]);
} }
$joins = $factory->getJoins(); $joins = $factory->getJoins();

View File

@ -141,10 +141,16 @@ abstract class AbstractCommonClubsModelFactory
* @param int $id * @param int $id
* @return AbstractCommonClubsModel * @return AbstractCommonClubsModel
*/ */
public function loadById($id) { public function loadById($id, $throwErr = true)
{
$arr = $this->loadElements("main.id = " . ((int)$id) ); $arr = $this->loadElements("main.id = " . ((int)$id) );
if(sizeof($arr) == 0) if(sizeof($arr) == 0)
throw new ElementNotFoundException(); {
if($throwErr)
throw new ElementNotFoundException();
else
return null;
}
return $arr[0]; return $arr[0];
} }

View File

@ -8,14 +8,14 @@ class CommonClubsModelFactoryUser extends AbstractCommonClubsModelFactory
public function fetchAttributes() public function fetchAttributes()
{ {
return array( return array(
new CommonClubsModelColumnString('user'), new CommonClubsModelColumnString('user', new CommonClubsControllerMappingCmd('Benutzername')),
new CommonClubsModelColumnString('name'), new CommonClubsModelColumnString('name', new CommonClubsControllerMappingString('Bürgerlicher Name')),
new CommonClubsModelColumnString('password'), new CommonClubsModelColumnString('password', new CommonClubsControllerMappingString('Passwort', false)),
new CommonClubsModelColumnString('address'), new CommonClubsModelColumnString('address', new CommonClubsControllerMappingString('Adresse')),
new CommonClubsModelColumnString('city'), new CommonClubsModelColumnString('city', new CommonClubsControllerMappingString('Stadt')),
new CommonClubsModelColumnString('mail'), new CommonClubsModelColumnString('mail', new CommonClubsControllerMappingString('E-Mail')),
new CommonClubsModelColumnString('phone'), new CommonClubsModelColumnString('phone', new CommonClubsControllerMappingString('Telefonnummer', false)),
new CommonClubsModelColumnString('mobile') new CommonClubsModelColumnString('mobile', new CommonClubsControllerMappingString('Handynummer', false))
); );
} }

View File

@ -114,6 +114,12 @@ class CommonClubsModelUser extends AbstractCommonClubsModel
$this->setValue('password', $hash); $this->setValue('password', $hash);
} }
public function isPasswordSet()
{
$password = $this->getValues()['password'];
return isset($password) && strlen($password) > 0;
}
public function getPositions() public function getPositions()
{ {
return $this->fetchAssociatedElements(new CommonClubsModelFactoryUserassoc(), 'userid'); return $this->fetchAssociatedElements(new CommonClubsModelFactoryUserassoc(), 'userid');
@ -139,7 +145,7 @@ class CommonClubsModelUser extends AbstractCommonClubsModel
return true; return true;
} }
public function isUsernameSuitable($user) public function isUsernameFree($user)
{ {
$factory = new CommonClubsModelFactoryUser(); $factory = new CommonClubsModelFactoryUser();
$users = $factory->loadElements(null, null, function($q) use ($user){ $users = $factory->loadElements(null, null, function($q) use ($user){
@ -167,5 +173,33 @@ class CommonClubsModelUser extends AbstractCommonClubsModel
$db->execute(); $db->execute();
} }
public function dataIsValid()
{
if(! parent::dataIsValid())
return false;
if(! $this->usernameIsValid())
{
return false;
}
return true;
}
private function usernameIsValid()
{
$factory = $this->getFactory();
$medb = $factory->loadById($this->getId(), false);
if($medb !== null && $medb->getUserName() === $this->getUsername())
// No change was made
return true;
if(! $this->isUsernameFree($this->getUsername()) )
return false;
return true;
}
} }

View File

@ -28,7 +28,7 @@ class ClubsControllerClub extends AbstractClubsController
); );
} }
protected function filterRawCheck(&$values) protected function filterRaw(&$values)
{ {
if(is_null($values['charitable'])) if(is_null($values['charitable']))
$values['charitable'] = false; $values['charitable'] = false;

View File

@ -18,7 +18,7 @@ class ClubsControllerUser extends AbstractClubsController
return ClubsUser::isUserNameFree($username, $id); return ClubsUser::isUserNameFree($username, $id);
} }
protected function getNameOfElement() protected function getSingleBaseName()
{ {
return 'user'; return 'user';
} }
@ -38,49 +38,44 @@ class ClubsControllerUser extends AbstractClubsController
); );
} }
/**
* {@inheritDoc} protected function rawDataIsValid($values)
* @see AbstractClubsController::checkData()
*/
protected function rawDataIsValid($values, $isNew, $obj)
{ {
if(! parent::rawDataIsValid($values, $isNew, $obj)) if(! parent::rawDataIsValid($values))
return false; return false;
// TODO Auto-generated method stub if($this->passwordIsSet($values))
if(isset($values['pwd']) && strlen($values['pwd']) > 0)
{ {
$pwd = $values['pwd']; if( ! $this->passwordIsValid($values))
$pwdConfirm = $values['pwdConfirm'];
if(trim($pwd) != trim($pwdConfirm))
{
Factory::getApplication()->enqueueMessage('Die Passwörter stimmen nicht überein.', 'error');
return false; return false;
}
if(! ClubsUser::checkPasswordStrength(trim($pwd)))
{
Factory::getApplication()->enqueueMessage('Das Passwort ist zu schwach.', 'error');
return false;
}
}
else
{
if($isNew)
{
Factory::getApplication()->enqueueMessage('Für einen neuen Benutzer muss ein Passwort vergeben werden.', 'error');
return false;
}
} }
if(! $this->checkUserName(trim($values['user']), $obj)) return true;
}
private function passwordIsSet($values)
{
return isset($values['pwd']) && strlen($values['pwd']) > 0;
}
private function passwordIsValid($values)
{
$pwd = $values['pwd'];
$pwdConfirm = $values['pwdConfirm'];
if(trim($pwd) != trim($pwdConfirm))
{ {
Factory::getApplication()->enqueueMessage('Username ' . $$values['user'] . ' ist nicht gültig.', 'error'); Factory::getApplication()->enqueueMessage('Die Passwörter stimmen nicht überein.', 'error');
return false; return false;
} }
// FIXME Check password strength
// if(! ClubsUser::checkPasswordStrength(trim($pwd)))
// {
// Factory::getApplication()->enqueueMessage('Das Passwort ist zu schwach.', 'error');
// return false;
// }
return true; return true;
} }
@ -113,7 +108,54 @@ class ClubsControllerUser extends AbstractClubsController
$this->applyDataToObject($obj, $values, $mapping); $this->applyDataToObject($obj, $values, $mapping);
} }
protected function getFactory()
{
return new CommonClubsModelFactoryUser();
}
/**
*
* {@inheritDoc}
* @see AbstractClubsController::filterObject()
* @param CommonClubsModelUser $obj
*/
protected function filterObject($obj, $values)
{
// if($obj->isNew() && (empty($values['pwd']) || strlen($values['pwd']) == 0) )
if(isset($values['pwd']) && strlen($values['pwd']) > 0)
{
$obj->setPassword($values['pwd']);
}
}
/**
* {@inheritDoc}
* @see AbstractClubsController::objectValid()
* @param CommonClubsModelUser $obj
*/
protected function objectValid($obj)
{
if(! $obj->isPasswordSet())
{
Factory::getApplication()->enqueueMessage('Kein Passwort wurde vergeben.', 'error');
return false;
}
return true;
}
protected function filterRaw(&$values)
{
unset($values['password']);
}
protected function additionalData()
{
return array(
'pwd' => new CommonClubsControllerMappingString('Passwort'),
'pwdConfirm' => new CommonClubsControllerMappingString('Passwortwiederholung')
);
}
} }