/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new User('create'); // Uncomment the following line if AJAX validation is needed if (isset($_POST['User'])) { $model->attributes = $_POST['User']; if ($model->validate()) { if ($model->save()) { if (isset($_POST['UserAssignmentForm'])) { foreach ($_POST['UserAssignmentForm'] as $key => $value) { if (is_array($value)) { foreach ($value as $data) { $formModel = new AssignmentForm(); if ($key == 'role') { $formModel->itemname = $data; } if ($formModel->validate() === true) { $authorizer = Yii::app()->authManager; $authorizer->assign($formModel->itemname, $model->primaryKey); $item = $authorizer->getAuthItem($formModel->itemname); } } } } } UserLogs::createLog('create new ' . get_class($model) . ' ' . 'id:' . $model->primaryKey . ' name:' . $model->username); } Yii::app()->user->setFlash('success', 'User sudah ditambah.'); $this->redirect(array('index')); } } else { $model->timezone = 'Asia/Jakarta'; } $this->render('create', array('model' => $model, 'permissionModel' => new UserAssignmentForm())); }
/** * Displays the authorization assignments for an user. */ public function actionUser() { // Create the user model and attach the required behavior $userClass = $this->module->userClass; $model = CActiveRecord::model($userClass)->findByPk($_GET['id']); $this->_authorizer->attachUserBehavior($model); $assignedItems = $this->_authorizer->getAuthItems(null, $model->getId()); $assignments = array_keys($assignedItems); // Make sure we have items to be selected $assignSelectOptions = Rights::getAuthItemSelectOptions(null, $assignments); if ($assignSelectOptions !== array()) { $formModel = new AssignmentForm(); // Form is submitted and data is valid, redirect the user if (isset($_POST['AssignmentForm']) === true) { $formModel->attributes = $_POST['AssignmentForm']; if ($formModel->validate() === true) { // Update and redirect $this->_authorizer->authManager->assign($formModel->itemname, $model->getId()); $item = $this->_authorizer->authManager->getAuthItem($formModel->itemname); $item = $this->_authorizer->attachAuthItemBehavior($item); Yii::app()->user->setFlash($this->module->flashSuccessKey, Rights::t('core', 'Permission :name assigned.', array(':name' => $item->getNameText()))); $this->redirect(array('assignment/user', 'id' => $model->getId())); } } } else { $formModel = null; } // Create a data provider for listing the assignments $dataProvider = new RAuthItemDataProvider('assignments', array('userId' => $model->getId())); // Render the view $this->render('user', array('model' => $model, 'dataProvider' => $dataProvider, 'formModel' => $formModel, 'assignSelectOptions' => $assignSelectOptions)); }
public function save($data = null, $id, $key) { $authorizer = Yii::app()->authManager; if ($this->validate()) { $formModel = new AssignmentForm(); if ($key == 'role') { $formModel->itemname = $data; } if ($formModel->validate() === true) { $authorizer->assign($formModel->itemname, $id); $item = $authorizer->getAuthItem($formModel->itemname); return true; } return false; } else { return false; } }