public function testDeleteUser() { // Find the first user (root) $model = User::model()->findByPk(1); // Try to delete - assume failed (can't delete standard user root) $this->assertFalse($model->delete()); // Find the second user (devel) $model = User::model()->findByPk(2); // Delete - assume success $this->assertTrue($model->delete()); // Ensure there are no project roles for user #2 $roles = UserProjectAccess::model()->findAll('user_id=2'); $this->assertTrue(count($roles) == 0); }
public function testCreateNew() { // Create new record $model = new UserProjectAccess(); $model->user_id = 1; // root $model->project_id = 2; // wpack $model->usergroup_id = 1; // Admin // Apply changes $this->assertTrue($model->save()); // Find newly created record $model = UserProjectAccess::model()->findAll('user_id=1'); $this->assertTrue($model != null); }
/** * 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)); }
/** * 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; }
/** * 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 }