summaryrefslogtreecommitdiffstats
path: root/pki/base/ra/lib/perl/PKI/Base/UserStore.pm
blob: c056837921753baaec89f9ab5bebe20d26859f67 (plain)
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
#!/usr/bin/perl
#
# --- BEGIN COPYRIGHT BLOCK ---
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2007 Red Hat, Inc.
# All rights reserved.
# --- END COPYRIGHT BLOCK ---
#
#
#
#
package PKI::Base::UserStore;

use DBI;
use PKI::Base::TimeTool;

#######################################
# Constructs a request queue
#######################################
sub new {
  my $self = {};
  bless ($self);
  return $self;
}

#######################################
# Opens this store
#######################################
sub open {
  my ($self, $cfg) = @_;
  $self->{cfg} = $cfg;
  my $dbfile = $cfg->get("database.dbfile");
  $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
  my $timeout = $self->{dbh}->func("busy_timeout");
  $self->{dbh}->func($timeout * 10, "busy_timeout");
}

#######################################
# Maps user
#######################################
sub map_user {
  my ($self, $certificate) = @_;
  my $dbh = $self->{dbh};
  my $select = "select * from users " .
                    "where " .
                    "certificate=" . $dbh->quote($certificate);
  my $sth = $dbh->prepare($select);
  $sth->execute();
  my $ref = $sth->fetchrow_hashref();
  $sth->finish();
  return $ref;
}

#######################################
# Gets roles of the given user
#######################################
sub get_roles {
  my ($self, $uid) = @_;
  my $dbh = $self->{dbh};
  my $select = "select * from roles " .
                    "where " .
                    "uid=" . $dbh->quote($uid);
  my @roles;
  my $sth = $dbh->prepare($select);
  $sth->execute();
  while (my $ref = $sth->fetchrow_hashref()) {
    push(@roles, $ref->{'gid'});
  }
  $sth->finish();
  return @roles;
}


#######################################
# Reads a user
#######################################
sub read_group {
  my ($self, $gid) = @_;
  my $dbh = $self->{dbh};
  my $select = "select * from groups " .
                    "where gid=" . $dbh->quote($gid);
  my $sth = $dbh->prepare($select);
  $sth->execute();
  my $ref = $sth->fetchrow_hashref();
  $sth->finish();
  return $ref;
}

sub read_user {
  my ($self, $uid) = @_;
  my $dbh = $self->{dbh};
  my $select = "select * from users " .
                    "where uid=" . $dbh->quote($uid);
  my $sth = $dbh->prepare($select);
  $sth->execute();
  my $ref = $sth->fetchrow_hashref();
  $sth->finish();
  return $ref;
}

sub set_user {
  my ($self, $uid, $name, $value) = @_;
  my $dbh = $self->{dbh};

  my $timet = PKI::Base::TimeTool->new();
  my $now = $timet->get_time();
  my $update = "update users set " . 
                    $name . "=" .  $dbh->quote($value) . "," .
                    "updated_at=" .  $dbh->quote($now) . " " .
                    "where uid=" . $dbh->quote($uid);
  $dbh->do($update);

  my $select = "select * from users " .
                    "where uid=" . $dbh->quote($uid);
  my $sth = $dbh->prepare($select);
  $sth->execute();
  my $ref = $sth->fetchrow_hashref();
  $sth->finish();

  return $ref;
}

#######################################
# Lists all members in the given group
#######################################
sub list_all_members {
  my ($self, $gid) = @_;
  my $dbh = $self->{dbh};
  my $select = "select * from roles where " .
                    "gid=" . $dbh->quote($gid) . " " .
                    "order by uid desc ";
  my $sth = $dbh->prepare($select);
  $sth->execute();
  my @reqs;
  while (my $ref = $sth->fetchrow_hashref()) {
    push(@reqs, $ref);
  }
  $sth->finish();
  return @reqs;
}

#######################################
# Lists
#######################################
sub list_all_non_members {
  my ($self, $gid) = @_;
  my $dbh = $self->{dbh};
  # find members of the given group
  my $select1 = "select * from roles where " .
                    "gid=" . $dbh->quote($gid);
  my $sth1 = $dbh->prepare($select1);
  $sth1->execute();
  my $filter = "";
  while (my $ref1 = $sth1->fetchrow_hashref()) {
    if ($filter eq "") {
      $filter = "uid<>" . $dbh->quote($ref1->{'uid'});
    } else {
      $filter = $filter . " AND " . "uid<>" . $dbh->quote($ref1->{'uid'});
    }
  }
  $sth1->finish();

  my $select;
  if ($filter eq "") {
    $select = "select * from users " .
                    "order by uid desc ";
  } else {
    $select = "select * from users where (" .
                    $filter . ") " .
                    "order by uid desc ";
  }
  my $sth = $dbh->prepare($select);
  $sth->execute();
  my @reqs;
  while (my $ref = $sth->fetchrow_hashref()) {
    push(@reqs, $ref);
  }
  $sth->finish();
  return @reqs;
}

