[Bio] / FigKernelPackages / UserDB.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/UserDB.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (view) (download) (as text)

1 : olson 1.1 #
2 :     # Basic user management tools for a SEED.
3 :     #
4 :     # The user database explicitly isn't kept in the SEED database instance because
5 :     # it is intended to be persistent across database loads and the installation of
6 :     # new Data directories.
7 :     #
8 :     # If we had the freedom to require a new compiled environment, we might use
9 :     # DBD::SQLite to hold it. But that's not straightforward at this point, so
10 :     # we will hold the user data in a simple flat file, reading it into memory
11 :     # as needed and rewriting to disk when changes need to be made. At this point
12 :     # there will be fairly few accesses to it anyway.
13 :     #
14 :     # We define the following operations on a user database.
15 :     #
16 :     # get_users(): returns the list of usernames that we know about on this system.
17 :     #
18 :     # ensure_user($user): Ensure that a database entry for $user exists.
19 :     #
20 :     # get_user($user): Returns the user parameters as a hash ref.
21 :     #
22 :     # set_user_param($user, $param, $value): Set a user parameter $param to $value.
23 :     #
24 :     # get_user_param($user, $param): Retrieve the user paramter $param.
25 :     #
26 :     # write(): Writes the modified user database to disk.
27 :     #
28 :     # The file format for the user database is intended to be exceedingly simple to generate and
29 :     # parse.
30 :     #
31 :     # It consists of blocks separated by // lines. The first line in the block is the
32 :     # user name, and the remaining lines are tab-delimited pairs 'param-name'\t'param-value'
33 :     #
34 :     # Parameter values are not allowed to contain newlines.
35 :     #
36 :    
37 :     package UserDB;
38 :     use FIG_Config;
39 :    
40 :     use Data::Dumper;
41 :    
42 :     use strict;
43 :    
44 :     sub new
45 :     {
46 :     #
47 :     # We require a $fig reference so we can poke at the database.
48 :     #
49 :    
50 :     my($class, $fig) = @_;
51 :    
52 :     my $file = "$FIG_Config::fig_disk/config/user.db";
53 :    
54 :     my $fh;
55 :     if (!open($fh, "<$file"))
56 :     {
57 :     #
58 :     # DB file doesn't exist yet. Create a new database initialized
59 :     # from the users that show up in the annotations and assignments
60 :     # in the database.
61 :     #
62 :    
63 :     ref($fig) or die "UserDB::new requires a valid FIG object\n";
64 :    
65 :     open(my $wfh, ">$file") or die "Cannot write new user database $file: $!\n";
66 :    
67 :     my @initial_users = _get_initial_users($fig);
68 :     for my $user (@initial_users)
69 :     {
70 :     print $wfh "$user\n";
71 :     print $wfh "//\n";
72 :     }
73 :     close($wfh);
74 :     open($fh, "<$file") or die "Cannot open newly-created user database $file: $!\n";
75 :     }
76 :    
77 :     #
78 :     # Ensure it stays writable.
79 :     #
80 :    
81 :     chmod(0666, $file);
82 :    
83 :     my $self = {
84 :     users => {},
85 :     file => $file,
86 :     };
87 :    
88 :     local($/);
89 :     $/ = "//\n";
90 :    
91 :     while (<$fh>)
92 :     {
93 :     chomp;
94 :    
95 :     my ($user, @params) = split(/\n/, $_);
96 :    
97 :     my $phash = $self->{users}->{$user} = {};
98 :    
99 :     warn "Parse gets user $user\n";
100 :    
101 :     for my $param (@params)
102 :     {
103 :     my ($name, $value) = split(/\t/, $param, 2);
104 :    
105 :     $phash->{$name} = $value;
106 :     }
107 :     }
108 :     close($fh);
109 :    
110 :     bless($self, $class);
111 :    
112 :     return $self;
113 :     }
114 :    
115 :     sub get_users
116 :     {
117 :     my($self) = @_;
118 :    
119 :     return keys(%{$self->{users}});
120 :     }
121 :    
122 :     sub ensure_user
123 :     {
124 :     my($self, $user) = @_;
125 :    
126 :     if (!defined($self->{users}->{$user}))
127 :     {
128 :     $self->{users}->{$user} = {};
129 :     }
130 :     }
131 :    
132 :     sub get_user
133 :     {
134 :     my($self, $user) = @_;
135 :    
136 :     return $self->{users}->{$user};
137 :     }
138 :    
139 :     sub set_user_param
140 :     {
141 :     my($self, $user, $param, $value) = @_;
142 :    
143 :     $self->{users}->{$user}->{$param} = $value;
144 :     }
145 :    
146 :     sub get_user_param
147 :     {
148 :     my($self, $user, $param) = @_;
149 :    
150 :     return $self->{users}->{$user}->{$param};
151 :     }
152 :    
153 :     sub write
154 :     {
155 :     my($self) = @_;
156 :     my $fh;
157 :    
158 :     open($fh, ">$self->{file}") or die "UserDB::write: could not open $self->{file} for writing: $!\n";
159 :    
160 :     while (my ($user, $params) = each (%{$self->{users}}))
161 :     {
162 :     print $fh "$user\n";
163 :     while (my($k, $v) = each(%$params))
164 :     {
165 :     print $fh "$k\t$v\n";
166 :     }
167 :     print $fh "//\n";
168 :     }
169 :     close($fh);
170 :     }
171 :    
172 :     #
173 :     # Return a list of usernames that appear in the database.
174 :     #
175 :     # Strip master:name to just name.
176 :     #
177 :     sub _get_initial_users
178 :     {
179 :     my($fig) = @_;
180 :     my %names;
181 :    
182 :     my $db = $fig->db_handle();
183 :     my $res;
184 :    
185 :     #
186 :     # Pull names from annotations.
187 :     #
188 :    
189 :     $res = $db->SQL("SELECT DISTINCT who FROM annotation_seeks");
190 :     for my $ent (@$res)
191 :     {
192 :     my $who = $ent->[0];
193 :     $who =~ s/^master://;
194 :     $names{$who}++;
195 :     }
196 :    
197 :     #
198 :     # And from assignments
199 :     #
200 :    
201 :     $res = $db->SQL("SELECT DISTINCT made_by FROM assigned_functions");
202 :     for my $ent (@$res)
203 :     {
204 :     my $who = $ent->[0];
205 :     $who =~ s/^master://;
206 :     $names{$who}++;
207 :     }
208 :    
209 :     return keys %names;
210 :     }
211 :    
212 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3