Skip to content

Commit

Permalink
feat: Add optional restriction of script execution to certain object …
Browse files Browse the repository at this point in the history
…fields and values (parse-community#2488)
  • Loading branch information
dblythy authored Jun 27, 2024
1 parent ed3312f commit 8feac9b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,31 @@ You can specify scripts to execute Cloud Functions with the `scripts` option:
]
```

You can also specify custom fields with the `scrips` option:

```json
"apps": [
{
"scripts": [
{
"title": "Delete account",
"classes": [
{
"name": "_User",
"fields": [
{ "name": "createdAt", "validator": "value => value > new Date(\"2025\")" }
]
}
],
"cloudCodeFunction": "deleteAccount"
}
]
}
]

```


Next, define the Cloud Function in Parse Server that will be called. The object that has been selected in the data browser will be made available as a request parameter:

```js
Expand Down
37 changes: 31 additions & 6 deletions src/components/BrowserCell/BrowserCell.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,48 @@ export default class BrowserCell extends Component {
});
}

const { className, objectId } = this.props;
const validScripts = (this.props.scripts || []).filter(script =>
script.classes?.includes(this.props.className)
);
const { className, objectId, field, scripts = [], rowValue } = this.props;
let validator = null;
const validScripts = (scripts || []).filter(script => {
if (script.classes?.includes(className)) {
return true;
}
for (const script of script?.classes || []) {
if (script?.name !== className) {
continue;
}
const fields = script?.fields || [];
if (script?.fields.includes(field) || script?.fields.includes('*')) {
return true;
}
for (const currentField of fields) {
if (Object.prototype.toString.call(currentField) === '[object Object]') {
if (currentField.name === field) {
if (typeof currentField.validator === 'string') {
validator = eval(currentField.validator);
} else {
validator = currentField.validator;
}
return true;
}
}
}
}
});
if (validScripts.length) {
onEditSelectedRow &&
contextMenuOptions.push({
text: 'Scripts',
items: validScripts.map(script => {
return {
text: script.title,
disabled: validator?.(rowValue, field) === false,
callback: () => {
this.selectedScript = { ...script, className, objectId };
if (script.showConfirmationDialog) {
this.toggleConfirmationDialog();
} else {
this.executeSript(script);
this.executeScript(script);
}
},
};
Expand All @@ -363,7 +388,7 @@ export default class BrowserCell extends Component {
return contextMenuOptions;
}

async executeSript(script) {
async executeScript(script) {
try {
const object = Parse.Object.extend(this.props.className).createWithoutData(
this.props.objectId
Expand Down
2 changes: 2 additions & 0 deletions src/components/BrowserRow/BrowserRow.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class BrowserRow extends Component {
order,
readOnlyFields,
row,
rowValue,
rowWidth,
selection,
selectRow,
Expand Down Expand Up @@ -122,6 +123,7 @@ export default class BrowserRow extends Component {
className={className}
field={name}
row={row}
rowValue={rowValue}
col={j}
type={type}
readonly={isUnique || readOnlyFields.indexOf(name) > -1}
Expand Down
4 changes: 4 additions & 0 deletions src/components/ContextMenu/ContextMenu.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ const MenuSection = ({ level, items, path, setPath, hide }) => {
<li
key={`menu-section-${level}-${index}`}
className={styles.option}
style={item.disabled ? { opacity: 0.5, cursor: 'not-allowed' } : {}}
onClick={() => {
if (item.disabled === true) {
return;
}
item.callback && item.callback();
hide();
}}
Expand Down
2 changes: 2 additions & 0 deletions src/dashboard/Data/Browser/BrowserTable.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export default class BrowserTable extends React.Component {
order={this.props.order}
readOnlyFields={READ_ONLY}
row={index}
rowValue={this.props.data[index]}
rowWidth={rowWidth}
selection={this.props.selection}
selectRow={this.props.selectRow}
Expand Down Expand Up @@ -310,6 +311,7 @@ export default class BrowserTable extends React.Component {
order={this.props.order}
readOnlyFields={READ_ONLY}
row={i}
rowValue={this.props.data[i]}
rowWidth={rowWidth}
selection={this.props.selection}
selectRow={this.props.selectRow}
Expand Down

0 comments on commit 8feac9b

Please sign in to comment.