meeting-room-booking-system / mrbs-code

MRBS application code
Other
124 stars 61 forks source link

different area different admin #3146

Open jberanek opened 2 years ago

jberanek commented 2 years ago

I have 3 areas in the system can I have different admins per area.

Reported by: *anonymous

Original Ticket: mrbs/support-requests/2461

jberanek commented 2 years ago

Only if you modify the MRBS code. If you are running MRBS 1.9.4 you need to modify the function is_book_admin() in mrbs_auth.inc and replace line 436 which is

  return (isset($mrbs_user) && ($mrbs_user->level >= 2));

by something like

  if (!isset($mrbs_user))
  {
    return false;
  }

  if ($mrbs_user->level >= 2)
  {
    return true;
  }

  $area = get_area($rooms)

  switch ($area)
  {
    case 2:
      if (in_arrayi($mrbs_user->username), array('tom', 'dick', 'harry'))
      {
        return true;
      }
      break;
    case 4:
      if (in_arrayi($mrbs_user->username), array('alice', 'bob'))
      {
        return true;
      }
      break;
    // etc for more areas
    default:
      return false;
      break;
  }

Original comment by: campbell-m

jberanek commented 2 years ago

I tried it but not working sir. Pls can you put the whole function after fixing it? Thanks in advance

Original comment by: *anonymous

jberanek commented 2 years ago

Im using version no. 2

Original comment by: *anonymous

jberanek commented 2 years ago
function is_book_admin($rooms=null, $all=true)
{
  if (is_array($rooms) && (count($rooms) > 0))
  {
    if ($all)
    {
      // We want the user to be a booking admin for all the rooms,
      // so if for any one room they are not, then return false.
      foreach ($rooms as $room)
      {
        if (!is_book_admin($room))
        {
          return false;
        }
      }
      return true;
    }
    else
    {
      // We want the user to be a booking admin for at least one room,
      // so if there are no rooms for which they are, then return false.
      foreach ($rooms as $room)
      {
        if (is_book_admin($room))
        {
          return true;
        }
      }
      return false;
    }
  }

  if (is_null($rooms) && !$all)
  {
    // Not yet supported.  Could support it but need to decide what $rooms=null means.
    // Does it mean all rooms in the system or just all rooms in the current area?
    throw new \Exception('$rooms===null and $all===false not yet supported.');
  }

  $mrbs_user = session()->getCurrentUser();

  if (!isset($mrbs_user))
  {
    return false;
  }

  if ($mrbs_user->level >= 2)
  {
    return true;
  }

  $area = get_area($rooms)

  switch ($area)
  {
    case 2:
      if (in_arrayi($mrbs_user->username), array('tom', 'dick', 'harry'))
      {
        return true;
      }
      break;
    case 4:
      if (in_arrayi($mrbs_user->username), array('alice', 'bob'))
      {
        return true;
      }
      break;
    // etc for more areas
    default:
      return false;
      break;
  }
}

Original comment by: campbell-m

jberanek commented 2 years ago

still there is an error

Original comment by: *anonymous

Attachments: https://sourceforge.net/p/mrbs/support-requests/_discuss/thread/0146fe3d6b/713a/attachment/error.png

jberanek commented 2 years ago

What is the error? You need to change the area ids and usernames to suit your requirements.

Original comment by: campbell-m

jberanek commented 2 years ago

where should I change the area Id. For example my area id is "4". can you change it in the script please

Original comment by: *anonymous

jberanek commented 2 years ago

No, you will need to change it yourself. The code above is just an example that makes usernames 'tom', 'dick' and 'harry' admins for area 2, and 'alice' and 'bob' admins for area 4.

Original comment by: campbell-m

jberanek commented 2 years ago

I did exactly like this as you mentioned . For area 1 and area 3 but code error

  $area = get_area($rooms)

  switch ($area)
  {
    case 1:
      if (in_arrayi($mrbs_user->username), array('ranjith', 'majed', 'chunky'))
      {
        return true;
      }
      break;
    case 3:
      if (in_arrayi($mrbs_user->username), array('ranjith', 'maryam'))
      {
        return true;
      }
      break;
    // etc for more areas
    default:
      return false;
      break;
  }
}

Original comment by: *anonymous

jberanek commented 2 years ago

And what is the error?

Original comment by: campbell-m

jberanek commented 2 years ago

in my Dreamweaver see the picture , Once I save it page not working

Original comment by: *anonymous

Attachments: https://sourceforge.net/p/mrbs/support-requests/_discuss/thread/0146fe3d6b/cfed/attachment/error2.png

jberanek commented 2 years ago

Sorry, there's a semicolon missing:

$area = get_area($rooms);

Original comment by: campbell-m

jberanek commented 2 years ago

still line 444 and 450 error showing red color and page not showing sir.

Original comment by: *anonymous

jberanek commented 2 years ago

Sorry, it should be

if (in_arrayi($mrbs_user->username, array('ranjith', 'majed', 'chunky')))

