Lithium Auth::check user with relationships, relational database - php

I am using Auth::check('user', $this->request), where 'user' is the name of my Auth::config configuration, to successfully authenticate and login users against a Users table. Upon successful user login, the user information is stored in the session and can be accessed via Auth::check('user').
I have a relational design in place where the Users table has a many-to-one relationship with a table named Sites. When Auth::check queries the Users table to authenticate a user logging in, it only returns information from the Users table. I need Auth:check to return the User with its related data from the Sites table. Both the User and Site models have the appropriate hasOne and hasMany relationships defined. How can I ensure that the user is queried and returned with its corresponding related data via Auth::check()?

While it's generally inadvisable to store more session information than is absolutely necessary, it's possible to do this through the 'query' configuration key of the Form auth adapter. Just create a wrapper method in your Users model that can take a query configuration array, and return a user object with associated relationships. Probably something like this:
public static function authenticate (array $query) {
return static::all($query + array('with' => 'Sites'));
}
Then, add 'query' => 'authenticate' to your 'user' auth configuration.

Related

Laravel Auth checking for users table when i need admins table

I am new to Laravel and i am creating a new SMS (Student Management System) for my first Laravel Project. So, i need 3 tables.
Admins
Teachers
Students
Admins will be me, teachers will be the actual teachers and students will be students. I will have more tables in the futures but i like to keep all the different types of users in their own table. This was simple before laravel. I am in 5.6 and i am using this to login a user:
public function auth()
{
if (Auth::attempt(['username' => request(['username']), 'password' => request(['password'])])) {
return redirect('/admin');
}
}
I want to know how i can get the Auth::attempt to check the "admins" table when trying to auth someone in the admins controller. And how do i do this so that way when i am working on the teacher/student authentication, it will check their specific table. I am so confused.

Authentication using multiple tables in Laravel 5.2

I am trying to create a laravel application with version 5.2. Where there will be 3 types of users
Administrator (website manager) - using default "users" table for
this.
Owners (Website listing creator from frontend) - using a
table "owners" for this.
Customer (Visitors or registered
visitors) - using a table "customers" for this.
Now my problem is:
i want to make sure login Owners will get proper authentication and redirect to their own (other then default Auth route) route.
And same with customer, and they will be mainly login through frontend of the website, so their route will be different from owners and Administrator. And these customer will also get authentication.
How can i manage that? I have worked around with single table, but being as a new person to Laravel i am not sure how i can achieve with multiple table.
I have checked laravel 5.2 started supporting multiple gaurds now, but not sure how can i do this.
There are certain packages for this, but i dont want to relay on package for this.
Thank you!
I would suggest you follow a Polymorphic approach for this.
Let's say there are three different tables - administrators, owners, customers
Now for all of them, there is a common table with the name users which will have the columns :- profile_id, profile_type.
Now profile_id will become the foreign key for tables administrators, owners and customers and profile_type will tell which Model the user belongs to.
Relation would be like,
class User {
public function profile() {
return $this->morphTo();
}
}
--
class Administrator {
public function user() {
return $this->morphOne('App\User', 'profile');
}
}
Here we are using morphOne instead of morphMany because the profile_id field in users table should have only one row for one admin.
Lastly, for the purpose of creation/storing. You'll have to :-
Create an admin like
$admin = Administrator::create($inputs);
Then do
$user = new User($inputs);
$admin->user()->save($user);
You're done!
You can learn more about it this approach from https://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations
Thanks,

How to retrieve data from additional columns in a pivot table and also be able to update it in laravel 5.2

I was working on making a group functionality for my website which uses a many to many relationship between groups and users.
My User model looks like this:
public function groups(){
return $this->belongsToMany('App\Group')->withPivot('role')->withTimestamps();
}
My Groups model looks like this:
public function users(){
return $this->belongsToMany('App\User')->withPivot('role')->withTimestamps();
}
So my third column has the name of role which is a string variable and is set to a default of "member" for members of my group and I set it to "admin" for the actual user who creates a new group. But I want the admin to have the option of making multiple members admins as well which would require me to check weather the current current user who sent the request is an admin or not. If he is, then I wanna be able to take his request of making a member an admin which would require me to update the role for that particular "member" to an "admin".
In the laravel documentation it only shows you how to attach and detach data in a pivot table and else where I have only seen methods of retrieving data from the first two columns but how can I do the same for additional columns and also be able to update it using the updateExistingPivot method?
You could access the column simply using pivot e.g :
$user->pivot->role
Take a look at Retrieving Intermediate Table Columns in documentation Eloquent Relationships.
Hope this helps.

Admin and non-admin authentication in Laravel 4

I want to have an authentication for Admin and for Non-admin. I'm having the admin infromation in 'users' db table, and non-admin information in 'customers' table.
How to use Laravel's Auth class to create authentication for this two different user roles?
for starter you should merge the two tables and then define a new column named "role_id" and then a new table called "roles" then as you attempt like this:
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
Session::put('role', Auth::user()->role_id);
return Redirect::intended('dashboard');
}
then via Session you can get the role whenever it's needed and decide to whatever yo want to do with it...
note: also edit your User model so role_id can be accessible
happy coding

Managing a 2 way relationship in CakePHP without retrieving data twice

I am having troubling linking some of my models together. Users have one of three roles:
Student
Lecturer
Admin
Information that is shared between the 3 roles is stored in a User table. When a User wants to change their role they make a role request that must be accepted by Admin.
User has one Student
User has one Lecturer
User has one Admin
User has one RoleRequest
Student belongs to User
Lecturer belongs to User
Admin belongs to User
What I am struggling with is that I cannot find a way of retrieving the data of a user without retrieving their role or their user data twice. If I retrieve the User object with recursive set to 2 I get the user data twice as it is also inside the Lecturer object. If I do the same with the Lecturer object I get the lecturer data twice as it is also inside the User object.
Don't use recursive. Set public $recursive=-1; in your AppModel, then use CakePHP's AMAZING ContainableBehavior to retrieve whatever data you'd like.
If I understood your question correctly, I'm guessing you're not using Contaible.
With that behaviour, you can do something like
$this->User->find('all', array('contain'=>'Lecturer'));
and that will retrieve an array similar to
[User] => array(/*user data*/),
[Lecturer] => array(/*Lecturer data*/)
Just remember to define your models as containable.

Categories