1074

Members Module: Persisting logged in member name right through to the view/template

Comments for “...”
 

Posted by D3mon on Monday 31st January 2022 at 19:30 GMT

Hi guys, first project using Trongate.

I'm working on a small site that uses David's members module from the Module Market to lock down access to all pages to registered members only. The template I'm using has an area to display the logged in person's name. Can I persist it through the view $data or get it from the session data somehow?

Thanks all.
Level One Member

D3mon

User Level: Level One Member

Date Joined: 6/07/2021

Posted by Myhiideas on Monday 31st January 2022 at 20:19 GMT

Hello,
Yes that is possible. Would help to see your controller code.
That said. Here is a good practice.
Always use these two lines of code displaying a view:
These allow you to set Member level in the Trongate_security Module
$this->module('trongate_security');
$this->trongate_security->_make_sure_allowed();

Set Members level in Trongate_users_level
And then get the Member's Object
$member_obj = $this->_get_member_obj();
$data['trongate_user_level'] = $member_obj->user_level_id;

You can use the $data['?????'] = Set your variable or constant to be passed to the view.

Let me know how it goes.
Dan

Myhiideas

User Level: Guest

Date Joined: 1/11/2021

Posted by D3mon on Monday 31st January 2022 at 20:43 GMT

Thanks Dan. Sorted it by adding the following to trongate_security.php :

switch ($scenario) {
            case 'members area':
                $this->module('members');
                $token = $this->members->_make_sure_allowed();

                // next two lines pass logged in username via $data to view
                $member_obj = $this->members->_get_member_obj();
                $data['username'] = $member_obj->username;

                break;
            default: ...


Thanks! 🙂

[UPDATE] This doesn't 100% work yet - see below for the fix

This comment was edited by D3mon on Monday 31st January 2022 at 21:54 GMT

Level One Member

D3mon

User Level: Level One Member

Date Joined: 6/07/2021

Posted by djnordeen on Monday 31st January 2022 at 20:48 GMT

That's Great!
I like the way you did that.
Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by D3mon on Monday 31st January 2022 at 21:42 GMT

Ah OK. Scratch that.

I hadn't fully tested it right the way through to the view. Got the username successfully but setting the $data there doesn't seem to carry all the way through to the view.

Fixed it as follows:

in trongate_security.php
class Trongate_security extends Trongate {

    // set up a public property to store the username
    public $username;

    function _make_sure_allowed($scenario='admin panel') {
        //returns EITHER (trongate)token OR initialises 'not allowed' procedure

        switch ($scenario) {
            case 'members area':
                $this->module('members');
                $token = $this->members->_make_sure_allowed();

                // next two lines load and populate the logged in username
                $member_obj = $this->members->_get_member_obj();
                $this->username = $member_obj->username;
                break;
            default:
                $this->module('trongate_administrators');
                $token = $this->trongate_administrators->_make_sure_allowed();
                break;
        }

        return $token;
    }

}


and then in the module controller:
...
        // view only visible to logged in users
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed('members area');
        
        // add logged in username to view data for use in the template/theme
        $data['username'] = $this->trongate_security->username;
...

This comment was edited by D3mon on Monday 31st January 2022 at 21:53 GMT

Level One Member

D3mon

User Level: Level One Member

Date Joined: 6/07/2021

Posted by djnordeen on Monday 31st January 2022 at 22:40 GMT

You can try this
When you get the trongate_user id
Pass it here
Trongate_user_levels

$this->module('trongate_user_levels');
$this->trongate_user_levels->_get_user_level();
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by D3mon on Monday 31st January 2022 at 22:53 GMT

Thanks Dan.

That's a little advanced for me just now but it's useful as a reference.
Level One Member

D3mon

User Level: Level One Member

Date Joined: 6/07/2021

×