yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

MINOR: \yii\rbac\PhpManager: alias not expanded on save() #8086

Closed mcd-php closed 9 years ago

mcd-php commented 9 years ago

file_put_contents(@common/rbac/data/items.php): failed to open stream: No such file or directory

Not so critical, will use __DIR__, but anyway violation of given example.

mcd-php commented 9 years ago

Here's fix, too lazy to fork and PR. Is where any way to send fixes by patches ?

diff --git a/rbac/PhpManager.php b/rbac/PhpManager.php
index 151a634..9490ce2 100644
--- a/rbac/PhpManager.php
+++ b/rbac/PhpManager.php
@@ -713,7 +713,7 @@ class PhpManager extends BaseManager
      */
     protected function saveToFile($data, $file)
     {
-        file_put_contents($file, "<?php\nreturn " . VarDumper::export($data) . ";\n", LOCK_EX);
+        file_put_contents(Yii::getAlias($file), "<?php\nreturn " . VarDumper::export($data) . ";\n", LOCK_EX);
     }

     /**
klimov-paul commented 9 years ago

Such problem could not appear: all file names are passed through Yii::getAlias() at init() method: https://github.com/yiisoft/yii2/blob/master/framework/rbac/PhpManager.php#L88

mcd-php commented 9 years ago

Oops, I see it's my fault, forgot to call parent::init();.

But anyway it's more intuitive to expand upon use - what if user change them after creation ?

klimov-paul commented 9 years ago

No: this is not acceptable, because these names are also used while loading data: https://github.com/yiisoft/yii2/blob/master/framework/rbac/PhpManager.php#L630

FantomX1-github commented 6 years ago

How does this Yii::getAlias($file) help guys? I have Yii 2 version 2.0.13.1 and this issue persists still. The actual reason is hidden behind it. As is written in documentation of getAlias method - "Note, this method does not check if the returned path exists or not." . Thank you. I am unable without fix (or creating folder rbac manually), to use file-based rbac authentication. Although I worked it around by creating rbac directory myself in Rbac command mkdir(Yii::getAlias("@app/rbac")); but I don't know if it's the best practice.