Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Accessing custom user variables

Solved by pamtbaau View solution

Started by Marco 8 years ago · 4 replies · 965 views
8 years ago

Hi there,

I'm trying to add a custom field variable when creating a new user and access it via grav.user object, but it shows up empty.
I'm able to add the variable to new user creation form via blueprints/user/account.yaml with
userplan:
type: text
size: large
label: Plan

and it does show up in the respective user.yaml file as userplan: 'XXX' but when I try to add it to pages via grav.user.userplan it only shows an empty string. I feel like I'm missing a step - would someone be very kind to assist? Many Thanks!

Marco

8 years ago Solution

@markic I guess the step you are missing is... There is no 'user' when the page is requested, only an anonymous visitor.

When an anonymous visitor is accessing the page, the 'user' object does exists in Twig, but is empty. Try {{ dump(user) }} to see the user object.

If you add a login form to the page, Grav knows who is accessing the page and will be able to show the right yaml file for that user.

👍 1
8 years ago

@pamtbaau thank you very much for your swift reply - the solution worked perfectly. I'm still a bit unclear on why it worked though - without the dump I'm able to access other logged-in user variables like grav.user.fullname or grav.user.title, the only ones that are not accessible are the custom ones I've added. Why is that?

8 years ago

@markic Are you sure? I cannot reproduce that behaviour. When not logged in, I do get a 'user' object showing the definition of the 'account.yaml' and default values are shown, but no sensitive data belonging to a real user account.

Custom data fields are also shown in both case, but does not contain data when not logged in.

Would you mind checking again? It might be a vulnerability if you can reproduce.

8 years ago

Okay so it turns out I had to log out and log back in for custom user fields to show. Previously I tried clearing cache which didn't have any effect.

In regards to your question - private information does not show if user is not logged in so I don't think there is a vulnerability.

Thanks for your help - question answered and I have a better understanding of Grav's innards now.

Suggested topics

Topic Participants Replies Views Activity
General · by Hanns Mattes, 10 hours ago
1 11 2 hours ago
General · by Andy Miller, 3 hours ago
0 7 3 hours ago
General · by Jerry Hunt, 5 days ago
2 136 1 day ago
General · by pamtbaau, 2 days ago
1 93 1 day ago
General · by Andy Miller, 2 days ago
0 77 2 days ago