Nette stores current user’s identity in session. There are a few situations when you want to reload the identity or change it completely. For example, the user updates his profile and you want the change reflected in the page header. Or a super administrator is allowed to re-authenticate as a different user without having to know the other user’s password.
NUser has a convenient method called setIdentity() but unfortunately, it’s declared as private so you can’t use it. The solution is either to loop through the current identity and update its fields one by one, or do it nice and clean with a custom authenticator. Let me show you.
Here’s the looping method. It just does the job.
-
span class=”st0″>’id’ => 123, ‘name’ => ‘Joe Sixpack’, ‘role’ => ‘administrator’
And here’s the custom authenticator. One for refreshing updated profile and another one for swapping identities.
-
span class=”st0″>’id’// in presenter
In this example, the authenticator actually does some job – it verifies that the current user is allowed to change his identity. It’s in fact authorization, not authentication, but that’s not the point here.
-
span class=”st0″>’administrator’‘role’‘consultant’, ‘manager’‘id’‘consultant’‘role’] == ‘manager’‘id’"Insufficient permissions."// in presenter
Tags: Nette Framework 2.0-beta (revision 8a3182e released on 2011-10-11)
-