I am using Laravel built-in Authentication for users which I run from the terminal php artisan make:auth
It has one table in the database, users table and has an isAdmin column, so I am assuming it has two roles an admin and not_amin account.
Now, what I want is to modify this built-in functionality of Laravel make:auth to be able to add some roles like isEmployee and/or isCustomer.
How can I achieve this using the available option I have?
I hope, I somewhat explained my case. Thanks.
Having a column is_admin is targeted to only one role. This tells you if user is an admin or not.
I would suggest you to change depending on any of below cases :
1. If user can have just 1 role :
Modify users table and change is_admin to role. Inside role column, you can store the role of that particular user like admin, employee, 'customer` etc.
2. If user has multiple roles(many-to-many) :
Remove column is_admin from users table and create a new table role_user which will have user_id and role_id. You will need to have another table called roles to store all available roles in your system.
Related
does anyone know how to show multiple roles that are under a user? I have setup my permissions so a user can be under multiple roles, which is set easily enough using the following
$user->assignRole(['Root', 'IT', 'HR']); // assigning roles
I am however having trouble showing all roles a user relongs to in the same way?
Has anyone done this and knows how to? I simply want to show on a page which role a user belongs to.
Refer to this link
https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage
Get the names of the user's roles
$roles = $user->getRoleNames();
Returns a collection
if there are two tables (roles and user_roles) than you can check through inner join ('inner join' to check role must exist in 'roles' table that is assigned to a user) by passing user_id (user_id to get specific roles assigned by a user).
You can get a collection of roles assigned to the user via $user->roles
What is the most effective method to add User roles in Laravel 5.5?
I came across a couple of methods i.e.
Admin Middleware
Pivot Table for user_roles
I've tried and implemented both methods in my application but asking this question by keeping in mind my application's scalability on how they would be effective.
If you want you can add permissions on role table create a new permission table to define permissions to user.
Eg:
Without permission table: Based on cartalyst sentinel
User Table -> name, email, password etc
Role Table -> name, slug, permissions
Users_role -> user_id, role_id
With permission table: Based in Zizaco entrust
User Table -> name, email, password etc
Role Table -> name, slug, permissions
Users_role -> user_id, role_id
Permission table -> name, slug, description
role_permission -> permission_id, role_id
Why would you try to reinvent the wheel? Mayb you can use a package for that. The spatie/laravel-permission package is one I particulary like.
After a quick search I also found this discussion on laracasts with a few other options.
I'd say you need to have both: the middleware to check for access on each request & the permissions table (& pivot) for storing your permissions.
For a working example, you could:
take a look here at a package that is part of
a bigger app, and try out
a working demo
Disclaimer: I am a contributor to these packages.
In the above examples, in brief, permissions are tied to a role, which can be assigned to an owner, and the owner has users.
To me it looks that you need both of things mentioned in your question. You should have a Roles table that defines all possible roles a user can have in your application.
A user_roles pivot table that you would use to associate users to particular roles
And a Middleware which you will use to check for roles before authorizing user to access the functionality.
Detained demo is given in this link for roles based authentication in Laravel 5.5 https://www.5balloons.info/user-role-based-authentication-and-access-control-in-laravel/
I am using Ion Auth for user authentication. The system i am trying to build needs two different users tables for client and admin. As in the application/config/ion_auth.php i can change only one table name, how i can make sure two controllers uses two different users table?
Why not create groups instead? You can use the create_group method
then just check if the user belongs to that group.
Just create a Client group and an Admin group. You can also add more like Moderator group, etc.
This way is more convenient that having more than 1 users table
Make new tables with needed fields and relate it to group_id in users_groups.
Each table in a database should represent an entity. Since you need an entity User for authentication purposes you should have one table for all users. After that you can create tables for Admin and Client and create relationships between those and the Users table.
I want to design a DB which will be connected to PHP Application. In the app there are two types of users: company and person. Some functionality like adding articles will be done by both so in other tables there are author_id columns. So firstly I decided to create user column.
That's easy: id, username, password, role, active, created where role defines whether user is person or company.
Now I want to add profile table or profile tables depends on what you'd suggest (joined with the previous table by adding profile_id column there).
Both roles have different fields, which are required during registration.
The easiest thing would be to create one table with all required fields for both roles, allow them NULL values and in the PHP app (made in Yii Framework in this case) define requirements for each role in models.
The nicest thing would be to create separate tables for both roles BUT the questions is how to connect these two tables to one table using Foreign Key? Is it even possible. I know I may omit foreign key creation then based on role choose table, and from that table choose profile_id.
Or maybe you have another solution to my problem.
Thanks in advance for replies.
Adrian
You need an intermediary between the page and the database to assign the user to a group that has specific privileges. It's usually accomplished with a user-group-role design.
You can have a table for users system info (username , pass ...), and another for users profile (firstname , birthday ...), and another for groups(superuser , ...).
where user table can have multiple groups: user:one->group:many
user can have one profile user:one->profile:one
I think this is a decent solution.
I'm using Codeigniter with Ion Auth for user management. I'd like to create two different user roles - one for volunteers and one for organizations. I know that I can use Ion Auth's groups for things like access control, but what would be a good way to add fields to the different user types (for instance - the volunteer user will have a 'languages spoken' field while the organization will have a 'mission' field)? Should I extend the Ion Auth class for each new user type and handle CRUD seperately for each type, or use the 'groups' field and the user id to reference the fields in another table? Any insight as to an approach to this common problem?
I would recommend just adding all the fields you need into the meta table and only updating the ones you need per user group. If you need multiple groups per user check out Ion Auth v2.
I had the same problem before, what I ended up doing was building relation tables to handle different groups with different fields. Then I modified the profile() code a bit, to join the additional relation table according to the user's group settings.
I would start off building a relational database.
example:
**volunteers table**
id
user_id
languages
**organizations table**
id
user_id
mission
Then depending on user group, join the table in profile() function.
Hope this helps!