openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

commit 28adebb69e9f7e97c3631d15f8eccd5d53b30fba
parent 9657d5b743dc5e417578ce7fdce1b882c1d8efc2
Author: Jan Dankert <develop@jandankert.de>
Date:   Thu, 10 Sep 2020 18:02:54 +0200

The dispatcher is now able to call the action methods with parameters.

Diffstat:
modules/cms/Dispatcher.class.php | 12++++++++----
modules/cms/action/Action.class.php | 53+++++++++++++++++++++++++++++++++++++++++++++++------
modules/cms/action/RequestParams.class.php | 32+++++++++++++++++++++++---------
modules/cms/action/UserlistAction.class.php | 33++++++++++++++++++---------------
modules/util/exception/ValidationException.class.php | 14+++++---------
5 files changed, 101 insertions(+), 43 deletions(-)

diff --git a/modules/cms/Dispatcher.class.php b/modules/cms/Dispatcher.class.php @@ -317,15 +317,19 @@ class Dispatcher try { - $method = new \ReflectionMethod($do,$subactionMethodName); - $declaredClassName = $method->getDeclaringClass()->getShortName(); + $method = new \ReflectionMethod($do,$subactionMethodName); + $declaredClassName = $method->getDeclaringClass()->getShortName(); $declaredActionName = strtolower(substr($declaredClassName,0,strpos($declaredClassName,'Action'))); + $params = []; + foreach( $method->getParameters() as $parameter ) { + $params[ $parameter->getName() ] = $this->request->getRequiredRequestVar($parameter->getName(),OR_FILTER_RAW); + } - $method->invoke($do); // <== Executing the Action + $method->invokeArgs($do,$params); // <== Executing the Action } catch (\util\exception\ValidationException $ve) { - $do->addValidationError( $ve->fieldName ); + $do->addValidationError( $ve->fieldName,$ve->key ); } catch (\ReflectionException $re) { diff --git a/modules/cms/action/Action.class.php b/modules/cms/action/Action.class.php @@ -17,8 +17,11 @@ namespace { namespace cms\action { - use cms\model\User; - use util\Html; + use cms\model\BaseObject; + use cms\model\User; + use util\ClassUtils; + use util\exception\ValidationException; + use util\Html; use util\Session; use logger\Logger; use util\Http; @@ -218,6 +221,47 @@ namespace cms\action { // TODO - } + + /** + * @param $baseObject BaseObject + * @param $key String + * @param array $vars + * @param string $message + */ + protected function addNoticeFor($baseObject,$key,$vars = array(), $message='') { + $this->addNotice( strtolower(ClassUtils::getSimpleClassName($baseObject)),$baseObject->getName(),$key,OR_NOTICE_OK,$vars,array($message)); + } + + /** + * @param $baseObject BaseObject + * @param $key String + * @param array $vars + * @param string $message + */ + protected function addInfoFor($baseObject,$key,$vars = array(), $message='') { + $this->addNotice( strtolower(ClassUtils::getSimpleClassName($baseObject)),$baseObject->getName(),$key,OR_NOTICE_INFO,$vars,array($message)); + } + + /** + * @param $baseObject BaseObject + * @param $key String + * @param array $vars + * @param string $message + */ + protected function addWarningFor($baseObject,$key,$vars = array(), $message='') { + $this->addNotice( strtolower(ClassUtils::getSimpleClassName($baseObject)),$baseObject->getName(),$key,OR_NOTICE_WARN,$vars,array($message)); + } + + /** + * @param $baseObject BaseObject + * @param $key String + * @param array $vars + * @param string $message + */ + protected function addErrorFor($baseObject,$key,$vars = array(), $message='') { + $this->addNotice( strtolower(ClassUtils::getSimpleClassName($baseObject)),$baseObject->getName(),$key,OR_NOTICE_ERROR,$vars,array($message)); + } + /** * F�gt ein Meldung hinzu. * @@ -239,9 +283,6 @@ namespace cms\action { $this->templateVars['status'] = $status; $this->templateVars['success'] = ($status == OR_NOTICE_ERROR ? 'false' : 'true'); - if ($status == OR_NOTICE_OK && isset($_COOKIE['or_ignore_ok_notices'])) - return; - if (!is_array($log)) $log = array($log); @@ -250,7 +291,7 @@ namespace cms\action { $this->templateVars['notices'][] = array('type' => $type, 'name' => $name, - 'key' => 'NOTICE_' . $text, + 'key' => $text, 'vars' => $vars, 'text' => lang('NOTICE_' . $text, $vars), 'log' => $log, diff --git a/modules/cms/action/RequestParams.class.php b/modules/cms/action/RequestParams.class.php @@ -32,7 +32,8 @@ namespace { namespace cms\action { - use util\Text; + use util\exception\ValidationException; + use util\Text; class RequestParams { @@ -55,6 +56,18 @@ namespace cms\action { $this->isAction = @$_SERVER['REQUEST_METHOD'] == 'POST'; } + + + public function getRequiredRequestVar( $varName, $transcode ) { + $value = $this->getRequestVar($varName,$transcode); + + if ( empty( $value ) ) + throw new ValidationException($varName); + + return $value; + } + + /** * Ermittelt den Inhalt der gew�nschten Request-Variablen. * Falls nicht vorhanden, wird "" zur�ckgegeben. @@ -78,7 +91,12 @@ namespace cms\action { if (!isset($REQ[$varName])) return ''; + return $this->cleanText( $REQ[$varName], $transcode ); + } + + public function cleanText( $value, $transcode ) + { switch ($transcode) { case OR_FILTER_ALPHA: $white = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; @@ -102,25 +120,21 @@ namespace cms\action { case OR_FILTER_TEXT: // Allow all UTF-8 characters. - return mb_convert_encoding($REQ[$varName], 'UTF-8', 'UTF-8'); + return mb_convert_encoding($value, 'UTF-8', 'UTF-8'); case OR_FILTER_NUMBER: $white = '1234567890.'; break; case OR_FILTER_RAW: - return $REQ[$varName]; + return $value; default: throw new \LogicException('Unknown request filter', 'not found: ' . $transcode); } - $value = $REQ[$varName]; - $newValue = Text::clean($value, $white); - - return $newValue; - } - + return Text::clean($value, $white); + } /** * Ermittelt, ob der aktuelle Request eine Variable mit dem diff --git a/modules/cms/action/UserlistAction.class.php b/modules/cms/action/UserlistAction.class.php @@ -3,6 +3,8 @@ namespace cms\action; use cms\model\User; +use language\Messages; +use util\exception\ValidationException; // OpenRat Content Management System // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de @@ -64,22 +66,23 @@ class UserlistAction extends BaseAction function addView() { } - - - - function addPost() + + + /** + * @param $name name of the new user. + */ + public function addPost( $name ) { - if ( $this->getRequestVar('name') != '' ) - { - $this->user = new User(); - $this->user->add( $this->getRequestVar('name') ); - $this->addNotice('user',$this->user->name,'ADDED','ok'); - } - else - { - $this->addValidationError('name'); - $this->callSubAction('add'); - } + $name = $this->request->cleanText($name,OR_FILTER_ALPHANUM); + + $user = User::loadWithName($name); + + if ( !empty($user) ) + throw new ValidationException( 'name',Messages::USER_ALREADY_IN_DATABASE); + + $user = new User(); + $user->add( $name ); + $this->addNoticeFor($user, Messages::ADDED); } diff --git a/modules/util/exception/ValidationException.class.php b/modules/util/exception/ValidationException.class.php @@ -3,18 +3,17 @@ namespace util\exception; use Exception; -class ValidationException extends Exception +class ValidationException extends UIException { public $fieldName; // Die Exception neu definieren, damit die Mitteilung nicht optional ist - public function __construct($fieldName) + public function __construct($fieldName,$key='COMMON_VALIDATION_ERROR') { - $this->fieldName = $fieldName; // sicherstellen, dass alles korrekt zugewiesen wird - parent::__construct('Field validation: ' . $fieldName, 0, null); + parent::__construct( $key, ''); } // maßgeschneiderte Stringdarstellung des Objektes @@ -24,7 +23,4 @@ class ValidationException extends Exception . "{$this->getTraceAsString()}\n"; } -} - - -?>- \ No newline at end of file +}+ \ No newline at end of file