comparison m-toolbox/classes/@LTPDADatabaseConnectionManager/LTPDADatabaseConnectionManager.m @ 41:6def6533cb16 database-connection-manager

Report authentication errors to user
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 18:04:34 +0100
parents 5e7477b94d94
children f90d4f666cc7
comparison
equal deleted inserted replaced
40:977eb37f31cb 41:6def6533cb16
2 2
3 properties(SetAccess=private) 3 properties(SetAccess=private)
4 4
5 connections = {}; 5 connections = {};
6 credentials = {}; 6 credentials = {};
7 userMessage = [];
7 8
8 end % private properties 9 end % private properties
9 10
10 properties(Dependent=true) 11 properties(Dependent=true)
11 12
82 repo{jj} = []; 83 repo{jj} = [];
83 end 84 end
84 end 85 end
85 cm.add(utils.credentials(repo{:})); 86 cm.add(utils.credentials(repo{:}));
86 end 87 end
88
89 % reset user message
90 cm.userMessage = [];
87 end 91 end
88 92
89 93
90 function str = disp(cm) 94 function str = disp(cm)
91 disp(sprintf('%s()\n', class(cm))); 95 disp(sprintf('%s()\n', class(cm)));
347 end 351 end
348 352
349 cache = false; 353 cache = false;
350 if (numel(cred) > 1) || ~cred.complete 354 if (numel(cred) > 1) || ~cred.complete
351 % ask for password 355 % ask for password
352 [username, password, cache] = cm.inputCredentials(cred); 356 [username, password, cache] = cm.inputCredentials(cred, cm.userMessage);
353 357
354 % cache credentials 358 % cache credentials
355 cred = utils.credentials(varargin{1}, varargin{2}, username); 359 cred = utils.credentials(varargin{1}, varargin{2}, username);
356 cm.cacheCredentials(cred); 360 cm.cacheCredentials(cred);
357 361
358 % add password to credentials 362 % add password to credentials
365 catch ex 369 catch ex
366 % look for access denied errors 370 % look for access denied errors
367 if strcmp(ex.identifier, 'utils:mysql:connect:AccessDenied') 371 if strcmp(ex.identifier, 'utils:mysql:connect:AccessDenied')
368 % ask for new new credentials 372 % ask for new new credentials
369 utils.helper.msg(msg.IMPORTANT, ex.message); 373 utils.helper.msg(msg.IMPORTANT, ex.message);
374 cm.userMessage = 'Authentication error!';
370 conn = cm.getConnection(varargin{1}, varargin{2}, varargin{3}); 375 conn = cm.getConnection(varargin{1}, varargin{2}, varargin{3});
371 else 376 else
372 % error out 377 % error out
373 throw(ex); 378 throw(ex);
374 end 379 end
468 end 473 end
469 end 474 end
470 end 475 end
471 476
472 477
473 function [username, password, cache] = inputCredentials(cm, cred) 478 function [username, password, cache] = inputCredentials(cm, cred, msg)
474 % INPUTCREDENTIALS Queries the user for database username and password. 479 % INPUTCREDENTIALS Queries the user for database username and password.
480
481 % msg is an optional argument
482 if nargin < 3
483 msg = [];
484 end
475 485
476 % build a cell array of usernames 486 % build a cell array of usernames
477 users = {}; 487 users = {};
478 for id = 1:numel(cred) 488 for id = 1:numel(cred)
479 if ~isempty(cred(id).username) 489 if ~isempty(cred(id).username)
482 end 492 end
483 users = sort(unique(users)); 493 users = sort(unique(users));
484 494
485 parent = com.mathworks.mde.desk.MLDesktop.getInstance().getMainFrame(); 495 parent = com.mathworks.mde.desk.MLDesktop.getInstance().getMainFrame();
486 dialog = javaObjectEDT('connectionmanager.CredentialsDialog', ... 496 dialog = javaObjectEDT('connectionmanager.CredentialsDialog', ...
487 parent, cred(1).hostname, cred(1).database, users, cm.cachePassword); 497 parent, cred(1).hostname, cred(1).database, users, cm.cachePassword, msg);
488 dialog.show(); 498 dialog.show();
489 if dialog.cancelled 499 if dialog.cancelled
490 throw(MException('utils:mysql:connect:UserCancelled', '### user cancelled')); 500 throw(MException('utils:mysql:connect:UserCancelled', '### user cancelled'));
491 end 501 end
492 username = char(dialog.username); 502 username = char(dialog.username);