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 · Fixed by #2487
Closed

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

hroma opened this issue Mar 4, 2020 · 9 comments · Fixed by #2487
Assignees
Labels
type: bug 🐛 Is a bug; fixes a bug
Milestone

Comments

@hroma
Copy link

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

afbora commented Mar 4, 2020

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

@lukasbestle lukasbestle added the needs: information ❓ Requires more information to proceed label Mar 4, 2020
@hroma
Copy link
Author

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
Copy link
Member

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
Copy link
Member

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 afbora added type: bug 🐛 Is a bug; fixes a bug and removed needs: information ❓ Requires more information to proceed labels Mar 4, 2020
afbora added a commit that referenced this issue Mar 5, 2020
@afbora afbora self-assigned this Mar 5, 2020
@afbora afbora linked a pull request Mar 5, 2020 that will close this issue
4 tasks
@hroma
Copy link
Author

hroma commented Mar 5, 2020

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

@afbora
Copy link
Member

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.

@bastianallgeier
Copy link
Member

@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
Labels
type: bug 🐛 Is a bug; fixes a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants