blib/lib/Smolder/DB/Developer.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 99 CC 6 SCAR 15.8
CC
6
Cov
0%
groups
12.0
get_guest
6.0
_crypt_password
6.0
CRAP 4.8
1 package Smolder::DB::Developer;
2 use strict;
3 use warnings;
4 use base 'Smolder::DB';
5 use Data::Random qw(rand_chars);
6 use Smolder::DB::Project;
7  
8 __PACKAGE__->set_up_table('developer');
9  
10 =head1 NAME
11  
12 Smolder::DB::Developer
13  
14 =head1 DESCRIPTION
15  
16 L<Class::DBI> based model class for the 'developer' table in the database.
17  
18 =head1 METHODS
19  
20 =head2 ACCESSSOR/MUTATORS
21  
22 Each column in the borough table has a method with the same name
23 that can be used as an accessor and mutator.
24  
25 The following columns will return objects instead of the value contained in the table:
26  
27 =cut
28  
29 # encrypt the password if we are creating or updating
30 __PACKAGE__->add_trigger(before_create => \&_crypt_password,);
31 __PACKAGE__->add_trigger(before_update => \&_crypt_if_changed,);
32  
33 # make sure we delete a preference when we are deleted
34 __PACKAGE__->add_trigger(
35 after_delete => sub {
36 my $self = shift;
37 $self->preference->delete() if $self->preference;
38 },
39 );
40  
41 =over
42  
43 =item preference
44  
45 This is their default, L<Smolder::DB::Preference> object.
46  
47 =cut
48  
49 __PACKAGE__->has_a(preference => 'Smolder::DB::Preference');
50  
51 =item project_developers
52  
53 Returns a list of L<Smolder::DB::ProjectDeveloper> objects that are connected to this
54 Developer.
55  
56 =cut
57  
58 __PACKAGE__->has_many('project_developers' => 'Smolder::DB::ProjectDeveloper');
59  
60 =item smoke_reports
61  
62 A list of L<Smolder::DB::SmokeReport> that were added by this Developer.
63  
64 =cut
65  
66 __PACKAGE__->has_many('smoke_reports' => 'Smolder::DB::SmokeReport');
67  
68 =back
69  
70 =head2 OBJECT METHODS
71  
72 =head3 project_pref
73  
74 Given a L<Smolder::DB::Project> object, this returns the L<Smolder::DB::Preference>
75 object associated with that project and this Developer.
76  
77 =cut
78  
79 sub project_pref {
80 my ($self, $project) = @_;
81 my $sth = $self->db_Main->prepare_cached(
82 qq(
83 SELECT preference.* FROM preference, project_developer
84 WHERE preference.id = project_developer.preference
85 AND project_developer.developer = ?
86 AND project_developer.project = ?
87 )
88 );
89 $sth->execute($self->id, $project->id);
90  
91 # there should be only one, but it returns an iterator unless
92 # in list context
93 my @prefs = Smolder::DB::Preference->sth_to_objects($sth);
94 return $prefs[0];
95 }
96  
97 =head3 full_name
98  
99 Returns the full name of the Developer, in the following format:
100  
101 First Last
102  
103 =cut
104  
105 sub full_name {
106 my $self = shift;
107 return $self->fname . ' ' . $self->lname;
108 }
109  
110 =head3 email_hidden
111  
112 Returns the email address in HTML formatted to foil email harvesting bots.
113 For example, the email address
114
115 test@example.com
116  
117 Will become
118  
119 TODO
120  
121 =cut
122  
123 sub email_hidden {
124 my $self = shift;
125  
126 # TODO - hide somehow
127 return $self->email;
128 }
129  
130 =head3 reset_password
131  
132 Creates a new random password of between 6 and 8 characters suitable and
133 sets it as this Developer's password. This new password is returned unencrypted.
134  
135 =cut
136  
137 sub reset_password {
138 my $self = shift;
139 my $new_pw = join('', rand_chars(set => 'alphanumeric', min => 6, max => 8, shuffle => 1));
140 $self->set('password' => $new_pw);
141 $self->update();
142  
143 return $new_pw;
144 }
145  
146 =head3 projects
147  
148 Returns an array ref of all the L<Smolder::DB::Project>s that this Developer is a member
149 of (using the C<project_developer> join table).
150  
151 =cut
152  
153 sub projects {
154 my $self = shift;
155 my $sth = $self->db_Main->prepare_cached(
156 qq(
157 SELECT project.* FROM project, project_developer
158 WHERE project_developer.project = project.id AND project_developer.developer = ?
159 ORDER BY project_developer.added
160 )
161 );
162 $sth->execute($self->id);
163 return Smolder::DB::Project->sth_to_objects($sth);
164 }
165  
166 =head3 groups
167  
168 Returns the names of the groups this developer is in
169  
170 =cut
171  
172 sub groups {
173 my $self = shift;
174 my @groups;
175 push(@groups, 'developer') if !$self->guest;
176 push(@groups, 'admin') if $self->admin;
177 return @groups;
178 }
179  
180 =head2 CLASS METHODS
181  
182 =head2 get_guest
183  
184 This method will return a user 'anonymous' who is marked as a 'guest'. If this
185 user does not exist, one will be created.
186  
187 =cut
188  
189 sub get_guest {
190 my $pkg = shift;
191 my ($guest) = $pkg->search(
192 guest => 1,
193 username => 'anonymous',
194 );
195  
196 unless ($guest) {
197 my $fake_pw = join(
198 '',
199 rand_chars(
200 set => 'alphanumeric',
201 min => 6,
202 max => 8,
203 shuffle => 1
204 )
205 );
206 $guest = $pkg->create(
207 {
208 guest => 1,
209 username => 'anonymous',
210 password => $fake_pw,
211 preference => Smolder::DB::Preference->create({email_freq => 'never'}),
212 }
213 );
214 }
215  
216 return $guest;
217 }
218  
219 sub _crypt_password {
220 my $self = shift;
221 my $salt = join('', rand_chars(set => 'alphanumeric', size => 2, shuffle => 1));
222 my ($pw) = ($self->_attrs('password'));
223 if ($pw) {
224 $self->_attribute_set(password => crypt($pw, $salt));
225 }
226 }
227  
228 sub _crypt_if_changed {
229 my $self = shift;
230 if (grep { $_ eq 'password' } $self->is_changed()) {
231 $self->_crypt_password();
232 }
233 }
234  
235 1;
236