简体   繁体   中英

how i can do permissions in php with codeigniter?

I have builded small php script that include topics, users, comments, user profile and sessions with Codeigniter framework but i do not know now how to do permissions for users,

I mean how to make only registered people can add topics and comments and somethings like that

I try to make it by sessions

if($this->session->userdata('is_logged_in'))
{
    // here something like add topic or comment buttons 
} 

do i right ? this is only the way to do permissions ? or there's any another way better ?

You don't mention the version of CI that you use but for 2.x (CodeIgniter v3 requires the class names to be ucfirst().) I have found ion_auth to be quite good and very stylable. You can get it here. http://benedmunds.com/ion_auth/ It has some nice touches where you can restrict certain views/pages or methods by an assigned group or the main 'logged_in' method.

The script to put in your construct is as simple as

     if (!$this->ion_auth->logged_in()){
              redirect('auth/login', 'refresh');
 }

I don't think so that such thing is available in ci but you can do it by using user_type or categorize your user.

Like for admin user_type = "admin" or 1

for customer user_type = "customer" or 2

i have implemented this thing in 2 yrs ago and till now never face any problem with it and even further modification like creating another group is real easy with it.

I see you already have a table called "users" in the database, add a new column let's call it "privilege" in that table, it will contain the code for user privileges, let say 0 is for admin , 1 for customer, 2 for guest ...etc

and let say only admin and customer can post a comment, so your code would check for the user privilege and decide accordingly.

pseudo code:

if($this->session->userdata('is_logged_in'))
{
     // let say the user_id is loaded in the session
     $user_id = $this->session->userdata('user_id');

     $this->load->model('user_mdl');
     $user_priv = $this->user_mdl->get_user_priv($user_id);

     if($user_priv == 0 or $user_priv == 1)
     {
        // here something like add topic or comment buttons 
     }
    else
    {
      // sorry you don't have permission to do comment or something interesting 
    }


} 

you can also create a new table "privileges" and link it (via a foreign key) to users if you want to normalize it instead of having a column in the users table.

hope that helps cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM