Support the ongoing development of Laravel.io →
Authentication Session
Last updated 1 year ago.
0

Without knowing exactly what's being done (and what types of permissions are being used) I would look into the Auth::Login method to login to a predefined "ghost" user for the permissions you would need/want.

Last updated 1 year ago.
0

@jerimiahshort, thanks for the answer. :)

Auth::Login would certainly force the user. However, my problem is not to force that "ghost" user, but to make Auth::check() to return false if that's the user logged in.

I'm working on an already built app with lots of Auth::check() calls everywhere. It's kinda a content manager app. It handles dependant sites with autonomous logic. Let's say, if "http://MyApp" is the app, it controls "http://MyApp/Site1";, "http://MyApp/Site2";, etc.

This app will handle old sites, and will keep on making new sites.

What i want is, from now on, to call Auth::user()->can("some_custom_atomic_per_site_permission") in places like blade templates and controllers, without having to change all the calls to Auth::check() in the old sites AND without having to deal with the NULL value Auth::user() returns when there's no user logged in.

So, the problem is no to log in "anon", the problem is overriding the Laravel default Auth logic.

Is there any recomended way to override Auth::check() AND/OR Auth::user() ?

Last updated 1 year ago.
0

From what I'm gathering, I would think that extending Auth (or copying and modifying the existing Auth and registering it as a new service provider) would more than likely be a route to look at. Here is a question on SO that discusses something similar. While the OP was asking about a custom function, you could just as easily modify an existing function to return what you need to know based on a pre-existing session/auth or a specific value if not (so you may write you code knowing what to expect exactly)

I apologize if this is a bit vague, but I'm still learning Laravel myself. However, I'm no stranger to building apps; it's just a matter of how Laravel does it ;)

Last updated 1 year ago.
0

I think you can do a simple thing. I assume that for the guest you have an uniq account into user table. When you make Auth:check() you also van verify if Auth::user()->id is same with the id of you guest user id specially created for this thing. If you intend to have multiple guest accounts into the table you can create a new column to indentify the guest user ( isguest int 1) and you can check by (Auth::user()->isguest==1)

Last updated 1 year ago.
0

jerimiahshort said:

From what I'm gathering, I would think that extending Auth (or copying and modifying the existing Auth and registering it as a new service provider) would more than likely be a route to look at. Here is a question on SO that discusses something similar. While the OP was asking about a custom function, you could just as easily modify an existing function to return what you need to know based on a pre-existing session/auth or a specific value if not (so you may write you code knowing what to expect exactly)

I apologize if this is a bit vague, but I'm still learning Laravel myself. However, I'm no stranger to building apps; it's just a matter of how Laravel does it ;)

Your answers are fine, thank you very much. Googling around i've found lots of "create your own auth driver" and "create a custom user provider", which i didn't fully understood on a first fast read and felt those where too complex aproachs for a simple task like overriding a method. However, i think now that's the way to go, just as you point in your link.

I need to override, not to extend. So, I guess this similar links would be the ones i need: Here , and here .

closca said:

I think you can do a simple thing. I assume that for the guest you have an uniq account into user table. When you make Auth:check() you also van verify if Auth::user()->id is same with the id of you guest user id specially created for this thing. If you intend to have multiple guest accounts into the table you can create a new column to indentify the guest user ( isguest int 1) and you can check by (Auth::user()->isguest==1)

Sure, that's more or less exactly what i need. Nothing fancy, as you note. My problem is that i want to do those checks internally in Auth::check() and/or Auth::user(), and i wonder how to do it in the laravel's way.

I mean, i dont want to go to some core class and change its internal logic; but neither wanted to create a few clases and change my autoloader configurations (which changes in different environments and may be trouble for maintenace) in order to be able to override a method. But i guess now that last choice is not that bad after all.

I'll post here how did i solved it if i make it by using the techniques explained in the links here. Please feel free to add comments pointing for other possible sollutions. What i'm looking for is the easiest way to override Auth class methods.

Thanks.

Last updated 1 year ago.
0
Solution

I've finally solved this. I had to extend Illuminate\Auth\Guard, like this:

use Illuminate\Auth\Guard;

class OtherGuard extends Guard {

	public function check(){
		return ( ! is_null($this->user()) && $this->user()->username != "anon" );
	}
	
	public function user(){
		$ret = parent::user();
		$ret = is_null($ret) ? User::where("username","=","anon")->first() : $ret;
		return $ret;
	}
	
}

Then, register somewhere (i choose "app/global.php") a new Auth driver, like this:

Auth::extend('OtherDriver', function()
{
    return new OtherGuard(
        new Illuminate\Auth\EloquentUserProvider(new Illuminate\Hashing\BcryptHasher, "User"),
        App::make('session.store')
    );
});

And finally, go to "app/config/auth.php" and change the "driver" setting to "OtherDriver".

That's it.

Thanks to @jerimiahshort, and also this links: http://toddish.co.uk/blog/creating-a-custom-laravel-4-auth-dri... http://anthon.io/how-to-create-a-custom-auth-driver-in-laravel-4/ http://brianretterer.com/creating-new-auth-drivers-using-larav...

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Canta canta Joined 27 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.