Topic: Display Role Name of User

Hello everybody,

The default display of user information in FA is only name. I want to display role name as well at the top of header. It means that I have to add it inside renderer.php
But, in $_SESSION variabel, the information so far that I can get is [access] aka id field of security_roles db. 

The session variabel can be seen below

 [wa_current_user] => current_user Object
        (
            [user] => 1
            [loginname] => admin
            [username] => admin
            [name] => Administrator
            [company] => 0
            [pos] => 1
            [access] => 2
             ............
         )

How is the correct way to get this role name inside renderer.php?

Thanks for help smile

Re: Display Role Name of User

Role name is not stored in current_user object. role_id is id of the role in security_roles table.
Janusz

Re: Display Role Name of User

Thanks Janusz for your reply.

So, I have successfully added a role name by modifying current_user.inc, security_db.inc and renderer.php. Now I can access it via current_user object. smile

Re: Display Role Name of User

Hi bhoo-day,

I am very keen to know how you managed to add the role name to the header or footer. I would appreciate very much if you could share your code on the modification part of current_user.inc, security_db.inc and renderer.php.  I tried to figure out the three files but stumble at there because of lack of knowledge. Please help.

Thank you very much.

5 (edited by bhoo-day 09/09/2011 07:07:08 am)

Re: Display Role Name of User

Hi Apple,

I just made some modified lines in these files. Just add the modified lines into yours.

Just see the comment "MY CUSTOM CODE FOR ROLE NAME"
I couldn't share whole file because too long and it will make you confuse to read those a lot of lines smile

For current_user.inc (location at includes/current_user.inc)

class current_user
{
    var $user;
    var $loginname;
    var $username;
    var $name;
    var $company; // user's company
    var $pos;
    var $access;        
    var $timeout;
    var $last_act;
    var $role_set = false;
    var $old_db;
    var $logged;
    var $ui_mode = 0;
    
    var $prefs;
    var $cur_con; // current db connection (can be different from $company for superuser)
        
        /* custom */
        var $rolename; // MY CUSTOM CODE FOR ROLE NAME
        var $org_id;

    function current_user()
    {
        global $def_coy;
        
        $this->loginname = $this->username = $this->name = "";
        $this->company = isset($def_coy)? $def_coy : 0;
        $this->logged = false;

        $this->prefs = new user_prefs();
    }

    function logged_in()
    {
        return $this->logged;
    }

    function set_company($company)
    {
        $this->company = $company;
    }

    function login($company, $loginname, $password)
    {
        global $security_areas, $security_groups, $security_headings, $path_to_root;
        
        $this->set_company($company);
        $this->logged = false;

        $Auth_Result = get_user_for_login($loginname, $password);

        if (db_num_rows($Auth_Result) > 0)
        {
            $myrow = db_fetch($Auth_Result);
            $this->old_db = isset($myrow["full_access"]);
            if (! @$myrow["inactive"]) {
                if ($this->old_db) { 
                    // Transition code:
                    // db was not yet upgraded after source update to v.2.2
                    // give enough access for admin user to continue upgrade
                    if (!isset($security_groups) || !isset($security_headings)) {
                        echo "<center><br><br><font size='5' color='red'><b>";
                        echo _('Before software upgrade you have to include old $security_groups and $security_headings arrays from old config.php file to the new one.');
                        echo '<br>'."<a href=$path_to_root/index.php>"._("Back")."</a>";
                        echo "</b></font><br><br></center>";
                        exit;
                    }
                    $this->access = $myrow["full_access"];
                    if (in_array(20, $security_groups[$this->access]))
                        // temporary access for admin users
                        $this->role_set[] = $security_areas['SA_SOFTWAREUPGRADE'][0];
                    else {
                        echo "<center><br><br><font size='5' color='red'><b>";
                        echo _('System is available for site admin only until full database upgrade');
                        echo "</b></font><br><br></center>";
                        exit;
                    }
                } else {
                    $this->role_set = array();
                    $this->access = $myrow["role_id"];
                    // store area codes available for current user role
                    $role = get_security_role($this->access);
                    if (!$role) 
                        return false;
                    foreach( $role['areas'] as $code )
                        // filter only area codes for enabled security sections
                        if (in_array($code&~0xff, $role['sections'])) 
                            $this->role_set[] = $code;
                                                
                                         $this->rolename = $role['role'];  // MY CUSTOM CODE FOR ROLE NAME 
                }
                $this->name = $myrow["real_name"];
                $this->pos = $myrow["pos"];
                $this->org_id = $myrow["org_id"];
                $this->loginname = $loginname;
                $this->username = $this->loginname;
                $this->prefs = new user_prefs($myrow);
                $this->user = @$myrow["id"];
                update_user_visitdate($this->username);
                $this->logged = true;
                $this->last_act = time();
                $this->timeout = session_timeout();
            }
        }
        return $this->logged;
    }

        .....
        .....

For security_db.inc (location at admin/db/security_db.inc)

..........

function get_security_role($id)
{
    $sql = "SELECT * FROM ".TB_PREF."security_roles WHERE id=".(int)$id;
    $ret = db_query($sql, "could not retrieve security roles");
    $row = db_fetch($ret);
    if ($row != false) {

        $row['role'] = $row['role']; // MY CUSTOM CODE FOR ROLE NAME

        $row['areas'] = explode(';', $row['areas']);

        $row['sections'] = explode(';', $row['sections']);
    }
    return $row;
}

...................

renderer.php (location at themes/default/renderer.php)

..............
echo "<p id='user-info'>" . _("Welcome") . ", " . $_SESSION["wa_current_user"]->name .
       " (" . $_SESSION["wa_current_user"]->rolename . ")</p>";
..............

now I can access rolename via $_SESSION['wa_current_user']->rolename

Good luck brother

Feel free to ask me if you still need help smile

Re: Display Role Name of User

Hi bhoo-day,

You are so great. The code really work as it is. It really save me a lot of time. Without your code, I have no way to accomplish it.
Thank you very much for your kindness to allow us to share the code with you.

Re: Display Role Name of User

Apple wrote:

Hi bhoo-day,

You are so great. The code really work as it is. It really save me a lot of time. Without your code, I have no way to accomplish it.
Thank you very much for your kindness to allow us to share the code with you.

You're welcome bro.

Glad if you can make it smile