public function actionGenerate($id) { $station = $this->loadModel($id); $election = Elections::model()->findAll(); $oldtokens = Tokens::model()->findAll('station_id=:station_id', array(':station_id' => $id)); foreach ($oldtokens as $oldtoken) { Votes::model()->deleteAll('token_id=:token_id', array(':token_id' => $oldtoken->id)); } Tokens::model()->deleteAll('station_id=:station_id', array(':station_id' => $id)); $tokens = array(); for ($i = 0; $i < $station->voters_count; $i++) { $token = new Tokens(); $token->election_id = $election[0]->id; $token->station_id = $id; $token->token = $this->generatePassword(); $prevtoken = Tokens::model()->findAll('token=:token', array(':token' => $token->token)); while ($prevtoken) { $token->token = $this->generatePassword(); $prevtoken = Tokens::model()->findAll('token=:token', array(':token' => $token->token)); } $token->done_vote = 0; $token->save(); $tokens[] = $token; } $this->render('generate', array('election' => $election[0], 'tokens' => $tokens, 'station' => $station)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Tokens(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Tokens'])) { $model->attributes = $_POST['Tokens']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
public function generate(&$user) { if (!is_object($user)) { return false; } //delete all existing tokens $user->tokens->delete(); $secret = openssl_random_pseudo_bytes(32); //generate new token $token = new Tokens(); $token->users_id = $user->id; $token->secret_key = $this->crypt->encryptBase64($secret); $token->created = date('Y-m-d H:i:s'); $token->hmac = hash_hmac('sha256', strtolower($user->username) . '|' . $token->created, $secret, false); //save token information if ($token->save()) { //return encrypted token return $this->crypt->encryptBase64($this->crypt->encrypt($user->username . '|' . $token->created . '|' . $token->hmac)); } return false; }
public function actionGenerate($id) { $election = $this->loadModel($id); $oldtokens = Tokens::model()->findAll('election_id=:election_id', array(':election_id' => $id)); foreach ($oldtokens as $oldtoken) { Votes::model()->deleteAll('token_id=:token_id', array(':token_id' => $oldtoken->id)); } foreach ($election->seats() as $seat) { foreach ($seat->candidates() as $candidate) { $candidate->votescount = 0; $candidate->save(); } } Tokens::model()->deleteAll('election_id=:election_id', array(':election_id' => $id)); $tokens = array(); for ($i = 0; $i < $election->voters_count; $i++) { $token = new Tokens(); $token->election_id = $id; $token->token = $this->generatePassword(); $prevtoken = Tokens::model()->findAll('token=:token', array(':token' => $token->token)); while ($prevtoken) { $token->token = $this->generatePassword(); $prevtoken = Tokens::model()->findAll('token=:token', array(':token' => $token->token)); } $token->done_vote = 0; $token->save(); $tokens[] = $token; } $this->redirect(array('Tokens/index', 'election_id' => $election->id)); }