| 1 | package Smolder::Control::Developer::Prefs; | ||
| 2 | use base 'Smolder::Control'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Smolder::Constraints qw(enum_value length_between unsigned_int existing_field_value bool); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Smolder::Control::Developer::Prefs | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Controller module for dealing with developer preferences | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | sub setup { | ||
| 18 | my $self = shift; | ||
| 19 | $self->start_mode('show'); | ||
| 20 | $self->run_modes( | ||
| 21 | [ | ||
| 22 | qw( | ||
| 23 | show | ||
| 24 | update_pref | ||
| 25 | change_pw | ||
| 26 | process_change_pw | ||
| 27 | get_pref_details | ||
| 28 | ) | ||
| 29 | ] | ||
| 30 | ); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub require_group { 'developer' } | ||
| 34 | |||
| 35 | =head1 RUN MODES | ||
| 36 | |||
| 37 | =head2 change_pw | ||
| 38 | |||
| 39 | Show the form to allow a developer to change their password. Uses the | ||
| 40 | F<Developer/Prefs/change_pw.tmpl> template. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub change_pw { | ||
| 45 | my ($self, $tt_params) = @_; | ||
| 46 | $tt_params ||= {}; | ||
| 47 | return $self->tt_process($tt_params); | ||
| 48 | } | ||
| 49 | |||
| 50 | =head2 process_change_pw | ||
| 51 | |||
| 52 | Handled the incoming data from the C<change_pw> run mode. If | ||
| 53 | it passes validation then it updates the database and then returns | ||
| 54 | to the C<change_pw> mode. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub process_change_pw { | ||
| 59 | my $self = shift; | ||
| 60 | my $dev = $self->developer; | ||
| 61 | |||
| 62 | # validate the incoming info | ||
| 63 | my $form = { | ||
| 64 | required => [qw(current_pw new_pw new_pw_retyped)], | ||
| 65 | constraint_methods => { | ||
| 66 | |||
| 67 | # make sure it's the right encrypted pw | ||
| 68 | current_pw => sub { | ||
| 69 | my ($dfv, $val) = @_; | ||
| 70 | if (crypt($val, $dev->password) eq $dev->password) { | ||
| 71 | return $val; | ||
| 72 | } else { | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | }, | ||
| 76 | |||
| 77 | # make sure it's long enough (but not too long) | ||
| 78 | new_pw => length_between(4, 255), | ||
| 79 | |||
| 80 | # make sure it's the same as 'new_pw' | ||
| 81 | new_pw_retyped => sub { | ||
| 82 | my ($dfv, $val) = @_; | ||
| 83 | |||
| 84 | # get the value of 'new_pw' | ||
| 85 | my $data = $dfv->get_input_data(as_hashref => 1); | ||
| 86 | if ($data->{new_pw} eq $val) { | ||
| 87 | return $val; | ||
| 88 | } else { | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | }, | ||
| 92 | }, | ||
| 93 | }; | ||
| 94 | |||
| 95 | my $results = $self->check_rm('change_pw', $form) | ||
| 96 | || return $self->check_rm_error_page(); | ||
| 97 | my $valid = $results->valid(); | ||
| 98 | |||
| 99 | $dev->password($valid->{new_pw}); | ||
| 100 | $dev->update(); | ||
| 101 | $self->add_message(msg => "Password successfully changed."); | ||
| 102 | return $self->change_pw(); | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 show | ||
| 106 | |||
| 107 | Show all of the preferences for each project that this developer is assigned to. | ||
| 108 | Uses the F<Developer/Prefs/show.tmpl> template. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub show { | ||
| 113 | my ($self, $tt_params, $pref) = @_; | ||
| 114 | $tt_params ||= {}; | ||
| 115 | my $html = $self->tt_process($tt_params); | ||
| 116 | |||
| 117 | # if this wasn't an update form submission | ||
| 118 | # and we don't have a preference, then make it the default | ||
| 119 | if (!$self->query->param('update_pref') && !$pref) { | ||
| 120 | $pref = $self->developer->preference; | ||
| 121 | } | ||
| 122 | |||
| 123 | # if we have a preference use it to fill in the form | ||
| 124 | if ($pref) { | ||
| 125 | my %fill_data = | ||
| 126 | map { $_ => $pref->$_ } qw(id email_type email_freq email_limit show_passing); | ||
| 127 | return HTML::FillInForm->new()->fill( | ||
| 128 | scalarref => $html, | ||
| 129 | fdat => \%fill_data, | ||
| 130 | ); | ||
| 131 | } else { | ||
| 132 | return $html; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 get_pref_details | ||
| 137 | |||
| 138 | This run mode will return a JSON header which contains the name-value | ||
| 139 | pairs for this preferences data. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub get_pref_details { | ||
| 144 | my $self = shift; | ||
| 145 | my $pref = Smolder::DB::Preference->retrieve($self->query->param('id')); | ||
| 146 | my %data; | ||
| 147 | if ($pref) { | ||
| 148 | %data = map { $_ => $pref->$_ } qw(email_type email_freq email_limit show_passing); | ||
| 149 | } | ||
| 150 | |||
| 151 | return $self->add_json_header(%data); | ||
| 152 | } | ||
| 153 | |||
| 154 | =head2 update_pref | ||
| 155 | |||
| 156 | Update the information coming from the C<show> run mode. | ||
| 157 | If validation passes, the database is updated and the C<show> | ||
| 158 | run mode is returned. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub update_pref { | ||
| 163 | my $self = shift; | ||
| 164 | |||
| 165 | # validate the data | ||
| 166 | my $form = { | ||
| 167 | required => [qw(id email_type email_freq email_limit show_passing)], | ||
| 168 | constraint_methods => { | ||
| 169 | id => existing_field_value('preference', 'id'), | ||
| 170 | email_type => enum_value('preference', 'email_type'), | ||
| 171 | email_freq => enum_value('preference', 'email_freq'), | ||
| 172 | email_limit => unsigned_int(), | ||
| 173 | show_passing => bool(), | ||
| 174 | } | ||
| 175 | }; | ||
| 176 | |||
| 177 | my $results = $self->check_rm('show', $form) | ||
| 178 | || return $self->check_rm_error_page(); | ||
| 179 | my $valid = $results->valid(); | ||
| 180 | |||
| 181 | my $pref = Smolder::DB::Preference->retrieve($valid->{id}); | ||
| 182 | if ($pref) { | ||
| 183 | delete $valid->{id}; | ||
| 184 | $pref->set(%$valid); | ||
| 185 | $pref->update(); | ||
| 186 | |||
| 187 | # is this the default pref? | ||
| 188 | if ($pref->id eq $self->developer->preference) { | ||
| 189 | $self->add_message(msg => "Default preferences successfully updated."); | ||
| 190 | } else { | ||
| 191 | $self->add_message(msg => "Preference for project '" | ||
| 192 | . $pref->project->name | ||
| 193 | . "' has been successfully updated'"); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | # if we are updating the default pref and they want to sync them | ||
| 198 | if ($self->query->param('sync') && ($pref->id eq $self->developer->preference)) { | ||
| 199 | my @projs = $self->developer->project_developers; | ||
| 200 | foreach my $proj (@projs) { | ||
| 201 | $proj->preference->set(%$valid); | ||
| 202 | $proj->preference->update(); | ||
| 203 | } | ||
| 204 | $self->add_message(msg => "Preferences have been successfully synced with all Projects."); | ||
| 205 | } | ||
| 206 | return $self->show({}, $pref); | ||
| 207 | } | ||
| 208 | |||
| 209 | 1; |