Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in create action of \yii\rest\ActiveController #16812

Closed
aminkt opened this issue Oct 22, 2018 · 3 comments
Closed

Bug in create action of \yii\rest\ActiveController #16812

aminkt opened this issue Oct 22, 2018 · 3 comments

Comments

@aminkt
Copy link

aminkt commented Oct 22, 2018

What steps will reproduce the problem?

Consider that you are using ActiveController and want to create an entity in your db.
When you send post request to create action and want check access of user that requested.

What is the expected result?

You expect that the model object in checkAccess method filled by user input but it's not.

What do you get instead?

The model object is null.

Additional info

Q A
Yii version 2.0.15.1
PHP version 7.1
Operating system Docker ubuntu 16
@aminkt
Copy link
Author

aminkt commented Oct 22, 2018

Please look at this line :

if ($this->checkAccess) {

I think it's better we change it to something like this:

    /**
     * Creates a new model.
     * @return \yii\db\ActiveRecordInterface the model newly created
     * @throws ServerErrorHttpException if there is any error when creating the model
     */
    public function run()
    {
        /* @var $model \yii\db\ActiveRecord */
        $model = new $this->modelClass([
            'scenario' => $this->scenario,
        ]);

        $model->load(Yii::$app->getRequest()->getBodyParams(), '');

       // Change checkAccess method to here.
        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $this->id, $model);
        }

        if ($model->save()) {
            $response = Yii::$app->getResponse();
            $response->setStatusCode(201);
            $id = implode(',', array_values($model->getPrimaryKey(true)));
            $response->getHeaders()->set('Location', Url::toRoute([$this->viewAction, 'id' => $id], true));
        } elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
        }

        return $model;
    }

@samdark
Copy link
Member

samdark commented Oct 22, 2018

While it looks like a good change, no enhancements are accepted into 2.0 anymore. You can propose your change there: https://github.com/yiisoft/yii-rest

@samdark samdark closed this as completed Oct 22, 2018
@aminkt
Copy link
Author

aminkt commented Oct 23, 2018

Thank you. i will create a new issue in that is create a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants