0

Good evening,

I found myself in a bit of a pickle here, with an overcomplicated (i think) $_SESSION array that is set right after a user logs in and contains the info of all User Groups where that user is on, and also this user type of permissions on Group of Devices where that specific user group is allowed.

Here's the deal with irrelevant info ommited:

Array
(

... other stuff ...

[user_groups] => Array
    (
        [0] => Array
            (
                [GroupUsersId] => 4
                [GroupUsersName] => XXXX
                [idUserType] => 2
                [NameTypeUser] => Manager
                [DevicesAllowed] => Array
                    (
                        [GroupDevicesId] => Array
                            (
                                [0] => 2
                            )

                        [DevicesOnGroup] => Array
                            (
                                [0] => 22,24,16
                            )

                    )

            )

        [1] => Array
            (
                [GroupUsersId] => 5
                [GroupUsersName] => YYYY
                [idUserType] => 3
                [NameTypeUser] => Guest
                [DevicesAllowed] => Array
                    (
                    )

            )

        [2] => Array
            (
                [GroupUsersId] => 1
                [GroupUsersName] => ZZZ
                [idUserType] => 1
                [NameTypeUser] => Admin
                [DevicesAllowed] => Array
                    (
                        [GroupDevicesId] => Array
                            (
                                [0] => 2
                            )

                        [DevicesOnGroup] => Array
                            (
                                [0] => 1,5,13,12,17,21,22,24,16
                            )

                    )

            )

    )

... more stuff ...

I need to find out what kind of permissions, if any, does the guy has if trying to browse the device with, let's say, DeviceId = 5. If that particular Id is not on any of the arrays, the user isn't even allowed to see it...

I already tryed to change the code in this question How to search by key=>value in a multidimensional array in PHP, but I guess I'm missing some kind of iteration over the arrays.

Any help?

Cheers and thanks in advance.

Edit: $_SESSION can be changed if needed...

Community
  • 1
  • 1
Afonso Gomes
  • 902
  • 1
  • 14
  • 40

2 Answers2

2

(Updated as per comment below) I might be completely missing your point, but would not just iterative processing of your array help?

$user_groups = array(
  0 => array(
    'GroupUsersName' => 'XXX',
    'NameTypeUser' => 'Admin',
    'idUserType' => 3,
    'DevicesAllowed' => array(
      'DevicesOnGroup' => array(
        1, 2, 3
      )
    )
  ),
  1 => array(
    'GroupUsersName' => 'YYY',
    'NameTypeUser' => 'ReadOnly',
    'idUserType' => 1,
    'DevicesAllowed' => array(
      'DevicesOnGroup' => array(
        3, 4, 5
      )
    )
  )
);

$device = 3;
$right = 0;
foreach ($user_groups as $group) {
  if (array_key_exists('DevicesOnGroup', $group['DevicesAllowed'])) {
    if (in_array($device, $group['DevicesAllowed']['DevicesOnGroup'])) {
      if ($group['idUserType'] > $right) {
        $right = $group['idUserType'];
      }
    }
  }
}

print_r($right);

Outputs:

3

If you would ask for device which is in no group, it would return 0 (i.e. no access).

jhutar
  • 1,369
  • 2
  • 17
  • 32
  • You're not missing the point at all friend ... that's almost exactly what I want to achieve. But I don't need an array to the $rights variable. As some group of devices might overlap and a user might have access to the same device 2 or 3 times (you do that in your example)... now I just need to check what's the lowest "idUserType" (1 = Admin; 2 = Manager; 3 = Guest; Not set = can't even see that device exists...) to give permissions acordingly. Don't know if I'm making myself clear... – Afonso Gomes Nov 21 '14 at 23:25
  • Hello. Great. I have edited a code above to (hopefully) give you what you want. – jhutar Nov 22 '14 at 17:07
  • Yup! I worked your code and got the same thing. That's exactly it! thanks. – Afonso Gomes Nov 23 '14 at 18:36
1

iterate the array like this

$guysDeviceId ; 
$bGuyMayPass = false;
foreach($_SESSION["user_group"] as $userGroup ){
  if(!isset($userGroup[DevicesAllowed]) || !isset($userGroup[DevicesAllowed][DevicesOnGroup])){
   continue;
  }
  if(in_array($userGroup[DevicesAllowed][DevicesOnGroup], $guysDeviceId ){
   $bGuyMayPass= true;
  }
}

if($bGuyMayPass){
//login, whatever
}
Alexis Peters
  • 1,583
  • 1
  • 10
  • 17