protected function execute() { $db = $this->getContext()->getDB(); $id = $this->getOption('id'); // Verify parameters if (!$id) { $this->error('--id is required.'); } $checkId = $db->getOne(str_queryf('SELECT id FROM projects WHERE id = %s;', $id)); if (!$checkId || $checkId !== $id) { $this->error('Project "' . $id . '" does not exist.'); } if (!$this->getOption('quick')) { $this->timeWarningForScriptWill('invalidate the existing token'); } // New token $authToken = LoginAction::generateRandomHash(40); $authTokenHash = sha1($authToken); $isUpdated = $db->query(str_queryf('UPDATE projects SET auth_token = %s WHERE id = %s LIMIT 1;', $authTokenHash, $id)); if (!$isUpdated) { $this->error('Updating of row into database failed.'); } $this->out('Authentication token of project "' . $id . '" has been succesfully refreshed!' . PHP_EOL . 'The following auth token has been generated for this project:' . PHP_EOL . $authToken . PHP_EOL . PHP_EOL); }
/** * @param string $id * @param array $options * @return array Exposes the new auth token */ public function create($id, array $options = null) { $db = $this->getContext()->getDB(); $password = isset($options['password']) ? $options['password'] : null; $displayTitle = isset($options['displayTitle']) ? $options['displayTitle'] : null; $siteUrl = isset($options['siteUrl']) ? $options['siteUrl'] : ''; if (!$id || !$displayTitle || !$password) { $this->setError('missing-parameters'); return; } // Check if a project by this id doesn't exist already $row = $db->getOne(str_queryf('SELECT id FROM projects WHERE id = %s;', $id)); if ($row) { $this->setError('invalid-input', 'Unable to create project, a project by that name exists already.'); return; } // Validate project id if (!LoginAction::isValidName($id)) { $this->setError('invalid-input', 'Project ids must be in format: "' . LoginAction::getNameValidationRegex() . '".'); return; } // maxlength (otherwise MySQL will crop it) if (strlen($displayTitle) > 255) { $this->setError('Display title has to be no longer than 255 characters.'); return; } // Create the project $authToken = LoginAction::generateRandomHash(40); $authTokenHash = sha1($authToken); $isInserted = $db->query(str_queryf('INSERT INTO projects (id, display_title, site_url, password, auth_token, updated, created) VALUES(%s, %s, %s, %s, %s, %s, %s);', $id, $displayTitle, $siteUrl, LoginAction::generatePasswordHash($password), $authTokenHash, swarmdb_dateformat(SWARM_NOW), swarmdb_dateformat(SWARM_NOW))); if (!$isInserted) { $this->setError('internal-error', 'Insertion of row into database failed.'); return; } return array('authToken' => $authToken); }