Created basic class to handle Ajax changes to the user

This commit is contained in:
Christian Wolf 2019-06-19 16:14:28 +02:00
parent e89aac01a3
commit 55b44d9b6f
2 changed files with 190 additions and 8 deletions

View File

@ -12,28 +12,165 @@ class InvalidUserDataException extends UserException {}
class ClubsHelperControllerUser
{
/**
* @param string $fcnName
* @param CommonClubsModelUser $user
* @param JInput $post
* @throws Exception
* @return string
*/
public function parse($fcnName, $user, $post)
{
switch($fcnName)
{
case 'name':
return $this->editName($user, $post);
$this->editName($user, $post);
break;
case 'username':
$this->editUsername($user, $post);
break;
case 'address':
$this->editAddress($user, $post);
break;
case 'city':
$this->editCity($user, $post);
break;
case 'mail':
$this->editMail($user, $post);
break;
case 'phone':
$this->editPhone($user, $post);
break;
case 'mobile':
$this->editMobile($user, $post);
break;
default:
throw new Exception();
}
return "index.php?option=com_clubs&view=part&type=user.$fcnName";
}
private function checkLen($value, $len, $msg)
{
if(strlen($value) < 5)
throw new InvalidUserDataException($msg);
}
private function editName($user, $post)
{
$name = trim($post->getString('value'));
$this->checkLen($name, 5, "Der Name muss mindestens 5 Zeichen lang sein.");
$user->setName($name);
$user->save();
}
private function editUsername($user, $post)
{
$username = $post->getString('value');
$validator = new ClubsHelperValidator();
$res = $validator->validateUsername($user, $username);
switch($res)
{
case ClubsHelperValidator::USERNAME_VALID:
$user->setUsername($username);
$user->save();
return;
case ClubsHelperValidator::USERNAME_NOT_FREE:
throw new InvalidUserDataException('Der Benutzername ist bereits vergeben.');
case ClubsHelperValidator::USERNAME_NOT_OK:
throw new InvalidUserDataException('Der Benutzername ist nicht korrekt formaitert. Er muss mit einem Buchstaben beginnen.');
default:
throw new Exception();
}
}
public function editName($user, $post)
private function editAddress($user, $post)
{
$name = $post->getString('name');
if(strlen($name) < 5)
throw new InvalidUserDataException("Der Name muss mindestens 5 Zeichen lang sein.");
$address = trim($post->getString('value'));
$this->checkLen($address, 10, 'Die Adresse muss mindestens 10 Zeichen lang sein.');
$user->setName($name);
$parts = array_filter(explode("\n", str_replace("\r", "\n", $address)));
if(count($parts) < 2)
throw new InvalidUserDataException('Die Adresse muss mindestens aus 2 Zeilen bestehen.');
$addr = join("\n", $parts);
$user->setAddress($addr);
$user->save();
}
private function editCity($user, $post)
{
$city = trim($post->getString('value'));
$this->checkLen($city, 4, 'Die Stadt muss aus mindestens 4 Zeichen bestehen.');
$user->setCity($city);
$user->save();
}
private function editMail($user, $post)
{
$mail = trim($post->getString('value'));
$this->checkLen($mail, 8, "Die Mail-Adresse muss aus mindestens 8 Zeichen bestehen.");
if(! preg_match('/..*@..*\...*/', $mail))
throw new InvalidUserDataException('Die Mail-Adresse hat kein gültiges Format.');
$user->setMail($mail);
$user->save();
}
private function editPhone($user, $post)
{
$phone = trim($post->getString('value'));
if(strlen($phone) == 0)
{
$user->setPhone(null);
$user->save();
return;
}
$validator = new ClubsHelperValidator();
$phonePlain = '';
$phoneDigits = '';
$valid = $validator->validatePhoneNumber($phone, $phonePlain, $phoneDigits);
$this->checkLen($phoneDigits, 6, 'Die Telefonnummer muss mindestens 6 Ziffern umfassen.');
if(! $valid)
throw new InvalidUserDataException('Keine gültige Telefonnummer gegeben. Erlaubte Zeichen sind 0-9, -, + und Leerzeichen. Bitte Vorwahl mit angeben.');
$user->setPhone($phonePlain);
$user->save();
}
private function editMobile($user, $post)
{
$mobile = trim($post->getString('value'));
if(strlen($mobile) == 0)
{
$user->setMobile(null);
$user->save();
return;
}
$validator = new ClubsHelperValidator();
$mobilePlain = '';
$mobileDigits = '';
$valid = $validator->validatePhoneNumber($mobile, $mobilePlain, $mobileDigits);
$this->checkLen($mobileDigits, 6, 'Die Handynummer muss mindestens 6 Ziffern umfassen.');
if(! $valid)
throw new InvalidUserDataException('Keine gültige Handynummer gegeben. Erlaubte Zeichen sind 0-9, -, + und Leerzeichen. Bitte Vorwahl mit angeben.');
$user->setMobile($mobilePlain);
$user->save();
return 'index.php?option=com_clubs&view=mypage&layout=parts&type=name';
}
}

View File

@ -0,0 +1,45 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class ClubsHelperValidator
{
public function validatePhoneNumber($number, &$cleanedNumber, &$plainDigits = null)
{
if($plainDigits !== null)
$plainDigits = preg_replace('/[^+0-9]/', '', $number);
$cleanedNumber = preg_replace('/[^-+ 0-9]/', '', $number);
$validNational = preg_match('/^0[0-9 -]+$/', $cleanedNumber);
$validInternational = preg_match('/^\+[0-9 -]+$/', $cleanedNumber);
return ($validNational || $validInternational);
}
const USERNAME_VALID = 0;
const USERNAME_NOT_FREE = 1;
const USERNAME_NOT_OK = 2;
/**
* @param CommonClubsModelUser $user
* @param string $username
*/
public function validateUsername($user, &$username)
{
// Remove false chars
$username = preg_replace('/[^a-zA-Z0-9!@#$%^&*()[]{}_+=\/\\?-]/', '', $username);
// Ensure start with an alphabetic char
if(! preg_match('/^[a-zA-Z]/', $username))
return self::USERNAME_NOT_OK;
if(! $user->isUsernameFree($username))
return self::USERNAME_NOT_FREE;
return self::USERNAME_VALID;
}
}