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

Require users to consent to their own responsibility for side effects while editing project config via UI #151

Merged
merged 1 commit into from
Dec 2, 2019
Merged
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
17 changes: 17 additions & 0 deletions app/modules/core/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2014-present PlatformIO <contact@platformio.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export const USER_CONSENTS_KEY = 'userConsents';
16 changes: 16 additions & 0 deletions app/modules/core/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2014-present PlatformIO <contact@platformio.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export class ConsentRejectedError extends Error {}
30 changes: 30 additions & 0 deletions app/modules/core/sagas.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import { call, put, select, takeEvery, takeLatest } from 'redux-saga/effects';
import { deleteEntity, updateEntity, updateStorageItem } from '../../store/actions';
import { getSessionId, inIframe, reportException } from './helpers';

import { ConsentRejectedError } from '@core/errors';
import React from 'react';
import URL from 'url-parse';
import { USER_CONSENTS_KEY } from '@core/constants';
import { apiFetchData } from '../../store/api';
import { getStore } from '../../store/index';
import jsonrpc from 'jsonrpc-lite';
Expand Down Expand Up @@ -452,6 +454,34 @@ function* watchOpenTextDocument() {
});
}

export function* ensureUserConsent(id, modalOptions) {
const consents = yield select(selectors.selectUserConsents);
const consent = consents[id];
if (!consent) {
const showModal = () => {
return new Promise((resolve, reject) => {
Modal.confirm({
title: 'Confirm',
...modalOptions,
onOk: () => {
resolve();
},
onCancel: () => {
reject(new ConsentRejectedError());
}
});
});
};
yield call(showModal);
yield put(
updateStorageItem(USER_CONSENTS_KEY, {
...consents,
[id]: Date.now().valueOf()
})
);
}
}

export default [
watchShowAtStartup,
watchNotifyError,
Expand Down
5 changes: 5 additions & 0 deletions app/modules/core/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { USER_CONSENTS_KEY } from '@core/constants';
import { selectStorageItem } from '../../store/selectors';
import shajs from 'sha.js';

Expand Down Expand Up @@ -92,3 +93,7 @@ export function selectRouteBadges(state) {
const items = selectStorageItem(state, 'routeBadges') || {};
return Object.keys(items).map(key => ({ path: key, count: items[key] }));
}

export function selectUserConsents(state) {
return selectStorageItem(state, USER_CONSENTS_KEY) || {};
}
2 changes: 2 additions & 0 deletions app/modules/project/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ export const SECTIONS = Object.freeze([
SECTION_USER_ENV,
SECTION_CUSTOM
]);

export const PROJECT_CONFIG_SAVE_CONSENT_ID = 'project-config-save';
25 changes: 21 additions & 4 deletions app/modules/project/sagas.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import * as actions from './actions';
import * as pathlib from '@core/path';
import * as selectors from './selectors';

import { CONFIG_SCHEMA_KEY, PROJECT_CONFIG_KEY } from '@project/constants';
import {
CONFIG_SCHEMA_KEY,
PROJECT_CONFIG_KEY,
PROJECT_CONFIG_SAVE_CONSENT_ID
} from '@project/constants';
import {
INSTALL_PLATFORM,
UNINSTALL_PLATFORM,
Expand All @@ -29,6 +33,7 @@ import {
import { Modal, message } from 'antd';
import {
OS_RENAME_FILE,
// ensureUserConsent,
notifyError,
notifySuccess,
osRevealFile
Expand All @@ -43,10 +48,12 @@ import {
import { selectEntity, selectStorageItem } from '../../store/selectors';

import { ConfigFileModifiedError } from '@project/errors';
import { ConsentRejectedError } from '@core/errors';
import React from 'react';
import ReactGA from 'react-ga';
import { apiFetchData } from '../../store/api';
import { getSessionId } from '../core/helpers';
import { ensureUserConsent } from '@core/sagas';
import { getSessionId } from '@core/helpers';
import jsonrpc from 'jsonrpc-lite';

const RECENT_PROJECTS_STORAGE_KEY = 'recentProjects';
Expand Down Expand Up @@ -392,6 +399,13 @@ function* watchSaveProjectConfig() {
const { mtime, force } = options || {};
let error;
try {
yield call(ensureUserConsent, PROJECT_CONFIG_SAVE_CONSENT_ID, {
content: `Warning!
The entire file contents or platformio.ini will be rewritten with the current
configuration defined in this UI.
Continue to save the configuration?`,
okText: 'Save'
});
const configPath = pathlib.join(projectDir, 'platformio.ini');
if (!force) {
const currentMtime = yield call(apiFetchData, {
Expand All @@ -405,6 +419,7 @@ function* watchSaveProjectConfig() {
});
}
}

yield call(apiFetchData, {
query: 'project.config_dump',
params: [configPath, data]
Expand All @@ -420,8 +435,10 @@ function* watchSaveProjectConfig() {
// Reload list because displaying of config required project in the state
yield put(actions.loadProjects(true));
} catch (e) {
error = e;
yield put(notifyError('Could not save project config', e));
if (!(e && e instanceof ConsentRejectedError)) {
error = e;
yield put(notifyError('Could not save project config', e));
}
} finally {
yield call(onEnd, error);
}
Expand Down