public function onAuthenticationSuccess(Request $request, TokenInterface $token) { $username = $token->getUser()->getUsername(); $user = new UserModel(); $website = Website::get(); if (!$user->readByUserName($username) or $user->role != SystemRoles::SYSTEM_ADMIN and !($user->siteRole->offsetExists($website->domain) and $user->siteRole[$website->domain] != SiteRoles::NONE)) { return $this->httpUtils->createRedirectResponse($request, '/app/logout'); } $request->getSession()->set('user_id', $user->id->asString()); $request->getSession()->set('user', array('username' => $username)); $projectId = $user->getDefaultProjectId($website->domain); if ($projectId) { $request->getSession()->set('projectId', $projectId); } $referer = $this->determineTargetUrl($request); if ($referer and strpos($referer, '/app/') !== false) { return $this->httpUtils->createRedirectResponse($request, $referer); } elseif ($projectId) { $project = ProjectModel::getById($projectId); $url = '/app/' . $project->appName . '/' . $projectId; return $this->httpUtils->createRedirectResponse($request, $url); } else { return $this->httpUtils->createRedirectResponse($request, '/'); } }
public function run($userId, $mode = 'test') { $testMode = $mode != 'run'; $message = "Import Other Language Semantic Domain Projects\n\n"; $languages = array('es', 'fa', 'hi', 'id', 'km', 'ko', 'ne', 'pt', 'ru', 'te', 'th', 'ur', 'zh-CN'); //$languages = array('es', 'fa'); $languageData = new LanguageData(); $languageData->read(); foreach ($languages as $lang) { $projectCode = SemDomTransProjectModel::projectCode($lang); $languageName = $languageData->getLanguage($lang)->name; $projectName = SemDomTransProjectModel::projectName($lang, $languageName); $existingProject = new SemDomTransProjectModel(); $existingProject->readByCode($lang); if ($existingProject->id->asString() != '') { $message .= "{$projectName} already exists! Removing...\n"; if (!$testMode) { $existingProject->remove(); } } if (!$testMode) { $projectId = self::_createEmptyProject($lang, $languageName, $userId); $projectModel = ProjectModel::getById($projectId); $xmlFilePath = APPPATH . "resources/languageforge/semdomtrans/LocalizedLists-{$lang}.xml"; $projectModel->importFromFile($xmlFilePath); } $message .= "Finished importing the {$projectName} \n"; } return $message; }
/** * @param string $projectId * @param string $userId * @param Website $website * @return array */ public static function getSessionData($projectId, $userId, $website) { $sessionData = array(); $sessionData['userId'] = (string) $userId; $sessionData['baseSite'] = $website->base; // Rights $user = new UserModel($userId); $sessionData['userSiteRights'] = $user->getRightsArray($website); if ($projectId) { $project = ProjectModel::getById($projectId); $sessionData['project'] = array(); $sessionData['project']['id'] = (string) $projectId; $sessionData['project']['projectName'] = $project->projectName; $sessionData['project']['appName'] = $project->appName; $sessionData['project']['appLink'] = "/app/{$project->appName}/{$projectId}/"; $sessionData['project']['ownerRef'] = $project->ownerRef->asString(); $sessionData['project']['slug'] = $project->databaseName(); $sessionData['userProjectRights'] = $project->getRightsArray($userId); $sessionData['projectSettings'] = $project->getPublicSettings($userId); } // File Size $postMax = self::fromValueWithSuffix(ini_get("post_max_size")); $uploadMax = self::fromValueWithSuffix(ini_get("upload_max_filesize")); $fileSizeMax = min(array($postMax, $uploadMax)); $sessionData['fileSizeMax'] = $fileSizeMax; //return JsonEncoder::encode($sessionData); // This is handled elsewhere return $sessionData; }
/** * * @param Website $website * @return ProjectModel */ public static function getDefaultProject($website) { $project = new ProjectModel(); if ($project->readByProperties(array('projectCode' => $website->defaultProjectCode, 'siteName' => $website->domain))) { return ProjectModel::getById($project->id->asString()); } else { return null; } }
/** * @param string $projectId * @param string $userId * @returns array - the DTO array */ public static function encode($projectId) { $projectModel = ProjectModel::getById($projectId); $list = $projectModel->listUsers(); $data = array(); $data['userCount'] = $list->count; $data['users'] = $list->entries; $data['project'] = array('roles' => $projectModel->getRolesList(), 'ownerRef' => $projectModel->ownerRef, 'name' => $projectModel->projectName, 'appLink' => "/app/{$projectModel->appName}/{$projectId}/"); return $data; }
public function testUserCanAccessMethod_projectPageDto_NotAMember_false() { $userId = $this->environ->createUser('user', 'user', '*****@*****.**', SystemRoles::USER); $project = $this->environ->createProject('projectForTest', 'projTestCode'); $project->appName = 'sfchecks'; $project->write(); $projectId = $project->id->asString(); $project = ProjectModel::getById($projectId); $rh = new RightsHelper($userId, $project, $this->environ->website); $result = $rh->userCanAccessMethod('project_pageDto', array()); $this->assertFalse($result); }
public function testInitializeNewProject_defaultPartOfSpeechOptionListExists() { $e = new LexiconMongoTestEnvironment(); $e->clean(); $project = $e->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE); $optionlists = new \Api\Model\Languageforge\Lexicon\LexOptionListListModel($project); $optionlists->read(); $this->assertEqual(count($optionlists->entries), 0); $project = \Api\Model\ProjectModel::getById($project->id->asString()); $project->initializeNewProject(); $optionlists->read(); $this->assertTrue(count($optionlists->entries) > 0); $this->assertEqual($optionlists->entries[0]['items'][0]['key'], 'adj'); }
public static function encode($projectId) { $dto = array(); $project = ProjectModel::getById($projectId); $methodPrefix = 'project_management_report_' . $project->appName . '_'; $reports = array(); foreach (get_class_methods('\\Api\\Service\\Sf') as $methodName) { if (strpos($methodName, $methodPrefix) !== FALSE) { $reportId = explode($methodPrefix, $methodName)[1]; $displayName = ucwords(preg_replace('/([A-Z])/', ' $1', $reportId)); $reportId = $project->appName . '_' . $reportId; array_push($reports, array('id' => $reportId, 'name' => $displayName)); } } $dto['reports'] = $reports; return $dto; }
/** * Update the user project role in the project * @param string $projectId * @param string $userId * @param string $projectRole * @return string - userId */ public static function updateUserRole($projectId, $userId, $projectRole = ProjectRoles::CONTRIBUTOR) { CodeGuard::checkNotFalseAndThrow($projectId, '$projectId'); CodeGuard::checkNotFalseAndThrow($userId, 'userId'); //CodeGuard::assertInArrayOrThrow($role, array(ProjectRoles::CONTRIBUTOR, ProjectRoles::MANAGER)); // Add the user to the project $user = new UserModel($userId); $project = ProjectModel::getById($projectId); if ($userId == $project->ownerRef->asString()) { throw new \Exception("Cannot update role for project owner"); } // TODO: Only trigger activity if this is the first time they have been added to project $usersDto = ProjectCommands::usersDto($projectId); if (!$project->users->offsetExists($userId)) { ActivityCommands::addUserToProject($project, $userId); } $project->addUser($userId, $projectRole); $user->addProject($projectId); $project->write(); $user->write(); return $userId; }
<?php require_once 'e2eTestConfig.php'; use Api\Model\Languageforge\Lexicon\LexiconProjectModel; use Api\Model\Languageforge\Lexicon\Command\LexUploadCommands; use Api\Model\ProjectModel; use Palaso\Utilities\FileUtilities; $constants = json_decode(file_get_contents(TestPath . '/testConstants.json'), true); // cleanup test assets folder $project = new ProjectModel(); $project->readByProperties(array('projectCode' => $constants['testProjectCode'])); $testProject = $project->getById($project->id->asString()); $assetsFolderPath = $testProject->getAssetsFolderPath(); FileUtilities::removeFolderAndAllContents($assetsFolderPath); // cleanup mocked uploaded zip import (jpg file) $tmpFilePath = sys_get_temp_dir() . '/' . $constants['testMockJpgImportFile']['name']; @unlink($tmpFilePath); // cleanup mocked uploaded zip import (zip file) $tmpFilePath = sys_get_temp_dir() . '/' . $constants['testMockZipImportFile']['name']; @unlink($tmpFilePath);
public function checkPermissions($methodName, $params) { if (!self::isAnonymousMethod($methodName)) { if (!$this->_userId) { throw new UserNotAuthenticatedException("Your session has timed out. Please login again."); } try { $projectModel = ProjectModel::getById($this->_projectId); } catch (\Exception $e) { $projectModel = null; } $rightsHelper = new RightsHelper($this->_userId, $projectModel, $this->_website); if (!$rightsHelper->userCanAccessMethod($methodName, $params)) { throw new UserUnauthorizedException("Insufficient privileges accessing API method '{$methodName}'"); } } }
public static function UserEngagementReport($projectId) { $project = ProjectModel::getById($projectId); $output = str_pad('**** User Engagement Report ****', 120, " ", STR_PAD_BOTH) . "\n"; $output .= str_pad(date(DATE_RFC2822), 120, " ", STR_PAD_BOTH) . "\n\n"; $data = array(); $activeUsers = array(); $managerUsers = array(); $inactiveUsers = array(); $invalidUsers = array(); $listModel = new UserList_ProjectModel($projectId); $listModel->read(); if ($listModel->count > 0) { $textListModel = new TextListModel($project); $textListModel->read(); $questions = array(); foreach ($textListModel->entries as $text) { $questionListModel = new QuestionAnswersListModel($project, $text['id']); $questionListModel->read(); $questions = array_merge($questions, array_map(function ($q) use($text) { $q['textRef'] = $text['id']; return $q; }, $questionListModel->entries)); } $answerCtr = 0; $commentCtr = 0; foreach ($listModel->entries as $user) { $userModel = new UserModel($user['id']); $user['isActive'] = $userModel->active; $user['questions'] = 0; $user['texts'] = 0; $user['answers'] = 0; $user['comments'] = 0; $user['responses'] = 0; $user['textIds'] = array(); if (!$user['isActive']) { if (!$user['email']) { $user['email'] = $userModel->emailPending; } array_push($invalidUsers, $user); continue; } if ($project->users->offsetExists($user['id'])) { $user['role'] = $project->users[$user['id']]->role; } else { $user['role'] = ProjectRoles::NONE; } $answerCtr = 0; $commentCtr = 0; foreach ($questions as $question) { $responses = 0; foreach ($question['answers'] as $answer) { if (!$answer['content']) { continue; } $answerCtr++; foreach ($answer['comments'] as $comment) { if (!$comment['content']) { continue; } $commentCtr++; if ($comment['userRef'] && $comment['userRef']->{'$id'} == $user['id']) { $user['comments']++; $user['responses']++; array_push($user['textIds'], $question['textRef']); $responses++; } } if ($answer['userRef'] && $answer['userRef']->{'$id'} == $user['id']) { $user['answers']++; $user['responses']++; array_push($user['textIds'], $question['textRef']); $responses++; } } if ($responses > 0) { $user['questions']++; } } $user['texts'] = count(array_unique($user['textIds'])); if ($user['role'] == ProjectRoles::MANAGER) { array_push($managerUsers, $user); } elseif ($user['responses'] > 0) { array_push($activeUsers, $user); } else { array_push($inactiveUsers, $user); } } $output .= $project->projectName . " Project\n"; $output .= "Texts (T's) in Project: " . $textListModel->count . "\n"; $output .= "Questions (Q's) in Project: " . count($questions) . "\n"; $output .= "Responses (R's) in Project (Answers + Comments): " . ($answerCtr + $commentCtr) . "\n"; $output .= "Answers (A's) in Project: " . $answerCtr . "\n"; $output .= "Comments (C's) in Project: " . $commentCtr . "\n"; } else { $output .= "This project has no users\n\n"; } $sortByResponses = function ($a, $b) { if ($a['responses'] > $b['responses']) { return -1; } elseif ($a['responses'] < $b['responses']) { return 1; } else { if ($a['answers'] > $b['answers']) { return -1; } elseif ($a['answers'] < $b['answers']) { return 1; } else { if ($a['comments'] > $b['comments']) { return -1; } elseif ($a['comments'] < $b['comments']) { return 1; } else { return strcmp($a['username'], $b['username']); } } } }; $sortByName = function ($a, $b) { return strcasecmp($a['name'], $b['name']); }; usort($activeUsers, $sortByResponses); usort($managerUsers, $sortByResponses); usort($inactiveUsers, $sortByName); usort($invalidUsers, $sortByName); $output .= "\n\nManagers: " . count($managerUsers) . "\n" . str_pad("Name", 30) . str_pad("Email", 35) . str_pad("Username", 25) . str_pad("R's", 5) . str_pad("A's", 5) . str_pad("C's", 5) . str_pad("Q's", 5) . str_pad("T's", 5) . "\n\n"; foreach ($managerUsers as $user) { $output .= str_pad($user['name'], 30) . str_pad($user['email'], 35) . str_pad($user['username'], 25) . str_pad($user['responses'], 5) . str_pad($user['answers'], 5) . str_pad($user['comments'], 5) . str_pad($user['questions'], 5) . str_pad($user['texts'], 5) . "\n"; } $output .= "\n\nActive Users: " . count($activeUsers) . "\n" . str_pad("Name", 30) . str_pad("Email", 35) . str_pad("Username", 25) . str_pad("R's", 5) . str_pad("A's", 5) . str_pad("C's", 5) . str_pad("Q's", 5) . str_pad("T's", 5) . "\n\n"; foreach ($activeUsers as $user) { $output .= str_pad($user['name'], 30) . str_pad($user['email'], 35) . str_pad($user['username'], 25) . str_pad($user['responses'], 5) . str_pad($user['answers'], 5) . str_pad($user['comments'], 5) . str_pad($user['questions'], 5) . str_pad($user['texts'], 5) . "\n"; } $output .= "\n\nInactive Users (never engaged): " . count($inactiveUsers) . "\n" . str_pad("Name", 30) . str_pad("Email", 35) . str_pad("Username", 25) . "\n\n"; foreach ($inactiveUsers as $user) { $output .= str_pad($user['name'], 30) . str_pad($user['email'], 35) . str_pad($user['username'], 25) . "\n"; } $output .= "\n\nInvited Users (but never validated or logged in): " . count($invalidUsers) . "\n" . str_pad("Name", 30) . str_pad("Email", 35) . str_pad("Username", 25) . "\n\n"; foreach ($invalidUsers as $user) { $output .= str_pad($user['name'], 30) . str_pad($user['email'], 35) . str_pad($user['username'], 25) . "\n"; } $data['output'] = $output; $data['result'] = array('managerUsers' => $managerUsers, 'activeUsers' => $activeUsers, 'inactiveUsers' => $inactiveUsers, 'invitedUsers' => $invalidUsers); return $data; }