0

I have a table in DB where I put all the admins and their rights. I am setting theri rights with letters A-Z and each letter has access to something else. The thing is I want to check where they have access and show links only to those areas. For now I got an idea to put all those letters in some aray with for loop and then when rendering the menu output only those items where they have access with if (in_array(letter, rights)) I think this would work but I would like to know whether there is another way

DeiForm
  • 701
  • 9
  • 31
  • 1
    I wouldn't use letters, as it is difficult to maintain "hmmm giving access to user administration... was it `M` or `Q`?" – Carlos Campderrós Jul 23 '13 at 14:44
  • The idea of this was that I would have in administration option to add admins and there would be checkboxes with the letters as values. – DeiForm Jul 23 '13 at 14:46
  • This question might also be useful to read: http://stackoverflow.com/questions/199252/what-is-the-best-way-to-manage-permissions-for-a-web-application-bitmask-or-da – rink.attendant.6 Jul 23 '13 at 14:47
  • So I would end with 3 tables: admins, permitions, and admin_permitions. To the admin_permitions I would send admin_id and permition_id. Right? – DeiForm Jul 23 '13 at 14:51
  • Sorry to be the grammar nazi here, but 'permition' is actually "permission" - correct spelling will help any other developers dealing with your code – MDEV Jul 23 '13 at 14:57
  • yea np and thanks :). But still. would it be the final solution ? – DeiForm Jul 23 '13 at 15:15

1 Answers1

1

I wrote a PHP class that I use in all my projects that require different access for different roles. I put a copy of the class out on paste bin: class_role_restrictions.php.

This class also requires another class I wrote, which can be obtained here: better_mysqli.php

The initial setup involves the creation of some database tables (The SQL create statements are found in comments at end of the class_role_restrictions.php file) and adding roles / user memberships via it's admin interface.

Detailed setup/usage can be obtained here: role_restrictions_detailed_example.php

Once it is setup you can use it like this:

<?php

  include_once('class_role_restrictions.php');
  include_once('better_mysqli.php');


  $mysqli = new better_mysqli('your_server', 'your_user', 'your_pass', 'your_db_name');
  if (mysqli_connect_errno()) {
     error_log(sprintf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()));
     exit;
  }


  $role = new role_restrictions($mysqli, 'http://your_server.com/path/to/this/page/example.php');



  // == Restrict an entire page to authorized users only ==
    $role->restrict_to('role_name, to_restrict user_to', 'username_currently_logged_in');
  // .. now do stuff that only users in any of the roles listed are allowed to do

  // -- OR --

  // == Only do certain things if the username given is a member of any of the roles given ==
  if( $role->has_role('a_list, of_roles, allowed', 'username_currently_logged_in') ){
      // .. do stuff that only users in the given role(s) are allowed to do ..
  }


?>
Drew
  • 4,215
  • 3
  • 26
  • 40