-
Notifications
You must be signed in to change notification settings - Fork 175
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
Having multiple Authenticatable models is incorrectly tracked #121
Comments
It is possible - i think - to work arround this by using a custom UserRecorder and usersResolver. What i could also think of, is adapting pulse default behaviour by saving the model of the user (either in a new column or in the "values" field) and then serializing or something like it from that one. I am not sure about that or what would be the preferred way here, but if one of the team members has an idea of how to implement it or how - if at all - they would like to have it implemented, i'm happy to work on it and do a PR, just let me know. |
The recorder could probably capture a JSON serialized tuple of [Model, id], similar to what we do in other places. The resolving logic would need some tweaks though. |
It would be preferable that the solution requires no configuration if possible, to keep Laravel Pulse working out of the box with different Laravel setups. Storing a [Model, id] tuple sounds like a great idea, however that requires changes to the resolver function as well as changes to how custom resolvers using Pulse::users work. |
We also ran into this issue with a multitenant application. Ideally, you would have one admin panel for all your tenants if your primary goal is to look into server performance in general. |
We’ll likely need to capture the “user provider”, rather than the model. I’ve started working on a fix and we’ll have this out shortly. |
Would love some thoughts here. I could capture the user id + user provider. This would likely solve the problem for standard setups, but it does require that every user provider is registered in In the case of multi tenancy, where you may change your config based on the incoming request, this may be more difficult to handle in a generic manner. My thoughts are:
Pulse::setUser(fn () => [Request::tennant()->id, Auth::id()]); and then when resolving the users you could access those details... Pulse::users(function ($users) {
return $users->mapSpread(function ($tennant, $user) {
return Tennant::find($tennant)->user($user);
});
}); Would this solve the problem we have here? Are there any other suggestions on how this could be solved? |
Would this work for guards registered in packages, like this:
I'm guessing that yes, it would work. If that is the case, for my use-case your solution would be perfect. |
@AlexDanault I believe it would work by default with your setup. No extra config required. We would store The under the hood we would... $user = Auth::createUserProvider("admin")->retrieveById(432); |
Then I'm sure this would solve 95% of issues, and the hook you provide would fixe the remaining 5% easily. |
@timacdonald I think the suggested solution would work for multitenant applications. Our tenant ID is basically UUID4.
|
I like the ideas above however it does not allow for the possibility of multi tenant and multiple providers which is the situation that I find myself in. In order for pulse to be able to get the user information it needs to initiate the tenant and then get the user from the correct provider. I think that you would need an optional 3rd data saved which is the tenant id and then have a way of informing pulse how to initialize and end the tenant session. Love your work. Hope this helps |
Josh! 👋 I think what I'm proposing should suit your needs. In the following snippet... Pulse::setUser(fn () => [Request::tennant()->id, Auth::id()]); what is returned from the closure could be an array containing anything. So you could include something like You could do something like... Pulse::users(function ($users) {
return $users->mapSpread(function (string $tennantId, string $userId, string $userProvider) {
Tennant::initialize($tennantId);
$user = Auth::createUserProvider($userProvider)->retrieveById($userId);
Tennant::endSession();
return $user;
});
}); Totally just making up the user land APIs of course. How does that fit with you, @jny986 |
Yeah I think that would work perfectly. Is the Any idea when this will come out and is there anything else that I can do to help? |
In reply to @timacdonald's question in PHP Australia: (I'm not familiar with multiple auths and I'm not sure how pulse "segments" stats by user, so this is just multitenancy thoughts) Can you just allow the ability to "categorise/tag" instead of worrying about how multi-tenancy is done? There are three contexts to consider. I recommend a simple
Perhaps the docs can outline those 3 or 4 contexts as a home for Caveat: Both queue workers and Octane are daemons. Pulse can probably handle a What if: the user only unsets the tenant when it changes and three jobs in a row on the same worker can run without the "switch tenant" code running? IMO that's a userland problem that can be easily solved with Consideration:
Hope these considerations/experiences/contexts help ✌️ |
Pulse Version
v1.0.0-beta2
Laravel Version
10.26.2
PHP Version
8.2.13
Database Driver & Version
No response
Description
User requests are logged with only the ID value in the key-field, however it is possible to have multiple Authenticatable models in Laravel.
The default behavior for Pulse seems to be to log all Authenticated requests, also for other Authenticatable models, but not differentiate the model type.
This leads to a bug where request count are aggregated towards the User model with the ID even if another model is making the authenticated request.
Steps To Reproduce
Install Laravel Pulse on a project with a custom Authenticatable model that can login for instance using Laravel\Sanctum\HasApiTokens
The text was updated successfully, but these errors were encountered: