codeIgniter驗證表單,Validation用法,規則設定
阿新 • • 發佈:2019-02-01
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
protected $_codeList = array(); // 介面code列表
protected $_apiCodeList = array(); // 業務介面code
protected $_debug = FALSE;
public $userToken = NULL;
public $userSecret = NULL ;
public $adminUserId = 0;
public $isSuperAdmin = FALSE;
public $roleId = 0;
const TEXT_NO_CONTENT = '請填寫內容';
public function __construct()
{
parent::__construct();
$this->load->config('siteinfo');
// $this->load->library('OptLogLib'); TODO 日誌記錄
$this->load->library('Session');
$this->load->helpers('cookie');
$this->load->config('code_list');
$this->load->config('pass_access');
$this->_codeList = $this->config->item('code_list');
$this->_codeList = $this->_codeList + $this ->_apiCodeList; // 合併介面code
$this->_debug = $this->input->post_get('__debug__');
$this->userToken = get_cookie('user_token');
$this->userSecret = get_cookie('user_secret');
$passAccess = $this->config->item('pass_access');
foreach($passAccess as $key => $v)
{
$passAccess[$key] = strtolower($v);
}
$urlAction = strtolower($this->router->class . '/' . $this->router->method);
$this->adminUserId = $this->_getUserIdByToken();
$this->isSuperAdmin = $this->_isSuper($this->adminUserId);
$this->roleId = $this->_getUserInfo('role_id');
if(!in_array($urlAction, $passAccess) && !getenv('LOGIN_DEBUG')) //
{
if(!$this->_isLogined())
{
$this->outPutJson(409);
}
/* 驗證使用者許可權
if(!$this->_checkAccess($this->adminUserId))
{
$this->_outPutJson(301);
} */
}
}
/**
* 輸出json
* @param number $code
* @param array $data
*/
public function outPutJson($code = 0, array $data = array(), $msg = '')
{
$data = is_array($data) ? $data : array();
$this->_formatoutPutData($data);
header('Content-Type:application/json; charset=utf-8');
header('Cache-Control: no-cache, must-revalidate');
header("Access-Control-Allow-Origin: {$this->config->item('allow_header')}"); // 允許任何訪問(包括ajax跨域)
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$msg = isset($this->_codeList[$code]) ? $this->_codeList[$code] : $msg;
exit(json_encode(array(
'data' => $data,
'code' => intval($code),
'msg' => $msg,
)));
}
/**
* 是否已經登入 TODO
*/
protected function _isLogined()
{
if(empty($this->userToken) || empty($this->userSecret))
{
return FALSE;
}
$this->load->model('AdminUserModel');
if($this->AdminUserModel->isLogined($this->userToken, $this->userSecret))
{
return TRUE;
}
return FALSE;
}
/**
* 校驗引數情況
* @param array $require
* 傳必填欄位,格式為
* array('column1' => '請選擇填寫column1', 'column2' => '請選擇填寫column2')
* @return require提示的欄位,預設code為1001
*/
protected function _validate($require = array(), $method = 'post', $emptyRequire = array())
{
if($method == 'post')
{
$posts = $this->input->post();
}
elseif($method == 'get')
{
$posts = $this->input->get();
}
if(empty($posts))
{
return self::TEXT_NO_CONTENT;
}
if(!empty($require))
{
foreach($require as $key => $value)
{
if(!isset($posts[$key]) || $posts[$key] === '' ||
(is_array($posts[$key]) && empty($posts[$key])) ||
(in_array($posts[$key],$emptyRequire) && empty($posts[$key]))
)
{
return $value;
}
}
}
return TRUE;
}
/**
* 格式化陣列欄位
* @param array $data
*/
private function _formatoutPutData(array &$data)
{
if(is_array($data) && !empty($data)){
foreach ($data as $k => &$v){
if(!is_array($v)){
$v = trim($v);
}else{
$this->_formatoutPutData($v);
}
}
}
}
/**
* 根據token獲取userId
* @return boolean
*/
private function _getUserIdByToken()
{
$this->load->model('AdminUserModel');
return $this->AdminUserModel->getUserIdByToken($this->userToken, $this->userSecret);
}
/**
* 判斷是否超級管理員
* @return boolean
*/
private function _isSuper($userId)
{
$this->load->config('super_identity');
$this->load->model('AdminUserModel');
$info = $this->AdminUserModel->getUserInfo(array('id' => $userId, 'is_deleted' => 0, 'status' => 1), 'user_identity');
if(!empty($info) && $info['user_identity'] === $this->config->item('super_identity'))
{
return TRUE;
}
return FALSE;
}
private function _getUserInfo($field = '')
{
$userId = $this->_getUserIdByToken();
$fields = empty($field) ? 'username, system_id, ch_name, role_id' : $field;
$info = $this->AdminUserModel->getUserInfo(array('id' => $userId, 'is_deleted' => 0), $fields);
if($field)
{
return $info[$field];
}
return $info;
}
/**
* 檢查使用者許可權
*/
private function _checkAccess($userId)
{
$urlAction = strtolower($this->router->class . '/' . $this->router->method);
if($this->_isSuper($userId))
{
return TRUE;
}
$this->load->model('AdminUserModel');
$userInfo = $this->AdminUserModel->getUserInfo(array('id' => $userId, 'is_deleted' => 0, 'status' => 0), 'system_id,department_id,role_id,privilege_id');
if(!empty($userInfo))
{
$this->load->model('MenuModel');
$menuWhere = array(
'module' => $urlAction,
'is_deleted' => 0,
'status' => 1
);
$menuInfo = $this->MenuModel->getMenuInfo($menuWhere, 'id');
if(!empty($userInfo['privilege_id']))
{
$arrPrivilegeId = explode(',', $userInfo['privilege_id']);
if(!empty($menuInfo) && in_array($menuInfo['id'], $arrPrivilegeId))
{
return TRUE;
}
}
$this->load->model('RoleModel');
$this->load->model('MenuModel');
$roleWhere = array(
'id' => $userInfo['role_id'],
'system_id' => $userInfo['system_id'],
'department_id' => $userInfo['department_id'],
'is_deleted' => 0,
'status' => 1
);
$roleInfo = $this->RoleModel->getRoleInfo($roleWhere, 'id');
if(!empty($roleInfo))
{
$privilegeWhere = array(
'role_id' => $roleInfo['id'],
'is_deleted' => 0
);
$menuIds = $this->RoleModel->getPrivileges();
if(!empty($menuIds) && in_array($menuInfo['id'], $menuIds))
{
return TRUE;
}
}
}
return FALSE;
}
};