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/
Related
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.
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.
The problem I have is this: I have in my requirements a Users table and a Roles table separated.
users(id, login, password, id_role, name, last name, telephone, ...)
roles(id, role, description)
I already have read and followed the authentication example here but it has the role in the same table as the user's data. I don't know how can I relate both tables in cakePHP, especially in a login module. Can someone please help me?
You should read the CakePHP cookbook. The next topic after the simple authentication and authorization chapter is how to build an ACL-controlled application, which is what you’re after: http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
Change field roles to role_id and add relation Role hasMany User (User belongsTo Role)
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!