1

I have probably made a very simple mistake with this, but I do not see how.. If any of you could help me, I would appreciate it lots.

This is my error:
Notice: Undefined index: admin in C:\xampp\htdocs\forums\classes\User.php on line 75

Here is User.php

public function hasPermission($key) {
    $group = $this->_db->get('groups', array('id', '=', $this->data()->group));

    if ($group->count()) {
        $permissions = json_decode($group->first()->permissions, true);

        if ($permissions[$key] == true) { <<< This is line 75 <<<
            return true;
        }
    }
    return false;
}

Here is where I use hasPermission()

if($user->hasPermission("admin")){
    echo "You are an administrator";
}

var_dump($key);:

string(5) "admin"

var_dump($permissions);

array(1) { ["admin"]=> int(1) }

var_dump($permissions[$key]); outputs:

NULL
Amal Murali
  • 73,160
  • 18
  • 123
  • 143
Kondax Design
  • 175
  • 1
  • 1
  • 15

1 Answers1

1

Try to use this one:

if (isset($permissions[$key]) && $permissions[$key] == 1) {
   return true;
} else {
   return false;
}
Code Lღver
  • 15,434
  • 16
  • 54
  • 74