2017-06-11 33 views
0

ので、ここでのコード:明らかにキー値を設定し、定義されていますが、まだNULLとして定義され

// Check to see if user has admin permissions 
if($user->hasPermission('admin')) { 
    // Display Admin Backpage Link 
    echo "<li><a href='" . Config::get('links/app_root') . "admin/'>Admin</a></li>"; 
} 

私はhasPermission()メソッドを呼んでいるとき、私は、ユーザーが持っているかどうかを確認したいことを指定しています許可管理者。

ここに私のhasPermission方法です:私はこれを呼び出した後

// User Has Permission Check 
public function hasPermission($key) { 
    // Pull from the groups table, where the id (group) equals the assigned group of the user 
    // id: 1 for standard users (No permissions/Json stored) 
    // id: 2 for Moderators (Json: {"moderator":1} 
    // id: 3 for admins (Json: {"admin":1,"moderator":1} 
    $groups = $this->_db->get('groups', array('id', '=', $this->data()->groups)); 

    // Check if user is in a group or not 
    if($groups->count()) { 
     $permissions = json_decode($groups->first()->permissions, true); 
     if($permissions[$key] == true) { 
      return true; 
     } 
    } 

    // user does not have permission 
    return false; 
} 

、私はこのエラーを取得しています:

Notice: Undefined index: admin in /Applications/XAMPP/xamppfiles/htdocs/OOP Login System/core/classes/User.php on line 127

ライン127はこのラインである:

if($permissions[$key] == true) { 

私は」なぜadminがnullであるのか分からない。管理者のための私のデータベース権限は私のMySQLデータベース内で次のように設定されています

{ 
    "admin": 1, 
    "moderator": 1 
} 

いただきました本当にクレイジーなことは、これはすべてうまく働いていた、と私はそれを作ったので、私は全くUserクラスを編集していないです。

{ 
    "moderator": 1 
} 

とするたびに、私はこのエラーを取得しないhasPermission('moderator')を呼び出す:今私はに「モデレータの許可だけでなくJSONセットにしています。私が前に言ったように、何が悪くなったのか分からない。 、同様のエラーを軽減しようとするのcatchブロックにカプセル化することをお勧めかもしれません

if(!empty($permissions[$key])) { 
+0

(!空($パーミッション[$キー]))'の代わりに、これは、まったく設定されていないアクセス権を処理するためです。 –

+0

それは働いたように見える、うわー、私はばかみたいに感じる。しかし、私はまだそれが最初の場所で壊れた理由を理解していない。 :/真剣に... –

+0

'$ this-> data() - > groups'の値は何ですか?あなたはそれが3であると思われるようですが、それを確認すると良いでしょう。または、1または2の場合は、 'admin'キーが存在すると仮定しないでください。 – trincot

答えて

0

はに

if($permissions[$key] == true) { 

からライン127を交換しました。また、使用したコメントごとに検討し

try { 
    $error = 'Always throw this error'; 
    throw new Exception($error); 

    // Code following an exception is not executed. 
    echo 'Never executed'; 

} catch (Exception $e) { 
    echo 'Caught exception: ', $e->getMessage(), "\n"; 
} 

場合(!空($パーミッション[$キー])) `であれば使用することを検討してください

0

関連する問題