Skip to content

Bug with handling of admin context #701

@jettero777

Description

@jettero777

Hello,

I'm having issue when I need to access some models from inside of EVENT_AFTER_NGREST_FIND event handler.
When I do SomeModel::find() I expect to get model' fields as when I do find() in not admin context, but instead fields being processed by plugins and prepared for admin backend.
It is because of these parts of code, where context being determined solely by global GET vars:

    public function afterFind()
    {
        if ($this->getNgRestCallType()) {
            if ($this->getNgRestCallType() == 'list') {
                $this->trigger(self::EVENT_AFTER_NGREST_FIND);
            }
            if ($this->getNgRestCallType() == 'update') {
                $this->trigger(self::EVENT_AFTER_NGREST_UPDATE_FIND);
            }
        } else {
            return parent::afterFind();
        }
    }
    public function getNgRestCallType()
    {
        if ($this->_ngrestCallType === null) {
            $this->_ngrestCallType = (!Yii::$app instanceof \yii\web\Application) ? false : Yii::$app->request->get('ngrestCallType', false);
        }

        return $this->_ngrestCallType;
    }

As a result finding any model from inside admin context will call EVENT_AFTER_NGREST_FIND or EVENT_AFTER_NGREST_UPDATE_FIND though I'm not going to use model for REST and want to have raw field values and not processed (namely I need raw foreign keys but they are replaced with labels by plugin).

Furthermore it leads to more significant bug.

If you add to website url '?ngrestCallType=update' or '?ngrestCallType=list' or '?ngrestCallType=1' then website will become broken because all models will be processed as in admin context.

For example will be shown all translations in json format instead of only needed one, foreign keys will be replaces with fields names in case of selectModel plugin, etc.
In some cases will be just fatal error shown because some fields of a model contains arrays there should be strings (because it was decoded by plugin for admin context).

It is not expected behavior for a website.
I suppose getNgRestCallType context shouldn't not be set globally for all models but only for models explicitly specified in REST call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions