Mercurial > hg > ltpda-connection-manager
comparison credentials.m @ 3:b4f2f4c10918
Add help comments.
author | Daniele Nicolodi <daniele@science.unitn.it> |
---|---|
date | Sun, 23 May 2010 22:12:05 +0200 |
parents | d5fef23867bb |
children |
comparison
equal
deleted
inserted
replaced
2:b71833fb33ef | 3:b4f2f4c10918 |
---|---|
1 classdef credentials | 1 classdef credentials |
2 | |
2 properties | 3 properties |
3 | 4 |
4 hostname = []; | 5 hostname = []; |
5 database = []; | 6 database = []; |
6 username = []; | 7 username = []; |
7 password = []; | 8 password = []; |
8 expiry = 0; | 9 expiry = 0; |
9 | 10 |
10 end % properties | 11 end % properties |
11 | 12 |
12 methods | 13 methods |
13 | 14 |
14 % contructor | |
15 function obj = credentials(hostname, database, username, password) | 15 function obj = credentials(hostname, database, username, password) |
16 % CREDENTIALS Constructor for credentials objects. | |
17 % | |
18 % Those are simple container objects to hold credentials required for | |
19 % establishing a connection to a database server, in addition to an | |
20 % expiry time. | |
21 % | |
22 % CREDENTIALS(hostname, database, username, password) The constructor can | |
23 % be called with any number of arguments. The default value for the object | |
24 % properties is the empty vector. | |
25 | |
16 switch nargin | 26 switch nargin |
17 case 1 | 27 case 1 |
18 obj.hostname = hostname; | 28 obj.hostname = hostname; |
19 case 2 | 29 case 2 |
20 obj.hostname = hostname; | 30 obj.hostname = hostname; |
29 obj.username = username; | 39 obj.username = username; |
30 obj.password = password; | 40 obj.password = password; |
31 end | 41 end |
32 end | 42 end |
33 | 43 |
34 % convert to string representation | |
35 function str = char(obj, mode) | 44 function str = char(obj, mode) |
45 % CHAR Convert a credentials object to string representation. | |
46 % | |
47 % It takes an optional second argument that defines the representation to | |
48 % use. The default is to replace the password, if present, with the YES | |
49 % string, other possible modes are SHORT where password is omitted, or FULL | |
50 % where the password is shown at it is. | |
51 | |
36 if nargin < 2 | 52 if nargin < 2 |
37 mode = ''; | 53 mode = ''; |
38 end | 54 end |
39 switch mode | 55 switch mode |
40 case 'short' | 56 case 'short' |
41 % do not show password | 57 % do not show password |
42 frm = 'mysql://%s/%s username=%s'; | 58 frm = 'mysql://%s/%s username=%s'; |
43 str = sprintf(frm, obj.hostname, obj.database, obj.username); | 59 str = sprintf(frm, obj.hostname, obj.database, obj.username); |
44 case 'full' | 60 case 'full' |
45 % show password | 61 % show password |
46 frm = 'mysql://%s/%s username=%s password=%s'; | 62 frm = 'mysql://%s/%s username=%s password=%s'; |
47 str = sprintf(frm, obj.hostname, obj.database, obj.username, obj.password); | 63 str = sprintf(frm, obj.hostname, obj.database, obj.username, obj.password); |
48 otherwise | 64 otherwise |
49 % by default only show if a password is known | 65 % by default only show if a password is known |
50 passwd = []; | 66 passwd = []; |
51 if ischar(obj.password) | 67 if ischar(obj.password) |
52 passwd = 'YES'; | 68 passwd = 'YES'; |
53 end | 69 end |
54 frm = 'mysql://%s/%s username=%s password=%s'; | 70 frm = 'mysql://%s/%s username=%s password=%s'; |
55 str = sprintf(frm, obj.hostname, obj.database, obj.username, passwd); | 71 str = sprintf(frm, obj.hostname, obj.database, obj.username, passwd); |
56 end | 72 end |
57 end | 73 end |
58 | 74 |
59 % display | |
60 function disp(obj) | 75 function disp(obj) |
76 % DISP Overloaded display method for credentials objects. | |
77 % | |
78 % Uses the default string representation of the char() method where the | |
79 % password, if present, is replaced with the string YES. | |
80 | |
61 disp([' ' char(obj) char(10)]); | 81 disp([' ' char(obj) char(10)]); |
62 end | 82 end |
63 | 83 |
64 % check that a credentials object contails all the required informations | |
65 function rv = complete(obj) | 84 function rv = complete(obj) |
66 info = {'hostname', 'database', 'username', 'password'}; | 85 % COMPLETE Checks if the credentials are complete. |
86 % | |
87 % Credentials object are complete when they contains all the required | |
88 % information to connect to a database. Namely the HOSTNAME, DATABASE | |
89 % and USERNAME properties should not be empty, the PASSWORD property is | |
90 % allowed to be an empty string '' but not []. | |
91 | |
92 info = {'hostname', 'database', 'username'}; | |
67 for kk = 1:numel(info) | 93 for kk = 1:numel(info) |
68 if isempty(obj.(info{kk})) | 94 if isempty(obj.(info{kk})) |
69 rv = false; | 95 rv = false; |
70 return; | 96 return; |
71 end | 97 end |
72 end | 98 end |
99 if ~ischar(obj.password) | |
100 rv = false; | |
101 return; | |
102 end | |
73 rv = true; | 103 rv = true; |
74 end | 104 end |
75 | 105 |
76 % check if the credentials are expired | |
77 function rv = expired(obj) | 106 function rv = expired(obj) |
107 % EXPIRED Checks if the credentials are expired. | |
108 % | |
109 % Credential objects expire when their expiry time is smaller than the | |
110 % current time in seconds since the epoch, as obtained by the time() | |
111 % function. Credentials with zero or negative expiry time never expire. | |
112 | |
78 rv = false; | 113 rv = false; |
79 if obj.expiry > 0 && double(time()) > obj.expiry | 114 if obj.expiry > 0 && double(time()) > obj.expiry |
80 rv = true; | 115 rv = true; |
81 end | 116 end |
82 end | 117 end |
83 | 118 |
84 % check if the credentials object matches the given informations | |
85 function rv = match(obj, hostname, database, username) | 119 function rv = match(obj, hostname, database, username) |
120 % MATCH Check if the credentials object matches the given information. | |
121 % | |
122 % MATCH(obj, hostname, database) Returns true when HOSTANAME and DATABASE | |
123 % parameters match the object properties. | |
124 % | |
125 % MATCH(obj, hostname, database, username) Returns true when HOSTANAME, | |
126 % DATABASE, and USERNAME parameters match the object | |
127 % properties. USERNAME is compared only if both the given one and the | |
128 % object property are not the empty vector. | |
129 | |
130 % default arguments | |
86 if nargin < 4 | 131 if nargin < 4 |
87 username = []; | 132 username = []; |
88 end | 133 end |
89 | 134 |
90 % default value | 135 % default return value |
91 rv = true; | 136 rv = true; |
92 | 137 |
93 if ~strcmp(obj.hostname, hostname) | 138 if ~strcmp(obj.hostname, hostname) |
94 rv = false; | 139 rv = false; |
95 return; | 140 return; |
96 end | 141 end |
97 if ~strcmp(obj.database, database) | 142 if ~strcmp(obj.database, database) |
101 if ischar(username) && ischar(obj.username) && ~strcmp(obj.username, username) | 146 if ischar(username) && ischar(obj.username) && ~strcmp(obj.username, username) |
102 rv = false; | 147 rv = false; |
103 return; | 148 return; |
104 end | 149 end |
105 end | 150 end |
106 | 151 |
107 end % methods | 152 end % methods |
108 | 153 |
109 end | 154 end |