Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User.txt session refresh issue after $user->update #2486

Closed
hroma opened this issue Mar 4, 2020 · 9 comments
Closed

User.txt session refresh issue after $user->update #2486

hroma opened this issue Mar 4, 2020 · 9 comments
Assignees
Milestone

Comments

@hroma
Copy link

@hroma hroma commented Mar 4, 2020

Passing $user = $user->update… to a template is working as expected but
when a user edit his data (user.txt) in the frontend, change stored (in a session?) aren't updated (snippet for my case).

CASE SCENARIO:
LoggedIn user can subscribe (true) or unsubscribe (false) to a newsletter on a toggle basis:

$user = $kirby->user('user@email.com');
$currentStatus = $user->newsletter();
echo 'Old value: '.$currentStatus.'<br>';
$variable = ($currentStatus == 'true' ? 'false' : 'true');
$user->update(['subscribe' => $variable]);
echo 'New value: '.$user->newsletter();

RESULT:
Old value: false
New value: false

EXPECTED:
Old value: false
New value: true

Did I miss Something to update the User globally or it is a bug?
Hope it helps making Kirby even better

@distantnative

This comment has been minimized.

Copy link
Contributor

@distantnative distantnative commented Mar 4, 2020

It should be

$user = $user->update(['subscribe' => $variable]);

Kirby objects are immutable but return the altered object. So you have to put it back in your variable.

@hroma

This comment has been minimized.

Copy link
Author

@hroma hroma commented Mar 4, 2020

Ok, but then my snippet / included in the default template returns

Whoops \ Exception \ ErrorException (E_NOTICE)
Undefined variable: user

in the snippet
<?php $user = kirby()->user(); ?>

how to update kirby()->user(); itsef?

@afbora

This comment has been minimized.

Copy link
Contributor

@afbora afbora commented Mar 4, 2020

@hroma Can you share all the codes in snippet? I think you have diffrent problem.

@hroma

This comment has been minimized.

Copy link
Author

@hroma hroma commented Mar 4, 2020

Thank you for investigating the issue.
Snippet simplified (without all html stuff and form to sumbit "true" or "false"):

$user = kirby()->user(); 

if ($user->subscribe() == 'true') {  
    echo 'true';
} else { 
    echo 'false';
}

Controller:

return function ($site, $pages, $page) {
    $kirby = kirby();
    if ($kirby->request()->is('POST')) {
        $variable = get('newsletter'); 
        $user = $kirby->user('user@email.com');
        $user = $user->update(['newsletter' => $variable]);
    }
    return array('user' => $user);
}

Template:

echo $user->newsletter().' - ';
snippet('subscribe.php');

Result:

  1. for true = true - false
  2. for false = false - true

Expected:

  1. true - true
  2. false - false

If you refresh the browser (F5)…???:

  1. true - true
  2. false - false
@afbora

This comment has been minimized.

Copy link
Contributor

@afbora afbora commented Mar 4, 2020

Yes now I can reproduce the issue.

After updating the user, the user variable in the Auth class comes from the cache in during request.

@afbora

This comment has been minimized.

Copy link
Contributor

@afbora afbora commented Mar 4, 2020

We need to override update() method in UserActions like that:

trait UserActions
{    
    public function update(array $input = null, string $language = null, bool $validate = false)
    {
        $user = parent::update($input, $language, $validate);

        // set auth user data only if the current user is this user
        if ($user->isLoggedIn() === true) {
            $this->kirby()->auth()->setUser($user);
        }

        return $user;
    }
}
afbora added a commit that referenced this issue Mar 5, 2020
@afbora afbora mentioned this issue Mar 5, 2020
4 of 4 tasks complete
@afbora afbora self-assigned this Mar 5, 2020
@afbora afbora linked a pull request that will close this issue Mar 5, 2020
4 of 4 tasks complete
@hroma

This comment has been minimized.

Copy link
Author

@hroma hroma commented Mar 5, 2020

Great. It is working well now. Thank you for your fast support

@afbora

This comment has been minimized.

Copy link
Contributor

@afbora afbora commented Mar 5, 2020

@hroma I have updated the code on above comment: #2486 (comment)

However, I recommend not using it production website unless PR is approved.

afbora added a commit that referenced this issue Mar 5, 2020
bastianallgeier added a commit that referenced this issue Mar 5, 2020
@bastianallgeier

This comment has been minimized.

Copy link
Contributor

@bastianallgeier bastianallgeier commented Mar 5, 2020

@bastianallgeier bastianallgeier added this to the 3.3.5 milestone Mar 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

5 participants
You can’t perform that action at this time.