Issue #3011770: Add a setting that allows sensor data to be made public

This commit is contained in:
Michael Stenta 2018-11-07 15:31:00 -05:00
parent c522dd6485
commit 0115db0586
1 changed files with 14 additions and 11 deletions

View File

@ -110,14 +110,6 @@ function farm_sensor_listener_page_callback($public_key) {
// Load the private key from the URL query string.
$params = drupal_get_query_parameters();
// If the private key is not set, bail.
if (empty($params['private_key'])) {
return MENU_FOUND;
}
// Load the private key.
$private_key = $params['private_key'];
// Look up the sensor by it's public key.
$sensor = farm_sensor_listener_load($public_key);
@ -126,9 +118,12 @@ function farm_sensor_listener_page_callback($public_key) {
return MENU_NOT_FOUND;
}
// Compare the private key.
if ($sensor->sensor_settings['private_key'] != $private_key) {
return MENU_ACCESS_DENIED;
// If this is a GET request, and the sensor allows public API read access,
// proceed. Otherwise check the private key.
if (!($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($sensor->sensor_settings['public_api']))) {
if (empty($params['private_key']) || $params['private_key'] != $sensor->sensor_settings['private_key']) {
return MENU_ACCESS_DENIED;
}
}
// If this is a POST request, process the data.
@ -514,6 +509,14 @@ function farm_sensor_listener_settings_form($sensor, $settings = array()) {
'#default_value' => $settings['private_key'],
);
// Allow public access to sensor data.
$form['public_api'] = array(
'#type' => 'checkbox',
'#title' => t('Allow public API read access'),
'#description' => t('Checking this box will allow data from this sensor to be queried publicly via the API endpoint without a private key. See the <a href="@sensor_url">farmOS sensor guide</a> for more information.', array('@sensor_url' => 'https://farmOS.org/guide/assets/sensors')),
'#default_value' => isset($settings['public_api']) ? $settings['public_api'] : FALSE,
);
// Reminder to save the sensor entity before posting data.
if (empty($sensor->id)) {
$form['reminder'] = array(