/**
  * Create a project, checking permissions as necessary
  * @param string $projectName
  * @param string $projectCode
  * @param string $appName
  * @param string $userId
  * @param Website $website
  * @param array $srProject send receive project data
  * @return string - projectId
  */
 public static function createProject($projectName, $projectCode, $appName, $userId, $website, $srProject = null)
 {
     // Check for unique project code
     if (ProjectCommands::projectCodeExists($projectCode)) {
         return false;
     }
     $project = new ProjectModel();
     $project->projectName = $projectName;
     $project->projectCode = $projectCode;
     $project->appName = $appName;
     $project->siteName = $website->domain;
     $project->ownerRef->id = $userId;
     $project->addUser($userId, ProjectRoles::MANAGER);
     $projectId = $project->write();
     if ($srProject) {
         SendReceiveCommands::updateSRProject($projectId, $srProject);
     }
     $user = new UserModel($userId);
     $user->addProject($projectId);
     $user->write();
     $project = ProjectModel::getById($projectId);
     $project->initializeNewProject();
     ActivityCommands::addUserToProject($project, $userId);
     return $projectId;
 }
 public function getProjectMember($projectId, $userName)
 {
     $userId = $this->createUser($userName, $userName, '*****@*****.**');
     $user = new UserModel($userId);
     $user->addProject($projectId);
     $user->write();
     $project = new ProjectModel($projectId);
     $project->addUser($userId, ProjectRoles::CONTRIBUTOR);
     $project->write();
     return $userId;
 }
 /**
  * @expectedException Api\Library\Shared\Palaso\Exception\UserUnauthorizedException
  */
 public function testDeleteProjects_NotProjectOwner_Throw()
 {
     self::$environ->clean();
     $user1Id = self::$environ->createUser("user1name", "User1 Name", "*****@*****.**");
     $user2Id = self::$environ->createUser("user2name", "User2 Name", "*****@*****.**");
     $user1 = new UserModel($user1Id);
     $user2 = new UserModel($user2Id);
     $projectId = ProjectCommands::createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE, SfProjectModel::SFCHECKS_APP, $user1->id->asString(), self::$environ->website);
     $project = new ProjectModel($projectId);
     $project->addUser($user2->id->asString(), ProjectRoles::MANAGER);
     ProjectCommands::deleteProjects(array($projectId), $user2Id);
 }
 /**
  * @expectedException Exception
  */
 public function testGetRightsArray_Exception()
 {
     $userId = MongoTestEnvironment::mockId();
     $project = new ProjectModel();
     $project->addUser($userId, ProjectRoles::MANAGER);
     // rolesClass undefined in base ProjectModel
     $project->getRightsArray($userId);
 }
 /**
  * Sends an email to invite emailee to join the project
  * @param string $projectId
  * @param string $inviterUserId
  * @param Website $website
  * @param string $toEmail
  * @param DeliveryInterface $delivery
  * @throws \Exception
  * @return string $userId
  */
 public static function sendInvite($projectId, $inviterUserId, $website, $toEmail, DeliveryInterface $delivery = null)
 {
     $newUser = new UserModel();
     $inviterUser = new UserModel($inviterUserId);
     $project = new ProjectModel($projectId);
     $newUser->emailPending = $toEmail;
     // Check if email already exists in an account
     $identityCheck = UserCommands::checkIdentity('', $toEmail, $website);
     if ($identityCheck->emailExists) {
         $newUser->readByProperty('email', $toEmail);
     }
     // Make sure the user exists on the site
     if (!$newUser->hasRoleOnSite($website)) {
         $newUser->siteRole[$website->domain] = $website->userDefaultSiteRole;
     }
     // Determine if user is already a member of the project
     if ($project->userIsMember($newUser->id->asString())) {
         return $newUser->id;
     }
     // Add the user to the project
     $newUser->addProject($project->id->asString());
     $userId = $newUser->write();
     $project->addUser($userId, ProjectRoles::CONTRIBUTOR);
     $project->write();
     if (!$identityCheck->emailExists) {
         // Email communication with new user
         Communicate::sendInvite($inviterUser, $newUser, $project, $website, $delivery);
     } else {
         // Tell existing user they're now part of the project
         Communicate::sendAddedToProject($inviterUser, $newUser, $project, $website, $delivery);
     }
     return $userId;
 }