handlers.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Taddeus Kroes
  6. * @date 05-10-2012
  7. */
  8. namespace webbasics;
  9. require_once 'router.php';
  10. require_once 'security.php';
  11. abstract class BaseHandler implements RouteHandler {
  12. function handleRequest(array $data) {
  13. list($method_name, $args) = $this->extractMethodAndArgs($data);
  14. return call_user_func_array(array($this, $method_name), $args);
  15. }
  16. protected function extractMethodAndArgs(array $data) {
  17. $request_type = strtolower($_SERVER['REQUEST_METHOD']);
  18. // Try to use first match value as method name, e.g. getAction() if
  19. // first match value is "action" and request method is "GET"
  20. if (count($data)) {
  21. $method_name = $request_type . camelize($data[0], true);
  22. if (method_exists($this, $method_name)) {
  23. // getAction() or getAction($data)
  24. array_shift($data);
  25. } else {
  26. // get($data) or post($data)
  27. $method_name = $request_type;
  28. }
  29. } else {
  30. // get() or post()
  31. $method_name = $request_type;
  32. }
  33. $args = count($data) ? array($data) : array();
  34. return array($method_name, $args);
  35. }
  36. }
  37. abstract class AuthenticatedHandler extends BaseHandler {
  38. function handleRequest(array $data) {
  39. // A user must be logged in
  40. self::checkLogin();
  41. // Base class will call the corresponding method
  42. return parent::handleRequest();
  43. }
  44. static function checkLogin() {
  45. Authentication::getInstance()->requireLogin();
  46. }
  47. }
  48. abstract class TokenizedHandler extends BaseHandler {
  49. const TOKEN_NAME = 'auth_token';
  50. function handleRequest(array $data) {
  51. // Token must exist and have the right value
  52. self::checkToken();
  53. // Base class will call the corresponding method
  54. return parent::handleRequest();
  55. }
  56. static function checkToken() {
  57. if (!isset($_REQUEST[self::TOKEN_NAME]))
  58. throw new AuthenticationError('token missing in request data');
  59. Authentication::getInstance()->requireToken($_REQUEST[self::TOKEN_NAME]);
  60. }
  61. }
  62. abstract class AuthorizedHandler extends BaseHandler {
  63. function handleRequest(array $data) {
  64. // A user must be logged in
  65. AuthenticatedHandler::checkLogin();
  66. // The user must have access to the called method
  67. list($method_name, $args) = $this->extractMethodAndArgs($data);
  68. $role = $this->determineRequiredRole($method_name, $args);
  69. Authentication::getInstance()->requireUserRole($role);
  70. // Base class will call the corresponding method
  71. return parent::handleRequest();
  72. }
  73. abstract function getRequiredRole($method_name, array $args);
  74. }
  75. abstract class TokenizedAuthenticatedHandler extends TokenizedHandler {
  76. function handleRequest(array $data) {
  77. // A user must be logged in
  78. AuthenticatedHandler::checkLogin();
  79. // Parent class will check the token
  80. return parent::handleRequest();
  81. }
  82. }
  83. abstract class TokenizedAuthorizedHandler extends AuthorizedHandler {
  84. function handleRequest(array $data) {
  85. // Token must exist and have the right value
  86. TokenizedHandler::checkToken();
  87. // Parent class will verify that a user is logged in and has access to
  88. // the called method
  89. return parent::handleRequest($data);
  90. }
  91. }
  92. ?>