Field Access control with FilterFields
0
Sometimes you may want to control role readonly for fields in a few model. This filterFields function helps to control the field access but updating a $field_permissions variable instead of keep changing the code.
public function filterFields() {
$user_roles = \BackendAuth::getUser()->role()->pluck('code')->toArray();
if(!empty($model->field_permissions['roles'])) {
$readonlyroles = array_keys($model->field_permissions['roles']);
$user_got_role = array_intersect($readonlyroles, $user_roles);
if(!empty($user_got_role)) {
foreach($user_got_role as $role) {
foreach($model->field_permissions['roles']['readonly'][$role] as $rf) {
$fields->{$rf}->readOnly = true;
}
}
}
}
}
public $field_permissions = [
'roles' => [
'role1' => [
"name","number"
]
]
];
There are no comments yet
Be the first one to comment