Yii:使用模块特定数据库进行访问控制
我正在尝试为将通过 Yii 模块访问我的网站的特殊用户组设置一个单独的数据库,但我似乎不太能获得正确的配置。
这是我的模块层次结构的相关文件。
/protected/modules/special
/protected/modules/special/SpecialModule.php
/protected/modules/special/models/SpecialActiveRecord.php
/protected/modules/special/models/Account.php
/protected/modules/special/components/UserIdentity.php
按照说明 在这里,我更新了 main.config 以包含模块特定的数据库定义。
'modules'=>array(
'special'=>array(
'db'=>array(
'connectionString'=>'mysql:dbname=specialdatabase',
'username'=>'special',
'password'=>'special',
),
),
),
我还通过将 public $db
添加到 SpecialModule.php 来更新我的模块以支持数据库定义,并且我创建了一个利用数据库定义的模块特定活动记录。
class SpecialActiveRecord extends CActiveRecord
{
public function getDbConnection()
{
$db = Yii::app()->controller->module->db;
return Yii::createComponent($db);
}
}
我遇到问题的地方是帐户模型。我的主要 Web 应用程序还实现了帐户模型,堆栈跟踪显示该模块正在通过用户身份 (/protected/modules/special/components/UserIdentity.php
) 访问我的所有模块特定文件。但是,用于授权的帐户模型是在站点级别引用的 (/protected/models/Account.php
)。
关于使用模块特定数据库实现模块特定身份验证的正确方法有什么想法吗?
I'm trying to set up a separate database for special set of users who will access my site through a Yii module, but I can't quite seem to get the configuration right.
Here are the relevant files my module heirarchy.
/protected/modules/special
/protected/modules/special/SpecialModule.php
/protected/modules/special/models/SpecialActiveRecord.php
/protected/modules/special/models/Account.php
/protected/modules/special/components/UserIdentity.php
Per instructions here, I've updated my main.config to include a module specific database definition.
'modules'=>array(
'special'=>array(
'db'=>array(
'connectionString'=>'mysql:dbname=specialdatabase',
'username'=>'special',
'password'=>'special',
),
),
),
I have also updated my module to support the database definition by adding public $db
to SpecialModule.php and I have created a module specific active record that utilizes the database definition.
class SpecialActiveRecord extends CActiveRecord
{
public function getDbConnection()
{
$db = Yii::app()->controller->module->db;
return Yii::createComponent($db);
}
}
Where I'm having trouble is in the account model. My primary web application also implements an account model and the stack trace shows that the module is accessing all of my module specific files through user identity (/protected/modules/special/components/UserIdentity.php
). The account model that is being used for authorization, however, is referenced at the site level (/protected/models/Account.php
).
Any ideas on the proper way to implement module specific authentication using a module specific database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Yii 网站上的论坛帖子(我现在似乎找不到),我通过向我的模块模型添加前缀解决了这个问题——例如。
SAccount.php
和SActiveRecord.php
。此外,我必须对
getDbConnection
例程进行一些小修改才能激活数据库并让 Yii 1.0 中的所有内容正常工作(1.1 中可能并非如此)。这里我修改了 CActiveRecord 类的代码。Based on a forum post on the Yii site (that I can't seem to locate now), I overcame this issue by adding prefixes to my module models--eg.
SAccount.php
andSActiveRecord.php
.Additionally, I had to make minor modifications to the
getDbConnection
routine to activate the database and get everything working in Yii 1.0 (this might not be the case for 1.1). Here I modified the code from the CActiveRecord class.