File size: 13,026 Bytes
ae01f49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////

import React from 'react';
import pgAdmin from 'sources/pgadmin';
import ConnectServerContent from './ConnectServerContent';
import url_for from 'sources/url_for';
import gettext from 'sources/gettext';

import getApiInstance from '../api_instance';
import MasterPasswordContent from './MasterPasswordContent';
import ChangePasswordContent from './ChangePasswordContent';
import NamedRestoreContent from './NamedRestoreContent';
import ChangeOwnershipContent from './ChangeOwnershipContent';
import UrlDialogContent from './UrlDialogContent';
import RenameTabContent from './RenameTabContent';
import { BROWSER_PANELS } from '../../../browser/static/js/constants';
import ErrorBoundary from '../helpers/ErrorBoundary';
import QuickSearch from '../QuickSearch';

// This functions is used to show the connect server password dialog.
export function showServerPassword() {
  let title = arguments[0],
    formJson = arguments[1],
    nodeObj = arguments[2],
    nodeData = arguments[3],
    treeNodeInfo = arguments[4],
    itemNodeData = arguments[5],
    status = arguments[6],
    onSuccess = arguments[7],
    onFailure = arguments[8];

  pgAdmin.Browser.notifier.showModal(title, (onClose) => {
    return (
      <ConnectServerContent
        closeModal={()=>{
          onClose();
        }}
        data={formJson}
        onOK={(formData)=>{
          const api = getApiInstance();
          let _url = nodeObj.generate_url(itemNodeData, 'connect', nodeData, true);
          if (!status) {
            treeNodeInfo.setLeaf(itemNodeData);
            treeNodeInfo.removeIcon(itemNodeData);
            treeNodeInfo.addIcon(itemNodeData, {icon: 'icon-server-connecting'});
          }

          api.post(_url, formData)
            .then(res=>{
              onClose();
              return onSuccess(
                res.data, nodeObj, nodeData, treeNodeInfo, itemNodeData, status
              );
            })
            .catch((err)=>{
              return onFailure(
                err, null, nodeObj, nodeData, treeNodeInfo, itemNodeData, status
              );
            });
        }}
      />
    );
  });
}

function masterPassCallbacks(masterpass_callback_queue) {
  while(masterpass_callback_queue.length > 0) {
    let callback = masterpass_callback_queue.shift();
    callback();
  }
}

export function checkMasterPassword(data, masterpass_callback_queue, cancel_callback) {
  const api = getApiInstance();
  api.post(url_for('browser.set_master_password'), data).then((res)=> {
    let isKeyring = res.data.data.keyring_name.length > 0;

    if(!res.data.data.present) {
      if (res.data.data.invalid_master_password_hook){
        if(res.data.data.is_error){
          pgAdmin.Browser.notifier.error(res.data.data.errmsg);
        }else{
          pgAdmin.Browser.notifier.confirm(gettext('Reset Master Password'),
            gettext('The master password retrieved from the master password hook utility is different from what was previously retrieved.') + '<br>'
            + gettext('Do you want to reset your master password to match?') + '<br><br>'
            + gettext('Note that this will close all open database connections and remove all saved passwords.'),
            function() {
              let _url = url_for('browser.reset_master_password');
              api.delete(_url)
                .then(() => {
                  pgAdmin.Browser.notifier.info('The master password has been reset.');
                })
                .catch((err) => {
                  pgAdmin.Browser.notifier.error(err.message);
                });
              return true;
            },
            function() {/* If user clicks No */ return true;}
          );}
      }else{
        showMasterPassword(res.data.data.reset, res.data.data.errmsg, masterpass_callback_queue, cancel_callback, res.data.data.keyring_name);
      }

    } else {
      masterPassCallbacks(masterpass_callback_queue);

      if(isKeyring) {
        pgAdmin.Browser.notifier.alert(gettext('Migration successful'),
          gettext(`Passwords previously saved by pgAdmin have been successfully migrated to ${res.data.data.keyring_name} and removed from the pgAdmin store.`));
      }
    }
  }).catch(function(error) {
    pgAdmin.Browser.notifier.pgRespErrorNotify(error);
  });
}

