<?php
/**
 * 权限指派用户
 * @author ouyang
 * @email oyjqdlp@126.com
 */
class AuthorityAssignAction extends BaseAction
{
    public function run()
    {
        if(Yii::app()->request->getIsAjaxRequest())
        {
             $auth = Yii::app()->authManager;
             $itemName = $_POST['name'];
             $assign = $_POST['assign'];
             $assign = $this->remove($itemName,$assign);
             
             $auth->setAssignUser($itemName,$assign);
             return;
        }

        $users = Manager::model()->findAll();
        foreach($users as $user)
        {
            $list[] = $user->username;
        }
        $name = Yii::app()->getRequest()->getQuery('name');
        $hasuser =  Yii::app()->authManager->getAssignUser($name);
        $this->getController()->render('_assign', array(
                'title' => '权限指派',
                'users' => $list,
                'name' => $name,
                'hasuser' => $hasuser,
        ));
    }

    private function remove($itemName,$arr = null)
    {
        $auth = Yii::app()->authManager;
        $assigns = $auth->getAssignUser($itemName);
        foreach ($assigns as $assign)
        {
            if(isset($arr)&&in_array($assign, $arr))
            {
                unset ($arr[array_search($assign ,$arr)]);
            }else
            {
                 $auth->revoke($itemName,$assign);
            }
        }
        return $arr;
    }
}

