blib/lib/Smolder/Control/Admin/Developers.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 184 CC 14 SCAR 22.4
CC
14
Cov
0%
process_edit
20.0
details
20.0
reset_pw
12.0
CRAP 9.4
1 package Smolder::Control::Admin::Developers;
2 use strict;
3 use warnings;
4 use base 'Smolder::Control';
5 use Smolder::DB::Project;
6 use Smolder::DB::Developer;
7 use Smolder::Email;
8 use Smolder::Constraints qw(email unsigned_int length_max length_between bool unique_field_value);
9 use Email::Valid;
10  
11 =head1 NAME
12  
13 Smolder::Control::Admin::Developers
14  
15 =head1 DESCRIPTION
16  
17 Controller module for Admin activities concerning Developers
18  
19 =cut
20  
21 sub setup {
22 my $self = shift;
23 $self->start_mode('list');
24 $self->run_modes(
25 [
26 qw(
27 add
28 process_add
29 edit
30 process_edit
31 list
32 delete
33 reset_pw
34 details
35 )
36 ]
37 );
38 }
39  
40 sub require_group { 'admin' }
41  
42 =head1 RUN MODES
43  
44 =head2 reset_pw
45  
46 Allow an admin to reset the password of a developer to a new random string
47 and then email the new password to the developer. Uses the F<Email/reset_pw.tmpl>
48 template for the email and the F<Admin/Developers/resetpw_success.tmpl> for
49 displaying the result.
50  
51 =cut
52  
53 sub reset_pw {
54 my $self = shift;
55 my $dev = Smolder::DB::Developer->retrieve($self->param('id'));
56 return $self->error_message("Developer no longer exists!")
57 unless $dev;
58  
59 my $email = $dev->email;
60 my $user = $dev->username;
61 my $new_pw = $dev->reset_password();
62  
63 # send the email
64 my $error = Smolder::Email->send_mime_mail(
65 name => 'reset_pw',
66 to => $email,
67 subject => 'Reset of password by Admin',
68 tt_params => {
69 developer => $dev,
70 new_pw => $new_pw,
71 },
72 );
73 if ($error) {
74 $self->add_message(
75 msg => "Could not send email to '$user'. Please check error logs!",
76 type => 'warning',
77 );
78 $self->log->warning("Could not send 'reset_pw' email to $email - $error");
79 } else {
80 $self->add_message(msg =>
81 "Password for '$user' has been reset and an email sent with their new password.");
82 }
83 return ' ';
84 }
85  
86 =head2 edit
87  
88 Show the edit form to allow an admin to edit the data about a developer.
89 Uses the F<Admin/Developers/edit.tmpl> template.
90  
91 =cut
92  
93 sub edit {
94 my ($self, $err_msgs) = @_;
95 my $developer = Smolder::DB::Developer->retrieve($self->param('id'));
96 my $output;
97  
98 # if we have any error messages, then just re-fill the form
99 # and show them
100 if ($err_msgs) {
101 $err_msgs->{developer} = $developer;
102 $output = HTML::FillInForm->new->fill(
103 scalarref => $self->tt_process($err_msgs),
104 qobject => $self->query,
105 );
106  
107 # else get the developer in question
108 } else {
109 my %developer_data = $developer->vars();
110 $output = HTML::FillInForm->new->fill(
111 scalarref => $self->tt_process({developer => $developer}),
112 fdat => \%developer_data,
113 );
114 }
115 return $output;
116 }
117  
118 =head2 process_edit
119  
120 Processes the incoming data from the C<edit> mode and update the
121 developers info in the database if validation passes. Uses the
122 F<Admin/Developers/edit_success.tmpl> template.
123  
124 =cut
125  
126 sub process_edit {
127 my $self = shift;
128 my $id = $self->param('id');
129 my $form = {
130 required => [qw(username fname lname email admin)],
131 constraint_methods => {
132 username => [length_max(255), unique_field_value('developer', 'username', $id),],
133 fname => length_max(255),
134 lname => length_max(255),
135 email => email(),
136 admin => bool(),
137 },
138 };
139  
140 my $results = $self->check_rm('edit', $form)
141 || return $self->check_rm_error_page;
142 my $valid = $results->valid();
143  
144 my $developer = Smolder::DB::Developer->retrieve($id);
145 return $self->error_message("Developer no longer exists!")
146 unless $developer;
147 $developer->set(%$valid);
148  
149 # we need to eval{} since we don't want there to be duplicate usernames (id)
150 eval { $developer->update };
151  
152 # if there was a problem.
153 if ($@) {
154  
155 # if it was a duplicate developer, then we can handle that
156 if (Smolder::Conf->unique_failure_msg($@)) {
157 return $self->edit({err_unique_username => 1});
158  
159 # else it's something else, so just throw it again
160 } else {
161 die $@;
162 }
163 }
164  
165 # now show the successful message
166 $self->add_message(msg => "User '" . $developer->username . "' has been successfully updated.");
167 return $self->add_json_header(list_changed => 1);
168 }
169  
170 =head2 list
171  
172 Show a list of all developers. Uses the F<Admin/Developers/list_table.tmpl>
173 template.
174  
175 =cut
176  
177 sub list {
178 my $self = shift;
179 my $cgi = $self->query();
180 my @developers = Smolder::DB::Developer->search(guest => 0);
181  
182 my %tt_params;
183 $tt_params{developers} = \@developers if (@developers);
184  
185 return $self->tt_process(\%tt_params);
186 }
187  
188 =head2 add
189  
190 Show the add form for adding a new developer. Uses the
191 F<Admin/Developers/add.tmpl> template.
192  
193 =cut
194  
195 sub add {
196 my ($self, $tt_params) = @_;
197 $tt_params ||= {};
198 return $self->tt_process($tt_params);
199 }
200  
201 =head2 process_add
202  
203 Process the incoming data from the C<add> mode and add it to the
204 database if validation passes. Uses the F<Admin/Developers/add_success.tmpl>.
205  
206 =cut
207  
208 sub process_add {
209 my $self = shift;
210 my $form = {
211 required => [qw(username email password admin)],
212 optional => [qw(fname lname)],
213 constraint_methods => {
214 username => [length_max(255), unique_field_value('developer', 'username'),],
215 fname => length_max(255),
216 lname => length_max(255),
217 email => email(),
218 password => length_between(4, 255),
219 admin => bool(),
220 },
221 };
222  
223 my $results = $self->check_rm('add', $form)
224 || return $self->check_rm_error_page;
225 my $valid = $results->valid();
226  
227 # create a new preference for this developer;
228 my $pref = Smolder::DB::Preference->create();
229 $valid->{preference} = $pref;
230 my $developer;
231  
232 # we need to eval{} since we don't want there to be duplicate usernames
233 eval { $developer = Smolder::DB::Developer->create($valid) };
234  
235 # if there was a problem.
236 if ($@) {
237  
238 # if it was a duplicate developer, then we can handle that
239 if (Smolder::Conf->unique_failure_msg($@)) {
240 return $self->add({err_unique_username => 1});
241  
242 # else it's something else, so just throw it again
243 } else {
244 die $@;
245 }
246 }
247  
248 # now show the successful message
249 $self->add_message(msg => "New user '" . $developer->username . "' successfully created.");
250 return $self->add_json_header(list_changed => 1);
251 }
252  
253 =head2 delete
254  
255 Delete a Developer and all data associated with him. If
256 successful returns the C<list> mode.
257  
258 =cut
259  
260 sub delete {
261 my $self = shift;
262 my $id = $self->param('id');
263 my $developer = Smolder::DB::Developer->retrieve($id);
264  
265 if ($developer) {
266  
267 # remove all reports from this developer
268 my @smokes = $developer->smoke_reports();
269 foreach my $smoke (@smokes) {
270 $smoke->delete_files();
271 }
272  
273 my $username = $developer->username;
274 $developer->delete();
275 $self->add_message(msg => "User '$username' has been successfully deleted.");
276 }
277  
278 return $self->list();
279 }
280  
281 =head2 details
282  
283 Show the details of a developer. Uses the F<Admin/Developers/details.tmpl>
284 template.
285  
286 =cut
287  
288 sub details {
289 my ($self, $developer, $action) = @_;
290 my $new;
291  
292 # if we weren't given a developer, then get it from the query string
293 if (!$developer) {
294 my $id = $self->param('id');
295 $new = 0;
296 $developer = Smolder::DB::Developer->retrieve($id);
297 return $self->error_message("Can't find Developer with id '$id'!") unless $developer;
298 } else {
299 $new = 1;
300 }
301  
302 my %tt_params = (developer => $developer);
303 $tt_params{$action} = 1 if ($action);
304  
305 return $self->tt_process(\%tt_params);
306 }
307  
308 1;