$authority->getCurrentUser() not returning anything
Closed this issue · 11 comments
Hi,
Auth::user() return my loggedin uses while $authority->getCurrentUser() return nothing. My user model is in an different folder then the standard models directory.
Is it respecting the namespace defined in auth config file?
Regards, Ronnie
Hi Ronnie,
Did you set in app/config/packages/efficiently/authority-controller/config.php
:
$user = Auth::guest() ? App::make('MyNamespace\User') : $authority->getCurrentUser();
Is it respecting the namespace defined in auth config file?
I've moved my User
class in app/models/MyNamespace
folder and add it a namespace MyNamespace;
and it's OK.
So maybe you can tell me more on your environment ?
Cheers,
Tortue Torche
And of course if you're outside the app/config/packages/efficiently/authority-controller/config.php
file,
you need to use Authority::getCurrentUser()
instead of $authority->getCurrentUser()
In the code in your first comment you return if the user is a guest. What I am refering to is then when a user is logged in the $authority->getCurrentUser() return nothing and Auth::user() does.
So this is 50% a solution for my problem
Can you post your app/config/packages/efficiently/authority-controller/config.php
file ?
And tell me where is your User model and what's its namespace name?
$authority->getCurrentUser() return nothing
You mean, when you're in the config file, isn't it ?
exactly. and I am logged in since Auth::user() is giving me the User model.
my config file looks like this. I am using Auth::loginUsingId( 1 ); for testing purposes
Auth::loginUsingId( 1 );
$user = Auth::guest() ? new \Keebo\Models\User : $authority->getCurrentUser();
if( $user->hasRole( 'admin' ) ) {
$authority->allow( 'read', 'all' );
} else {
$authority->allow( 'read', 'all' );
}
the code that works
Auth::loginUsingId( 1 );
$user = Auth::guest() ? new \Keebo\Models\User : Auth::user();
if( $user->hasRole( 'admin' ) ) {
$authority->allow( 'read', 'all' );
} else {
$authority->allow( 'read', 'all' );
}
I also move my User.php
file in app/my_models/
path and edit the app/start/global.php
file like this:
//...
ClassLoader::addDirectories([
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/my_models',
app_path().'/database/seeds',
]);
//...
And it works ...
So, if you don't give more informations, I can do nothing :-/
Did you tried to clear your Cookies 🍰 ?
@Ronster I have the solution for your problem.
Edit your app/config/packages/efficiently/authority-controller/config.php
file and add the second line below:
Auth::loginUsingId(1);// for debugging purpose only
$authority->setCurrentUser(Auth::user());// for debugging purpose only
$user = Auth::guest() ? new \Keebo\Models\User : $authority->getCurrentUser();
if( $user->hasRole( 'admin' ) ) {
$authority->allow( 'read', 'all' );
} else {
$authority->allow( 'read', 'all' );
}
You need to set, in the config file, the current user because it was set in Authority-Controller before loading the config file.
So if you don't login a user before loading Authority-Controller, $authority->getCurrentUser()
will return a guest user by default, which is logical.
You can see here the code who's responsible of this behavior.
By the way, there is no issue with a namespaced User model or a User model in a different folder rather than the standard models directory.
You can also read this new Wiki page Testing Authority rules