Welcome Guest, Not a member yet? Register   Sign In
user permissions as per page basis
#1

i have 3 type of users namely assistant,officer,General manager.How i set permissions  to each roles to them(certain pages need permission to only managers) thankz in advance?/
Reply
#2

1) Create a restrict() method in your auth system and then check it in the controllers constructor.

Your Auth System:

PHP Code:
    // -------------------------------------------------------------------

    /**
     * restrict ()
     * -------------------------------------------------------------------
     *
     * Original Code by Adam Griffiths Auth 1.0.6
     * @author Adam Griffiths
     *         
     * Restricts access to a controller and page
     *
     * Takes a user's level (e.g. admin, user etc) and restricts access
     * to that user and above.
     *
     * Example:
     *
     * users can access a profile page, but so can admins
     * (who are above users)
     *
     * @param  null $group
     * @param  null $single
     * @return bool
     */
    
public function restrict($group null$single null)
    {
        if (
$group === null)
        {
            if (
$this->loggedIn() == true)
            {
                return 
true;
            }
            else
            {
                
show_error($this->CI->lang->line('insufficient_privs'));
            }
        }
        elseif (
$this->loggedIn() == true)
        {
            
$level $this->config['auth_groups'][$group];

            
$userLevel $this->CI->session->userdata('groupId');

            if (
$userLevel $level OR $single == true && $userLevel !== $level)
            {
                
show_error($this->CI->lang->line('insufficient_privs'));
            }

            return 
true;
        }
        else
        {
            
redirect($this->config['auth_login'], 'refresh');
        }
    } 

Change and modify the above to suit your needs.

2) In your controllers construct add the below.

Controller constructor:

PHP Code:
    public function __construct()
    {
        
parent::__construct();

 
               // restrict this controller to admins only
        
$this->auth->restrict('admin');
    } 

Hope that helps.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

error line

Code:
if ($userLevel > $level OR $single == true && $userLevel !== $level)
Reply
#4

(05-09-2018, 11:34 PM)renoldtrueman Wrote: i have 3 type of users namely assistant,officer,General manager.How i set permissions  to each roles to them(certain pages need permission to only managers) thankz in advance?/
Before i answer, do you use your own authentication or do you use an authentication library like Community Auth, IonAuth, etc?
Reply
#5

Sorry I forgot to add the config array for Groups.

You will need to add this to a config file and load it to use the Groups.

PHP Code:
/**
 * The array which holds your user groups and their ID.
 * If you have a database table for groups, these ID's must be the same as in the database.
 */
$config['auth_groups'] = array(
 
   'admin'  => '1',
 
   'editor' => '2',
 
   'user'   => '100'
); 

Hope that helps.

The $level is stored in the users table that matches the number of the group array.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB