public function checkAccess() { if (!empty($_GET['id'])) { $id = $_GET['id']; $model = $this->loadOneModel((int) $id); if (!$model) { throw new Exception('ElectionAuthAssignment with id = ' . $id . ' was not found'); } $election = $model->object; } else { $data = $this->data(); $election = Election::model()->findByPk((int) $data['object_id']); } if (!$election) { throw new Exception('Related Election can\'t be fetched'); } $params['election'] = $election; if ($this->action->id == 'restCreate' && Yii::app()->user->checkAccess('election_manageAdmins', $params)) { return true; } if ($this->action->id == 'restDelete' && Yii::app()->user->checkAccess('election_manageAdmins', $params)) { if ($model->right == 'election_creator') { throw new Exception('Election creator can\'t be deprived of power.'); } return true; } return false; }
public function checkAccess() { if (Yii::app()->user->checkAccess('superadmin')) { return true; } if (in_array($this->action->id, array('restDelete', 'restUpdate'))) { $id = (int) $_GET['id']; $group = $this->loadOneModel($id); if (!$group) { throw new CException('VoterGroup with id "' . $id . '" was not found'); } if ($group->type == VoterGroup::TYPE_LOCAL) { $params = array('election' => $group->election); return Yii::app()->user->checkAccess('election_administration', $params); } return false; } if ($this->action->id == 'restCreate') { $data = $this->data(); $type = $data['type']; if ($type == VoterGroup::TYPE_LOCAL) { $election = Election::model()->findByPk((int) $data['election_id']); if (!$election) { throw new CException('Election with id "' . $data['election_id'] . '" was not found'); } $params = array('election' => $election); return Yii::app()->user->checkAccess('election_administration', $params); } return false; } }
public function checkAccess() { if (!empty($_GET['id'])) { $id = $_GET['id']; $model = $this->loadOneModel((int) $id); if (!$model) { throw new Exception('ElectorRegistrationRequest with id = ' . $id . ' was not found'); } $election = $model->election; } else { $data = $this->data(); $election = Election::model()->findByPk((int) $data['election_id']); } if (!$election) { throw new Exception('Related Election can\'t be fetched'); } $params['election'] = $election; if ($this->action->id == 'restCreate' && Yii::app()->user->checkAccess('election_askToBecameElector', $params)) { return true; } if ($this->action->id == 'restUpdate' && Yii::app()->user->checkAccess('election_manage', $params)) { return true; } return false; }
public function testRegisterButtonNotShowsWhenCandidateCanBeRegisteredOnlyByAdmin() { $this->login('*****@*****.**', 'qwerty'); $election = Election::model()->findByPk(4); $election->cand_reg_type = Election::CAND_REG_TYPE_ADMIN; $election->save(false); $this->assertRegisterBtn($present = false, 4); }
public function setElection($election) { if (is_numeric($election)) { $election = Election::model()->findByPk($election); } if (!$election) { throw new CException('Election was not found'); } $this->election = $election; }
public function doRestList() { if (empty($this->plainFilter['election_id'])) { throw new Exception('Election id filter property missed'); } $election = Election::model()->findByPk($this->plainFilter['election_id']); if (!$election) { throw new Exception('Election was not found'); } $criteria = new CDbCriteria(array('condition' => 't.election_id = ' . $election->id)); if (isset($this->plainFilter['user_id'])) { $criteria->mergeWith(array('condition' => 't.user_id = ' . (int) $this->plainFilter['user_id'])); } if (isset($this->plainFilter['candidate_id'])) { $criteria->mergeWith(array('condition' => 't.candidate_id = ' . (int) $this->plainFilter['candidate_id'])); } if (isset($this->plainFilter['accepted_only']) && $this->plainFilter['accepted_only']) { $criteria->addCondition('t.status = ' . Vote::STATUS_PASSED); } $peopleSearch = new PeopleSearch(); if ($name = $this->plainFilter['name']) { $peopleSearch->name = $name; } if ($ageFrom = $this->plainFilter['ageFrom']) { $peopleSearch->ageFrom = $ageFrom; } if ($ageTo = $this->plainFilter['ageTo']) { $peopleSearch->ageTo = $ageTo; } if ($birth_place = $this->plainFilter['birth_place']) { $peopleSearch->birth_place = $birth_place; } if ($gender = $this->plainFilter['gender']) { $peopleSearch->gender = $gender; } $arProvCriteria = $peopleSearch->search('profile')->criteria; if ($arProvCriteria) { $originalCriteria = clone $criteria; $criteria->mergeWith($arProvCriteria); } $results = $this->getModel()->with($this->nestedRelations)->limit($this->restLimit)->offset($this->restOffset)->findAll($criteria); $totalCount = $this->getModel()->with($this->nestedRelations)->count($criteria); $extraData = $totalCount; if (isset($this->plainFilter['candidate_id'])) { if (isset($originalCriteria)) { $criteria = $originalCriteria; } $acceptedCountCritetia = clone $criteria; $acceptedCount = $this->getModel()->with($this->nestedRelations)->count($acceptedCountCritetia->addCondition('t.status = ' . Vote::STATUS_PASSED)); $extraData = array('totalCount' => $totalCount, 'acceptedCount' => $acceptedCount); } $this->outputHelper('Records Retrieved Successfully', $results, $extraData); }
public static function getMultipleResults($electionId) { $total_votes = Election::getTotalVotes($electionId); $multiples = Multiple::getListWithCandidates($electionId); if (empty($multiples)) { return null; } $votes = Vote::getMultipleVotes($electionId); if (empty($votes)) { return 'No multiple chair votes recorded.'; } $total_cast_votes = array(); foreach ($votes as $v) { $key = $v['multipleId']; if (!isset($total_cast_votes[$key])) { $total_cast_votes[$key] = 0; } $total_cast_votes[$key] += $v['votes']; $sorted_votes[$key][$v['candidateId']] = $v['votes']; } foreach ($multiples as $ballot) { $ballot_row['title'] = $ballot['title']; $ballot_row['seats'] = $ballot['seatNumber']; $candidates = array(); foreach ($ballot['candidates'] as $c) { if (isset($sorted_votes[$ballot['id']][$c['id']])) { $vote = $sorted_votes[$ballot['id']][$c['id']]; $template = new \Template(); $template->setModuleTemplate('election', 'Admin/Report/Candidate.html'); $template->add('name', $c['firstName'] . ' ' . $c['lastName']); $percentage = round($vote / $total_cast_votes[$ballot['id']] * 100, 1); $template->add('vote', "{$vote} ({$percentage}%)"); $template->add('picture', $c['picture']); $candidates[$vote . '.' . $c['id']] = $template->get(); } } if (!empty($candidates)) { krsort($candidates); $ballot_row['candidates'] = implode("\n", $candidates); } else { $ballot_row['candidates'] = self::noCandidateVotes(); } $tpl['ballots'][] = $ballot_row; } $template = new \Template(); $template->addVariables($tpl); $template->setModuleTemplate('election', 'Admin/Report/Multiple.html'); return $template->get(); }
public function checkAccess() { if (isset($_GET['id'])) { $model = $this->loadOneModel($id = (int) $_GET['id']); $election = $model->election; } else { $data = $this->data(); $election = Election::model()->findByPk((int) $data['election_id']); } if (!$election) { throw new CException('Related election was not found'); } $params = array('election' => $election); return Yii::app()->user->checkAccess('election_administration', $params); }
public function testRegistration() { $election = Election::model()->findByPk(1); $el2 = Election::model()->findByPk(2); $el2StartElectors = array(2, 3, 4); $this->checkUsersAreElectors(array('1', '2'), $election->id); $this->checkUsersAreElectors($el2StartElectors, $el2->id); $reg = new VoterGroupMembersRegistration($election); $reg->run(); $this->checkUsersAreElectors(array('1', '2', '3', '4', '5'), $election->id); $this->checkUsersAreElectors($el2StartElectors, $el2->id); $reg = new VoterGroupMembersRegistration($el2); $reg->run(); $this->checkUsersAreElectors(array('1', '2', '3', '4', '5'), $election->id); $this->checkUsersAreElectors(array(2, 3, 4, 5, 6), $el2->id); }
public function testRestrictCreatePostForNotParticipants() { $auth = Yii::app()->authManager; $user = Yii::app()->user; $election1 = Election::model()->findByPk(1); //user 5 is not a participant $user->id = 5; $this->assertFalse($auth->checkAccess('createPost', 5, array('disabledRoles' => array('poster'), 'election' => $election1))); // user 6 is participant $user->id = 6; $election1->assignRoleToUser($user->id, 'election_participant'); $this->assertTrue($auth->checkAccess('createPost', 6, array('disabledRoles' => array('poster'), 'election' => $election1))); // user 4 is moderator $user->id = 4; $this->assertTrue($auth->checkAccess('createPost', 4, array('disabledRoles' => array('poster'), 'election' => $election1))); // user ??? is creator ? }
public function testGroupsManagementMenuItemNotShowsForNotAdmins() { $election = Election::model()->findByPk(1); $election->status = Election::STATUS_REGISTRATION; $election->save(); $this->open('election/view/1'); $this->waitForPageToLoad(); $anchorSel = 'link=Voters and Groups Management'; $this->assertElementNotPresent($anchorSel); $this->login('*****@*****.**', 'qwerty'); $this->open('election/view/1'); $this->waitForPageToLoad(); $this->assertElementNotPresent($anchorSel); $this->logout(); $this->login('*****@*****.**', 'qwerty'); $this->open('election/view/1'); $this->waitForPageToLoad(); $this->assertElementPresent($anchorSel); }
public function testCreateMandateMethod() { $candidate = $this->getFixtureManager()->getRecord('candidate', 3); $electedCandidateId = $candidate->id; $election = Election::model()->findByPk($candidate->election_id); $election->quote = 1; $election->status = Election::STATUS_REGISTRATION; $this->assertTrue($election->save()); $election->status = Election::STATUS_ELECTION; $this->assertTrue($election->save()); $vote = new Vote(); $vote->candidate_id = $electedCandidateId; $vote->election_id = $candidate->election_id; $vote->user_id = $election->electors[0]->user_id; $this->assertTrue($vote->save(), print_r($vote->getErrors(), true)); $election->status = Election::STATUS_FINISHED; $election->save(); $mandate = Mandate::model()->findByAttributes(array('election_id' => $candidate->election_id, 'candidate_id' => $candidate->id)); $this->assertEquals($election->mandate, $mandate->name); $this->assertEquals($election->validity, $mandate->validity); }
/** * @dataProvider electionsWithoutVoterGroupsRestrictions */ public function testAutomaticRegistration($electionId) { $this->login('*****@*****.**', 'qwerty'); $this->open('election/view/' . $electionId); $this->waitForPageToLoad(); $this->waitForPresent($regBtnSel = 'css=button#register-candidate'); $this->assertVisible($regBtnSel); $election = Election::model()->findByPk($electionId); if ($election->voter_reg_type == Election::VOTER_REG_TYPE_SELF) { $this->waitForPresent($regElectorBtnSel = 'css=button#register-elector'); $this->assertVisible($regElectorBtnSel); } $this->click($regBtnSel); $this->waitForTextPresent('You have been registered as candidate and elector'); $this->waitForElementNotPresent($regElectorBtnSel); $this->open('election/electorate/' . $electionId); $this->waitForPageToLoad(); $this->waitForElementPresent($electorsContSel = 'css=#dest-tab.active div.items'); $this->waitForCssCount($electorsContSel .= ' > div.user-info', 1); $this->assertElementContainsText($electorsContSel, 'Another User'); }
public function checkAccess() { Yii::app()->authManager->defaultRoles = array_merge(Yii::app()->authManager->defaultRoles, array('election_updateCandidateOwnStatus')); $data = $this->data(); if (!empty($_GET['id'])) { $id = $_GET['id']; $model = $this->loadOneModel((int) $id); if (!$model) { throw new Exception('Candidate with id = ' . $id . ' was not found'); } $election = $model->election; } else { $election = Election::model()->findByPk((int) $data['election_id']); $params['candidate_user_id'] = $data['user_id']; } if (!$election) { throw new Exception('Related Election can\'t be fetched'); } $params['election'] = $election; if ($model) { $params['candidate'] = $model; if (isset($data['status'])) { $params['status'] = $data['status']; } } if ($this->action->id == 'restCreate' && Yii::app()->user->checkAccess('election_createCandidate', $params)) { return true; } if ($this->action->id == 'restDelete' && Yii::app()->user->checkAccess('election_deleteCandidate', $params)) { return true; } if ($this->action->id == 'restUpdate' && Yii::app()->user->checkAccess('election_updateCandidateStatus', $params)) { return true; } return false; }
public function testRevokeVoteFailsWithMessageBecauseOfTimeout() { $this->login("*****@*****.**", "qwerty"); $this->open('election/candidates/1'); $this->waitForPageToLoad("30000"); $voteBox = "css=div.checkbox.vote"; $this->waitForElementPresent($voteBox); //first vote $this->click($voteBox); $this->waitForElementContainsText($voteBox . ' span.value', '✓'); //Simulating that timer is expired $candidate = Candidate::model()->findByAttributes(array('electoral_list_pos' => 1, 'election_id' => 1)); $vote = Vote::model()->findByAttributes(array('candidate_id' => $candidate->id, 'user_id' => 1)); $voted = new DateTime($vote->date); $voted->sub(new DateInterval('PT' . (Election::model()->findByPk(1)->remove_vote_time - 1) . 'M' . '54S')); $voted = $voted->format('Y-m-d H:i:s'); Yii::app()->db->createCommand()->update('vote', array('date' => $voted), 'id = ' . $vote->id); $this->open('election/candidates/1'); $this->sleep(7000); $this->click($voteBox); $this->assertElementPresent($errorBox = 'css=div.flash-messages div.alert-error'); $this->assertElementContainsText($errorBox, 'Action is unavailable because of timeout'); //check all candidates are inactive for voting $this->waitForCssCount($voteBox . '.inactive', 3); }
<?php require 'mods.php'; require 'election.php'; $mods = new Mods(); $mods->startConnection(); $election = new Election(); $election->startReading('candidates'); $action = $mods->escapeString($_POST['action']); if ($action == $mods->getAction(5)) { $position = $mods->escapeString($_POST['position']); $election->connectNow(); echo $election->showVotes($position); } $mods->stopConnection();
/** * Assigns roles on creatrion of Election * * @param Election $model */ protected function assignRoles($model) { $model->assignRoleToUser($model->user_id, 'election_creator'); //to assign admins use //$model->assignRoleToUser($user_id, 'election_admin'); //to assign commentModerators user //$model->assignRoleToUser($user_id, 'election_commentModerator'); }
<?php $this->layout = '//layouts/main'; $election = Election::model()->findByPk(1); ?> <h1>RatesWidget sandbox</h1> <hr> <i>With fetching set of models</i> <div id="something-1"> <p>Lorem ipsum dolor. And comments to it below.</p> <?php $this->widget('RatesMarionetteWidget', array('jsConstructorOptions' => array('targetId' => $election->id, 'targetType' => 'Election', 'targetEl' => '#something-1'), 'show' => array('div'))); ?> </div>
public function testRegisterInElectionWithAddingToGroupWithoutConfirmation() { $electionId = 11; $this->login('*****@*****.**', 'qwerty'); $this->open('election/electorate/' . $electionId); $this->waitForPageToLoad(); $this->waitForCssCount('css=#electoral-list-tab .items div.user-info', 0); $this->click('css=#register-elector'); $this->sleep(500); $this->assertCssCount('css=div.flash-messages div.alert', 0); // Wait for visible modal $this->waitForPresent($modalSel = 'css=.modal'); $this->waitForVisible($modalSel); // With visible local groups to select $election = Election::model()->findByPk($electionId); $availGroups = $election->localVoterGroups; $this->assertGreaterThan(0, $count = count($availGroups)); $this->assertEquals(3, $count); $checkboxSel = 'css=div.modal-body > label.checkbox:nth-of-type({%index%}) > input'; foreach ($availGroups as $index => $group) { $this->assertElementContainsText('css=.modal-body', $group->name); $this->assertEquals($group->id, $this->getAttribute($this->parseSel($checkboxSel, array('index' => $index + 1)), 'value')); } //Check that register button is inactive $this->assertElementAttributeEquals($regBtn = 'css=.modal-footer > button', 'disabled', 'disabled'); // Select several $this->click($this->parseSel($checkboxSel, array('index' => 1))); $this->click($this->parseSel($checkboxSel, array('index' => 2))); //Check that register button was activated $this->assertElementAttributeEquals($regBtn, 'disabled', false); // Press submit button $this->click($regBtn); // Wait for modal hide $this->waitForNotPresent($modalSel); // Wait for #register-elector hide $this->waitForNotPresent('css=#register-elector'); // Wait for notification present $this->assertCssCount('css=div.flash-messages div.alert', 1); $this->assertElementContainsText('css=div.flash-messages div.alert', 'You have been registered as elector.'); $this->click('css=div.flash-messages div.alert a.close'); $this->waitForNotPresent('css=div.flash-messages div.alert'); $this->waitForCssCount('css=#dest-tab .items div.user-info', 1); $this->assertElementContainsText('css=#dest-tab .items div.user-info:nth-of-type(1) a', 'Another User'); // Look into DB and check that Elector really was added to selected groups $this->assertInstanceOf(VoterGroupMember, VoterGroupMember::model()->findByAttributes(array('user_id' => 2, 'voter_group_id' => $availGroups[0]->id))); $this->assertInstanceOf(VoterGroupMember, VoterGroupMember::model()->findByAttributes(array('user_id' => 2, 'voter_group_id' => $availGroups[1]->id))); }
public function testAdminCanCreateWithUnassignedLevelNone() { $election1 = Election::model()->findByPk(1); $election1->unassigned_access_level = Election::UNASSIGNED_CAN_NONE; $election1->save(); $this->authenticate('*****@*****.**', 'qwerty'); $result = $this->xhr('api/Election_comment', '{"target_id":"1","user_id":null,"user":{"user_id":null,"photo":"","displayName":""},"content":"Comment n+4","likes":null,"dislikes":null,"comments":[]}', 'POST', true); //assert created $this->assertTrue((bool) preg_match('~HTTP/1\\.1 2\\d\\d~m', $result)); $this->assertTrue((bool) preg_match('~"success":\\s?"?true"?~m', $result)); }
if ($row['Status'] == 1) { $mods->login($studentNumber, 'Voter'); $mods->sendStatus($mods->getError(0), '../index.php'); } else { if ($row['Status'] == 0) { $mods->sendStatus($mods->getError(3), '../index.php'); } else { if ($row['Status'] == 2) { $mods->sendStatus($mods->getError(4), '../index.php'); } } } } else { $mods->sendStatus($mods->getError(2), '../index.php'); } } else { if ($action == $mods->getAction(2)) { $username = $mods->escapeString($_POST['username']); $password = $mods->escapeString($_POST['password']); $election = new Election(); $xml = $election->startReading('accounts'); if ($election->checkAccount($username, md5($password)) != false) { $type = (string) $election->checkAccount($username, md5($password)); $mods->login($username, $type); $mods->sendStatus($mods->getError(0), '../admin.php'); } else { $mods->sendStatus($mods->getError(6), '../admin.php'); } } } $mods->stopConnection();
/** * Return TRUE if registration request is available to user * for specified election * * @param Election|int $election * @param int $user userId * @return boolean */ public static function isAvailable($election, $user) { $userId = $user; if (is_numeric($election)) { $electionId = $election; $election = Election::model()->findByPk($electionId); } else { $electionId = $election->id; } if (!$election->isElectorsRegistrationOpen()) { return false; } $elector = Elector::model()->findByAttributes(array('user_id' => $userId, 'election_id' => $electionId)); if ($elector) { return false; } $registration = self::model()->findByAttributes(array('user_id' => $userId, 'election_id' => $electionId)); if ($registration) { return false; } return true; }
public static function resetReferendum($electionId, $bannerId) { $referendumIds = Election::getReferendumIds($electionId); if (empty($referendumIds)) { return; } $db = \phpws2\Database::getDB(); $tbl = $db->addTable('elect_referendum_vote'); foreach ($referendumIds as $var) { $id = $var['id']; $hash = StudentFactory::getVoteHash($id, $bannerId); $tbl->addFieldConditional('voterHash', $hash); $tbl->addFieldConditional('referendumId', $id); $tbl->addFieldConditional('electionId', $electionId); $db->delete(); $db->clearConditional(); } }
public function testNotRevokesWithRevoteLimitEqualsToZero() { $election = Election::model()->findByPk(1); $election->revotes_count = 0; $this->assertTrue($election->save()); $vote = new Vote(); $vote->election_id = 1; $vote->candidate_id = 3; $vote->user_id = 1; $this->assertTrue($vote->save()); $this->setExpectedException('Exception', 'Revote limit has been reached'); $vote->status = Vote::STATUS_REVOKED; $this->assertFalse($vote->save()); $this->setExpectedException('Exception', 'Revote limit has been reached'); $vote = new Vote(); $vote->candidate_id = 5; $vote->user_id = 1; $vote->election_id = 1; $this->assertFalse($vote->save()); }
public function postEditElection() { //verify the user input $validator = Validator::make(Input::all(), array('Title' => 'required|max:60', 'Starting_Date' => 'required', 'Clossing_Date' => 'required')); if ($validator->fails()) { return Redirect::route('admin-view-elections-get')->with('globalerror', 'Please Try Again'); } else { $title = Input::get('Title'); $startingdate = date("Y-m-d", strtotime(Input::get('Starting_Date'))); $clossingdate = date("Y-m-d", strtotime(Input::get('Clossing_Date'))); $id = Input::get('Election_ID'); $election = Election::where('id', '=', $id)->first(); $election->Title = $title; $election->Starting_Date = $startingdate; $election->Clossing_Date = $clossingdate; if ($election->save()) { return Redirect::route('admin-view-elections-get')->with('globalsuccess', 'Election Details have been edited'); } } }
public function testAdminCanBlockCandidate() { $elecion = Election::model()->findByPk(5); $elecion->status = Election::STATUS_ELECTION; $elecion->save(false); $this->login('*****@*****.**', 'qwerty'); $this->open('/index-test.php/election/candidates/5/details/8'); $this->waitForPageToLoad(); $this->waitForVisible('css=#candidate-details #controls'); $this->waitForCssCount('css=#controls button', 1); $this->click('css=#controls button.block'); $this->waitForElementContainsText('css=#candidate-info .body > div:nth-of-type(3)', 'Refused'); $this->sleep(250); $this->assertElementNotPresent('css=#controls button'); //check nominations $this->logout(); $this->login('*****@*****.**', 'qwerty'); $this->click("link=My nominations"); $this->waitForPageToLoad(); $this->waitForCssCount('css=#nominations-feed-container .items > div', 2); $this->assertElementContainsText('css=#nominations-feed-container .items > div:nth-of-type(2) h4', 'Election 5'); $this->assertElementContainsText('css=#nominations-feed-container .items > div:nth-of-type(2) .status', 'Blocked'); }
if (!isset($user)) { $app->flash('error', 'You must be logged in to access that page.'); $app->redirect($app->urlFor('homepage')); } else { if ($user->isEC()) { $app->render('create-ballot.html'); } else { $app->flash('error', 'You do not have permission to access that page.'); $app->redirect($app->urlFor('homepage')); } } })->name('create.ballot'); $app->post('/dashboard/create-ballot', function () use($app, $db) { $user = $app->view->get('user'); $data = array(); $app->response->headers->set('Content-Type', 'application/json'); if (!isset($user) || !$user->isEC()) { $data['success'] = false; $data['message'] = 'You do not have permission to perform that action.'; } else { $postData = json_decode($app->request->getBody()); $election = new Election($postData); $election->save($db); if (isset($election->id)) { $data['success'] = true; $data['id'] = $election->id; $data['message'] = 'Successfully created ballot "' . $election->name . '"'; } } $app->response->setBody(json_encode($data)); });
<?php require 'mods.php'; require 'election.php'; $election = new Election(); $mods = new Mods(); $mods->startConnection(); $action = $mods->escapeString($_POST['action']); if ($action == $mods->getAction(3)) { $position = $mods->escapeString($_POST['position']); $election->startReading('candidates'); echo $election->getCandidates($position); } else { if ($action == $mods->getAction(4)) { $sessionID = $mods->getSession('user_id'); $mods->setQuery("SELECT * FROM voters WHERE Voter_ID='{$sessionID}'"); $row = $mods->getResults('array'); if ($row['Status'] == 0) { echo 'Voter not registered.'; } else { if ($row['Status'] == 1) { $ctr = 0; foreach ($_POST['votes'] as $vote) { if ($vote != '') { $mods->setQuery("INSERT INTO votes (Voter_ID, Candidate_ID) VALUES ('{$sessionID}', '{$vote}')"); if ($mods->getCount() > 0) { $ctr++; } } } if ($ctr > 0) {