public function testSearch() { // Create model $model = new UserProjectAccess(); // Search - this will create data provider $dataProvider = $model->search(); // Ensure some data returned $this->assertTrue(count($dataProvider->data) != 0); }
/** * Removes one or several users from project. */ public function actionDeleteUser() { if (!isset($_POST['DeleteRows']) || !isset($_POST['project_id'])) { throw new CHttpException(404, 'The parameter is invalid'); } // Get data from user $deleteRows = $_POST['DeleteRows']; $projectId = $_POST['project_id']; foreach ($deleteRows as $id) { $model = UserProjectAccess::model()->findByPk($id); if ($model === Null) { throw new CHttpException(404, 'The specified record doesn\'t exist in the database.'); } // Delete row from database table if (!$model->delete()) { throw new CHttpException(404, 'The specified record doesn\'t exist in the database or could not be deleted.'); } } Yii::app()->user->setFlash('success', 'User roles have been updated.'); // Redirect to index $this->redirect(array('project/view', 'id' => $projectId)); }
/** * Suggests project role for this user. * @param integer $projectId Project ID. * @return integer Usergroup ID. */ public function suggestProjectRole($projectId) { // Check if user already has a role in the given project. $criteria = new CDbCriteria(); $criteria->compare('user_id', $this->id); $criteria->compare('project_id', $projectId); $userRole = UserProjectAccess::model()->find($criteria); if ($userRole != null) { // Suggest the existing role return $userRole->usergroup_id; } return $this->usergroup; // else suggest this user's group as default role for this project }
/** * This callback is executed before deleting the AR from database. * @return boolean true on success. */ public function beforeDelete() { // Find all users participating in this project $models = UserProjectAccess::model()->findAll('project_id=' . $this->id); // Delete each role associated with this project foreach ($models as $model) { $model->delete(); } // Done return true; }
public function testCanAddProjectRole() { // Find the first user (root) $user = User::model()->find("username='******'"); // Check that root can be added to one project 'wpack' $this->assertTrue($user->canAddProjectRole()); // Add user to the second project 'wpack' $role = new UserProjectAccess(); $role->user_id = $user->id; $role->project_id = 2; $role->usergroup_id = 1; // make him admin $this->assertTrue($role->save()); // Add user to the third project 'WTLDemo' $role = new UserProjectAccess(); $role->user_id = $user->id; $role->project_id = 3; $role->usergroup_id = 1; // make him admin $this->assertTrue($role->save()); // Check that root can be added to any project now $this->assertFalse($user->canAddProjectRole()); }