sub delete_user {
  my ($self, $userid) = @_;
  my $dbh = $self->{dbh};

  my $cmd = "delete from roles where uid=" . $dbh->quote($userid);
  $dbh->do($cmd);
  $cmd = "delete from users where uid=" . $dbh->quote($userid);
  $dbh->do($cmd);
}

sub add_user_to_group {
  my ($self, $gid, $userid) = @_;
  my $dbh = $self->{dbh};

  my $timet = PKI::Base::TimeTool->new();
  my $now = $timet->get_time();

  my $cmd = "insert into roles (" . 
                   "gid" . "," .
                   "uid" .
               ") values (" .
                   $dbh->quote($gid) . "," .
                   $dbh->quote($userid) .
               ")";
  $dbh->do($cmd);
}

sub delete_user_from_group {
  my ($self, $gid, $userid) = @_;
  my $dbh = $self->{dbh};

  my $timet = PKI::Base::TimeTool->new();
  my $now = $timet->get_time();

  my $cmd = "delete from roles where " .
                   "gid=" . $dbh->quote($gid) . " AND " .
                   "uid=" . $dbh->quote($userid);
  $dbh->do($cmd);
}

sub add_user {
  my ($self, $userid, $name, $email, $certificate) = @_;
  my $dbh = $self->{dbh};

  my $timet = PKI::Base::TimeTool->new();
  my $now = $timet->get_time();

  my $cmd = "insert into users (" . 
                   "uid" . "," .
                   "name" . "," .
                   "email" . "," .
                   "certificate" . "," .
                   "created_at" .
               ") values (" .
                   $dbh->quote($userid) . "," .
                   $dbh->quote($name) . "," .
                   $dbh->quote($email) . "," .
                   $dbh->quote($certificate) . "," .
                   $dbh->quote($now) .
               ")";
REDO_ADD_USER:
  eval {
    $dbh->do($cmd);
  };
  if ($dbh->err == 5) {
    sleep(1);
    goto REDO_ADD_USER;
  }
}

sub add_group {
  my ($self, $gid, $name) = @_;
  my $dbh = $self->{dbh};

  my $timet = PKI::Base::TimeTool->new();
  my $now = $timet->get_time();

  my $cmd = "insert into groups (" . 
                   "gid" . "," .
                   "name" . "," .
                   "created_at" .
               ") values (" .
                   $dbh->quote($gid) . "," .
                   $dbh->quote($name) . "," .
                   $dbh->quote($now) .
               ")";
REDO_ADD_GROUP:
  eval {
    $dbh->do($cmd);
  };
  if ($dbh->err == 5) {
    sleep(1);
    goto REDO_ADD_GROUP;
  }
}

sub delete_group {
  my ($self, $gid) = @_;
  my $dbh = $self->{dbh};

  my $timet = PKI::Base::TimeTool->new();
  my $now = $timet->get_time();

  my $cmd = "delete from roles where gid=" . $dbh->quote($gid);
  $dbh->do($cmd);
  $cmd = "delete from groups where gid=" . $dbh->quote($gid);
  $dbh->do($cmd);
}

sub list_users {
  my ($self, $startpos, $maxcount) = @_;
  my $dbh = $self->{dbh};
  my $select = "select * from users " .
                    "order by uid desc " .
                    "limit $startpos, $maxcount";
  my $sth = $dbh->prepare($select);
  $sth->execute();
  my @reqs;
  while (my $ref = $sth->fetchrow_hashref()) {
    push(@reqs, $ref);
  }
  $sth->finish();
  return @reqs;
}

sub list_groups {
  my ($self, $startpos, $maxcount) = @_;
  my $dbh = $self->{dbh};
  my $select = "select * from groups " .
                    "order by gid desc " .
                    "limit $startpos, $maxcount";
  my $sth = $dbh->prepare($select);
  $sth->execute();
  my @reqs;
  while (my $ref = $sth->fetchrow_hashref()) {
    push(@reqs, $ref);
  }
  $sth->finish();
  return @reqs;
}
#######################################
# Closes this store
#######################################
sub close {
  my ($self) = @_;
  my $dbh = $self->{dbh};
  $dbh->disconnect();
}

1;