Have multiple users to one account

Published 3 months ago by afoster009

Hello, i am just curious how i would implement a feature where a user subscribes to my site but they have the option to add users to their account with restricted privileges.

The logic is a user subscribes and pays a monthly fee, by default this user would be the admin for the account. The admin now will have the option to add 2 supervisors to his/her account. How exactly would i allow multiple users to sign into this subscribers account?

admin will be the only one to access the billing info and restricted things of that nature while the supervisors can manage other aspects of the subscribers account.

I havent wrote any code for this yet just want to get a few opinions before i hack away at this.

thanks in advance everyone

Snapey
Snapey
3 months ago (959,275 XP)

All users should have their own user account

you can add a company or contract or tenancy model (whatever term makes sense in your application). I'll call this company here for ease of conversation

The user model would have a foreign key to this Company model so that each user belongs to the company.

The company would have an owner which is the foreign key of the user that started the whole thing.

Does this get you started?

jlrdw
jlrdw
3 months ago (237,650 XP)

Also do a good search for Multi-Tenant applications, you will learn a lot more.

afoster009

This particular app is going to share a single database, where as to my understanding a multi tenant app provides a database for each instance of the app which is not what i am doing.

@Snapey would the relationship basically be company hasMany users and if the role is admin they control the company settings IE billing?

Snapey
Snapey
3 months ago (959,275 XP)

yes company would have many users

user that is owner can admin the account

Company model

public function owner()
{
    return $this->belongsTo(User::class, 'owner_id' , 'id');
}


afoster009

@Snapey and how exactly would authentication work since multiple user types will be logging into a single company account?

Snapey
Snapey
3 months ago (959,275 XP)

they all can login and are associated to the company (including the owner)

Then when deciding if to show the owners admin screens, you just need to check

if(Auth::user()->company->owner_id == Auth::id()) { // its their company

of course you could extract this to a method

afoster009

Also, the admin would be in charge of adding the other user accounts. would the relationship be created through a column like company_id? if so how would that column be populated?

afoster009

@Snapey do you have an implementation of this you can point me to? for some reason i cant get it to work

Please sign in or create an account to participate in this conversation.