Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create trusted registration vouchers #724

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,228 changes: 1,228 additions & 0 deletions ci/files/db_0.13.0.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ci/files/prepare.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

cd /var/www/html/hashtopolis
cd /var//html/hashtopolis
git checkout $1
chown -R www-data /var/www/html/hashtopolis
chmod -R 0777 /var/www/html/hashtopolis/src/inc/
14 changes: 13 additions & 1 deletion src/dba/models/RegVoucher.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ class RegVoucher extends AbstractModel {
private $regVoucherId;
private $voucher;
private $time;
private $trusted;

function __construct($regVoucherId, $voucher, $time) {
function __construct($regVoucherId, $voucher, $time, $trusted) {
$this->regVoucherId = $regVoucherId;
$this->voucher = $voucher;
$this->time = $time;
$this->trusted = $trusted;
}

function getKeyValueDict() {
$dict = array();
$dict['regVoucherId'] = $this->regVoucherId;
$dict['voucher'] = $this->voucher;
$dict['time'] = $this->time;
$dict['trusted'] = $this->trusted;

return $dict;
}
Expand Down Expand Up @@ -61,8 +64,17 @@ function getTime() {
function setTime($time) {
$this->time = $time;
}

function getTrusted() {
return $this->trusted;
}

function setTrusted($trusted) {
$this->trusted = $trusted;
}

const REG_VOUCHER_ID = "regVoucherId";
const VOUCHER = "voucher";
const TIME = "time";
const TRUSTED= "trusted";
}
4 changes: 2 additions & 2 deletions src/dba/models/RegVoucherFactory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getCacheValidTime() {
* @return RegVoucher
*/
function getNullObject() {
$o = new RegVoucher(-1, null, null);
$o = new RegVoucher(-1, null, null, false);
return $o;
}

Expand All @@ -33,7 +33,7 @@ function getNullObject() {
* @return RegVoucher
*/
function createObjectFromDict($pk, $dict) {
$o = new RegVoucher($dict['regVoucherId'], $dict['voucher'], $dict['time']);
$o = new RegVoucher($dict['regVoucherId'], $dict['voucher'], $dict['time'], $dict['trusted']);
return $o;
}

Expand Down
3 changes: 2 additions & 1 deletion src/dba/models/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@
$CONF['RegVoucher'] = [
'regVoucherId',
'voucher',
'time'
'time',
'trusted'
];
$CONF['RightGroup'] = [
'rightGroupId',
Expand Down
3 changes: 2 additions & 1 deletion src/inc/api/APIRegisterAgent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function execute($QUERY = array()) {

//create access token & save agent details
$token = Util::randomString(10);
$agent = new Agent(null, $name, "", -1, "", "", 0, 1, 0, $token, PActions::REGISTER, time(), Util::getIP(), null, 0, "");
$isTrusted = $voucher->getTrusted();
$agent = new Agent(null, $name, "", -1, "", "", 0, 1, $isTrusted, $token, PActions::REGISTER, time(), Util::getIP(), null, 0, "");

if (SConfig::getInstance()->getVal(DConfig::VOUCHER_DELETION) == 0) {
Factory::getRegVoucherFactory()->delete($voucher);
Expand Down
2 changes: 1 addition & 1 deletion src/inc/handlers/AgentHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function handle($action) {
break;
case DAgentAction::CREATE_VOUCHER:
AccessControl::getInstance()->checkPermission(DAgentAction::CREATE_VOUCHER_PERM);
AgentUtils::createVoucher($_POST["newvoucher"]);
AgentUtils::createVoucher($_POST["newvoucher"], $_POST["trusted"]);
break;
case DAgentAction::DELETE_VOUCHER:
AccessControl::getInstance()->checkPermission(DAgentAction::DELETE_VOUCHER_PERM);
Expand Down
5 changes: 3 additions & 2 deletions src/inc/utils/AgentUtils.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,15 +536,16 @@ public static function setActive($agentId, $active, $user, $toggle = false) {
* @param string $newVoucher
* @throws HTException
*/
public static function createVoucher($newVoucher) {
public static function createVoucher($newVoucher, $isTrusted) {
$qF = new QueryFilter(RegVoucher::VOUCHER, $newVoucher, "=");
$check = Factory::getRegVoucherFactory()->filter([Factory::FILTER => $qF]);
if ($check != null) {
throw new HTException("Same voucher already exists!");
}

$key = htmlentities($newVoucher, ENT_QUOTES, "UTF-8");
$voucher = new RegVoucher(null, $key, time());
$trusted = ($isTrusted) ? 1 : 0;
$voucher = new RegVoucher(null, $key, time(), $trusted);
Factory::getRegVoucherFactory()->save($voucher);
}

Expand Down
3 changes: 2 additions & 1 deletion src/install/hashtopolis.sql
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,8 @@ CREATE TABLE `Pretask` (
CREATE TABLE `RegVoucher` (
`regVoucherId` INT(11) NOT NULL,
`voucher` VARCHAR(100) NOT NULL,
`time` BIGINT NOT NULL
`time` BIGINT NOT NULL,
`trusted` TINYINT(4) NOT NULL
) ENGINE = InnoDB;

CREATE TABLE `RightGroup` (
Expand Down
6 changes: 6 additions & 0 deletions src/install/updates/update_v0.12.x_v0.x.x.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,9 @@
$EXECUTED["v0.12.x_agentBinariesUpdateTrack"] = true;
}

if (!isset($PRESENT["v0.12.x_TrustedVoucher"])) {
if (!Util::databaseColumnExists("RegVoucher", "trusted")) {
Factory::getAgentFactory()->getDB()->query("ALTER TABLE `RegVoucher` ADD `trusted` TINYINT(4) NOT NULL;");
}
$EXECUTED["v0.12.x_TrustedVoucher"] = true;
}
4 changes: 4 additions & 0 deletions src/templates/agents/new.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ <h3>Vouchers</h3>
<tr>
<td><code>[[voucher.getVoucher()]]</code></td>
<td>[[date([[config.getVal(DConfig::TIME_FORMAT)]], [[voucher.getTime()]])]]</td>
<td>[[voucher.getTrusted() ? "Trusted" : "Untrusted"]]</td>
<td>
<form action="agents.php?new=true" method="POST" onSubmit="if (!confirm('Really delete this voucher?')) return false;">
<input type="hidden" name="action" value="[[$DAgentAction::DELETE_VOUCHER]]">
Expand All @@ -75,6 +76,9 @@ <h3>Vouchers</h3>
<td>
<input type="text" class='form-control' name="newvoucher" value="[[Util::randomString(8)]]" title="Voucher Code">
</td>
<td>
<input type="checkbox" name="trusted" value="[[false]]" title="Trusted Voucher"><label for="trusted">Automatically trust agents?</label>
</td>
<td>
<input type="submit" class='btn {{IF [[toggledarkmode]] > 0}}btn-primary{{ELSE}}btn-light{{ENDIF}}' value="Create">
</td>
Expand Down