// This functions is used to show the master password dialog.
export function showMasterPassword(isPWDPresent, errmsg, masterpass_callback_queue, cancel_callback, keyring_name='') {
  const api = getApiInstance();
  let title =  keyring_name.length > 0 ? gettext('Migrate Saved Passwords') : isPWDPresent ? gettext('Unlock Saved Passwords') : gettext('Set Master Password');

  pgAdmin.Browser.notifier.showModal(title, (onClose)=> {
    return (
      <MasterPasswordContent
        isPWDPresent= {isPWDPresent}
        data={{'errmsg': errmsg}}
        keyringName={keyring_name}
        closeModal={() => {
          onClose();
        }}
        onResetPassowrd={(isKeyRing=false)=>{
          pgAdmin.Browser.notifier.confirm(gettext('Reset Master Password'),
            gettext('This will remove all the saved passwords. This will also remove established connections to '
            + 'the server and you may need to reconnect again. Do you wish to continue?'),
            function() {
              let _url = url_for('browser.reset_master_password');

              api.delete(_url)
                .then(() => {
                  onClose();
                  if(!isKeyRing) {
                    showMasterPassword(false, null, masterpass_callback_queue, cancel_callback);
                  }
                })
                .catch((err) => {
                  pgAdmin.Browser.notifier.error(err.message);
                });
              return true;
            },
            function() {/* If user clicks No */ return true;}
          );
        }}
        onCancel={()=>{
          cancel_callback?.();
        }}
        onOK={(formData) => {
          onClose();
          checkMasterPassword(formData, masterpass_callback_queue, cancel_callback);
        }}
      />
    );
  });
}

export function showChangeServerPassword() {
  let title = arguments[0],
    nodeData = arguments[1],
    nodeObj = arguments[2],
    itemNodeData = arguments[3],
    isPgPassFileUsed = arguments[4];

  const panelId = BROWSER_PANELS.SEARCH_OBJECTS;
  const onClose = ()=>{pgAdmin.Browser.docker.close(panelId);};
  pgAdmin.Browser.docker.openDialog({
    id: panelId,
    title: title,
    content: (
      <ChangePasswordContent
        onClose={onClose}
        onSave={(isNew, data)=>{
          return new Promise((resolve, reject)=>{
            const api = getApiInstance();
            let _url = nodeObj.generate_url(itemNodeData, 'change_password', nodeData, true);

            api.post(_url, data)
              .then(({data: respData})=>{
                pgAdmin.Browser.notifier.success(respData.info);
                // Notify user to update pgpass file
                if(isPgPassFileUsed) {
                  pgAdmin.Browser.notifier.alert(
                    gettext('Change Password'),
                    gettext('Please make sure to disconnect the server'
                    + ' and update the new password in the pgpass file'
                      + ' before performing any other operation')
                  );
                }

                resolve(respData.data);
                onClose();
              })
              .catch((error)=>{
                reject(error);
              });
          });
        }}
        userName={nodeData.user.name}
        isPgpassFileUsed={isPgPassFileUsed}
      />
    )
  }, pgAdmin.Browser.stdW.md, pgAdmin.Browser.stdH.md);
}

export function showChangeUserPassword(url) {
  const panelId = BROWSER_PANELS.SEARCH_OBJECTS;
  const onClose = ()=>{pgAdmin.Browser.docker.close(panelId);};
  pgAdmin.Browser.docker.openDialog({
    id: panelId,
    title: gettext('Change pgAdmin User Password'),
    content: (
      <ChangePasswordContent
        getInitData={()=>{
          const api = getApiInstance();
          return new Promise((resolve, reject)=>{
            api.get(url)
              .then((res)=>{
                resolve(res.data);
              })
              .catch((err)=>{
                reject(err);
              });
          });
        }}
        onClose={()=>{
          onClose();
        }}
        onSave={(_isNew, data)=>{
          const api = getApiInstance();
          return new Promise((resolve, reject)=>{
            const formData =  {
              'password': data.password,
              'new_password': data.newPassword,
              'new_password_confirm': data.confirmPassword,
              'csrf_token': data.csrf_token
            };

            api({
              method: 'POST',
              url: url,
              data: formData,
            }).then((res)=>{
              resolve(res.data.info);
              onClose();
              pgAdmin.Browser.notifier.success(res.data.info);
            }).catch((err)=>{
              reject(err);
            });
          });
        }}
        hasCsrfToken={true}
        showUser={false}
      />
    )
  }, pgAdmin.Browser.stdW.md, pgAdmin.Browser.stdH.md);
}

