blib/lib/UserDB.pm

filter: Statementstmt 8.4% 18 / 214 Branchbran 1.3% 1 / 74 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 31.2% 5 / 16 Podpod 100% 12 / 12 totaltotal 9.3% 36 / 386 CC 54 SCAR 39.2
CC
54
Cov
6%
add_to_group
90.0
remove_from_group
90.0
members_of_group
90.0
CRAP 50.5
↑ Summary
1 package UserDB;
2
3 1 ▶use 5.016003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 1 our $VERSION = '1.00';
9
10 # Open a user database or create one if needed
11 sub new
12 1 ▶{
Subroutine new: called CC=5 SCAR=27.4
Pod documented
13 1 my $class = shift;
14 1 my ($filename) = @_;
15 1 my $sql;
16 0 ▶ if(!$filename) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 1 1
17 1 ▶ my $db = DBI->connect("dbi:SQLite:dbname=" . $filename, "", "", { RaiseError => 0, PrintError => 0 }) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $db = "DBI"->connect('dbi:SQLite:dbname=' . $filename, "", "", {"RaiseError", 0, "PrintError", 0}) or return undef 0 0 0
18 $sql = $db->prepare("SELECT * FROM users WHERE 0 = 1;") or do
19 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$sql = $db->prepare("SELECT * FROM users WHERE 0 = 1;") or do { $sql = $db->prepare("CREATE TABLE users (username TEXT, password TEXT, name TEXT, email TEXT, profile TEXT, phone TEXT, manager TEXT, department TEXT, url TEXT, notes TEXT);"); $sql->execute } 0 0 0
20 0 $sql = $db->prepare("CREATE TABLE users (username TEXT, password TEXT, name TEXT, email TEXT, profile TEXT, phone TEXT, manager TEXT, department TEXT, url TEXT, notes TEXT);");
21 0 $sql->execute();
22 };
23 0 $sql->finish();
24 $sql = $db->prepare("SELECT * FROM groups WHERE 0 = 1;") or do
25 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$sql = $db->prepare("SELECT * FROM groups WHERE 0 = 1;") or do { $sql = $db->prepare("CREATE TABLE groups (groupname TEXT, members TEXT);"); $sql->execute } 0 0 0
26 0 $sql = $db->prepare("CREATE TABLE groups (groupname TEXT, members TEXT);");
27 0 $sql->execute();
28 };
29 0 $sql->finish();
30 0 my $self = bless({ db => $db, error => ""}, $class);
31 0 return $self;
32 }
33
34 # Create a new user
35 sub create_user
36 0 ▶{
Subroutine create_user: not called CC=4 SCAR=30.0
Pod documented
37 0 my ($self, $username) = @_;
38 0 my $sql;
39 0 my $db = $self->{db};
40 0 $self->{error} = "";
41 0 ▶ if(!$username || $username eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $username or $username eq "") 0 0 0
Condition0%0/3
not $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $username or $username eq ""
not $username $username eq ""
Truth table0%0/3
not(A) or B
A= not $usernameB= $username eq ""
ABresult
1X1
011
000
42 {
43 0 $self->{error} = "User name not specified.";
44 0 return undef;
45 }
46 0 $sql = $db->prepare("SELECT ROWID FROM users WHERE username = ?;");
47 0 $sql->execute($username);
48 0 ▶ while(my @res = $sql->fetchrow_array())
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
49 {
50 0 $self->{error} = "User name already exists.";
51 0 return undef;
52 }
53 0 my $sql = $db->prepare("INSERT INTO users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
54 0 $sql->execute($username, "", "", "", "", "", "", "", "", "");
55 0 return 1;
56 }
57
58 # Get attributes for a username
59 sub get_attributes
60 0 ▶{
Subroutine get_attributes: not called CC=4 SCAR=30.0
Pod documented
61 0 my ($self, $username) = @_;
62 0 my $sql;
63 0 my $db = $self->{db};
64 0 $self->{error} = "";
65 0 ▶ if(!$username || $username eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $username or $username eq "") 0 0 0
Condition0%0/3
not $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $username or $username eq ""
not $username $username eq ""
Truth table0%0/3
not(A) or B
A= not $usernameB= $username eq ""
ABresult
1X1
011
000
66 {
67 0 $self->{error} = "User name not specified.";
68 0 return undef;
69 }
70 0 $sql = $db->prepare("SELECT * FROM users WHERE username = ?;");
71 0 $sql->execute($username);
72 0 ▶ while(my @res = $sql->fetchrow_array())
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
73 {
74 0 return ("name", $res[2], "email", $res[3], "profile", $res[4], "phone", $res[5], "manager", $res[6], "department", $res[7], "url", $res[8], "notes", $res[9]);
75 }
76 0 $self->{error} = "Username not found.";
77 0 return undef;
78 }
79
80 # Set attributes for a userid
81 sub set_attributes
82 0 ▶{
Subroutine set_attributes: not called CC=5 SCAR=34.0
Pod documented
83 0 my ($self, $username, %attrs) = @_;
84 0 my $sql;
85 0 my $db = $self->{db};
86 0 $self->{error} = "";
87 0 ▶ if(!$username || $username eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $username or $username eq "") 0 0 0
Condition0%0/3
not $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $username or $username eq ""
not $username $username eq ""
Truth table0%0/3
not(A) or B
A= not $usernameB= $username eq ""
ABresult
1X1
011
000
88 {
89 0 $self->{error} = "User name not specified.";
90 0 return undef;
91 }
92 0 foreach my $attr (("name", "email", "profile", "phone", "manager", "department", "url", "notes"))
93 {
94 0 ▶ if($attrs{lc($attr)})
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs{lc $attr}) 0 0 0
95 {
96 0 $sql = $db->prepare("UPDATE users SET " . $attr . " = ? WHERE username = ?;");
97 0 $sql->execute($attrs{lc($attr)}, $username);
98 }
99 }
100 0 return 1;
101 }
102
103 # Create a new group
104 sub create_group
105 0 ▶{
Subroutine create_group: not called CC=4 SCAR=30.0
Pod documented
106 0 my ($self, $groupname) = @_;
107 0 my $sql;
108 0 my $db = $self->{db};
109 0 $self->{error} = "";
110 0 ▶ if(!$groupname || $groupname eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $groupname or $groupname eq "") 0 0 0
Condition0%0/3
not $groupname or $groupname eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $groupname or $groupname eq ""
not $groupname $groupname eq ""
Truth table0%0/3
not(A) or B
A= not $groupnameB= $groupname eq ""
ABresult
1X1
011
000
111 {
112 0 $self->{error} = "Group name not specified.";
113 0 return undef;
114 }
115 0 $sql = $db->prepare("SELECT ROWID FROM groups WHERE groupname = ?;");
116 0 $sql->execute($groupname);
117 0 ▶ while(my @res = $sql->fetchrow_array())
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
118 {
119 0 $self->{error} = "Group name already exists.";
120 0 return undef;
121 }
122 0 my $sql = $db->prepare("INSERT INTO groups VALUES (?, ?);");
123 0 $sql->execute($groupname, "");
124 0 return 1;
125 }
126
127 # Add a user to a group
128 sub add_to_group
129 0 ▶{
Subroutine add_to_group: not called CC=9 SCAR=45.0
Pod documented
130 0 my ($self, $username, $groupname) = @_;
131 0 my $sql;
132 0 my $db = $self->{db};
133 0 $self->{error} = "";
134 0 ▶ if(!$username || $username eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $username or $username eq "") 0 0 0
Condition0%0/3
not $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $username or $username eq ""
not $username $username eq ""
Truth table0%0/3
not(A) or B
A= not $usernameB= $username eq ""
ABresult
1X1
011
000
135 {
136 0 $self->{error} = "User name not specified.";
137 0 return undef;
138 }
139 0 ▶ if(!$groupname || $groupname eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $groupname or $groupname eq "") 0 0 0
Condition0%0/3
not $groupname or $groupname eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $groupname or $groupname eq ""
not $groupname $groupname eq ""
Truth table0%0/3
not(A) or B
A= not $groupnameB= $groupname eq ""
ABresult
1X1
011
000
140 {
141 0 $self->{error} = "Group name not specified.";
142 0 return undef;
143 }
144 0 my $userid = -1;
145 0 $sql = $db->prepare("SELECT ROWID FROM users WHERE username = ?;");
146 0 $sql->execute($username);
147 0 ▶ while(my @res = $sql->fetchrow_array()) { $userid = $res[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
148 0 ▶ if($userid == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($userid == -1) 0 0 0
149 {
150 0 $self->{error} = "User name not found.";
151 0 return undef;
152 }
153 0 $sql = $db->prepare("SELECT members FROM groups WHERE groupname = ?;");
154 0 $sql->execute($groupname);
155 0 my $members;
156 0 ▶ while(my @res = $sql->fetchrow_array()) { $members = $res[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
157 0 ▶ if(!defined($members))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $members) 0 0 0
158 {
159 0 $self->{error} = "Group name not found.";
160 0 return undef;
161 }
162 0 $members =~ s/(\|$userid\|)//g;
163 0 $members .= "|" . $userid . "|";
164 0 $sql = $db->prepare("UPDATE groups SET members = ? WHERE groupname = ?");
165 0 $sql->execute($members, $groupname);
166 0 return 1;
167 }
168
169 # Remove a user from a group
170 sub remove_from_group
171 0 ▶{
Subroutine remove_from_group: not called CC=9 SCAR=45.0
Pod documented
172 0 my ($self, $username, $groupname) = @_;
173 0 my $sql;
174 0 my $db = $self->{db};
175 0 $self->{error} = "";
176 0 ▶ if(!$username || $username eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $username or $username eq "") 0 0 0
Condition0%0/3
not $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $username or $username eq ""
not $username $username eq ""
Truth table0%0/3
not(A) or B
A= not $usernameB= $username eq ""
ABresult
1X1
011
000
177 {
178 0 $self->{error} = "User name not specified.";
179 0 return undef;
180 }
181 0 ▶ if(!$groupname || $groupname eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $groupname or $groupname eq "") 0 0 0
Condition0%0/3
not $groupname or $groupname eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $groupname or $groupname eq ""
not $groupname $groupname eq ""
Truth table0%0/3
not(A) or B
A= not $groupnameB= $groupname eq ""
ABresult
1X1
011
000
182 {
183 0 $self->{error} = "Group name not specified.";
184 0 return undef;
185 }
186 0 my $userid = -1;
187 0 $sql = $db->prepare("SELECT ROWID FROM users WHERE username = ?;");
188 0 $sql->execute($username);
189 0 ▶ while(my @res = $sql->fetchrow_array()) { $userid = $res[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
190 0 ▶ if($userid == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($userid == -1) 0 0 0
191 {
192 0 $self->{error} = "User name not found.";
193 0 return undef;
194 }
195 0 $sql = $db->prepare("SELECT members FROM groups WHERE groupname = ?;");
196 0 $sql->execute($groupname);
197 0 my $members;
198 0 ▶ while(my @res = $sql->fetchrow_array()) { $members = $res[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
199 0 ▶ if(!defined($members))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $members) 0 0 0
200 {
201 0 $self->{error} = "Group name not found.";
202 0 return undef;
203 }
204 0 $members =~ s/(\|$userid\|)//g;
205 0 $sql = $db->prepare("UPDATE groups SET members = ? WHERE groupname = ?");
206 0 $sql->execute($members, $groupname);
207 0 return 1;
208 }
209
210 # Return members of a group
211 sub members_of_group
212 0 ▶{
Subroutine members_of_group: not called CC=9 SCAR=45.0
Pod documented
213 0 my ($self, $groupname) = @_;
214 0 my $sql;
215 0 my $db = $self->{db};
216 0 $self->{error} = "";
217 0 ▶ if(!$groupname || $groupname eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $groupname or $groupname eq "") 0 0 0
Condition0%0/3
not $groupname or $groupname eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $groupname or $groupname eq ""
not $groupname $groupname eq ""
Truth table0%0/3
not(A) or B
A= not $groupnameB= $groupname eq ""
ABresult
1X1
011
000
218 {
219 0 $self->{error} = "Group name not specified.";
220 0 return undef;
221 }
222 0 $sql = $db->prepare("SELECT members FROM groups WHERE groupname = ?;");
223 0 $sql->execute($groupname);
224 0 my @memberids;
225 0 ▶ while(my @res = $sql->fetchrow_array()) { @memberids = split(/\|/, $res[0]); }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
226 0 ▶ if(!@memberids)
Branch: 1 branch
BranchTrueFalseTotal
unless (@memberids) 0 0 0
227 {
228 0 $self->{error} = "Group name not found.";
229 0 return undef;
230 }
231 0 my @users;
232 0 $sql = $db->prepare("SELECT ROWID,username FROM users;");
233 0 $sql->execute();
234 0 ▶ while(my @res = $sql->fetchrow_array()) { $users[int($res[0])] = $res[1]; }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
235 0 my @members;
236 0 foreach my $id (@memberids)
237 {
238 0 ▶ if($id ne "|" && $id ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($id ne "|" and $id ne "") 0 0 0
Condition0%0/3
$id ne "|" and $id ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$id ne "|" and $id ne ""
$id ne "|" $id ne ""
Truth table0%0/3
A and B
A= $id ne "|"B= $id ne ""
ABresult
0X0
100
111
239 {
240 0 push(@members, $users[int($id)]);
241 }
242 }
243 0 return @members;
244 }
245
246 # Set password for a user
247 sub set_password
248 0 ▶{
Subroutine set_password: not called CC=5 SCAR=34.0
Pod documented
249 0 my ($self, $username, $password) = @_;
250 0 my $sql;
251 0 my $db = $self->{db};
252 0 $self->{error} = "";
253 0 ▶ if(!$username || $username eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $username or $username eq "") 0 0 0
Condition0%0/3
not $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $username or $username eq ""
not $username $username eq ""
Truth table0%0/3
not(A) or B
A= not $usernameB= $username eq ""
ABresult
1X1
011
000
254 {
255 0 $self->{error} = "User name not specified.";
256 0 return undef;
257 }
258 0 ▶ if(!$password || $password eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $password or $password eq "") 0 0 0
Condition0%0/3
not $password or $password eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $password or $password eq ""
not $password $password eq ""
Truth table0%0/3
not(A) or B
A= not $passwordB= $password eq ""
ABresult
1X1
011
000
259 {
260 0 $self->{error} = "Password not specified.";
261 0 return undef;
262 }
263 0 $sql = $db->prepare("UPDATE users SET password = ? WHERE username = ?");
264 0 $sql->execute(sha256_hex($password), $username);
265 0 return 1;
266 }
267
268 # Check password for a user
269 sub check_password
270 0 ▶{
Subroutine check_password: not called CC=7 SCAR=40.3
Pod documented
271 0 my ($self, $username, $password) = @_;
272 0 my $sql;
273 0 my $db = $self->{db};
274 0 $self->{error} = "";
275 0 ▶ if(!$username || $username eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $username or $username eq "") 0 0 0
Condition0%0/3
not $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $username or $username eq ""
not $username $username eq ""
Truth table0%0/3
not(A) or B
A= not $usernameB= $username eq ""
ABresult
1X1
011
000
276 {
277 0 $self->{error} = "User name not specified.";
278 0 return undef;
279 }
280 0 ▶ if(!$password || $password eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (not $password or $password eq "") 0 0 0
Condition0%0/3
not $password or $password eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not $password or $password eq ""
not $password $password eq ""
Truth table0%0/3
not(A) or B
A= not $passwordB= $password eq ""
ABresult
1X1
011
000
281 {
282 0 $self->{error} = "Password not specified.";
283 0 return undef;
284 }
285 0 $sql = $db->prepare("SELECT password FROM users WHERE username = ?");
286 0 $sql->execute($username);
287 0 ▶ while(my @res = $sql->fetchrow_array())
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
288 {
289 0 ▶ if($res[0] eq sha256_hex($password)) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($res[0] eq &sha256_hex($password)) 0 0 0
290 }
291 0 $self->{error} = "Invalid password.";
292 0 return undef;
293 }
294
295 # List users
296 sub list_users
297 0 ▶{
Subroutine list_users: not called CC=3 SCAR=24.8
Pod documented
298 0 my ($self) = @_;
299 0 my $sql;
300 0 my $db = $self->{db};
301 0 $self->{error} = "";
302 0 $sql = $db->prepare("SELECT username FROM users;");
303 0 $sql->execute();
304 0 my @users;
305 0 ▶ while(my @res = $sql->fetchrow_array()) { push(@users, $res[0]); }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sql->fetchrow_array) 0 0 0
306 0 ▶ if(!@users) { $self->{error} = "No user found."; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@users) 0 0 0
307 0 return @users;
308 }
309
310 # Get the last error
311 sub error
312 0 ▶{
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
313 0 my ($self) = @_;
314 0 return $self->{error};
315 }
316
317 1 1;
318
319 __END__
↑ Summary