public function testProjectRemoveUser_ExistingProject_Removed() { $environ = new MongoTestEnvironment(); // setup user and projects $userId = $environ->createUser('jsmith', 'joe smith', '*****@*****.**'); $userModel = new UserModel($userId); $projectModel = $environ->createProject('new project', 'newProjCode'); $projectId = $projectModel->id->asString(); // create the reference $projectModel->addUser($userId, ProjectRoles::CONTRIBUTOR); $userModel->addProject($projectId); $projectModel->write(); $userModel->write(); // assert the reference is there $this->assertArrayHasKey($userId, $projectModel->users, "'{$userId}' not found in project."); $otherProject = new ProjectModel($projectId); $this->assertArrayHasKey($userId, $otherProject->users, "'{$userId}' not found in other project."); // remove the reference $projectModel->removeUser($userId); $userModel->removeProject($projectId); $projectModel->write(); $userModel->write(); // testing $this->assertArrayNotHasKey($userId, $projectModel->users, "'{$userId}' not found in project."); $otherProject = new ProjectModel(self::$savedProjectId); $this->assertArrayNotHasKey($userId, $otherProject->users, "'{$userId}' not found in other project."); new ProjectModel(self::$savedProjectId); }
/** * Removes users from the project (two-way unlink) * @param string $projectId * @param array<string> $userIds * @return string $projectId * @throws \Exception */ public static function removeUsers($projectId, $userIds) { $project = new ProjectModel($projectId); foreach ($userIds as $userId) { // Guard against removing project owner if ($userId != $project->ownerRef->id) { $user = new UserModel($userId); $project->removeUser($user->id->asString()); $user->removeProject($project->id->asString()); $project->write(); $user->write(); } else { throw new \Exception("Cannot remove project owner"); } } return $projectId; }
public function run($mode = 'test') { $testMode = $mode == 'test'; $message = ""; $userlist = new UserListModel(); $userlist->read(); $userIds = array_map(function ($e) { return $e['id']; }, $userlist->entries); $projectlist = new ProjectListModel(); $projectlist->read(); $projectIds = array_map(function ($e) { return $e['id']; }, $projectlist->entries); $deadProjectLinks = 0; foreach ($userlist->entries as $userParams) { // foreach existing user $userId = $userParams['id']; $user = new UserModel($userId); $userProjectRefs = $user->projects->refs; foreach ($userProjectRefs as $ref) { // foreach project the user belongs to if (!in_array($ref, $projectIds)) { $user->removeProject($ref); // remove dead project link $deadProjectLinks++; $message .= "Removed dead project link {$ref} from user {$userId}\n"; } } if (!$testMode) { $user->write(); } } if ($deadProjectLinks > 0) { $message .= "\n\nRemoved {$deadProjectLinks} dead project links from the users collection\n\n"; } else { $message .= "\n\nNo dead project links were found\n\n"; } $deadUserLinks = 0; foreach ($projectlist->entries as $projectParams) { // foreach existing project $projectId = $projectParams['id']; $project = new ProjectModel($projectId); $projectUserRefs = array_keys($project->users); foreach ($projectUserRefs as $ref) { // foreach user that is a member of this project if (!in_array($ref, $userIds)) { $project->removeUser($ref); // remove dead user link $deadUserLinks++; $message .= "Removed dead user link {$ref} for project {$projectId}\n"; } } if (!$testMode) { $project->write(); } } if ($deadUserLinks > 0) { $message .= "\n\nRemoved {$deadUserLinks} dead user links from the projects collection\n\n"; } else { $message .= "\n\nNo dead user links were found\n\n"; } return $message; }
/** * Removes this project from the collection. * User references to this project are also removed */ public function remove() { foreach ($this->users as $userId => $roleObj) { $user = new UserModel($userId); $user->removeProject($this->id->asString()); $user->write(); } $this->cleanup(); MapperUtils::dropAllCollections($this->databaseName()); MapperUtils::drop($this->databaseName()); ProjectModelMongoMapper::instance()->remove($this->id->asString()); }