export function showNamedRestorePoint() {
  let title = arguments[0],
    nodeData = arguments[1],
    nodeObj = arguments[2],
    itemNodeData = arguments[3];

  const panelId = BROWSER_PANELS.SEARCH_OBJECTS;
  const onClose = ()=>{pgAdmin.Browser.docker.close(panelId);};
  pgAdmin.Browser.docker.openDialog({
    id: panelId,
    title: title,
    content: (
      <NamedRestoreContent
        closeModal={onClose}
        onOK={(formData)=>{
          const api = getApiInstance();
          let _url = nodeObj.generate_url(itemNodeData, 'restore_point', nodeData, true);

          api.post(_url, formData)
            .then(res=>{
              onClose();
              pgAdmin.Browser.notifier.success(res.data.data.result);
            })
            .catch(function(error) {
              pgAdmin.Browser.notifier.pgRespErrorNotify(error);
            });
        }}
      />
    )
  }, pgAdmin.Browser.stdW.md, 180);
}

export function showChangeOwnership() {
  let title = arguments[0],
    userList = arguments[1],
    noOfSharedServers = arguments[2],
    deletedUser = arguments[3],
    deleteUserRow = arguments[4];

  // Render Preferences component
  pgAdmin.Browser.notifier.showModal(title, (onClose) => {
    return <ChangeOwnershipContent
      onClose={()=>{
        onClose();
      }}
      onSave={(isNew, data)=>{
        const api = getApiInstance();

        return new Promise((resolve, reject)=>{
          if (data.newUser == '') {
            deleteUserRow();
            onClose();
          } else {
            let newData = {'new_owner': `${data.newUser}`, 'old_owner': `${deletedUser['id']}`};
            api.post(url_for('user_management.change_owner'), newData)
              .then(({data: respData})=>{
                pgAdmin.Browser.notifier.success(gettext(respData.info));
                onClose();
                deleteUserRow();
                resolve(respData.data);
              })
              .catch((err)=>{
                reject(err);
              });
          }
        });
      }}
      userList = {userList}
      noOfSharedServers = {noOfSharedServers}
      deletedUser = {deletedUser['name']}
    />;
  },
  { isFullScreen: false, isResizeable: true, showFullScreen: true, isFullWidth: true,
    dialogWidth: pgAdmin.Browser.stdW.md, dialogHeight: pgAdmin.Browser.stdH.md});
}

export function showUrlDialog() {
  let title = arguments[0],
    url = arguments[1],
    helpFile = arguments[2],
    width = arguments[3],
    height = arguments[4];

  pgAdmin.Browser.notifier.showModal(title, (onClose) => {
    return <UrlDialogContent url={url} helpFile={helpFile} onClose={onClose} />;
  },
  { isFullScreen: false, isResizeable: true, showFullScreen: true, isFullWidth: true,
    dialogWidth: width || pgAdmin.Browser.stdW.md, dialogHeight: height || pgAdmin.Browser.stdH.md});
}

export function showRenameTab(panelId, panelDocker) {
  pgAdmin.Browser.notifier.showModal(gettext('Rename Tab'), (onClose) => {
    return (
      <ErrorBoundary>
        <RenameTabContent
          closeModal={()=>{
            onClose();
          }}
          panelId={panelId}
          panelDocker={panelDocker}
        />
      </ErrorBoundary>
    );
  });
}

export function showQuickSearch() {
  pgAdmin.Browser.notifier.showModal(gettext('Quick Search'), (closeModal) => {
    return <QuickSearch closeModal={closeModal}/>;
  },
  { isFullScreen: false, isResizeable: false, showFullScreen: false, isFullWidth: false, showTitle: false}
  );
}