Search code examples
phparrayscodeigniteractiverecord

CodeIgniter query to get user's highest permission level


I am creating a helper function to get name field value from user group array. So whole idea is to pass value in function parameter like system_admin and than will pass in switch to get content according to the user group defined.

Till now I have done as below. I'm sure done is crappy way but no idea how to make it work.

Function

function admin_heading($groupname)
{
    $CI =& get_instance();        
    $groups = $CI->ion_auth->groups()->result();

    foreach ($groups as $key => $value)
    {
        foreach ($value as $val => $result){
            echo $result . '<br/>';
        }

    }

    //return $groups;
}

If I return $group than getting this Array

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => system_admin
            [description] => System Administrator
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => admin
            [description] => Administrator
        )

    [2] => stdClass Object
        (
            [id] => 3
            [name] => HR
            [description] => Human Resources
        )

    [3] => stdClass Object
        (
            [id] => 4
            [name] => REC
            [description] => Recruitment
        )

    [4] => stdClass Object
        (
            [id] => 5
            [name] => TRA
            [description] => Training
        )

    [5] => stdClass Object
        (
            [id] => 6
            [name] => AMs
            [description] => Account Managers
        )

    [6] => stdClass Object
        (
            [id] => 7
            [name] => TLs
            [description] => Team Leaders
        )

    [7] => stdClass Object
        (
            [id] => 8
            [name] => FIN
            [description] => Finance
        )

    [8] => stdClass Object
        (
            [id] => 9
            [name] => FAC
            [description] => Facilities
        )

    [9] => stdClass Object
        (
            [id] => 10
            [name] => AHT
            [description] => After Hours Transportation
        )

    [10] => stdClass Object
        (
            [id] => 11
            [name] => member
            [description] => Member
        )

)

By echoing $result getting below result

1
system_admin
System Administrator
2
admin
Administrator
3
HR
Human Resources
4
REC
Recruitment
5
TRA
Training
6
AMs
Account Managers
7
TLs
Team Leaders
8
FIN
Finance
9
FAC
Facilities
10
AHT
After Hours Transportation
11
member
Member
1

So the final result I want is for instance

When I pass system_admin in function admin_heading('system_admin') that it will check if system_admin group exists in db. If exists than it will pass in switch($var) and I will have file with the group name which will be called base on it.

Now this type of conditional code doing first time so don't know if any better way than this. So if there is any better way please learn me. I would appreciate.


Edit

So what exactly I am looking for is

I want to load the file/content and heading as per the user group. So for example if logged in user is system_admin it will check group automatically and load file/content and heading accordingly same as for the other groups.

Note: User also may be in multiple groups so need to check the higher group and load content accordingly.

I hope this would be easy to understand.


Solution

  • I would suggest you should save rank as well in DB. It will help if user having multiple access (e.g System Administrator, Administrator).

    Create a separate function which will check usergroup and return user role.

     <?php
    function admin_heading($groupname) {
        $userId = 101; // You can get login userid here.
        $userRole = $this->checkUserRole($userId);
        switch ($userRole) {
            case 'administrator':
                // Load heading for administrator.
                break;
            case 'hr':
                // Load Heading for HR
                break;
            default:
                // May ur default role :)
                break;
        }
    }
    
    function checkUserRole() {
        $CI = & get_instance();
        //$userRole = ''
        // Run a single query which will return user max group.
    
    
        return $userRole;
    }
    

    I suppose you are able to fetch user details and roles available in DB. So run a single query to get user higher role, may be you need join user table with your role table.