TABOO/Data/User/Contributor.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 185 CC 14 SCAR 41.5
CC
14
Cov
0%
save
132.0
load
12.0
load_authlevel
6.0
CRAP 63.2
1 package AxKit::App::TABOO::Data::User::Contributor;
2 use strict;
3 use warnings;
4 use Carp;
5  
6 use AxKit::App::TABOO::Data;
7 use AxKit::App::TABOO::Data::User;
8 use vars qw/@ISA/;
9 @ISA = qw(AxKit::App::TABOO::Data::User);
10  
11 use DBI;
12  
13  
14 our $VERSION = '0.21';
15  
16  
17 =head1 NAME
18  
19 AxKit::App::TABOO::Data::User::Contributor - Contributor Data objects for TABOO
20  
21 =head1 SYNOPSIS
22  
23 use AxKit::App::TABOO::Data::User::Contributor;
24 $user = AxKit::App::TABOO::Data::User::Contributor->new(@dbconnectargs);
25 $user->load(what => '*', limit => {'username' => 'kjetil'});
26 my $fullname = $user->load_authlevel('kjetil');
27  
28 =head1 DESCRIPTION
29  
30 This Data class subclasses L<AxKit::App::TABOO::Data::User> to add an
31 authentication level and optional biographical information for a
32 contributor to a site.
33  
34  
35 =cut
36  
37 AxKit::App::TABOO::Data::User::Contributor->dbfrom("users INNER JOIN contributors ON (users.username = contributors.username)");
38 AxKit::App::TABOO::Data::User::Contributor->dbtable("users,contributors");
39 AxKit::App::TABOO::Data::User::Contributor->elementorder("username, name, email, uri, passwd, bio, authlevel");
40  
41 =head1 METHODS
42  
43 This class implements two methods, the rest is inherited from
44 L<AxKit::App::TABOO::Data::User>.
45  
46 =over
47  
48 =item C<new(@dbconnectargs)>
49  
50 The constructor. Makes sure that we inherit the data members from our
51 superclass. Apart from that, nothing special.
52  
53 =cut
54  
55 sub new {
56 my $that = shift;
57 my $class = ref($that) || $that;
58 my $self = $class->SUPER::new(@_);
59 $self->{authlevel} = 0;
60 $self->{bio} = undef;
61 bless($self, $class);
62 return $self;
63 }
64  
65  
66  
67 # We reimplement the load method with no changes to the API. Actually,
68 # the reason is to preserve the API, we need to do it because we're
69 # getting data from two tables, and they both have a username field.
70  
71 sub load {
72 my ($self, %args) = @_;
73 my $tmp = $args{'limit'}{'username'};
74 if ($tmp) {
75 delete $args{'limit'}{'username'};
76 $args{'limit'}{'users.username'} = $tmp;
77 }
78  
79 my $data = $self->_load(%args);
80 if ($data) {
81 ${$self}{'ONFILE'} = 1;
82 } else {
83 return undef;
84 }
85 foreach my $key (keys(%{$data})) {
86 ${$self}{$key} = ${$data}{$key};
87 }
88 return $self;
89 }
90  
91  
92  
93 =item C<load_authlevel($username)>
94  
95 This is an ad hoc method to retrieve the authorization level of a
96 user, and it takes a C<$username> key to identify whose level to
97 retrieve. It will return a number that may be used to decide whether
98 or not to grant access to an object or a data member. It will also
99 populate the corresponding data fields of the object. You may
100 therefore call C<write_xml> on the object afterwards and have markup
101 for the username and level.
102  
103 =cut
104  
105 sub load_authlevel {
106 my $self = shift;
107 my $username = shift;
108 my $dbh = DBI->connect($self->dbconnectargs());
109 my $sth = $dbh->prepare("SELECT authlevel FROM contributors WHERE username=?");
110 $sth->execute($username);
111 my ($data) = $sth->fetchrow_array;
112 if ($data) {
113 ${$self}{'ONFILE'} = 1;
114 }
115 ${$self}{'authlevel'} = $data;
116 ${$self}{'username'} = $username;
117 return ${$self}{'authlevel'};
118 }
119  
120  
121  
122 =item C<save()>
123  
124 The C<save()> method has been reimplemented in this class. It is less
125 generic than the method of the grandparent class, but it saves data to
126 two different tables, and should do its job well. It takes no
127 parameters.
128  
129  
130 =cut
131  
132 sub save {
133 my $self = shift;
134 my $dbh = DBI->connect($self->dbconnectargs());
135 my $seq_id;
136 my (@fields, @confields);
137 my $i=0;
138 my $j=0;
139 foreach my $key (keys(%{$self})) {
140 next if ($key =~ m/[A-Z]/); # Uppercase keys are not in db
141 next unless defined(${$self}{$key}); # No need to insert something that isn't there
142 if (($key eq 'bio') || ($key eq 'authlevel')) {
143 # TODO: This is too ad-hoc, should have a better way to split the keys
144 push(@confields, $key);
145 $j++;
146 } elsif (($key eq 'username') && (! ${$self}{'ONFILE'})) {
147 push(@confields, $key);
148 $j++;
149 push(@fields, $key);
150 $i++;
151 } else {
152 push(@fields, $key);
153 $i++;
154 }
155 }
156 if (($i == 0) && ($j == 0)) {
157 carp "No data fields with anything to save";
158 } else {
159 my ($sth1, $sth2, $query);
160 if (${$self}{'ONFILE'}) {
161 $sth1 = $dbh->prepare("UPDATE users SET " . join('=?,', @fields) . "=? WHERE username=?");
162 $sth2 = $dbh->prepare("UPDATE contributors SET " . join('=?,', @confields) . "=? WHERE username=?");
163 } else {
164 ($seq_id) = $dbh->selectrow_array("SELECT NEXTVAL('users_id_seq')");
165 $sth1 = $dbh->prepare("INSERT INTO users (" . join(',', @fields) . ",ID) VALUES (" . '?,' x $i . '?)');
166 $query = "INSERT INTO contributors (" . join(',', @confields) . ",Users_ID) VALUES (" . '?,' x $j . '?)';
167 $sth2 = $dbh->prepare($query);
168 }
169 # warn "QUERY: $query";
170 my $k=1;
171 foreach my $key (@fields) {
172 $sth1->bind_param($k, ${$self}{$key});
173 $k++;
174 }
175 if (${$self}{'ONFILE'}) {
176 $sth1->bind_param($k, ${$self}{'username'});
177 } else {
178 $sth1->bind_param($k, $seq_id);
179 }
180 $k=1;
181 foreach my $key (@confields) {
182 $sth2->bind_param($k, ${$self}{$key});
183 $k++;
184 }
185 if (${$self}{'ONFILE'}) {
186 $sth2->bind_param($k, ${$self}{'username'});
187 } else {
188 $sth2->bind_param($k, $seq_id);
189 }
190 if ($i > 0) {
191 $sth1->execute();
192 }
193 if ($j > 0) {
194 $sth2->execute();
195 }
196 }
197 return $self;
198 }
199  
200  
201  
202 =back
203  
204 =head1 STORED DATA
205  
206 The data is stored in named fields, and for certain uses, it is good
207 to know them. If you want to subclass this class, you might want to
208 use the same names, see the documentation of
209 L<AxKit::APP::TABOO::Data> for more about this.
210  
211 In addition to the names of the
212 L<parent|AxKit::APP::TABOO::Data::User>, this class adds the following
213 fields:
214  
215  
216 =over
217  
218 =item * authlevel
219  
220 An integer representing the authorization level of a user. In the
221 present implementation, it is a signed two-byte integer. It is
222 intended to be used to decide whether or not to grant access to an
223 object or a data member.
224  
225 =item * bio
226  
227 The contributors biographical information.
228  
229 =back
230  
231 This is likely to be extended in future versions.
232  
233 =head1 BUGS/TODO
234  
235 You cannot use the save method in this class to save an object in the
236 case where there is a record for the parent class, but lacks one for
237 this class.
238  
239  
240 =head1 FORMALITIES
241  
242 See L<AxKit::App::TABOO>.
243  
244 =cut
245  
246 1;