and similarly for the other line.

Original comment by: campbell-m

jberanek commented 2 years ago

ts working now thanks a lot.

Original comment by: *anonymous

jberanek commented 2 years ago

But still other room admin can admin all the areas. not restricted to one area

Original comment by: *anonymous

jberanek commented 2 years ago

What do you mean by "other room admin"?

Original comment by: campbell-m

jberanek commented 2 years ago

I need separate admin for separate areas. once I done this for example area 1 admin can admin area 3 also. I want area 1 admin only for that area not for area 3. Now its like having 2 admin.

Original comment by: *anonymous

jberanek commented 2 years ago

Which user are you talking about? If it's ranjith then, yes, they will be admins for both areas as they are in both arrays in the code above.

Original comment by: campbell-m

jberanek commented 2 years ago

in the example mariam should not be able to admin area 1 but ranjith should be admin for area 1 and area 3

Original comment by: *anonymous

jberanek commented 2 years ago

Can you post your version of the code here please?

Original comment by: campbell-m

jberanek commented 2 years ago
function is_book_admin($rooms=null, $all=true)
{
  if (is_array($rooms) && (count($rooms) > 0))
  {
    if ($all)
    {
      // We want the user to be a booking admin for all the rooms,
      // so if for any one room they are not, then return false.
      foreach ($rooms as $room)
      {
        if (!is_book_admin($room))
        {
          return false;
        }
      }
      return true;
    }
    else
    {
      // We want the user to be a booking admin for at least one room,
      // so if there are no rooms for which they are, then return false.
      foreach ($rooms as $room)
      {
        if (is_book_admin($room))
        {
          return true;
        }
      }
      return false;
    }
  }

  if (is_null($rooms) && !$all)
  {
    // Not yet supported.  Could support it but need to decide what $rooms=null means.
    // Does it mean all rooms in the system or just all rooms in the current area?
    throw new \Exception('$rooms===null and $all===false not yet supported.');
  }

  $mrbs_user = session()->getCurrentUser();

  if (!isset($mrbs_user))
  {
    return false;
  }

  if ($mrbs_user->level >= 2)
  {
    return true;
  }

  $area = get_area($rooms);

  switch ($area)
  {
    case 1:
      if (in_arrayi($mrbs_user->username, array('ranjith')))
      {
        return true;
      }
      break;
    case 3:
      if (in_arrayi($mrbs_user->username, array('ranjith', 'maryam')))
      {
        return true;
      }
      break;
    // etc for more areas
    default:
      return false;
      break;
  }
}

Original comment by: *anonymous

jberanek commented 2 years ago

Is maryam also a normal admin?

Original comment by: campbell-m

jberanek commented 2 years ago

No . But I tried maryam as an admin and normal user. still cannot get it work

Original comment by: *anonymous

jberanek commented 2 years ago

And are you definitely sure that the room that maryam is able to be an admin for is in area 1?

Original comment by: campbell-m

jberanek commented 2 years ago

yes sir. I checked again and again still maryam can admin area 1. should I make maryam normal user or admin user?

Original comment by: *anonymous

jberanek commented 2 years ago

Yes sir. I checked again and again changing the user to admin and normal user. When maryam is admin she can admin both and when normal user cannot admin any.

Original comment by: *anonymous

jberanek commented 2 years ago

You should make maryam an ordinary user if you don't want her to have admin rights in all areas. If she is an ordinary user she should have admin rights for rooms in area 3.

Original comment by: campbell-m

jberanek commented 2 years ago

no sir I tried that way once I make her ordinary user she cannot admin any area.

Original comment by: *anonymous

jberanek commented 2 years ago

You'll need to start debugging the code. I've inserted some debug output below which should appear in the browser. Let me know what appears when maryam tries to delete somebody else's booking in area 1.

function is_book_admin($rooms=null, $all=true)
{
  if (is_array($rooms) && (count($rooms) > 0))
  {
    if ($all)
    {
      // We want the user to be a booking admin for all the rooms,
      // so if for any one room they are not, then return false.
      foreach ($rooms as $room)
      {
        if (!is_book_admin($room))
        {
          return false;
        }
      }
      return true;
    }
    else
    {
      // We want the user to be a booking admin for at least one room,
      // so if there are no rooms for which they are, then return false.
      foreach ($rooms as $room)
      {
        if (is_book_admin($room))
        {
          return true;
        }
      }
      return false;
    }
  }

  if (is_null($rooms) && !$all)
  {
    // Not yet supported.  Could support it but need to decide what $rooms=null means.
    // Does it mean all rooms in the system or just all rooms in the current area?
    throw new \Exception('$rooms===null and $all===false not yet supported.');
  }

  $mrbs_user = session()->getCurrentUser();

  if (!isset($mrbs_user))
  {
    return false;
  }

  if ($mrbs_user->level >= 2)
  {
    return true;
  }

  $area = get_area($rooms);

  var_dump($area);  //DEBUG
  var_dump($mrbs_user->username); // DEBUG

  switch ($area)
  {
    case 1:
      if (in_arrayi($mrbs_user->username, array('ranjith')))
      {
        return true;
      }
      break;
    case 3:
      if (in_arrayi($mrbs_user->username, array('ranjith', 'maryam')))
      {
        return true;
      }
      break;
    // etc for more areas
    default:
      return false;
      break;
  }
}

