Storing remember_token to a separate table than users? - laravel

is it possible to set another table & column to store my remember tokens? I know that the framework tries to automatically find a remember_token column in my "users" model, but I want to store it separately from users. Is there a way to configure my default tokens table? Thank you
P.S - I'm using laravel 5

First, you need to create separate model for storing remember tokens and define a relationship on your User model like so
public function rememberToken() {
return $this->hasOne('RememberToken');
}
Then you need to override methods on your User model, originally defined in Authenticatable trait. Override getRememberToken() and setRememberToken() methods. You will also need to override getRememberTokenName() as it is used in where clause in EloquentUserProvider::retrieveByToken() see EloquentUserProvider line 60. In order for this to work properly you probably have to add global scope to your User model to join remember_tokens table on every query, and return 'remember_tokens.token' from getRememberTokenName() method.
Think twice as it seems more trouble than it is worth. Why would you want to store your tokens separately anyway?

I believe the way Laravel works uses a seperate column in the users table to store a single remember_me token. From my understanding it seems that logging out resets this token regardless of storing the token in a cookie (correct me if I'm wrong).
https://github.com/laravel/ideas/issues/971
If you log in with remember_me checked on your personal computer,
then again on your phone,
and maybe again with any other device,
then finally the act of signing out on any device using the remember_me token or not will reset this token in the DB.
If Laravel had a separate table, able to remember each device, it might solve the problem.

Related

Eloquent - Adding custom field like timestamps in all model by default with logged user data

I would like to add to all models on my Laravel project two custom fields called updated_by and created_by, automatically filled and saved on db with the name of the logged user (I'm using JetStream), exactly like the fields updated_at and created_at.
I wonder if there is a clean procedure, before starting to add the fields on model one-by-one, and handle them in the controllers, in every store/update function I have. Extending Model and using the new class for every models seems a good solution, but I'm not really sure. And I do not know how and where to fill one-time the fields with logger user data.
Thanks, I'm newbie in Laravel and hope this is not a basic question.

Laravel: Check if the field was filled in the route, using Trait and Scope

I have an api that I am implementing multi-tenant based on an additional column in the tables.
For this I created a trait that implements a
static::addGlobalScope() and static::creating().
I have a user_idXtenant_id table. In this table I store the accesses that the user has.
My Route looks like this:
Route::get('{tenant_id}/products', function ($tenant_id) {
return App\Products::where(['tenant_id' => $tenant_id])->get();
})->middleware('role_or_permission:admin|seller|get-products');
Route::get('products', function () {
return App\Products::get();
})->middleware('role_or_permission:admin|seller|get-products');
The idea is that in the first route it is verified if the user has access to tenant_id and if he has access, return only the products of that tenant_id (here Polices can be useful and solve the problem).
The second route, on the other hand, must return all products of all tenant_id that the user has access to. For this, I use static :: addGlobalScope() to set the tenants that the user has access to in this way:
$builder->whereIn('tenant_id', $tenants);
The question is how to check in GlobalScope if tenant_id was filled in the model. So, if it is present, I only check the permission and if it is not present I include the tenant_id that the user has access to.
I hope I have been able to adequately exemplify the problem.
Any help is most welcome.
Thank you!

Laravel Client Switching Functionality

I am new to laravel and do not have much experience with using ORM. I am building a system in which I need build functionality to allow user to switch clients with another user. I am not sure what is the best method to achieve this.
Currently client belongs to user, do I need another association for client and user model using different foreign key ?
public function clients()
{
return $this->hasMany('App\Client');
}
public function user()
{
return $this->belongsTo('App\User');
}
Even though a client can only have one user switch request I would like to track all the requests so for example if user A makes request to switch client Test with user B and then make another request to switch the same client with user C I would like to soft delete the first record and create new record for new request. Once the other user accepts the request we change the primary key for client Test in the clients table. Will this be a One To Many / Many To Many relationship ?
What table naming conventions do I need to follow ? Any help will be much appreciated.
If I understand you correctly, you would like to temporarily switch clients for one user to another. What you should do is have another table say switched-clients that holds records for the switches with columns user1 and user2. When you want to retrieve clients, your controller checks this table first and if the record exists where('user1', 'user A') (according to your example), then you retrieve the clients for user2.

How to save a form into different tables

I am new with laravel
I used the default Authentication and I added one other input that I wanted it to be saved on an other table how can I do that
If you're talking about how to save registration form input into different table (not to users) when user is registering, add something like this to create() method in app\Http\Auth\RegisterController.php:
Profile::create(['custom_column' => $data->custom_form_field]);
I think in this situation will be better to use connections in model. If this data is about user of course. Also there is important thing about submit method(simple submit or AJAX).

Joomla user registration insert file

I am looking for file in joomla 3, where is the query that inputs into db values from registration fields?
Thanks in advance.
If you follow the code from com_users you will see that it loads and uses the Model UsersModelUser (loaded from /administrator/models/user.php ) which in turn uses the class JUser (loaded from /libraries/joomla/user/user.php ).
If you want to create/alter users, you should load and instance of UsersModelUser and use it's load() to load an existing user and it's save() method to store any change or to create new user entries.
By using the UsersModelUser and therefore JUser you will get all of the niceties like two-factor authentication (if it's configured) and the correct hash for the password based on the authentication system/plugins in use. It will even generate a random password if you don't pass one in the with data used to create a new user.

Resources