php - User rights in my cms -


i developing cms hobby , got stuck on something....in mysql db have different classes of users:admins, normal users, veterans, premium etc.....is there way create php file wich contains settings each user class? , use function or check if user has right to...create page example....

for moment checking users rights sessions...

  if($_session['user_type']=='admin'||$_session['user_type']=='premium'){  //do stuff  }else if()......... {   // ..............   } 

but want that

check_user_right(user_name); if ($can_create_page) == true{  do......}else{....} 

first of all, should know should storing user information in database. then, when logs in , verify login, can store or user id in session, , other user information, user_type, query database based on id. not sure if you're doing yet, should if aren't.

as far user rights go, have 2 options.

the oop way

this 1 recommend. entails creating user class encapsulates of logic retrieving user database , subsequently checking if user has specific right.

class user {     protected static $_rights = array(         'admin'=>array(             'create_page','remove_page', etc...         )     );     public static retrieve($id) {         // call database or persistent storage retrieve user info based on id         return new static($retrieved_user_data);     }      public function has_right($right) {         return in_array($right, static::$_rights[$this->user_type]);     } } 

the non-oop way

this lower-level , may better in meantime. create array of rights per user level store in same file function use check them. function must in turn included on every page plan use it.

e.g., put function in file called functions.php, , looks this:

$user_rights = array(     'admin'=>array('create_page','remove_page','edit_user',...),     'veteran'=>array('ban_user','edit_page'),     // other rights here ); function has_right($user_id, $right) {     global $user_rights;     // retrieve information persistent storage user     // i'm assuming store in $user_info     return in_array($right, $user_rights[$user_info['user_type']]); } 

then should include file on other file want check user rights, , need user id (stored in session after log in) , user type, can database or other persistent storage.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -