<?php
/**
 * 用户权根指派
 * @author ouyang
 * @email oyjqdlp@126.com
 * @updated 2010-07-08
 */
class AssignUserAction extends BaseAction
{
    public function run()
    {
        if(Yii::app()->request->getIsAjaxRequest())
        {
             $username = $_POST['username'];
             $assign = $_POST['assign'];
             $assign = $this->getController()->removeUserAssign($username,$assign);

             #用户权限指派
             $this->assign($assign, $username);
        }
    }

    private function assign($assign,$username)
    {
        if(!isset($assign))
        {
            return;
        }
        $auth=Yii::app()->authManager;
        foreach($assign as $name)
        {
            try
            {
              $auth->assign($name,$username);
            }catch (Exception $e)
            {
            // Yii RBAC authitem表的name字段的值不能重复，但本身又没提供一个检测重复的机制，所以此处抛出异常后不做任何处理
            }
        }
    }
}

