TABOO/Data/User.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 84 CC 3 SCAR 12.0
CC
3
Cov
0%
load_name
6.0
load_passwd
6.0
new
2.0
CRAP 3.3
1 package AxKit::App::TABOO::Data::User;
2 use strict;
3 use warnings;
4 use Carp;
5 use Encode;
6  
7 use AxKit::App::TABOO::Data;
8 use vars qw/@ISA/;
9 @ISA = qw(AxKit::App::TABOO::Data);
10  
11 use DBI;
12  
13 our $VERSION = '0.3';
14  
15  
16 =head1 NAME
17  
18 AxKit::App::TABOO::Data::User - User Data objects for TABOO
19  
20 =head1 SYNOPSIS
21  
22 use AxKit::App::TABOO::Data::User;
23 $user = AxKit::App::TABOO::Data::User->new(@dbconnectargs);
24 $user->load(what => '*', limit => {username => 'kjetil'});
25 my $fullname = $user->load_name('kjetil');
26  
27 =head1 DESCRIPTION
28  
29 This Data class contains basic user information, such as name, e-mail
30 address, an encrypted password, and so on.
31  
32 =cut
33  
34 AxKit::App::TABOO::Data::User->dbfrom("users");
35 AxKit::App::TABOO::Data::User->dbtable("users");
36 AxKit::App::TABOO::Data::User->dbprimkey("username");
37 AxKit::App::TABOO::Data::User->elementorder("username, name, email, uri, passwd");
38  
39 =head1 METHODS
40  
41 This class implements two methods, the rest is inherited from
42 L<AxKit::App::TABOO::Data>.
43  
44 =over
45  
46 =item C<new(@dbconnectargs)>
47  
48 The constructor. Nothing special.
49  
50 =cut
51  
52 sub new {
53 my $that = shift;
54 my $class = ref($that) || $that;
55 my $self = {
56 username => undef,
57 name => undef,
58 email => undef,
59 uri => undef,
60 passwd => undef,
61 DBCONNECTARGS => \@_,
62 XMLELEMENT => 'user',
63 XMLPREFIX => 'user',
64 XMLNS => 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/User/Output',
65 ONFILE => undef,
66 };
67 bless($self, $class);
68 return $self;
69 }
70  
71 =item C<load_name($username)>
72  
73 This is an ad hoc method to retrieve the full name of a user, and it
74 takes a C<$username> key to identify the user to retrieve. It will
75 return a string with the full name, but it will also populate the
76 corresponding data fields of the object. You may therefore call
77 C<write_xml> on the object afterwards and have markup for the username
78 and name.
79  
80 =cut
81  
82 sub load_name {
83 my $self = shift;
84 my $username = shift;
85 my $dbh = DBI->connect($self->dbconnectargs());
86 my $sth = $dbh->prepare("SELECT name FROM users WHERE username=?");
87 $sth->execute($username);
88 my @data = $sth->fetchrow_array;
89 if (@data) {
90 ${$self}{'ONFILE'} = 1;
91 }
92 ${$self}{'name'} = Encode::decode_utf8(join('', @data));
93 ${$self}{'username'} = $username;
94 return ${$self}{'name'};
95 }
96  
97 =item C<load_passwd($username)>
98  
99 This is an ad hoc method to retrieve the encrypted password of a user,
100 and it takes a C<$username> key to identify the user to retrieve. It
101 will return a string with the encrypted password, but it will also
102 populate the corresponding data fields of the object. You may
103 therefore call C<write_xml> on the object afterwards and have markup
104 for the username and passwd.
105  
106 =cut
107  
108 sub load_passwd {
109 my $self = shift;
110 my $username = shift;
111 my $dbh = DBI->connect($self->dbconnectargs());
112 my $sth = $dbh->prepare("SELECT passwd FROM users WHERE username=?");
113 $sth->execute($username);
114 my @data = $sth->fetchrow_array;
115 if (@data) {
116 ${$self}{'ONFILE'} = 1;
117 }
118 ${$self}{'passwd'} = join('', @data);
119 ${$self}{'username'} = $username;
120 return ${$self}{'passwd'};
121 }
122  
123 =back
124  
125 =head1 STORED DATA
126  
127 The data is stored in named fields, and for certain uses, it is good
128 to know them. If you want to subclass this class, you might want to
129 use the same names, see the documentation of
130 L<AxKit::App::TABOO::Data> for more about this.
131  
132 It is natural to subclass this as TABOO grows: One may record more
133 information about contributors to the site, or customers for a
134 webshop. For an example, see
135 L<AxKit::App::TABOO::Data::User::Contributor>
136  
137 These are the names of the stored data of this class:
138  
139 =over
140  
141 =item * username
142  
143 A simple word containing a unique name and identifier for the
144 category. Usually known as a username...
145  
146 =item * name
147  
148 The person's full name.
149  
150 =item * email
151  
152 The person's e-mail address.
153  
154 =item * uri
155  
156 In the Semantic Web you'd like to identify things and their
157 relationships with URIs. So, we try to record URIs for everybody. For
158 those who have stable home page, it may be convenient to use that URL,
159 but for others, we may just have to come up with something smart.
160  
161 =item * passwd
162  
163 The user's encrypted password. Allthough it I<is> encrypted, you may
164 not want to throw it around too much. Perhaps it should have been
165 stored somewhere else entirely. YMMV.
166  
167 =back
168  
169 =head1 XML representation
170  
171 The C<write_xml()> method, implemented in the parent class, can be
172 used to create an XML representation of the data in the object. The
173 above names will be used as element names. The C<xmlelement()>,
174 C<xmlns()> and C<xmlprefix()> methods can be used to set the name of
175 the root element, the namespace URI and namespace prefix
176 respectively. Usually, it doesn't make sense to change the default
177 namespace or prefix, that are
178  
179  
180 =over
181  
182 =item * C<user>
183  
184 =item * C<http://www.kjetil.kjernsmo.net/software/TABOO/NS/User/Output>
185  
186 =item * C<user>
187  
188 =back
189  
190  
191  
192 =head1 FORMALITIES
193  
194 See L<AxKit::App::TABOO>.
195  
196 =cut
197  
198 1;