Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once('class_role_restrictions.php'); // can be obtained from: http://pastebin.com/VubBkakb
- include_once('better_mysqli.php'); // can be obtained from: http://pastebin.com/ATyzLUfK
- // == open a connection to the database that contains the required role tables ==
- /* Notes:
- - The class_role_restrictions.php class requires an open mysqli database object that was opened with
- the better_mysqli class.
- - However, you can use the $mysqli object in the same way that you are used to because the better_mysqli
- class extends the mysqli class and uses the same constructor.
- - Refer to this post if you are interested in using the features that the better_mysqli class offers
- in your own projects: *** URL HERE
- */
- $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;
- }
- // == instantiate the role object ==
- // Pass in the better_mysqli database object.
- // pass in the URL to this page (or the page you want to return to when leaving the role admin page)
- $role = new role_restrictions($mysqli, 'http://your_server.com/path/to/this/page/example.php');
- // == Accessors to change any of the default options ==
- // Note: I am just setting what are already the defaults so you don't actually need to keep these unless you want to change something
- $role->set_debug(false); // set to true if you would like a verbose output of what this thing is doing
- $role->set_dbprefix(''); // If you created the required role tables with a common prefix then specify it here. e.g tables: project_1_role_types and project_1_role_assignments then the prefix would be 'project_1_'
- $role->set_display_title('Role Admin Page'); // The title displayed in the browser address bar and the top of the admin page.
- $role->set_admin_functions(array('types','assignments')); // set which features to display in the role admin page. types == add/remove role names and description, assignments == add users to and remove users from existing roles
- // == Access the admin interface for the first time! ==
- /* Notes:
- -- Browsing to this page will always load the role admin interface until you remove: $role->draw-admin_page();
- -- Takes you straight into the roles admin interface so that you can add the initial roles and assign users to those roles.
- -- You should create a 'role_admin' user and assign yourself to that role, the examples further below assume this is what you have done.
- **IMPORTANT**
- Delete this once you have created the intial roles.
- */
- $role->draw_admin_page();
- // == Access the admin interface after it is already setup ==
- // IMPORTANT:
- // You should take steps to secure the entire page from unauthorized access when using the 'process_any_commands' method as anyone who
- // knows how the class works could access the admin role interface with simple modifications to their query string!!!!
- $role->restrict_to('role_name', 'username_currently_logged_in'); // stops script execution here if username given is not a member of role name given !!!!
- $role->process_any_commands(); // tell the class to look for specific post key/values
- // create a link that will load the role admin interface:
- ?>
- <a href="url_to_this_page.php?doCmd=draw_role_restrictions_admin_page">Role Admin Interface</a>
- <?php
- // == Restrict an entire page to authorized users only ==
- /* Notes:
- -- If the username given is not a member of the role(s) given then
- script execution is stopped and a message is displayed.
- -- The first parameter is a string whose value is the role name or names
- of roles that the username given must be a member of. The delimiter used
- to separate each of the allowed roles can be a space and/or comma (see example below)
- */
- $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
- // == 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 ..
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement