Laravel - Sentinel User is not the same as eloquent User - php

I am using Laravel and Sentinel for my authentication.
Authentication works fine, I can log in using Sentinel and get back a User and I can see all of the User's fields.
However, I cannot access it's relationships (other than the Sentinel roles).
$user = Sentinel::findById($userId);
dd($user->telephones); // returns null
$user = App\User::find($userId);
dd($user->telephones); // displays a dump of the associated eloquent collection
Is there a way to retrieve the eloquent User from Sentinel so I can look at it's relationships?

In your User model extend Sentinel EloquentUser.
class User extends EloquentUser
And in your catalyst sentinel config set user model like-
'users' => [
'model' => 'App\User',
],
And don't forget to run-
php artisan config:clear

Related

Laravel 5.7 + Spatie Permissions + JWT auth

I'm setting up a REST API using Laravel 5.7. To validate authentication I JWT-auth and for permissions and roles I use Spatie.
My problem: when trying to link a role to a user I get the following error
Spatie \ Permission \ Exceptions \ RoleDoesNotExist
There is no role named admin.
The role do exist in the database:
This is how I'm trying to assign a role to the user:
$user = User::findOrFail(1);
$user->assignRole('admin');
As I'm new to Laravel I'm not sure if it's relevant, but setting the JWT I had to change the driver of the guard in the config/auth.php to jwt
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
I can't see what I'm doing wrong. I added the roles and then tried to add the role to a user.
Check your app namespace. If you updated it from App, be sure to update it in config/auth.php.
On the other hand, if you didn't update the App namespace, try clearing your cache and re-seed the database tables.
php artisan config:cache
php artisan cache:clear
Also check the user model if you have protected $guard_name = 'api'; in there.
Hope this helps. Cheers!
I'm assuming you've manually added the roles to the database. The roles are cached and it causes issues if you don't use the built in create methods.
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit articles']);
From the GitHub docs
You will need to manually clear the cache with php artisan cache:clear.
The better way to do this is either to use a seeder (for permanent roles) or Tinker to run the code to create roles and permissions, which will trigger the cache to be cleared.
Guard has been changed from the web to api but still try to find web from the database
assign a role like this.
$roleToAssign = Role:: findByName('administrator', 'api');
$user->assignRole($roleToAssign);
#Link
If none of other answers solved your problem
Check that you are using queue
if so
restart the queue

how to add an admin in laravel 5.1 auth manually from database

any body has any idea how to pass the authentication of laravel ? or to add an admin from database or the source code ? assuming you have created an admin panel and an admin user but you lost your database and now you just want to create a new admin user for your panel . i used default laravel authentication and middle ware for admin panel . here is the middle ware added in kernel.php
'admin' => \App\Http\Middleware\AuthAdmin::class,
and here we got role controller
if(Auth::user()->isSuperAdmin()) {
$objects = Role::select($this->dt_fields_db)->where('id','<>',1)->where('id','<>',3);
} else {
$objects = Role::select($this->dt_fields_db)->where('id','>',3);
}
Make a new Seeder class, create a user and the super admin role, assign the role to the user and be a super admin forever and ever.
public class SuperAdminSeeder {
public function run () {
// modify to following commands fit your table structure
$role = Role::create(['name' => 'super_admin'];
$user = User::create(['email' => 'your#email.com', 'password' => bcrypt('secret')]);
DB::table('role_user')->insert(['user_id' => $user->id, 'role_id' => $role->id]);
}
}
Call it via the command line:
php artisan db:seed --class=SuperAdminSeeder::class
Just run php artisan make:auth and php artisan migrate. Then, navigate your browser to http://your-app.dev/register or any other URL that is assigned to your application. These two commands will take care of scaffolding your entire authentication system.

Laravel use custom USER-Model in package

I´ve developed a package stored in LaravelRoot/packages/myname/project/ in Laravel.
Inside my package i´ll have to use an extended user-model containing a relationship not mentioned in the default usermodel.
Now here is my question:
How can i override the default User-Model inside my package?
More details:
If i receive the current user via Auth::user() inside my package i´ll receive an object using the default App\User.
I have extended this model containing now a new relationfunction, stored in LaravelRoot/packages/myname/project/src/App/Models/User (namespace: Myname\Myproject\App\Models).
Here´s my relation-function:
/**
* The roles that belong to the user.
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(Role::class)->withTimestamps();
}
This relation is only used in my package, so i don´t want to store this function inside the default App\User. Is it possible to receive an object of my extended usermodel using Auth::user() in my package or do i have to receive this manually each time i need this one?
I think, the easiest way is to make your package model via extending of App\User model. You need just to open config/auth.php and to setup your model class.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => Project\Models\User::class,
],
You will get correct model via Auth::user().

Yii Framework get user data

How can I get currently online user data with all the parameters from the database? Right now I get it's ID by using this:
echo Yii::$app->user->getId();
Can I reach other data somehow or do I have to create a function which gets all the information by the user ID?
You can access to all the identity values in this way
this for username
Yii::$app->user->identity->username
check for your User models for others attributes
http://www.yiiframework.com/doc-2.0/guide-security-authentication.html
http://www.yiiframework.com/doc-2.0/yii-web-identityinterface.html
http://www.yiiframework.com/doc-2.0/yii-web-user.html
(and your User model of course)
You can access the model of the currently logged in user with:
$user = Yii::$app->user->identity;
This will return either null (if the user is not logged in) or an instance of the identityClass you defined for the user component in your config. Ex:
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
So you can use it like any other model class and access it's attributes and/or methods. Just make sure you configure correctly the user component and the identityClass exists and can be accessed.
current user is:
Yii::$app->user->identity
it can be null, so check it before accessing fields

Integrate authentication to existing Laravel project and users database

I am a totally newbie to Laravel, but after following lots and lots of guide, my project was really going well.
Now, I have a user table named UsersTable, which is going to store both Username and Password.
However, after trying php artisan make:auth and a lot of Googling, I found no clear, exact way of defining both the User table name, and the Username-Password field used for authentication.
I've tried the original Email-Password authentication on the table named users on another project I created for trying this, and it works like a charm. I believe that as Laravel already provide the authentication system, why reinvent the wheels when (I believe) there's a way to customise it.
Thanks in advance! :D
Use php artisan make:auth and then :
To change the default table from users which uses the User model
you have to navigate to : config/auth.php
And make these changes
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\UsersTable::class, //put the right model name here or you can use database driver
],
],
To change the table for an eloquent model you can do this
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UsersTable extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'UsersTable';
}
To change the default login credential from email to user name , just add this to the Authcontroller generated by laravel
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/home';
protected $username = 'username'; // you can put whatever column you want here from your table
Now
edit laravel generated Views and change the email input name to name="username" then extend the main.blade.php, do some styling and you are good to go i guess
EDIT : Dont forget to change the validation stuff
Laravel basic authentication system is not the fastest way to create your own auth method ... I suggest to you Sentinel Package to customize your authentications ...
for example to authenticate your user by using user name you can do like this :
Sentinel::authenticate(array(
'username' => 'john.doe',
'password' => 'foobar',
));
for me it's the fastest way to customize your authentication method i hope that will help you.

Categories