Closed IntegrityFoundation closed 4 years ago
Can you clarify some details.
You've got an Admin table and a User table with id as the PK Your id PK of the GroupDetails table is the same as the user id.
Do all users get an admin and employee entry otherwise you've got a conflict where different users will have the same id.
e.g. Admin ID | User |
---|---|
1 | Joe Bloggs |
Employee ID | User |
---|---|
1 | John Smith |
They'll both share the same row in the GroupDetails table.
Hi,
Admin Table
id // primary key of admin table. Name username password
Employee Table
id // Primary key of Employee table Name username password
Groupdetails Table
GroupId // This is the Primary key of Groupdetails id // This is the foreign key. One employee can have many groupdetails. GroupName Stage
The users in admin table will have access to CRUD in groupdetails whereas the users in employee table will only create, update, view and delete their own groupdetails.
Each user will log in either from admin or employee table. This is working fine. But users from employee table who have created their own groupdetails cant view, update and delete it.
In your rule, what does $user
contain?
You only save the integer part of the id in GroupDetails
but it may have your prefix.
Yes. For example if the user is employee-2 then in groupdetails table id will be 2.
So what does $user
contain?
If it's your prefixed id it's never going to match.
If we checked from log, we will find the the identity of the user is employee-2
public function execute($user, $item, $params)
{
Yii::debug($user);
return isset($params['model']) ? $params['model']->id == $user : false;
}
So this logs the $user
param as employee-2?
Yes. it logs the user as employee-4 where 4 is the id of the employee.
Then it's not going to match, you need to remove your prefix from $user
or add it to the comparison.
public function execute($user, $item, $params)
{
return isset($params['model']) ? ('employee-' . $params['model']->id == $user) : false;
}
Yes it is working. Thankyou
But if the model is admin then the user from admin table will still access the CRUD of all groupdetails.
Suppose if the case is that the users in admin table are not going to access the CRUD of groupdetails. Instead they are going to access some other page then the execute method you suggested is secure and correct.
public function execute($user, $item, $params)
{
return isset($params['model']) ? ('employee-' . $params['model']->id == $user) : false;
}
I'm lost as to what your issue is now. I think this is better suited to the forums as it's not a framework issue.
Ok. Thankyou.
Hi, I am using Yii2 basic. I two tables admin and employee. Both tables have id which is primary key, username, password. I have implemented the scenario where user can login from two different tables. Now I have RBAC implementation. Here I have created four tables which are required for RBAC. Now I have also created rules, permissions and roles in RBAC init(). I have assigned permissions to roles and roles to users.
I have a MultiUser.php model where it checks the instance of user whether it is of admin or employee and on that basis the user logs in.
Following is the MultiUser.php model
I have a table called groupdetails which has GroupId. employee table has a relation with grouodetails table. meaning One employee has many groupdetails. I created the CRUD for groupdetails. I have also checked for authorization in controller
Following is the Groupdetails controller.
In auth_assignment table I have the following,
Now when the user logs in from admin table and acccess groupdetails he can have access to create, view, update and delete actions.
But now when the user logs in from employee table he can create a record for groupdetails but he cant view, update, delete his groupdetails.
Below is my rule for RBAC
No I have only two roles i.e admin and Field Officer. So I have following in RBAC controller.
When I place Yii::debug($params) in rule as follows
Then in debug log, I have below