Controller.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Controller is the customized base controller class.
  4. * All controller classes for this application should extend from this base class.
  5. */
  6. class Controller extends CController
  7. {
  8. /**
  9. * @var string the default layout for the controller view. Defaults to '//layouts/column1',
  10. * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
  11. */
  12. public $layout='//layouts/main';
  13. /**
  14. * @var array context menu items. This property will be assigned to {@link CMenu::items}.
  15. */
  16. public $menu=array();
  17. /**
  18. * @var array the breadcrumbs of the current page. The value of this property will
  19. * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
  20. * for more details on how to specify this property.
  21. */
  22. public $breadcrumbs=array();
  23. public $c_time;
  24. public function __construct($id, $module=null){
  25. parent::__construct($id, $module);
  26. $this->c_time = time();
  27. // $debug = Yii::app()->request->getParam('debug');
  28. // if ($debug !== null){
  29. // error_reporting(E_ALL);
  30. // ini_set('display_errors', '1');
  31. // }
  32. }
  33. /**
  34. * 获取管理员的信息
  35. */
  36. public function getAdminInfo(){
  37. $admin_user = Yii::app()->user->getId();
  38. $criteria = new EMongoCriteria();
  39. $criteria->_id('==', $admin_user);
  40. $cursor = User::model()->find($criteria);
  41. return $cursor->attributes;
  42. }
  43. /**
  44. * Checks if srbac access is granted for the current user
  45. * @param String $action . The current action
  46. * @return boolean true if access is granted else false
  47. */
  48. protected function beforeAction($action) {
  49. if(APPLICATION == 'common'){
  50. Yii::app()->runController('common/index/index');
  51. die();
  52. }
  53. $za = new ZAuth();
  54. $access = $za->getAuthItem($this);
  55. if(substr(str_replace('http://', '', Yii::app()->request->hostInfo) , 0,3)=='api'){
  56. if(isset($this->module->id)&&$this->module->id=='api'){
  57. return true;
  58. }else{
  59. return false;
  60. }
  61. }
  62. if(substr(str_replace('http://', '', Yii::app()->request->hostInfo) , 0,5)=='admin'){
  63. if(isset($this->module->id)&&$this->module->id=='api'){
  64. return false;
  65. }
  66. }
  67. //Always allow access if $access is in the allowedAccess array
  68. $always_allow = $this->allowedAccess();
  69. foreach ($always_allow as $k => $v){
  70. $always_allow[$k] = strtolower($v);
  71. }
  72. if (in_array(strtolower($access), $always_allow)) {
  73. return true;
  74. }
  75. // Check for access
  76. if (!Yii::app()->user->checkAccess($access)) {
  77. if ($this->isSuperAdmin()){
  78. return true;
  79. } else {
  80. return $this->onUnauthorizedAccess();
  81. }
  82. } else {
  83. return true;
  84. }
  85. }
  86. /**
  87. * 总是允许访问的操作
  88. */
  89. protected function allowedAccess(){
  90. return array('sitelogin', 'siteregister', 'siteerror', 'sitelogout','admin-sitelogin');
  91. }
  92. /**
  93. * 是否是超级管理员
  94. */
  95. protected function isSuperAdmin(){
  96. $auth = Yii::app()->getAuthManager();
  97. $user_id = Yii::app()->user->getId();
  98. if (!$user_id){
  99. return false;
  100. }
  101. $user_auth = $auth->getAuthAssignment($auth->super_admin, $user_id);
  102. if ($user_auth){
  103. return true;
  104. } else {
  105. return false;
  106. }
  107. }
  108. /**
  109. * 未通过验证
  110. */
  111. protected function onUnauthorizedAccess(){
  112. if (Yii::app()->user->isGuest) {
  113. if (Yii::app()->request->isAjaxRequest) {
  114. CommonFn::requestAjax(false, '请重新登陆');
  115. } else {
  116. Yii::app()->user->loginRequired();
  117. }
  118. } else {
  119. if (Yii::app()->request->isAjaxRequest) {
  120. CommonFn::requestAjax(false, '你没有权限!');
  121. } else {
  122. $za = new ZAuth();
  123. $access = $za->getAuthItem($this);
  124. //列表管理已完成,登录后可以进入首页
  125. if (strtolower($access) == 'siteindex'){
  126. return true;
  127. }
  128. throw new CHttpException(403, '你没有权限!', 403);
  129. }
  130. return false;
  131. }
  132. }
  133. }