private function getModel() { if (!$this->isGuest && $this->_model === null) { if ('Users' == Yii::app()->session->get("typeAuthorize")) { $this->_model = Users::model()->findByPk($this->id, array('select' => 'login, idRole')); } else { if ('Admins' == Yii::app()->session->get("typeAuthorize")) { $this->_model = Admins::model()->findByPk($this->id, array('select' => 'login, role')); } } } return $this->_model; }
/** * 判断用户是否有权限 * @param type $type 判断权限类型 * @param type $fuid 用户ID,默认为当前登录用户 * @param type $return 是否返回 * @param type $json 是否以JSON格式输出 * @return boolean */ public function checkPower($type, $fuid = '', $return = false, $json = false) { $uid = $fuid ? $fuid : Yii::app()->user->id; if (!$uid) { if ($return) { return false; } elseif (!$json and !Yii::app()->request->isAjaxRequest) { $this->redirect(array('/site/login')); } else { $this->jsonOutPut(0, '请先登录'); } } if ($type == 'login') { $pinfo = Admins::model()->find('uid=:uid', array(':uid' => $uid)); if (!$pinfo) { if ($return) { return false; } elseif (!$json and !Yii::app()->request->isAjaxRequest) { throw new CHttpException(403, '您无权该操作'); } else { $this->jsonOutPut(0, '不是管理员'); } } return true; } $power = Admins::model()->find('powers=:p AND uid=:uid', array(':p' => $type, ':uid' => $uid)); if (!$power) { if ($return) { return false; } elseif (!$json and !Yii::app()->request->isAjaxRequest) { throw new CHttpException(403, '您无权该操作'); } else { $this->jsonOutPut(0, '您无权该操作'); } } return true; }
public function actionDeladmin($id) { $this->checkPower('delAdmin'); Admins::model()->deleteAll('uid=:uid', array(':uid' => $id)); Users::model()->updateByPk($id, array('isAdmin' => 0)); $this->redirect(array('users/admins')); }
protected function beforeSave() { if (parent::beforeSave()) { $string = !empty($this->description) ? $this->description : $this->text; $charset = mb_detect_encoding($string); $strLength = iconv_strlen($string, $charset); if (500 < $strLength) { $string = strip_tags($string); $string = substr($string, 0, 500); $string = rtrim($string, "!,.-"); $string = substr($string, 0, strrpos($string, ' ')); $this->description = $string . "… "; } if ($this->isNewRecord) { $this->dateCreate = new CDbExpression('NOW()'); if (Admins::model()->checkAccess(Yii::app()->user->role, Yii::app()->params['permission']['2'])) { $this->moderationAppruv = 1; } } else { $this->dateCreate = date('Y-m-d H:i:s', strtotime($this->dateCreate)); $this->dateUpdate = new CDbExpression('NOW()'); } return true; } else { return false; } }
public function actionDelete_admin() { if (isset($_GET['idAdmin'])) { Admins::model()->deleteByPk($_GET['idAdmin']); $this->redirect($this->createAbsoluteUrl('default/list_admins', $_GET)); } }