How to use Auth::login() without real authentication - laravel

I would like to use the "Laravel" Auth features.
I have a middleware which finds some data, extract :
$member = $this->memberService->getMemberRolesFromLdap($accessToken, $id);
$member is an array.
Just after this research, I would like to store this $member somewhere to retrieve some data later in some other classes. Like that :
$roles = Auth::user()->roles;
So I thought it was a good opportunity to use these Auth features. But for the moment, I have this error :
Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, array given
So if I understand, I must declare somewhere that $member uses (or is) an instance of Authenticable. And there I admit my limits.
Is it a good idea to use Auth features to do that, and if so, how can I do that?

I tried this. It works, but I am not sure if it is best practice:
$ldapData = $this->memberService->getMemberRolesFromLdap($accessToken, $id);
// login this member
$user = new User();
$user->ldapData = $ldapData;
Auth::login($user);
And now, I can manipulate the user like that:
Auth::user()->ldapData

Related

Laravel Gate User parameter with eager loading relationship

As far as i know, the $user paramater on Laravel's Gate by default will returning instance of user credentials object which came from EloquentServiceProvider (not from User Model).
Gate::define('create-something', function ($user) {
// Code
}
But, how to add eager loading of relationship on it ? Lets say i have relationship called stuff on User Model, so i want to access it like so :
Gate::define('create-something', function ($user) {
dd($user->stuff);
}
Maybe easiest solution is by passing another parameters which came from Models\User , but it seems duplicating/redundant considering the $user parameter already there by default.
Is the another way to achieve this ?
Add notes : Stuff is many to many relationship
you can do something like this:
Gate::define('create-something', function ($user) {
$user->load('stuff');
dd($user->stuff);
}

Laravel Illuminate\Database\Eloquent\Collection::createToken does not exist

After I create a new user I try to create token via
$user = User::where('user_id', '=', $userid)->get();
$user->createToken('name')->accessToken;
Then i got the following error:
Method Illuminate\Database\Eloquent\Collection::createToken does not
exist.
Thanks
You are calling for a collection of users when you use the get() method, which won't have the createToken method (which is exactly what the error message is telling you).
You need to call for a single User model:
$user = User::find($userid);
And then, assuming you have the createToken method on your User model, this should work.
EDIT per comments:
You may have some other issue that is preventing the creation of the token in addition to the original issue of the collection vs an object. Try to create the token first:
$user = User::find($userid);
$token = $user->createToken('name');
Then if you will either get an error (if your createToken method is incorrect, or the parameter 'name' is not correct, etc), or you will have the token, and can then draw the accessToken from the new variable, $token.
Like this:
$accessToken = $token->accessToken
Either way, this will give you the diagnostics to bug hunt.

how to hash a password laravel 5.2 model create

I'm creating admin user via model and it saving record successfully but password is not being hashed as follows:
$request->password = bcrypt($request->input('password'));
Admin::create($request->except('_token'));
you can not modify $request properties like that.
Give it a try:
$input = $request->except('_token');
$input['password'] = bcrypt($input['password']);
Admin::create($input);
OR, handle it in your Admin Model
public function setPasswordAttribute($value)
{
$this->attributes['password'] = bcrypt($value);
}
Then you can
Admin::create($request->except('_token'));
Take a look at Laravel's Hashing documentation. It shows that you should be hashing any strings like so:
Hash::make($request->newPassword)
However looking at your code, i'd say this issue is actually the fact you're trying to modify the request $request->password, this is not going to work how you expect. Look at your Admin model class and see what the code is expecting, perhaps this is already in built if you pass the correct arguments.

Laravel socialite $user->getId()?

I'm not sure if this is what is really causing my issue, but perhaps someone will know. When I use Laravel Socialite and go:
$social_user = Socialite::driver($provider)->user();
Then somewhere else in my code is do this:
if ($authUser = User::where('provider_id', $social_user->id))
return $authUser;
For some crazy reason I get an error like this:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Database\Eloquent\Builder given
However if I do this I don't get an error:
if($authUser = User::where('email', $social_user->email)->first())
return $authUser;
I log this user in like this:
Auth::login($authUser, true);
Does anyone know why? I'm using laravel 5.2
The error message is actually self explained. When you do User::where('provider_id', $social_user->id) you end up with builder object that implements Illuminate\Database\Eloquent\Builder. You can call ->get() on it to get the collection of results (a collection of objects that implements Illuminate\Contracts\Auth\Authenticatable in your case, so you can iterate over them), or as you did, you can get the first match with ->first() (one object that implement Illuminate\Contracts\Auth\Authenticatable). You can read more in the Eloquent documentation.
The main point is that until you call ->get() or ->first() you are working with builder object. There is actually ->find() method also, that is used to retrieve record by pk, you cannot use it to search for records by constraints (where), but it also returns model object.

Laravel how to route old urls

I am using Laravel 4.
I have an old url that needs to be routable. It doesn't really matter what it's purpose is but it exists within the paypal systems and will be called regularly but cannot be changed (which is ridiculous I know).
I realise that this isn't the format url's are supposed to take in Laravel, but this is the url that will be called and I need to find a way to route it:
http://domain.com/forum/index.php?app=subscriptions&r_f_g=xxx-paypal
(xxx will be different on every request)
I can't figure out how to route this with laravel, i'd like to route it to the method PaypalController#ipbIpn so i've tried something like this:
Route::post('forum/index.php?app=subscriptions&r_f_g={id}-paypal', 'PaypalController#ipbIpn');
But this doesn't work, infact I can't even get this to work:
Route::post('forum/index.php', 'PaypalController#ipbIpn');
But this will:
Route::post('forum/index', 'PaypalController#ipbIpn');
So the question is how can I route the url, as it is at the top of this question, using Laravel?
For completeness I should say that this will always be a post not a get, but that shouldn't really make any difference to the solution.
Use this:
Route::post('forum/{file}', 'PaypalController#ipbIpn');
And then in the controller, use
public function forum($file) {
$request = Route::getRequest();
$q = (array) $request->query; // GET
$parameters = array();
foreach($q as $key => $pararr) {
$parameters = array_merge($parameters, $pararr);
}
}
You can then access the get parameters via e.g.
echo $parameters['app'];
you can use route redirection to mask and ending .php route ex:
Route::get('forum/index', ['uses'=> 'PaypalController#ipbIpn']);
Route::redirect('forum/index.php', 'forum/index');

Resources