Inconsistent role checking? How to do it properly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tothewine
    Active Community Member
    • Jan 2018
    • 373

    Inconsistent role checking? How to do it properly?

    I gave my user the $roleId role. I tested this code:


    PHP Code:
    $user = $this->getUser();
    $userId = $user->id;
    $userRepository = $this->getEntityManager()->getRepository('User');
    
    $roleId = '5ec00777b9d015264';
    $role = $this->getEntityManager()->getEntity('Role', $roleId);
    $roleRepository = $this->getEntityManager()->getRepository('Role');
    
    $a = count($roleRepository->findRelated($role, 'users')-> getDataList());
    $b = $roleRepository->isRelated($role, 'users', $userId);
    
    $c = count($userRepository->findRelated($user, 'roles')-> getDataList());
    $d = $userRepository->isRelated($user, 'roles', $roleId);
    
    $get = $user->get('roles');
    
    echo "get=".json_encode($get)." a=$a b=".json_encode($b)." c=$c d=".json_encode($d); 
    
    Output is this:
    Code:
    get={} a=1 b=false c=0 d=false

    Why does it print that? The only expected result is $a. What am I doing wrong? I think I'm using the correct relation names.
    Why doesn't isRelated work and findRelated does? And why does it only work from the Role repository? This looks like a bug...

    Is there a builtin function to check for roles?

    Thanks!
    Last edited by tothewine; 05-16-2020, 04:45 PM.
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Not sure if this is what are you looking for, but id it helps, this is how I just use straight sql to check for a user roles:
    PHP Code:
    $userId = {{any user id value}}
    $pdo = $this->getEntityManager()->getPDO();
    $sqlString = 'SELECT role.name FROM role_user INNER JOIN user ON role_user.user_id = user.id INNER JOIN role ON role_user.role_id = role.id WHERE user.id = "'.$userId.'"';
    $data = $pdo->query($sqlString)->fetchAll(); 
    
    Last edited by telecastg; 05-17-2020, 12:04 AM.

    Comment

    • tothewine
      Active Community Member
      • Jan 2018
      • 373

      #3
      Thanks for the alternative approach! I also figured I was getting the 'system' account instead of mine when calling $this->getUser(); for some reason. Now it's working liek this:

      Code:
      public function userCheckRole($roleId, $user=null) : bool { // return true if the specified user (or current) has role
        return $this->getEntityManager()->getRepository('User')->isRelated($user ?? $this->getUser(), 'roles', $roleId);
      }
      Last edited by tothewine; 05-22-2020, 11:09 AM.

      Comment


      • telecastg
        telecastg commented
        Editing a comment
        You're very welcome, I like your approach better, it's less complicated :-)

      • tothewine
        tothewine commented
        Editing a comment
        it looks like one of those things that should be built-in to allow fast implementation of conditional business logic by role
    Working...