我无法让我的in_array在我的库上工作,两个函数都在同一个库中。我不会让我做的
$this->user_auth->hasPermission('modify', 'folder/controller-name');
通过使用上述功能,我可以检查是否有权修改。
在我的库函数hasPermission上$ key& $ value不起作用。
在我的登录功能上,我将权限取消序列化
我无法弄清楚为什么hasPermission总是返回false。
登录
public function login($username, $password) {
$user_query = $this->CI->db->query("SELECT * FROM " . $this->CI->db->dbprefix . "user
WHERE username = " . $this->CI->db->escape($username) . "
AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1(" . $this->CI->db->escape($password) . ")))))
OR password = " . $this->CI->db->escape(md5($password)) . ")
AND status = '1'
");
if ($user_query->num_rows() > 0) {
$this->user_id = $user_query->row('user_id');
$this->username = $user_query->row('username');
$this->user_group_id = $user_query->row('user_group_id');
$data_session = array(
'logged' => true,
'user_id' => $this->user_id
);
$this->CI->session->set_userdata($data_session);
$user_group_query = $this->CI->db->query("SELECT permission FROM " . $this->CI->db->dbprefix . "user_group
WHERE user_group_id = '" . (int)$user_query->row('user_group_id') . "'");
$permissions = unserialize($user_group_query->row('permission')); // Vardumps fine.
if (is_array($permissions)) {
foreach ($permissions as $key => $value) {
$this->permission[$key] = $value;
}
}
return true;
} else {
return false;
}
}
有权限
public function hasPermission($key, $value) {
if (isset($this->permission[$key])) {
return in_array($value, $this->permission[$key]);
} else {
return false;
}
}
Var Dump
array(2) {
["access"]=> array(18) {
[0]=> string(18) "catalog/Categories"
[1]=> string(27) "code_examples/Code_examples"
[2]=> string(23) "dashboard_modules/Chart"
[3]=> string(34) "dashboard_modules/Latest_customers"
[4]=> string(30) "dashboard_modules/Latest_users"
[5]=> string(14) "design/Banners"
[6]=> string(14) "design/Layouts"
[7]=> string(16) "extension/Module"
[8]=> string(15) "module/Category"
[9]=> string(16) "module/Slideshow"
[10]=> string(43) "module_code_examples/Codeigniter_controller"
[11]=> string(38) "module_code_examples/Codeigniter_email"
[12]=> string(39) "module_code_examples/Codeigniter_routes"
[13]=> string(16) "settings/Setting"
[14]=> string(14) "settings/Store"
[15]=> string(8) "tool/Log"
[16]=> string(10) "user/Users"
[17]=> string(17) "user/Users_groups"
}
["modify"]=> array(18) {
[0]=> string(18) "catalog/Categories"
[1]=> string(27) "code_examples/Code_examples"
[2]=> string(23) "dashboard_modules/Chart"
[3]=> string(34) "dashboard_modules/Latest_customers"
[4]=> string(30) "dashboard_modules/Latest_users"
[5]=> string(14) "design/Banners"
[6]=> string(14) "design/Layouts"
[7]=> string(16) "extension/Module"
[8]=> string(15) "module/Category"
[9]=> string(16) "module/Slideshow"
[10]=> string(43) "module_code_examples/Codeigniter_controller"
[11]=> string(38) "module_code_examples/Codeigniter_email"
[12]=> string(39) "module_code_examples/Codeigniter_routes"
[13]=> string(16) "settings/Setting"
[14]=> string(14) "settings/Store"
[15]=> string(8) "tool/Log"
[16]=> string(10) "user/Users"
[17]=> string(17) "user/Users_groups"
}
}
更新这是我使用hasPermission
的方式public function index() {
if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validateForm()) {
redirect();
}
// load view area & content
}
public function validateForm() {
if (!$this->user_auth->hasPermission('modify', 'folder/controller-name')) {
// Displays error
}
// Other checks.
}
提前致谢
答案 0 :(得分:2)
我在一个简单的php文件上做了一些测试:
<?php
$mydata = array(
"test" => array("one", "two", "three")
);
var_dump(hasPermission("test", "two"));
function hasPermission($key, $value)
{
if (isset($mydata[$key]))
{
echo "here";
return in_array($value, $mydata[$key]);
}
else
{
return false;
}
}
?>
回声:布尔值假
结论:$ mydata [$ key]不存在。
<?php
$mydata = array(
"test" => array("one", "two", "three")
);
var_dump(hasPermission("test", "two", $mydata));
function hasPermission($key, $value, $thearray) //Pass the array through parameter
{
if (isset($thearray[$key]))
{
echo "here";
return in_array($value, $thearray[$key]);
}
else
{
return false;
}
}
?>
回声:&#34;在这里&#34; |布尔值
结论:in_array()正常工作。您的脚本无法正常工作,因为hasPermission无法访问您的权限数组。
答案 1 :(得分:0)
您可以在下面编写代码来验证权限。
function hasPermission($key, $value)
{
foreach($this->permission[$key] as $k=>$v)
{
if($v == $value) return true;
}
return false
}