Laravel-activitylog: Activity triggered User update on user logout

Created on 18 Oct 2018  路  13Comments  路  Source: spatie/laravel-activitylog

I noticed that after a user logs out, the LogsActivity Traits is triggered the updated event. After some searching I discovered that is caused by the remember_token in the User model, because this will be updated after loggin out.

Now I set the protected static $logAttributesToIgnore = [ 'remember_token']; in the User model to ignore the activity, because the logger will attach a new record with {"attributes":[],"old":[]} properties in the database. But the logAttributesToIgnore is not working, because the record will still be added, but now without properties []

Is there a setting I missed or is it a bug?
For you info, this is the record which is added:
Before ignoring the remember_token

| id | log_name | description | subject_id | subject_type | causer_id | causer_type | properties |
|---|---|---|---|---|---|---|---|
| 42 | user | updated | 1 | Modules\Users\Entities\User | 1 | Modules\Users\Entities\User | {"attributes":[],"old":[]} |

After ignoring the remember_token

| id | log_name | description | subject_id | subject_type | causer_id | causer_type | properties |
|---|---|---|---|---|---|---|---|
| 68 | user | updated | 1 | Modules\Users\Entities\User | 1 | Modules\Users\Entities\User | []聽|

Thanks!

question

All 13 comments

I think that it's related to #448 - like said there I can't reproduce it on my local machine.
I will try to reproduce your given example to solve it now and forever.

The method in question should be shouldLogEvent().
https://github.com/spatie/laravel-activitylog/blob/68eb6e65382f94fe86ce5ff6572159f80d024ba9/src/Traits/LogsActivity.php#L107-L125

If you could provide some debug information out of this method (what everything has as value, what it returns and do on) would be great.
Independent I will try to get some details on my own - could be that I need until tomorrow to debug it.

@dennis1502 I got the solution for you - add this (additional to $logAttributesToIgnore) to your Model.

protected static $ignoreChangedAttributes = ['remember_token'];

I have to dive deeper into this and why there are two properties for this. One is in the LogsActivity and the other in DetectsChanges trait. One controls the attributes listed in the attributes and old arrays and the other if it should create the log or not.
At first this sounds like the same but it doesn't have to be.

Reason:
If you have an attribute that shouldn't create a log, if it's the only changed one, but should be listed if another attribute creates a log.
@freekmurze is this the reason? Or is it a mistake and should be fixed to one property?
I will also take a look in the documentation and unittests if there is something missing - tomorrow.

https://github.com/spatie/laravel-activitylog/blob/59c3a6cf8abdcad7143b8b9d0a114148cc857a91/src/Traits/LogsActivity.php#L98-L105

https://github.com/spatie/laravel-activitylog/blob/59c3a6cf8abdcad7143b8b9d0a114148cc857a91/src/Traits/DetectsChanges.php#L46-L48

@Gummibeer Thank you so much, that actually worked for me. I checked that file earlier yesterday, but didn't noticed the ignoreChangedAttributes I could use. Now it works lik a charm.

I really don't know the reason yet, maybe @freekmurze can confirm your idea about it. But for now it works! :D

Many thanks!

I'm facing the same problem and the solution provided here didn't help me solve it. This only occurs on deploy, not on localhost.

@Zolak94 I thought your problem is that the causer is empty? Like described in #588

@Gummibeer Yes I do, I also have this problem, on my deploy (live) server whenever a user logs out it's also updated I figured out that remember token is the thing that's changing and triggering that. But your solution with putting protected static $ignoreChangedAttributes = ['remember_token']; in myUser.php model doesn't work. On the other hand, everything works normally on my local machine.

@Zolak94 do you add it in addition to protected static $logAttributesToIgnore = [ 'remember_token'];?

@Gummibeer Yes, I did.

And you still get an update record for the user? Is there any other changed attribute in the properties listed?

Yeah. No only remember token is changed.

So where's the bug/problem? If there are other changed properties than the ignored ones - it will create an activity entry.

I'm sorry that I didn't answer fully. Nothing else except remember_token changes when a user logs out.

And this is only happening on a live website, not on my local machine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lflucasferreira picture lflucasferreira  路  3Comments

lucianobosco picture lucianobosco  路  5Comments

DjKhireddine picture DjKhireddine  路  5Comments

TheFrankman picture TheFrankman  路  5Comments

Hesesses picture Hesesses  路  5Comments