comparison credentials.m @ 0:d5fef23867bb

First workig implementation.
author Daniele Nicolodi <daniele@science.unitn.it>
date Sun, 23 May 2010 10:51:35 +0200
parents
children b4f2f4c10918
comparison
equal deleted inserted replaced
-1:000000000000 0:d5fef23867bb
1 classdef credentials
2 properties
3
4 hostname = [];
5 database = [];
6 username = [];
7 password = [];
8 expiry = 0;
9
10 end % properties
11
12 methods
13
14 % contructor
15 function obj = credentials(hostname, database, username, password)
16 switch nargin
17 case 1
18 obj.hostname = hostname;
19 case 2
20 obj.hostname = hostname;
21 obj.database = database;
22 case 3
23 obj.hostname = hostname;
24 obj.database = database;
25 obj.username = username;
26 case 4
27 obj.hostname = hostname;
28 obj.database = database;
29 obj.username = username;
30 obj.password = password;
31 end
32 end
33
34 % convert to string representation
35 function str = char(obj, mode)
36 if nargin < 2
37 mode = '';
38 end
39 switch mode
40 case 'short'
41 % do not show password
42 frm = 'mysql://%s/%s username=%s';
43 str = sprintf(frm, obj.hostname, obj.database, obj.username);
44 case 'full'
45 % show password
46 frm = 'mysql://%s/%s username=%s password=%s';
47 str = sprintf(frm, obj.hostname, obj.database, obj.username, obj.password);
48 otherwise
49 % by default only show if a password is known
50 passwd = [];
51 if ischar(obj.password)
52 passwd = 'YES';
53 end
54 frm = 'mysql://%s/%s username=%s password=%s';
55 str = sprintf(frm, obj.hostname, obj.database, obj.username, passwd);
56 end
57 end
58
59 % display
60 function disp(obj)
61 disp([' ' char(obj) char(10)]);
62 end
63
64 % check that a credentials object contails all the required informations
65 function rv = complete(obj)
66 info = {'hostname', 'database', 'username', 'password'};
67 for kk = 1:numel(info)
68 if isempty(obj.(info{kk}))
69 rv = false;
70 return;
71 end
72 end
73 rv = true;
74 end
75
76 % check if the credentials are expired
77 function rv = expired(obj)
78 rv = false;
79 if obj.expiry > 0 && double(time()) > obj.expiry
80 rv = true;
81 end
82 end
83
84 % check if the credentials object matches the given informations
85 function rv = match(obj, hostname, database, username)
86 if nargin < 4
87 username = [];
88 end
89
90 % default value
91 rv = true;
92
93 if ~strcmp(obj.hostname, hostname)
94 rv = false;
95 return;
96 end
97 if ~strcmp(obj.database, database)
98 rv = false;
99 return;
100 end
101 if ischar(username) && ischar(obj.username) && ~strcmp(obj.username, username)
102 rv = false;
103 return;
104 end
105 end
106
107 end % methods
108
109 end