Visible fields of a model depending on user role - REST API

Hi, I’m trying to figure it out an elegant way to change the visibility of some fields on my model via REST API, depending on the roles that my authenticated user have. Yii2 model allow us to create a method called field() so that we can add or remove fields to the model during the serialization. But check for the authenticated user role inside the model seems to be a bad approach. Any suggestions?

You can use ‘field()’ or output fields manually. I don’t see other ways.