What is the better wat ro create admin role in Laravel? - laravel

I want to create simple SPA with admin-panel. I did panel and fronted part. Now I have two solutions way:
Use enum type of row in database: $table->enum('role', ['user', 'admin]);
Create another 'roles' table and insert there: 'user' and 'admin'
Which is the better way and why?

Always try to go for the simplest, clearest solution. As long a you only have 2 roles, admin and user, and a customer can only have one role, the first solution is much easier.
Defining a new table would give unwanted complexity, and also to ask for each user it's role from a different table could be timeconsuming.
Perhaps if you have more roles, and a user can have multiple roles, the second solution is more clear.

Related

Dynamic role on hasura

I need to know that how we can implement dynamic role on hasura.I mean if we have lots of tables and want to have 4 access control per the table (insert, delete, update, select), a one way is that we create 4 roles for each table for example if we had two tables that named users, cars, we would have these roles:
1-add_user
2-delete_user
3-update_user
4-select_user
and
5-add_car
6-delete_car
7-update_car
8-select_car
it is not a efficient way...
please help me to find the best way.
thank you.
As far as my understanding goes of roles, in your case role will just be user. Your tables users and cars will have permissions assigned to user role. So for e.g
Role user will have (insert, delete, update, select) permissions for
users table but can only have (select) permission for cars table.
For implementation, you can easily create them in permission section of your respective table.
Let me know if this was helpful to you or if you have any furthur doubts.

How to add 'OR' condition to call middleware on Laravel Routes?

I want to add 'OR' condition with middleware on laravel routes for Role and Permission. Can anyone please help me here. Below is the code which I am trying currently through some internet research but it is not working.
Route::get('get-all-users', 'API\Admin\AdminController#get_all_users')->middleware(['role:admin' OR 'permission:view.all.users']);
In this scenario, you will need to change the DB structure a bit. Tables would look like:
Actions
Roles
Role_action_mapping => This will have foreign keys of both the above tables as a many to many relationship.
Now, every action should have a role assigned to it. So, the permission view.all.users will have an entry in the table with admin(or any desired role) assigned to it by it's side in Role_action_mapping.
So all you will have to do is have a single middleware say permission:view.all.users on the route and just check if the current role of the user has an entry with view.all.users in the Role_action_mapping table.

Laravel Multi table auth

I have three tables
Admins for admin,
Teachers for teacher,
students for student.
Each of these users have their own dashboard (AdminDash, TeacherDash, StudentDash). Now all I want to do is, each user are sent to their respective dashboard after login. Is it even possible to do so.
Also can this be done using single login form or do I need to create different login form for each case and different controllers to authenticate.
Edit:
Actually, here teachers and students fill different registration forms with different value so I don't want to store them in same table. And admins is for admin and superadmin who run the site. Also the dashboard they use after login are different.
I published a custom laravel for manage multi auth in one laravel project. In example branch I commit step by step for describe everything you most know. larasin
Also can this be done using single login form or do I need to create different login form for each case and different controllers to authenticate.
You can create a login form component an use it every where you want.
First extend your 3 tables with Authenticable like:
class User extends Authenticatable
Then in your config/auth, set up the providers like:
'teachers' => [
'driver' => 'eloquent',
'model' => App\Models\Teacher::class,
],
Then you should be able to auth::attempt your model instance.
Do let me know if this doesn't work, I am pretty new in Laravel also :) cheers!
p/s: I do think it is better to just use a user table with a "role" column, however I believe you probably have different columns needed for each of these user groups. But you can still use new tables such as "teacher_profiles","user_profiles" for those columns, and you can refer to different tables according to your role.
For example, if "teacher" role_id is 3, then if user role==3, you will refer to "teacher_profiles" table.

Complicated Eloquent Relationships

I have a many-to-many relationship, users and roles, which I have worked out just fine. However, I have another table I would like to relate as well, called leads. A user and that user's role can be assigned to a lead.
So for example, I've created a Role and let's call it 'Manager'. Now, when I am managing my Users, I'd need to be able to assign different users to different roles which will be a many-to-many relationship (a role can have many users, a user can have many roles). So I will assign the role of 'Manager' to User A.
Now, when I am modifying my leads, I'd like to be able to assign a role_user to my lead (In my example, I'd like to assign a user to that lead), but I'd first need to assign a role to that Lead (Manager), and then be able to assign a user that is of that role to that lead.
Currently, I've got a many-to-many relationship setup for users and roles, using the pivot table name role_user. I've then got a many-to-many relationship setup on that pivot table, role_user, and leads, using another pivot table named lead_role_user.
Models + Controller - http://paste.laravel.com/D6h
My error: Call to undefined method Illuminate\Database\Query\Builder::roles()
It feels as though I am making this much more difficult than it should be.

Create a settings page

I'm making an ASP.Net MVC 3 application in VS 2010. I have a task to create a settings page which would make the columns from the tables in my database with specific permissions (read, read/write etc.).
It's the first time I have a task of this kind and I have no idea on how to make this.
I tried going right click on the project in VS and then go to Settings. There was a link which says that my project didn't have a settings page, so I clicked it to create one. There appeared a table with settings but there are just public/internal access modifiers. I can't seem to find write/read.
Is this the right way of creating a settings page? Or is there another?
I'm sorry Andrew. I answered your question in quite a rush previously. So, I think my answer became quite unclear. I'm so sorry. I will try my best to answer this more clearly. My answer can be quite long and I hope you are patient enough to finish reading this. :-)
Actually, your problem can be solved in very easy way. Believe me! You actually don't need a Setting page for this.
I suppose you will have a Users table in your database, for storing user accounts for your system. Right! And again, I suppose that Users table will have at least these following fields.
UserID [ This must be the primary key for the table. Right! ]
UserName
Password
Email [ This is kind of optional. ]
I only suppose your database has this kind of schema. Or else, there must be other ways to set the permissions for your users in the table.
Alright, create another table called Permissions in your database. That Permissions table will handle the permission rights for your users in the above Users table. Ok! Then, you have two tables. One is your original Users table and another is Permissions table.
Ok! Our new Permissions table will have at least following fields:
ID [ This is the primary key for this table. ]
UserID [ This will come as foreign key from your previous Users table. ]
PermissionRead [ this field will hold boolean data type, or bit data type. True or False for Boolean and 0 or 1 for bit. This is entirely depends on the type of DBMS you use. ]
PermissionWrite [ again, same as PermissionRead. ]
Alright, now you have two tables. These Permission read and write fields are for holding the permission rights for your users.
If you have the exact db schema as I described above, then you will have the following kind of relationship like this:
Users table
UserID | UserName | PW
U-001 | Tim | timpassword
U-002 | Jim | jimpassword
Permissions table
ID | UserID | PermissionRead | PermissionWrite
1 | U-001 | True | False
2 | U-002 | True | True
So, you can see that, User Tim which is UserID U-001 has Read-only permission and User Jim who is U-002 has both read-write permissions.
So, you can check the condition of these fields when a particular user login to the system. If he or she has PermissionRead value True and PermissionWrite value false, then that user has read-only permission right. Or else, if both values are true then that user has read-write permission. Ok!
I tried my best to explain this, and I really do hope you can understand my answer. I really do...!!!
My suggestion is that, you should try this method first. And, if you are alright with this, I can explain more how to set group level permissions from this method. Ok! 'Cause my answer became quite long, and I fear you become bored reading this... ;-)
You don't actually need a setting page for tasks like this. Easiest way to give permissions to the users in ASP.Net is that, you need to create a table in your database. Let's just call it Permissions. Then create these fields in that table:
PermissionID (datatype something you want)
UserID (this must be the foreign key from your Users table)
Read (boolean or bit type)
Write (the same as Read)
Both (the same as Read)
You have a table which is linked to your user table. You can set permissions for the users in the Users table in this Permission table, by setting these boolean (true or false), or bit (0 or 1).
Then, when your user login to the system, you can check these "read", "write" and "both" values from that Permission table, and allow that particular user based on these true or false values.
For instance, if a particular has only Read value true and the other values false, then you can tell that, that user has "read-only" permission.
That's the basic idea for creating user permissions with databases. O'course, you can do some advanced features from database tools. But, I think this is the simplest way to do so. And you can add many permission types you want in that table.
You can even create groups with this method. Like, giving permissions to a particular group will give permission all users belong to that group.
Wish you good luck...!!!

Resources