Original comment by: campbell-m

jberanek commented 2 years ago

HI Campbell

Im just trying to get this working at our site so only certain users can administer certain areas. in the code you have posted how do you identify which room you want to be selected i can see it says case 1 and case 3 but how do they correlate the the actual rooms?

Any help would be appreciated.

Regards Scott

Original comment by: scottlangshaw

jberanek commented 2 years ago

The 1 and 3 are examples and are area ids.

Original comment by: campbell-m

jberanek commented 2 years ago

thanks for the quick response. Where would we find the area ids on the system?

Original comment by: scottlangshaw

jberanek commented 2 years ago

Either in the area table in the database, or else just by hovering over a link to a room and looking at the area id (index.php?area=7&room=2 etc).

Original comment by: campbell-m

jberanek commented 2 years ago

amazing thank you ill give it a test tomorrow and if i get stuck ill stick another message on here.

Thanks

Original comment by: scottlangshaw

jberanek commented 2 years ago

Hi Campbell

I may be doing something wrong but ive found the room numbers and entered it into the code below but im not sure if its correct as i still cant edit the bookings in that room?

function is_book_admin($rooms=null, $all=true)
{
  if (is_array($rooms) && (count($rooms) > 0))
  {
    if ($all)
    {
      // We want the user to be a booking admin for all the rooms,
      // so if for any one room they are not, then return false.
      foreach ($rooms as $room)
      {
        if (!is_book_admin($room))
        {
          return false;
        }
      }
      return true;
    }
    else
    {
      // We want the user to be a booking admin for at least one room,
      // so if there are no rooms for which they are, then return false.
      foreach ($rooms as $room)
      {
        if (is_book_admin($room))
        {
          return true;
        }
      }
      return false;
    }
  }

  if (is_null($rooms) && !$all)
  {
    // Not yet supported.  Could support it but need to decide what $rooms=null means.
    // Does it mean all rooms in the system or just all rooms in the current area?
    throw new \Exception('$rooms===null and $all===false not yet supported.');
  }

  $mrbs_user = session()->getCurrentUser();

  if (!isset($mrbs_user))
  {
    return false;
  }

  if ($mrbs_user->level >= 2)
  {
    return true;
  }

  $area = get_area($rooms);

  switch ($area)
  {
    case 1:
      if (in_arrayi($mrbs_user->username, array('ranjith')))
      {
        return true;
      }
    case 39:
      if (in_arrayi($mrbs_user->username, array('teststaff')))
      {
        return true;
      }
      break;

    // etc for more areas
    default:
      return false;
      break;
  }
}

Original comment by: scottlangshaw

jberanek commented 2 years ago

Ive sorted it :) i put the room id and not the area id.

One question are you able to allow users to edit certain rooms within an area or does this just work with areas?

Thanks Scott

Original comment by: scottlangshaw

jberanek commented 2 years ago

Yes, you can do just rooms, but the code would be slightly different. Do you want certain rooms, or areas or both?

Original comment by: campbell-m

jberanek commented 2 years ago

If we can do both within the code that would be amazing.

Original comment by: scottlangshaw

jberanek commented 2 years ago

Replace

  switch ($area)
  {
    case 1:
      if (in_arrayi($mrbs_user->username, array('ranjith')))
      {
        return true;
      }
    case 39:
      if (in_arrayi($mrbs_user->username, array('teststaff')))
      {
        return true;
      }
      break;

    // etc for more areas
    default:
      return false;
      break;
  }

by

  // First check areas
  switch ($area)
  {
    case 1:
      if (in_arrayi($mrbs_user->username, array('ranjith')))
      {
        return true;
      }
    case 39:
      if (in_arrayi($mrbs_user->username, array('teststaff')))
      {
        return true;
      }
      break;

    // etc for more areas
    default:
      break;
  }

  // Now check rooms
  switch ($rooms)
  {
    case 11:  // replace 11 etc by the relevant room id
      if (in_arrayi($mrbs_user->username, array('tom')))
      {
        return true;
      }
      break;
    case 32:
      if (in_arrayi($mrbs_user->username, array('dick', 'harry')))
      {
        return true;
      }
      break;
    // etc for more rooms
    default:
      return false;
      break;
  }

Original comment by: campbell-m

jberanek commented 2 years ago

Amazing Campbell, Thank you so much it works an absolute treat :).

Original comment by: *anonymous

jberanek commented 1 year ago

Hello! How can i do this in version 1.10.0 ?

Thanks!

Original comment by: *anonymous

jberanek commented 1 year ago

I think it should be the same code that's needed in MRBS 1.10.0.

Original comment by: campbell-m