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); }
/** * Adds a user to project. */ public function actionAddUser() { if (!isset($_POST['id'])) { throw new CHttpException(403, 'Invalid request'); } $projectId = $_POST['id']; $project = Project::model()->findByPk($projectId); if ($project === Null) { throw new CHttpException(403, 'Invalid request'); } if (isset($_POST['User'])) { // Get items $items = $_POST['User']; // Get values of checkbox column $checks = array(); if (isset($_POST['check'])) { $checks = $_POST['check']; } // Walk through items foreach ($items as $userId => $item) { // Find user project access record $model = UserProjectAccess::model()->findByAttributes(array('user_id' => $userId, 'project_id' => $projectId)); // Find out if this item is checked or not if (in_array($userId, $checks)) { if ($model === Null) { // Give him access $model = new UserProjectAccess(); $model->user_id = $userId; $model->project_id = $projectId; $model->usergroup_id = $item['usergroup']; if (!$model->save()) { throw new CHttpException(403, 'Error saving record to database'); } } } else { if ($model != Null) { // Revoke access if (!$model->delete()) { throw new CHttpException(403, 'Error deleting record from database'); } } } } Yii::app()->user->setFlash('success', 'User roles have been updated.'); $this->redirect(array('project/view', 'id' => $projectId)); return; } $criteria = new CDbCriteria(); $dataProvider = new CActiveDataProvider('User', array('criteria' => $criteria, 'sort' => array('defaultOrder' => 'username ASC'), 'pagination' => false)); $this->render('addUser', array('project' => $project, 'dataProvider' => $dataProvider)); }
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()); }