E
E
Express7772015-07-14 06:48:37
Yii
Express777, 2015-07-14 06:48:37

Error when upgrading profile Setting unknown property: yii\filters\AccessRule::0?

Hello.
I am learning examples from the book YII2 for Beginners by Bill Keck.
Example from page 207.
There is a user profile, on the profile/view page, which has an Update button. If the user has User_type == "Paid" he is allowed to update the profile ( User_type is stored in the database, ). If "Free" then it should redirect to the upgrade/index.php page.
When a user has a paid profile ( Paid ), then everything works fine. At Free there is an error.

Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\filters\AccessRule::0
 in F:\OpenServer\domains\yii2build.lc\vendor\yiisoft\yii2\base\Component.php at line 197

Here is the method in the Profile controller responsible for Update
public function actionUpdate()
    {
        PermissionHelpers::requireUpgradeTo("Paid");

        if( $model = Profile::find()->where(["user_id" => Yii::$app->user->identity->id])->one()){
            if( $model->load( Yii::$app->request->post()) && $model->save()){
                return $this->redirect(["view", 'id' => $model->id]);
            }
            else{
                return $this->render("update", [
                    "model" => $model,
                ]);
            }
        }
        else{
            throw new NotFoundHttpException("No such profile");
        }
//        $model = $this->findModel($id);
    }

PermissionHelpers. Just the method in this class redirects to upgrade/index.
public static function requireUpgradeTo($user_type_name)
  {
    if (!ValueHelpers::userTypeMatch($user_type_name)) {
      return Yii::$app->getResponse()->redirect(Url::to(['upgrade/index']));
    }
  }

ValueHelpers
public static function userTypeMatch($user_type_name)
  {
    $userHasUserTypeName = Yii::$app->user->identity->userType->user_type_name;
    return $userHasUserTypeName == $user_type_name ? true : false;
  }

And here is the Upgrade controller
class UpgradeController extends \yii\web\Controller
{
    public function behaviors()
    {
        return[
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'only' => ['index'],
                'rules' => [
                    'actions' => ['index'],
                    'allow' => true,
                    'roles' => ['@'],
                    'matchCallback' => function ($rule, $action){
                        return PermissionHelpers::requireStatus('Active');
                    }
                ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ]
            ]
        ];
    }

    public function actionIndex()
  {
    $name = Profile::find()->where(['user_id' => Yii::$app->user->identity->id])->one();
    return $this->render('index', ['name' => $name]);
  }
}

UPD1.
Perhaps UpgradeController/ is to blame.
After all, if you directly enter the address yii2build.lc/index.php?r=upgrade/index, then the same error comes out.
UPD2.
The answer is in the comments Dimon.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimon, 2015-07-14
@Express777

public function actionUpdate()   {
        PermissionHelpers::requireUpgradeTo("Paid");
}
you need to wrap this construction in if( PermissionHelpers::requireUpgradeTo("Paid")) { }
in your case, this method returns true / false, which does not affect anything,
try this
public function behaviors()
    {
        return[
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'only' => ['index', 'update'],
                'rules' => [
                    'actions' => ['index', 'update'],
                    'allow' => true,
                    'roles' => ['@'],
                    'matchCallback' => function ($rule, $action){
                        return PermissionHelpers::requireStatus('Active') && PermissionHelpers::requireUpgradeTo("Paid") ;
                    }
                ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ]
            ]
        ];
    }

and from the action PermissionHelpers::requireUpgradeTo("Paid") remove

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question