Merge branch 'AddPoints'

bug-1390
Michael Tänzer 14 years ago
commit 6cf8a11300

@ -0,0 +1,182 @@
#!/usr/bin/php
<?php
/**
* Script to create more dummy assurer accounts (for use on the test system)
* which can be used by the test manager to create automated assurances
*
* @author Michael Tänzer
*/
class CreateAssurers {
protected $db;
// define constants for the positions in $argv
const ARGV_SCRIPT = 0;
const ARGV_DB_HOST = 1;
const ARGV_DB_USER = 2;
const ARGV_DB_PASSWD = 3;
const ARGV_DB_NAME = 4;
const ARGV_QUANTITY = 5;
// required length of $argv
const ARGV_LENGTH = 6;
function __construct(mysqli $db){
$this->db = $db;
}
public static function echoUsage($script_name){
print 'Usage: ' . $script_name; ?> <db_host> <db_user> <db_passwd>
<db_name> <quantity>
Script to create more dummy assurer accounts (for use on the test system)
which can be used by the test manager to create automated assurances
<quantity> specifies how many new dummy accounts should be created
<?php
}
public static function main(){
$argc = $_SERVER['argc'];
$argv = $_SERVER['argv'];
if ($argc != self::ARGV_LENGTH){
self::echoUsage($argv[self::ARGV_SCRIPT]);
fwrite(STDERR, "Error: Invalid number of parameters\n");
exit(1);
}
$quantity = (int)($argv[self::ARGV_QUANTITY]);
if (!is_numeric($argv[self::ARGV_QUANTITY]) || $quantity < 0){
self::echoUsage($argv[self::ARGV_SCRIPT]);
fwrite(STDERR, "Error: Last parameter has to be a positive integer\n");
exit(2);
}
// Try to connect to the database
$db = new mysqli($argv[self::ARGV_DB_HOST], $argv[self::ARGV_DB_USER],
$argv[self::ARGV_DB_PASSWD], $argv[self::ARGV_DB_NAME]);
if (mysqli_connect_error()){
fwrite(STDERR, "Error: Could not connect to the data base\n".
"(".mysqli_connect_errno()."): ".mysqli_connect_error());
exit(3);
}
$me = new self($db);
$status = $me->createAssurers($quantity);
$db->close();
exit($status);
}
public function createAssurers($quantity){
// get last assurer
$result = $this->db->query('select `mname` from `users` where `id`=
(select max(`id`) from `users` where `email` like
\'john.doe-___@example.com\')');
$row = $result->fetch_assoc();
if ($row === NULL){
$last_assurer = 0;
printf("1\n");
} else {
$last_assurer = (int)($row['mname']);
printf("2: \$last_assurer: %d\n", $last_assurer);
}
// prepare the statements
$insert_user = $this->db->prepare('insert into `users` set
`email` = ? ,
`password` = \'invalid\' ,
`fname` = ? ,
`mname` = ? ,
`lname` = \'Doe\' ,
`suffix` = \'\' ,
`dob` = ? ,
`Q1` = SHA1(rand()) ,
`Q2` = SHA1(rand()) ,
`Q3` = SHA1(rand()) ,
`Q4` = SHA1(rand()) ,
`Q5` = SHA1(rand()) ,
`A1` = SHA1(rand()) ,
`A2` = SHA1(rand()) ,
`A3` = SHA1(rand()) ,
`A4` = SHA1(rand()) ,
`A5` = SHA1(rand()) ,
`created` = now() ,
`uniqueID` = SHA1(rand()) ,
`verified` = 1 ,
`assurer` = 1 ');
$insert_user->bind_param('ssss', $email, $fname, $mname, $dob);
$insert_email = $this->db->prepare('insert into `email` set
`email` = ? ,
`hash` = \'\' ,
`created` = now() ,
`modified` = now() ,
`memid` = ? ');
$insert_email->bind_param('si', $email, $memid);
$insert_alerts = $this->db->prepare('insert into `alerts` set
`memid` = ? ,
`general` = 0 ,
`country` = 0 ,
`regional` = 0 ,
`radius` = 0 ');
$insert_alerts->bind_param('i', $memid);
$insert_points = $this->db->prepare('insert into `notary` set
`from` = ? ,
`to` = ? ,
`points` = 150 ,
`awarded` = 150 ,
`location` = \'Init Points\' ,
`date` = curdate() ,
`method` = \'Administrative Increase\' ,
`when` = now() ');
$insert_points->bind_param('ii', $memid, $memid);
$insert_cats = $this->db->prepare('insert into `cats_passed` set
`user_id` = ? ,
`pass_date` = now() ,
`variant_id` =
(select `id` from `cats_variant` where `type_id` = 1) ');
$insert_cats->bind_param('i', $memid);
// do the actual work
for ($i = $last_assurer + 1; $i <= $last_assurer + $quantity; $i++){
$email = sprintf('john.doe-%03u@example.com', $i);
$fname = sprintf('John %u', $i);
$mname = sprintf('%u', $i);
$dob = sprintf('19%02u-01-%02u', $i % 90, (int)(($i/90) + 1) );
$insert_user->execute();
$memid = $insert_user->insert_id;
if ($memid == 0){
fwrite(STDERR, "Error: didn't get a valid ID for the user\n");
return 10;
}
$insert_email->execute();
$insert_alerts->execute();
$insert_points->execute();
$insert_cats->execute();
printf('Assurer number %u %s Doe <%s>'."\n".
'born on %s with the ID %d has been added'."\n", $i,
$fname, $email, $dob, $memid);
}
return 0;
}
}
CreateAssurers::main();

@ -0,0 +1,173 @@
<?php
/**
* @author Michael Tänzer
*/
class AddPointsController extends Zend_Controller_Action
{
const MAX_POINTS_PER_ASSURANCE = 35;
const MAX_ASSURANCE_POINTS = 100;
protected $db;
public function init()
{
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini',
APPLICATION_ENV);
$this->db = Zend_Db::factory($config->ca_mgr->db->auth->pdo,
$config->ca_mgr->db->auth);
}
public function indexAction()
{
$this->view->assurance_form = $this->getAssuranceForm();
$this->render('index');
}
public function assuranceAction()
{
// Validate form
if (!$this->getRequest()->isPost()) {
return $this->_forward('index');
}
$form = $this->getAssuranceForm();
if (!$form->isValid($_POST)) {
$this->view->assurance_form = $form;
return $this->render('index');
}
// Form is valid -> get values for processing
$values = $form->getValues();
// Check identity of the user
$session = Zend_Registry::get('session');
if ($session->authdata['authed'] !== true) {
throw new Exception(__METHOD__ . ': you need to log in to use this feature');
}
$query = 'select `id` from `users` where `id` = :user';
$query_params['user'] = $session->authdata['authed_id'];
$result = $this->db->query($query, $query_params);
if ($result->rowCount() !== 1) {
throw new Exception(__METHOD__ . ': user ID not found in the data base');
}
$row = $result->fetch();
$user['id'] = $row['id'];
// Get current points of the user
$query = 'select sum(`points`) as `total` from `notary` where `to` = :user';
$query_params['user'] = $user['id'];
$row = $this->db->query($query, $query_params)->fetch();
if ($row['total'] === NULL) $row['total'] = 0;
$user['points'] = $row['total'];
// Do the actual assurances
$assurance = array(); // Make sure the array is empty
$assurance['to'] = $user['id'];
$assurance['location'] = $values['location'];
$assurance['date'] = $values['date'];
$assurance['when'] = new Zend_Db_Expr('now()');
$this->view->assurancesDone = array();
$quantity = $values['quantity'];
do {
// split up into multiple assurances
if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
$assurance['awarded'] = self::MAX_POINTS_PER_ASSURANCE;
$quantity -= self::MAX_POINTS_PER_ASSURANCE;
} else {
$assurance['awarded'] = $quantity;
$quantity = 0;
}
// Get the assurer for this assurance
$assurance['from'] = $this->getNewAssurer($user['id']);
// only assign points whithin the limit
if ($user['points'] + $assurance['awarded'] > self::MAX_ASSURANCE_POINTS){
$assurance['points'] = self::MAX_ASSURANCE_POINTS - $user['points'];
} else {
$assurance['points'] = $assurance['awarded'];
}
$this->db->insert('notary', $assurance);
$user['points'] += $assurance['points'];
$this->view->assurancesDone[] = $assurance['points'];
} while ($quantity > 0);
// Fix the assurer flag
$query = 'UPDATE `users` SET `assurer` = 1 WHERE `users`.`id` = :user AND '.
'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '.
'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '.
'`cp`.`user_id` = :user) AND '.
'(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '.
'`expire` < now()) >= 100';
$query_params['user'] = $user['id'];
$this->db->query($query, $query_params);
return;
}
/**
* Get the first assurer who didn't already assure the user
*
* @param int $user_id The ID of the user who should get assured
* @return int The ID of the selected assurer
*/
protected function getNewAssurer($user_id)
{
$query = 'select min(`id`) as `assurer` from `users` ' .
'where `email` like \'john.doe-___@example.com\' and ' .
'`id` not in (select `from` from `notary` where `to` = :user)';
$query_params['user'] = $user_id;
$row = $this->db->query($query, $query_params)->fetch();
if ($row['assurer'] === NULL) {
throw new Exception(__METHOD__ . ': no more assurers that haven\'t '.
'already assured this account');
}
return $row['assurer'];
}
protected function getAssuranceForm()
{
$form = new Zend_Form();
$form->setAction('/add-points/assurance')->setMethod('post');
$quantity = new Zend_Form_Element_Text('quantity');
$quantity->setRequired(true)
->setLabel(I18n::_('Number of Points'))
->addFilter(new Zend_Filter_Int())
->addValidator(new Zend_Validate_Between(0, 100));
$form->addElement($quantity);
$location = new Zend_Form_Element_Text('location');
$location->setRequired(true)
->setLabel(I18n::_('Location'))
->setValue(I18n::_('CACert Test Manager'))
->addValidator(new Zend_Validate_StringLength(1,255));
$form->addElement($location);
$date = new Zend_Form_Element_Text('date');
$date->setRequired(true)
->setLabel(I18n::_('Date of Assurance'))
->setValue(date('Y-m-d H:i:s'))
->addValidator(new Zend_Validate_StringLength(1,255));
$form->addElement($date);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel(I18n::_('Assure Me'));
$form->addElement($submit);
return $form;
}
}

@ -0,0 +1,20 @@
<?php
/**
* @author Michael Tänzer
*/
?>
<h1><?php print I18n::_('Points added successfully')?></h1>
<p><?php print I18n::_('The following assurances were added to your account:')?></p>
<table>
<thead>
<tr><th>#</th><th><?php print I18n::_('Number of points')?></th></tr>
</thead>
<tbody>
<?php foreach ($this->assurancesDone as $i => $points) {
printf('<tr><td> %1$d </td><td> %2$d </td></tr>', $i, $points);
}?>
</tbody>
</table>

@ -0,0 +1,14 @@
<?php
/**
* @author Michael Tänzer
*/
?>
<h1><?php print I18n::_('Add Assurance Points to your Account') ?></h1>
<h2><?php print I18n::_('Get Points by Automated Assurance') ?></h2>
<p><?php print I18n::_('Assign the points by doing an automated assurance '.
'which looks just like a normal assurance done by a real person.') ?></p>
<p><?php print I18n::_('If you enter more than 35 points they will be split '.
'into multiple assurances. You can do zero point assurances.') ?></p>
<?php print $this->assurance_form ?>

@ -0,0 +1,53 @@
<?php
require_once (FWACTIONS_PATH . '/FWAction.php');
class AddPoints extends FWAction {
/**
* get a list of required permissions that are needed to access this action
* @return array
*/
public static function getRequiredPermissions() {
return array();
}
/**
* get a role that is required for accessing that action
* @return string
*/
public static function getRequiredRole() {
return 'User';
}
/**
* sort order for top navigation
* @return integer
*/
public static function getTopNavPrio() {
return 50;
}
/**
* controller to invoke
* @return string
*/
public static function getController() {
return 'add-points';
}
/**
* action to invoke
* @return string
*/
public static function getAction() {
return 'index';
}
/**
* get text for menu, caller is responsible for translating
* @return string
*/
public static function getMenuText() {
return 'Add Points';
}
}
Loading…
Cancel
Save