예제 #1
0
파일: Users.php 프로젝트: ph7pal/wedding
 /**
  * 权限判断
  * @param type $type 权限名
  * @param type $json 是否以json返回
  * @param type $return 是否不终止运行
  * @return boolean
  */
 public function checkPower($type, $json = false, $return = false)
 {
     if (Yii::app()->user->isGuest) {
         if ($return) {
             return false;
         } elseif (!$json and !Yii::app()->request->isAjaxRequest) {
             T::message(0, Yii::t('default', 'loginfirst'), Yii::app()->createUrl('site/login'));
         } else {
             T::jsonOutPut(0, Yii::t('default', 'loginfirst'));
         }
     } else {
         $uid = Yii::app()->user->id;
     }
     if ($type == 'login') {
         return true;
     }
     $userinfo = Users::getUserInfo($uid);
     if (!$userinfo) {
         if ($return) {
             return false;
         } elseif (!$json and !Yii::app()->request->isAjaxRequest) {
             T::message(0, '不存在的用户,请核实', Yii::app()->createUrl('site/logout'));
         } else {
             T::jsonOutPut(0, '不存在的用户,请核实');
         }
     }
     $gid = $userinfo['groupid'];
     $groupinfo = UserPower::getInfo($gid);
     if (!$groupinfo) {
         if ($return) {
             return false;
         } elseif (!$json and !Yii::app()->request->isAjaxRequest) {
             T::message(0, '您所在用户组不存在,请核实', Yii::app()->createUrl('site/logout'));
         } else {
             T::jsonOutPut(0, '您所在用户组不存在,请核实');
         }
     }
     $power = GroupPowers::model()->findByAttributes(array('powers' => $type), 'gid=:gid', array(':gid' => $gid));
     if (!$power) {
         $power = GroupPowers::model()->findByAttributes(array('powers' => 'all'), 'gid=:gid', array(':gid' => $gid));
     }
     if (!$power) {
         if ($return) {
             return false;
         } elseif (!$json and !Yii::app()->request->isAjaxRequest) {
             T::message(0, '您所在用户组【' . $groupinfo['title'] . '】无权该操作');
         } else {
             T::jsonOutPut(0, '您所在用户组【' . $groupinfo['title'] . '】无权该操作');
         }
     }
     return true;
 }