AuditController.php 14.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
<?php
class AuditController extends Controller
{
    /**
     * @var string the default layout for the views. Defaults to 'column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column1';

    /**
     * 应用操作对象
     * @var object
     */
    private $_appModel;
    /**
     * 实例操作对象
     * @var object
     */
    private $_caseModel;
    /**
     * 用户权限判断对象
     * @var object
     */
    private $isAllowed;
    /**
     * 存放URL请求CALL参数
     * @var object
     */
    private $mRequest;
    
    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            //'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        /*
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'audit', 'accept', 'reject'),
                'users' => array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions' => array('admin', 'delete'),
                'users' => array('admin'),
            ),
            array('deny',  // deny all users
                'users' => array('*'),
            ),
        );
        */
    }
    
    /**
     * 审核记录管理
     */
    public function actionAdmin()
    {
        $model = new History('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['History']))
            $model->attributes = $_GET['History'];

        $this->render('admin', array('model' => $model));
    }
    
    /**
     * 查看详细
     */
    public function actionView()
    {
        $dataProvider = null;
        
        $this->checkUserIsAllowed();
        if($this->isAllowed && isset($this->mRequest->item, $this->mRequest->id))
        {
            $dataProvider = AuditManage::getViewData($this->mRequest->item, $this->mRequest->id);
            $viewClass = AuditManage::getViewClass($this->mRequest->item);
        }
        else
        {
            throw new CHttpException(404, '抱歉,您访问的页面不存在!');
        }
        $this->render('view', array(
                                       'dataProvider' => $dataProvider,
                                       'view' => $viewClass
                     ));    
    }
    
    /**
     * 应用审核
     */
    public function actionAudit()
    {
        $dataProvider = null;
        
        $this->checkUserIsAllowed();
        if($this->isAllowed && isset($this->mRequest->item))
        {
            $dataProvider = AuditManage::getAuditData($this->mRequest->item);
        }
        else
        {
            throw new CHttpException(404, '抱歉,您访问的页面不存在!');
        }
        $this->render('audit', array(
                                        'dataProvider' => $dataProvider,
                                        'item' => $this->mRequest->item
                     ));    
    }
    
    /**
     * 拒绝回退
     */
    public function actionReject()
    {
        $caseModel = $this->_loadCaseModel();

        if(!is_object($this->mRequest) || !isset($this->mRequest->item))
        {
            throw new CHttpException(404, '抱歉,您访问的页面不存在!');
        }
        
        if(is_array($caseModel) && count($caseModel))
        {
            foreach ($caseModel as $model)
            {
                // 检查实例是否有状态
                if($model->swHasStatus())
                {
                    // 获取工作流中允许转变状态 
                    $nextStatus = $model->swGetNextStatus();
                    // 过滤转变当前状态为回退
                    if (is_array($nextStatus) && $model->swNextStatus($nextStatus[0]))
                    {
                        if (isset($this->mRequest->aid)) 
                        {   // 应用升级拒绝
                            $status = $model->swGetStatus()->getId();
                            $model->case_status = $status;
                            $model->save();
                        }
                        else 
                        {   // 应用审核拒绝
                            $model->delete();
                        }
                    }
                }
            }
            
            // 工作流中断时同步应用当前状态
            $model = $this->_loadAppModel();
            
            if(isset($this->mRequest->aid))
            {   // 升级回退到已发布状态
                $model->app_state = Platform::APP_STATUS_PUBLISHED;
            }
            else
            {   // 审核未通过回退到开发状态
                $model->app_state = Platform::APP_STATUS_REJECT;
            }
            
            if (!$model->save())
            {
                print_r($model->getErrors());
            }
        }
        unset($this->_caseModel);
        unset($this->_appModel);
        
        if (Yii::app()->request->isPostRequest)
        {
            echo 1; // 批量拒绝成功
        }
        else
        {
            $this->redirect($this->createUrl('audit/audit', array("CALL" => CJSON::encode(array('item' => $this->mRequest->item)))));
        }
    }
    
    /**
     * 批准转变
     */
    public function actionAccept()
    {
        $caseModel = $this->_loadCaseModel();
        $status = null;
        
        if ( !is_object($this->mRequest) || !isset($this->mRequest->item) )
        {
            throw new CHttpException(404, '抱歉,您访问的页面不存在!');
        }
        
        if (is_array($caseModel) && count($caseModel))
        {
            foreach ($caseModel as $model)
            {
                // 检查实例是否有状态
                if($model->swHasStatus())
                {
                    // 获取工作流中允许转变状态 
                    $nextStatus = $model->swGetNextStatus();
                    // 过滤转变当前状态为批准
                    if(is_array($nextStatus) && $model->swNextStatus($nextStatus[1]))
                    {
                        $status = $model->swGetStatus()->getId();
                        
                        $model->case_status = $status;
                        
                        if(!$model->save())
                        {
                            print_r($model->getErrors());
                        }
                    }
                }
                
                // 工作流完成时同步应用当前状态
                if(Cases::STATUS_ARCHIVED === intval($status))
                {
                    // 更改应用当前状态
                    $model = $this->_loadAppModel();
                    
                    if(is_object($model) && ($model instanceof Platform))
                    {
                        $model->app_state = Platform::APP_STATUS_APPROVED;
                        
                        if(!$model->save())
                        {
                            print_r($model->getErrors());
                        }
                    }
                }
            }
        }
        unset($this->_caseModel);
        unset($this->_appModel);
        
        if (Yii::app()->request->isPostRequest)
        {
            echo 1; // 批量批准成功
        }
        else
        {
            $this->redirect($this->createUrl('audit/audit', array("CALL" => CJSON::encode(array('item' => $this->mRequest->item)))));
        }
    }
    
    /**
     * 应用升级操作
     *
     * 步骤如下:
     * [
     *   A.更新上线表中应用属性信息
     *   B.更新上线表中应用平台设置
     *   C.更新开发者测试表中应用当前状态
     * ]
     * 
     */
    public function actionUpgrade()
    {
        $this->_getRequest();
        
        if(!is_object($this->mRequest) || !isset($this->mRequest->aid, $this->mRequest->pid, $this->mRequest->item))
        {
            throw new CHttpException(404, '抱歉,您访问的页面不存在!');
        }
        
        $connection = Yii::app()->db;
        $transaction = $connection->beginTransaction();
        try 
        {
            $sqlStatement = "UPDATE `wave_func` AS `t1`, `wave_func_dev` AS `t2`
                                SET t1.pic = t2.pic, t1.info = t2.info, t1.sort_id = t2.sort_id, t1.appTime = t2.appTime,
                                        t1.recommend = t2.recommend, t1.developer = t2.developer, t1.upTime = t2.upTime, t1.www = t2.www
                                     WHERE t1.id=t2.id AND t2.id=:aid";
            $command = $connection->createCommand($sqlStatement);
            $command->bindValue(':aid', $this->mRequest->aid);
            $command->execute();

            $sqlStatement = "UPDATE `wave_func_plat` AS `t1`, `wave_func_plat_dev` AS `t2` 
                                SET t1.appTime = t2.appTime, 
                                    t1.upTime = t2.upTime, 
                                    t1.appHome = t2.appHome, 
                                    t1.classID = t2.classID, 
                                    t1.prop = t2.prop, 
                                    t1.dataFormat = t2.dataFormat, 
                                    t1.lang = t2.lang, 
                                    t1.ver = t2.ver, 
                                    t1.devType = t2.devType, 
                                    t1.isLinkWave = t2.isLinkWave, 
                                    t1.callback = t2.callback, 
                                    t1.ifWidth = t2.ifWidth, 
                                    t1.ifHeight = t2.ifHeight 
                                WHERE t1.app_id = t2.app_id 
                                  AND t1.plat_id = t2.plat_id 
                                  AND t2.app_id = :aid 
                                  AND t2.plat_id = :pid";
            $command = $connection->createCommand($sqlStatement);
            $command->bindValue(':aid', $this->mRequest->aid);
            $command->bindValue(':pid', $this->mRequest->pid);
            $command->execute();
 
            $sqlStatement = "UPDATE `wave_func_plat_dev` SET app_state = :status WHERE app_id = :aid AND plat_id = :pid";
            $command = $connection->createCommand($sqlStatement);
            $command->bindValue(':status', Platform::APP_STATUS_PUBLISHED);
            $command->bindValue(':aid', $this->mRequest->aid);
            $command->bindValue(':pid', $this->mRequest->pid);
            $command->execute();
            
            $transaction->commit();
            $this->redirect($this->createUrl('audit/audit', array("CALL" => CJSON::encode(array('item' => $this->mRequest->item)))));
        } 
        catch ( Exception $e ) 
        {
            $transaction->rollback();
            echo $e->getMessage();
        }
    }
    
    /**
     * 检查当前用户所分配的角色是否被允许操作
     * 
     * @throws CHttpException
     */
    protected function checkUserIsAllowed()
    {
        $this->_getRequest();
        $items = array();

        if($this->isAllowed === null)
        {
            if(!is_object($this->mRequest))
            {
                throw new CHttpException(404, '抱歉,您访问的页面不存在!');
            }
            
            Yii::import('application.modules.srbac.models.*');
            $userId = Yii::app()->user->id;
            $roles = Helper::getUserAssignedRoles($userId);
            if(is_array($roles) && count($roles))
            {
                foreach ($roles as $role)
                {
                    $items[] = $role->name;
                }
                if(isset($this->mRequest->item))
                {
                    $this->isAllowed = in_array($this->mRequest->item, $items) ? true : false;
                }
            }
            if($this->isAllowed === null || $this->isAllowed === false)
            {
                throw new CHttpException(404, '抱歉,您无权访问此页面!');
            }
        }
    }
    
    /**
     * 加载应用平台信息
     * 
     * @throws CHttpException
     * @return object
     */
    private function _loadAppModel()
    {
        $this->_getRequest();
        
        if (is_object($this->mRequest))
        {
            if ( isset($this->mRequest->aid, $this->mRequest->pid) )
            {
                $condition = "app_state = ".Platform::APP_STATUS_UPGRADE; 
            }
            else 
            {
                $condition = "app_state = ".Platform::APP_STATUS_PENDING; 
            }
            $this->_appModel = Platform::model()->findByPk($this->mRequest->id, $condition);
        }
        if ($this->_appModel === null)
        {
            throw new CHttpException(403, '抱歉,您访问的页面不存在!');
        }
        return $this->_appModel;
    }
    
    /**
     * 加载审核实例信息
     * 
     * @throws CHttpException
     * @return object
     */
    private function _loadCaseModel()
    {
        $this->_getRequest();

        if (is_object($this->mRequest) && isset($this->mRequest->id))
        {
            $condition = "case_id IN ( ".$this->mRequest->id." ) AND case_status IN ( ".Cases::STATUS_DRAFT.','.Cases::STATUS_RETRIAL.','.Cases::STATUS_CHECK.','.Cases::STATUS_UPGRADE." )";
            $this->_caseModel = Cases::model()->findAll($condition);
        }
        if (empty($this->_caseModel))
        {
            throw new CHttpException(402, '抱歉,您访问的页面不存在!');
        }
        return $this->_caseModel;
    }
    
    /**
     * 获取URL请求CALL参数的内容
     * 
     * @throws CHttpException
     */
    private function _getRequest()
    {
        if (!isset($_REQUEST['CALL']))
        {
            throw new CHttpException(404, '抱歉,您访问的页面不存在!');
        }
        else
        {
            $this->mRequest = CJSON::decode($_REQUEST['CALL'], false);
        }
    }
}
?>