SBaseController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * SBaseController class file.
  4. *
  5. * @author Spyros Soldatos <spyros@valor.gr>
  6. * @link http://code.google.com/p/srbac/
  7. */
  8. /**
  9. * SBaseController must be extended by all of the applications controllers
  10. * if the auto srbac should be used.
  11. * You can import it in your main config file as<br />
  12. * 'import'=>array(<br />
  13. * 'application.modules.srbac.controllers.SBaseController',<br />
  14. * ),
  15. *
  16. *
  17. * @author Spyros Soldatos <spyros@valor.gr>
  18. * @package srbac.controllers
  19. * @since 1.0.2
  20. */
  21. Yii::import("srbac.components.Helper");
  22. class SBaseController extends CController {
  23. /**
  24. * Checks if srbac access is granted for the current user
  25. * @param String $action . The current action
  26. * @return boolean true if access is granted else false
  27. */
  28. protected function beforeAction($action) {
  29. $del = Helper::findModule('srbac')->delimeter;
  30. //srbac access
  31. $mod = $this->module !== null ? $this->module->id . $del : "";
  32. $contrArr = explode($del, $this->id);
  33. $contrArr[sizeof($contrArr) - 1] = ucfirst($contrArr[sizeof($contrArr) - 1]);
  34. $controller = implode(".", $contrArr);
  35. $contr = str_replace($del, ".", $this->id);
  36. $access = $mod . $controller . ucfirst($this->action->id);
  37. //Always allow access if $access is in the allowedAccess array
  38. if (in_array($access, $this->allowedAccess())) {
  39. return true;
  40. }
  41. //Allow access if srbac is not installed yet
  42. if (!Yii::app()->getModule('srbac')->isInstalled()) {
  43. return true;
  44. }
  45. //Allow access when srbac is in debug mode
  46. if (Yii::app()->getModule('srbac')->debug) {
  47. return true;
  48. }
  49. // Check for srbac access
  50. if (!Yii::app()->user->checkAccess($access) || Yii::app()->user->isGuest) {
  51. $this->onUnauthorizedAccess();
  52. } else {
  53. return true;
  54. }
  55. }
  56. /**
  57. * The auth items that access is always allowed. Configured in srbac module's
  58. * configuration
  59. * @return The always allowed auth items
  60. */
  61. protected function allowedAccess() {
  62. Yii::import("srbac.components.Helper");
  63. return Helper::findModule('srbac')->getAlwaysAllowed();
  64. }
  65. protected function onUnauthorizedAccess() {
  66. /**
  67. * Check if the unautorizedacces is a result of the user no longer being logged in.
  68. * If so, redirect the user to the login page and after login return the user to the page they tried to open.
  69. * If not, show the unautorizedacces message.
  70. */
  71. if (Yii::app()->user->isGuest) {
  72. Yii::app()->user->loginRequired();
  73. } else {
  74. $mod = $this->module !== null ? $this->module->id : "";
  75. $access = $mod . ucfirst($this->id) . ucfirst($this->action->id);
  76. $error["code"] = "403";
  77. $error["title"] = Helper::translate('srbac', 'You are not authorized for this action');
  78. $error["message"] = Helper::translate('srbac', 'Error while trying to access') . ' ' . $mod . "/" . $this->id . "/" . $this->action->id . ".";
  79. //You may change the view for unauthorized access
  80. if (Yii::app()->request->isAjaxRequest) {
  81. $this->renderPartial(Yii::app()->getModule('srbac')->notAuthorizedView, array("error" => $error));
  82. } else {
  83. $this->render(Yii::app()->getModule('srbac')->notAuthorizedView, array("error" => $error));
  84. }
  85. return false;
  86. }
  87. }
  88. }