-1

I'm building a CodeIgniter application with SQL server.

I' reading user permission from the database and then store it so I can show sidebar menu according to the user permission but I'm getting this error :

in_array() expects parameter 2 to be array, string given

here's my controller code:

    public function __construct() 
{
    parent::__construct();

    $group_data = array();
    if(empty($this->session->userdata('logged_in'))) {
        $session_data = array('logged_in' => FALSE);
        $this->session->set_userdata($session_data);
    }
    else {
        $user_id = $this->session->userdata('id');
        $this->load->model('model_groups');
        $group_data = $this->model_groups->getUserGroupByUserId($user_id);



        preg_replace_callback('!s:(\d+):"(.*?)";!', 
            function($match) {
             return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';},
             $group_data['permission'];);



        $this->data['user_permission'] = $group_data['permission'];
        var_dump($this->data['user_permission']);
        $this->permission = $group_data['permission'] ;

    }
}

When I try to show what's stored in my data var_dump($this->data['user_permission']); I get : enter image description here

and I'm trying to use them in my view like this:

  <?php if(in_array('createUser', $this->data['user_permission']) || in_array('updateUser', $this->data['user_permission']) || in_array('viewUser', $this->data['user_permission']) || in_array('deleteUser', $this->data['user_permission'])): ?>
        <li class="treeview" id="userSideTree">
        <a href="#">
          <i class="fa fa-users"></i>
          <span>Utilisateurs</span>
          <span class="pull-right-container">
            <i class="fa fa-angle-left pull-right"></i>
          </span>
        </a>

Can you help me with this?

M_M
  • 491
  • 2
  • 10
  • 22
  • 1
    `$this->data['user_permission']` is a serialized string. You'll have to unserialize it before you can use it as an array. – aynber Jun 27 '19 at 15:13
  • `$this->data['user_permission'] = unserialize($this->data['user_permission']);` ? – nice_dev Jun 27 '19 at 15:13
  • 1
    @vivek_23 When I do that I get this error: Message: unserialize(): Error at offset 10 of 729 bytes – M_M Jun 27 '19 at 15:15
  • Hi, it looks like your $this->data['user_permission'] is a serialized string. Try to unserialize it: `unserialize($this->data['user_permission'])` – Auris Jun 27 '19 at 15:16
  • @M_M Then it's most likely an invalid serialized string. – nice_dev Jun 27 '19 at 15:17

1 Answers1

2

You have a serialized string and the quotes are escaped. Strip the slashes and unserialize:

$this->data['user_permission'] = unserialize(stripslashes($group_data['permission']));

You didn't show the entire string so there could be other problems with it. Text instead of image would help.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87