This repository was archived by the owner on Feb 15, 2024. It is now read-only.
forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityDialog.react.js
More file actions
140 lines (122 loc) · 4.11 KB
/
Copy pathSecurityDialog.react.js
File metadata and controls
140 lines (122 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import Parse from 'parse'
import PermissionsDialog from 'components/PermissionsDialog/PermissionsDialog.react';
import React from 'react';
import styles from 'dashboard/Data/Browser/Browser.scss';
import { CurrentApp } from 'context/currentApp';
const pointerPrefix = 'userField:';
function validateEntry(pointers, text, parseServerSupportsPointerPermissions) {
if (parseServerSupportsPointerPermissions) {
let fieldName = text.startsWith(pointerPrefix)
? text.substring(pointerPrefix.length)
: text;
if (pointers.includes(fieldName)) {
return Promise.resolve({ entry: fieldName, type: 'pointer' });
}
}
let userQuery;
let roleQuery;
if (text === '*') {
return Promise.resolve({ entry: '*', type: 'public' });
}
if (text.toLowerCase() === 'requiresAuthentication') {
return Promise.resolve({ entry: 'requiresAuthentication', type: 'auth' });
}
if (text.startsWith('user:')) {
let user = text.substring(5);
userQuery = new Parse.Query.or(
new Parse.Query(Parse.User).equalTo('username', user),
new Parse.Query(Parse.User).equalTo('objectId', user)
);
// no need to query roles
roleQuery = {
find: () => Promise.resolve([])
};
} else if (text.startsWith('role:')) {
let role = text.substring(5);
roleQuery = new Parse.Query.or(
new Parse.Query(Parse.Role).equalTo('name', role),
new Parse.Query(Parse.Role).equalTo('objectId', role)
);
// no need to query users
userQuery = {
find: () => Promise.resolve([])
};
} else {
// query both
userQuery = Parse.Query.or(
new Parse.Query(Parse.User).equalTo('username', text),
new Parse.Query(Parse.User).equalTo('objectId', text)
);
roleQuery = Parse.Query.or(
new Parse.Query(Parse.Role).equalTo('name', text),
new Parse.Query(Parse.Role).equalTo('objectId', text)
);
}
return Promise.all([
userQuery.find({ useMasterKey: true }),
roleQuery.find({ useMasterKey: true })
]).then(([user, role]) => {
if (user.length > 0) {
return { entry: user[0], type: 'user' };
} else if (role.length > 0) {
return { entry: role[0], type: 'role' };
} else {
return Promise.reject();
}
});
}
export default class SecurityDialog extends React.Component {
static contextType = CurrentApp;
constructor() {
super();
this.state = { open: false };
this.handleClose = this.handleClose.bind(this);
this.handleOpen = this.handleOpen.bind(this);
}
/**
* Allows opening this dialog by reference
*/
handleOpen() {
if (!this.props.disabled) {
this.setState({ open: true }, () => this.props.onEditPermissions(true));
}
}
handleClose() {
this.setState({ open: false },() => this.props.onEditPermissions(false));
}
render() {
let dialog = null;
let parseServerSupportsPointerPermissions = this.context.serverInfo.features.schemas.editClassLevelPermissions;
if (this.props.perms && this.state.open) {
dialog = (
<PermissionsDialog
title='Edit Class Level Permissions'
enablePointerPermissions={parseServerSupportsPointerPermissions}
advanced={true}
confirmText='Save CLP'
columns={this.props.columns}
details={<a target="_blank" href='http://docs.parseplatform.org/ios/guide/#security'>Learn more about CLPs and app security</a>}
permissions={this.props.perms}
userPointers={this.props.userPointers}
validateEntry={entry =>
validateEntry(this.props.userPointers, entry, parseServerSupportsPointerPermissions)}
onCancel={this.handleClose}
onConfirm={perms =>
this.props.onChangeCLP(perms).then(this.handleClose)}
/>
);
}
let classes = [styles.toolbarButton];
if (this.props.disabled) {
classes.push(styles.toolbarButtonDisabled);
}
return dialog;
}
}