diff --git a/docs/guide/authorization.md b/docs/guide/authorization.md index df40f2fae4..0e65381786 100644 --- a/docs/guide/authorization.md +++ b/docs/guide/authorization.md @@ -248,7 +248,7 @@ public function editArticle($id) throw new NotFoundHttpException; } if (!\Yii::$app->user->checkAccess('edit_article', ['article' => $article])) { - throw new AccessDeniedHttpException; + throw new ForbiddenHttpException; } // ... } diff --git a/extensions/debug/Module.php b/extensions/debug/Module.php index 06f2b76fbf..c1138433ec 100644 --- a/extensions/debug/Module.php +++ b/extensions/debug/Module.php @@ -10,7 +10,7 @@ namespace yii\debug; use Yii; use yii\base\Application; use yii\web\View; -use yii\web\AccessDeniedHttpException; +use yii\web\ForbiddenHttpException; /** * The Yii Debug Module provides the debug toolbar and debugger @@ -80,7 +80,7 @@ class Module extends \yii\base\Module } elseif ($action->id === 'toolbar') { return false; } else { - throw new AccessDeniedHttpException('You are not allowed to access this page.'); + throw new ForbiddenHttpException('You are not allowed to access this page.'); } } diff --git a/extensions/gii/Module.php b/extensions/gii/Module.php index a7bb3ed43f..30302b5ef7 100644 --- a/extensions/gii/Module.php +++ b/extensions/gii/Module.php @@ -8,7 +8,7 @@ namespace yii\gii; use Yii; -use yii\web\AccessDeniedHttpException; +use yii\web\ForbiddenHttpException; /** * This is the main module class for the Gii module. @@ -110,7 +110,7 @@ class Module extends \yii\base\Module if ($this->checkAccess()) { return parent::beforeAction($action); } else { - throw new AccessDeniedHttpException('You are not allowed to access this page.'); + throw new ForbiddenHttpException('You are not allowed to access this page.'); } } diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index c86d22f143..d05bbf8526 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,8 @@ Yii Framework 2 Change Log 2.0.0 beta under development ---------------------------- +- Enh #2103: Adds docblock descriptions and links to HTTP specs for new HTTP exception classes (danschmidt5189) +- Enh #2103: Renames AccessDeniedHttpException to ForbiddenHttpException (danschmidt5189) - Bug #1265: AssetController does not override 'js' and 'css' for compressed bundles (klimov-paul) - Bug #1326: The `visible` setting for `DetailView` doesn't work as expected (qiangxue) - Bug #1446: Logging while logs are processed causes infinite loop (qiangxue) diff --git a/framework/classes.php b/framework/classes.php index ea50822b22..38c6987f1c 100644 --- a/framework/classes.php +++ b/framework/classes.php @@ -196,7 +196,7 @@ return [ 'yii\validators\ValidationAsset' => YII_PATH . '/validators/ValidationAsset.php', 'yii\validators\Validator' => YII_PATH . '/validators/Validator.php', 'yii\web\AccessControl' => YII_PATH . '/web/AccessControl.php', - 'yii\web\AccessDeniedHttpException' => YII_PATH . '/web/AccessDeniedHttpException.php', + 'yii\web\ForbiddenHttpException' => YII_PATH . '/web/ForbiddenHttpException.php', 'yii\web\AccessRule' => YII_PATH . '/web/AccessRule.php', 'yii\web\Application' => YII_PATH . '/web/Application.php', 'yii\web\AssetBundle' => YII_PATH . '/web/AssetBundle.php', diff --git a/framework/web/AccessControl.php b/framework/web/AccessControl.php index b2230a7d6d..4499f5ce81 100644 --- a/framework/web/AccessControl.php +++ b/framework/web/AccessControl.php @@ -130,14 +130,14 @@ class AccessControl extends ActionFilter * The default implementation will redirect the user to the login page if he is a guest; * if the user is already logged, a 403 HTTP exception will be thrown. * @param User $user the current user - * @throws AccessDeniedHttpException if the user is already logged in. + * @throws ForbiddenHttpException if the user is already logged in. */ protected function denyAccess($user) { if ($user->getIsGuest()) { $user->loginRequired(); } else { - throw new AccessDeniedHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); + throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } } } diff --git a/framework/web/BadRequestHttpException.php b/framework/web/BadRequestHttpException.php index 3a6cfbb641..6e596dab1a 100644 --- a/framework/web/BadRequestHttpException.php +++ b/framework/web/BadRequestHttpException.php @@ -10,6 +10,12 @@ namespace yii\web; /** * BadRequestHttpException represents a "Bad Request" HTTP exception with status code 400. * + * Use this exception to represent a generic client error. In many cases, there + * may be an HTTP exception that more precisely describes the error. In that + * case, consider using the more precise exception to provide the user with + * additional information. + * + * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1 * @author Qiang Xue * @since 2.0 */ diff --git a/framework/web/AccessDeniedHttpException.php b/framework/web/ConflictHttpException.php similarity index 60% rename from framework/web/AccessDeniedHttpException.php rename to framework/web/ConflictHttpException.php index d83700bac4..6fa3f57bab 100644 --- a/framework/web/AccessDeniedHttpException.php +++ b/framework/web/ConflictHttpException.php @@ -8,12 +8,13 @@ namespace yii\web; /** - * AccessDeniedHttpException represents an "Access Denied" HTTP exception with status code 403. + * ConflictHttpException represents a "Conflict" HTTP exception with status code 409 * - * @author Qiang Xue + * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10 + * @author Dan Schmidt * @since 2.0 */ -class AccessDeniedHttpException extends HttpException +class ConflictHttpException extends HttpException { /** * Constructor. @@ -23,6 +24,6 @@ class AccessDeniedHttpException extends HttpException */ public function __construct($message = null, $code = 0, \Exception $previous = null) { - parent::__construct(403, $message, $code, $previous); + parent::__construct(409, $message, $code, $previous); } } diff --git a/framework/web/ForbiddenHttpException.php b/framework/web/ForbiddenHttpException.php new file mode 100644 index 0000000000..e96c225293 --- /dev/null +++ b/framework/web/ForbiddenHttpException.php @@ -0,0 +1,35 @@ + + * @since 2.0 + */ +class ForbiddenHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(403, $message, $code, $previous); + } +} diff --git a/framework/web/GoneHttpException.php b/framework/web/GoneHttpException.php new file mode 100644 index 0000000000..b78aa01b59 --- /dev/null +++ b/framework/web/GoneHttpException.php @@ -0,0 +1,34 @@ + + * @since 2.0 + */ +class GoneHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(410, $message, $code, $previous); + } +} diff --git a/framework/web/NotAcceptableHttpException.php b/framework/web/NotAcceptableHttpException.php new file mode 100644 index 0000000000..5a749c91e8 --- /dev/null +++ b/framework/web/NotAcceptableHttpException.php @@ -0,0 +1,33 @@ + + * @since 2.0 + */ +class NotAcceptableHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(406, $message, $code, $previous); + } +} diff --git a/framework/web/TooManyRequestsHttpException.php b/framework/web/TooManyRequestsHttpException.php new file mode 100644 index 0000000000..b5eb8898a5 --- /dev/null +++ b/framework/web/TooManyRequestsHttpException.php @@ -0,0 +1,33 @@ + + * @since 2.0 + */ +class TooManyRequestsHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(429, $message, $code, $previous); + } +} diff --git a/framework/web/UnauthorizedHttpException.php b/framework/web/UnauthorizedHttpException.php new file mode 100644 index 0000000000..0bea209046 --- /dev/null +++ b/framework/web/UnauthorizedHttpException.php @@ -0,0 +1,34 @@ + + * @since 2.0 + */ +class UnauthorizedHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(401, $message, $code, $previous); + } +} diff --git a/framework/web/UnsupportedMediaTypeHttpException.php b/framework/web/UnsupportedMediaTypeHttpException.php new file mode 100644 index 0000000000..715117e0ed --- /dev/null +++ b/framework/web/UnsupportedMediaTypeHttpException.php @@ -0,0 +1,34 @@ + + * @since 2.0 + */ +class UnsupportedMediaTypeHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(415, $message, $code, $previous); + } +} diff --git a/framework/web/User.php b/framework/web/User.php index d6948b62c6..b6f3568c9f 100644 --- a/framework/web/User.php +++ b/framework/web/User.php @@ -323,7 +323,7 @@ class User extends Component * Note that when [[loginUrl]] is set, calling this method will NOT terminate the application execution. * * @return Response the redirection response if [[loginUrl]] is set - * @throws AccessDeniedHttpException the "Access Denied" HTTP exception if [[loginUrl]] is not set + * @throws ForbiddenHttpException the "Access Denied" HTTP exception if [[loginUrl]] is not set */ public function loginRequired() { @@ -334,7 +334,7 @@ class User extends Component if ($this->loginUrl !== null) { return Yii::$app->getResponse()->redirect($this->loginUrl); } else { - throw new AccessDeniedHttpException(Yii::t('yii', 'Login Required')); + throw new ForbiddenHttpException(Yii::t('yii', 'Login Required')); } }