Role-based access control continue 1

2012.5.11 continue 1
>>> Role-based access control continue 1<<<
1. create protected/commands/shell/RbacCommand.php
内容如下:

<?php
class RbacCommand extends CConsoleCommand
{
    private $_authManager;
 
    public function getHelp()
    {
        return <<<EOD
USAGE
  rbac
DESCRIPTION
  This command generates an initial RBAC authorization hierarchy.
EOD;
    }
 
    /**
     * Execute the action.
     * @param array command line parameters specific for this command
     */
    public function run($args)
    {
        //ensure that an authManager is defined as this is mandatory for creating an auth heirarchy
        if(($this->_authManager=Yii::app()->authManager)===null)
        {
            echo "Error: an authorization manager, named 'authManager' must be configured to use this command.\n";
            echo "If you already added 'authManager' component in application configuration,\n";
            echo "please quit and re-enter the yiic shell.\n";
            return;
        }
 
        //provide the opportunity for the use to abort the request
        echo "This command will create three roles: Owner, Member, and Reader and the following premissions:\n";
        echo "create, read, update and delete user\n";
        echo "create, read, update and delete device\n";
        echo "Would you like to continue? [Yes|No] ";
 
        //check the input from the user and continue if they indicated yes to the above question
        if(!strncasecmp(trim(fgets(STDIN)),'y',1))
        {
            //first we need to remove all operations, roles, child relationship and assignments
            $this->_authManager->clearAll();
 
            //create the lowest level operations for users
            $this->_authManager->createOperation("createUser","create a new user");
            $this->_authManager->createOperation("readUser","read user profile information");
            $this->_authManager->createOperation("updateUser","update a users information");
            $this->_authManager->createOperation("deleteUser","remove a user from a device");
 
            //create the lowest level operations for devices
            $this->_authManager->createOperation("createDevice","create a new device");
            $this->_authManager->createOperation("readDevice","read device information");
            $this->_authManager->createOperation("updateDevice","update device information");
            $this->_authManager->createOperation("deleteDevice","delete a device");
 
            //create the reader role and add the appropriate permissions as children to this role
            $role=$this->_authManager->createRole("reader");
            $role->addChild("readDevice");
 
            //create the member role, and add the appropriate permissions, as well as the reader role itself, as children
            $role=$this->_authManager->createRole("member");
            $role->addChild("reader");
            $role->addChild("createDevice");
            $role->addChild("updateDevice");
            $role->addChild("deleteDevice");
            
            //create the owner role, and add the appropriate permissions, as well as both the reader and member roles as children
            $role=$this->_authManager->createRole("owner");
            $role->addChild("reader");
            $role->addChild("member");
            $role->addChild("createUser");
            $role->addChild("updateUser");
            $role->addChild("deleteUser");

            //provide a message indicating success
            echo "Authorization hierarchy successfully generated.";
        }
    }
}



2. use phpmyadmin to import sql file from C:\xampp\yii\framework\web\auth\schema-mysql.sql

C:\xampp\yii\power> yiic shell
>> rbac


>>> End of Role-based access control continue 1 <<<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值