Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Field Access control with FilterFields

0
by skywalker, last modified on February 23rd, 2025

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"
                        ]
            ]

        ]; 

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?