jeudi 30 mai 2019

Method hasRole of Zizaco/entrust always returns false

In my lasravel 5.7 app using https://github.com/Zizaco/entrust I have problem using hasRole method, as it always returns false, even in db I have valid rows. Calling in control app/Http/Controllers/PersonalController.php wrapped method:

public function get($id)
{
    $user            = User::find($id);
    $check_roles_as_text= $user->checkRolesAsText();
...

app/User.php :

<?php

namespace App;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Auth\Passwords\CanResetPassword;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Authenticatable implements JWTSubject
{
    use EntrustUserTrait;

    use Notifiable;
    protected $primaryKey = 'id';

   public function checkRolesAsText() {
        $arr= [];
        echo '<pre>$this->id::'.print_r($this->id,true).'</pre>'; // that shows 5 - my logged user
        // dd($this); // If uncomment out put below
        if ( $this->hasRole(ACCESS_ROLE_ADMIN) ) {
            $arr[]= ACCESS_ROLE_ADMIN_LABEL;
            echo '-1';
        }
        if ( $this->hasRole(ACCESS_ROLE_MANAGER) ) {
            $arr[]= ACCESS_ROLE_MANAGER_LABEL;
            echo '-2';
        }
        if ( $this->hasRole(ACCESS_ROLE_CUSTOMER) ) {
            $arr[]= ACCESS_ROLE_CUSTOMER_LABEL;
            echo '-3';
        }
        echo '<pre>$arr::'.print_r($arr,true).'</pre>';
        return $this->concatArray($arr);
    }
}

Checking slq tracement I see that user_id is null :

   SELECT `ad_roles`.*, `ad_role_user`.`user_id`     AS `pivot_user_id`, `ad_role_user`.`role_id`     AS `pivot_role_id` 
    FROM `ad_roles` 
    INNER JOIN `ad_role_user` on `ad_roles`.`id` = `ad_role_user`.`role_id` 
    WHERE `ad_role_user`.`user_id` is null 

Output of dd($this):

User {#368 ▼
  #primaryKey: "id"
  #connection: "mysql"
  #table: "users"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:19 [▼
    "id" => 5
    "username" => "Admin"
    "email" => "admin@mail.com"
    "password" => "$2y$10$vxBkNOEteK5GwP/j1VEDiutmR92XVW1ixSEEQ5ZMq6WPY4RkAO97S"
    "remember_token" => null
    "status" => "A"
    "account_type" => "I"
    "verified" => 1
    "verification_token" => null
    "first_name" => "Red"
    "last_name" => "Song"
    "phone" => "247-541-7172"
    "website" => "red_song@make_vote.site.com"
    "notes" => null
    "creator_id" => null
    "activated_at" => null
    "avatar" => "5.jpeg"
    "created_at" => "2018-03-25 12:39:36"
    "updated_at" => "2018-05-20 19:48:03"
  ]
  #original: array:19 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
  #rememberTokenName: "remember_token"

I do not see why in sql user_id is null and how to fix it?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire