user4419336
user4419336

Reputation:

in_array not working codeigniter

I cannot get my in_array to work on my library both functions are on same library. I will not let me do

$this->user_auth->hasPermission('modify', 'folder/controller-name');

By using the function above I can check if has permission to modify.

On my library function hasPermission the $key & $value does not work.

On my login function I unserialize the permissions

I cannot figure out why that the hasPermission always returns false.

Login

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;
    }
}

Has Permission

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" 

} 

} 

Update this is how I use the 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.
}

Thanks in advance

Upvotes: 1

Views: 1198

Answers (2)

AdrienXL
AdrienXL

Reputation: 3008

I've done some test on a simple php file :

<?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;
    }
}

?>

Echo : boolean false

Conclusion : $mydata[$key] does not exists.

<?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;
    }
}

?>

Echo : "here" | boolean true

Conclusion : in_array() works correctly. Your script doesn't work because hasPermission can't access to your permission array.

Upvotes: 2

Ganesh Kanawade
Ganesh Kanawade

Reputation: 381

You can write below code to validate the permission.

function hasPermission($key, $value)
{
    foreach($this->permission[$key] as $k=>$v)
    {
        if($v == $value) return true;
    }
    return false
}

Upvotes: 0

Related Questions