forked from pgadmin-org/pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDialog.jsx
More file actions
238 lines (222 loc) · 7.48 KB
/
UserDialog.jsx
File metadata and controls
238 lines (222 loc) · 7.48 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2025, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import React, { useMemo } from 'react';
import SchemaView from '../../../../static/js/SchemaView';
import BaseUISchema from '../../../../static/js/SchemaView/base_schema.ui';
import gettext from 'sources/gettext';
import url_for from 'sources/url_for';
import getApiInstance, { parseApiError } from '../../../../static/js/api_instance';
import {AUTH_METHODS} from 'pgadmin.browser.constants';
import current_user from 'pgadmin.user_management.current_user';
import { isEmptyString } from '../../../../static/js/validators';
import ErrorBoundary from '../../../../static/js/helpers/ErrorBoundary';
import PropTypes from 'prop-types';
import { usePgAdmin } from '../../../../static/js/PgAdminProvider';
class UserSchema extends BaseUISchema {
constructor(options, pgAdmin) {
super({
auth_source: 'internal',
role: 1,
active: true,
refreshBrowserTree: false
});
this.options = options;
this.pgAdmin = pgAdmin;
this.authOnlyInternal = (current_user['auth_sources'].length == 1 &&
current_user['auth_sources'].includes(AUTH_METHODS['INTERNAL']));
}
isUserNameEnabled(state) {
return this.isNew(state) && state.auth_source != AUTH_METHODS['INTERNAL'];
}
isNotCurrentUser(state) {
return state.id != current_user['id'];
}
get baseFields() {
let obj = this;
return [
{
id: 'auth_source', label: gettext('Authentication source'),
type: (state) => {
return {
type: 'select',
options: () => {
if (obj.isNew(state)) {
return Promise.resolve(obj.options.authSources.filter((s) => current_user['auth_sources'].includes(s.value)));
}
return Promise.resolve(obj.options.authSources);
},
optionsReloadBasis: obj.isNew(state)
};
},
controlProps: {
allowClear: false,
openOnEnter: false,
},
readonly: function (state) {
return !obj.isNew(state) || obj.authOnlyInternal;
}
}, {
id: 'username', label: gettext('Username'), type: 'text',
deps: ['auth_source'],
depChange: (state) => {
if (!obj.isUserNameEnabled(state)) {
return { username: undefined };
}
},
readonly: (state) => {
return !obj.isUserNameEnabled(state);
}
}, {
id: 'email', label: gettext('Email'), type: 'text',
deps: ['id'],
readonly: (state) => {
if (obj.isNew(state)) {
return false;
} else {
return !obj.isNotCurrentUser(state) || state.auth_source == AUTH_METHODS['INTERNAL'];
}
}
}, {
id: 'role', label: gettext('Role'), type: 'select',
options: obj.options.roles,
controlProps: {
allowClear: false,
openOnEnter: false,
},
readonly: (state) => {
if (obj.isNew(state)) {
return false;
}
return !obj.isNotCurrentUser(state);
}
}, {
id: 'active', label: gettext('Active'), type: 'switch',
readonly: (state) => {
if (obj.isNew(state)) {
return false;
}
return !obj.isNotCurrentUser(state);
}
}, {
id: 'newPassword', label: gettext('New password'), type: 'password',
deps: ['auth_source'], controlProps: {
autoComplete: 'new-password',
},
visible: (state)=>obj.isNotCurrentUser(state) && state.auth_source == AUTH_METHODS['INTERNAL'],
}, {
id: 'confirmPassword', label: gettext('Confirm password'), type: 'password',
deps: ['auth_source'], controlProps: {
autoComplete: 'new-password',
},
visible: (state)=>obj.isNotCurrentUser(state) && state.auth_source == AUTH_METHODS['INTERNAL'],
}, {
id: 'locked', label: gettext('Locked'), type: 'switch',
readonly: (state) => {
return !state.locked;
}
}
];
}
validate(state, setError) {
let msg;
let obj = this;
let minPassLen = this.pgAdmin.password_length_min;
if (obj.isUserNameEnabled(state) && isEmptyString(state.username)) {
msg = gettext('Username cannot be empty');
setError('username', msg);
return true;
} else {
setError('username', null);
}
if (state.auth_source == AUTH_METHODS['INTERNAL']) {
let email_filter = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (isEmptyString(state.email)) {
msg = gettext('Email cannot be empty');
setError('email', msg);
return true;
} else if (!email_filter.test(state.email)) {
msg = gettext('Invalid email address: %s', state.email);
setError('email', msg);
return true;
} else {
setError('email', null);
}
if (obj.isNew(state) && isEmptyString(state.newPassword)) {
msg = gettext('Password cannot be empty for user %s', state.email);
setError('newPassword', msg);
return true;
} else if (state.newPassword?.length < minPassLen) {
msg = gettext('Password must be at least %s characters for user %s', minPassLen, state.email);
setError('newPassword', msg);
return true;
} else {
setError('newPassword', null);
}
if (obj.isNew(state) && isEmptyString(state.confirmPassword)) {
msg = gettext('Confirm Password cannot be empty for user %s', state.email);
setError('confirmPassword', msg);
return true;
} else {
setError('confirmPassword', null);
}
if (state.newPassword !== state.confirmPassword) {
msg = gettext('Passwords do not match for user %s', state.email);
setError('confirmPassword', msg);
return true;
} else {
setError('confirmPassword', null);
}
}
return false;
}
}
export default function UserDialog({user, options, onClose}) {
const pgAdmin = usePgAdmin();
const schema = useMemo(() => new UserSchema(options, pgAdmin), []);
const isEdit = Boolean(user.id);
const api = getApiInstance();
const onSaveClick = (_isNew, changeData)=>{
return new Promise((resolve, reject)=>{
try {
api.post(url_for('user_management.save'), changeData)
.then(()=>{
pgAdmin.Browser.notifier.success(gettext('Users Saved Successfully'));
resolve();
onClose(null, true);
})
.catch((err)=>{
reject(err instanceof Error ? err : Error(gettext('Something went wrong')));
});
} catch (error) {
reject(Error(parseApiError(error)));
}
});
};
return <ErrorBoundary>
<SchemaView
formType={'dialog'}
getInitData={()=>{ return Promise.resolve(user); }}
schema={schema}
viewHelperProps={{
mode: isEdit ? 'edit' : 'create',
}}
onSave={onSaveClick}
onClose={onClose}
hasSQL={false}
disableSqlHelp={true}
disableDialogHelp={true}
isTabView={false}
/>
</ErrorBoundary>;
}
UserDialog.propTypes = {
user: PropTypes.object,
options: PropTypes.object,
onClose: PropTypes.func,
};