Rubric

 view release on metacpan or  search on metacpan

lib/Rubric/WebApp.pm  view on Meta::CPAN

#pod settings may be changed.
#pod
#pod =cut

sub preferences {
  my ($self) = @_;

  return $self->login unless $self->param('current_user');

  return $self->template("preferences")
    unless my %prefs = $self->_get_prefs_form;

  if (my %errors = $self->validate_prefs(\%prefs)) {
    return $self->template("preferences", { %prefs, %errors } );
  }

  $self->update_user(\%prefs);
}

#pod =head2 update_user(\%prefs)
#pod
#pod This method will update the current user object with the changes in C<%prefs>,
#pod which is passed by the C<preferences> method.
#pod
#pod =cut

sub update_user {
  my ($self, $prefs) = @_;
  for ($self->param('current_user')) {
    $_->password(md5_hex($prefs->{password_1})) if $prefs->{password_1};
    $_->email($prefs->{email});
    $_->update;
  }
  $self->redirect_root('updated');
}

sub _get_prefs_form {
  my ($self) = @_;

  my %form;
  for (qw(password password_1 password_2 email)) {
    $form{$_} = $self->query->param($_) if $self->query->param($_);
  }
  return %form;
}

#pod =head2 validate_prefs(\%prefs)
#pod
#pod Given a set of preference updates from a form submission, this method validates
#pod them and returns a description of the validation results.  This method will
#pod probably be redesigned (possibly with Data::FormValidator) in the future.
#pod Don't count on its interface.
#pod
#pod =cut

#pod =begin future
#pod
#pod sub validate_prefs {
#pod   my ($self, $prefs) = @_;
#pod   require Data::FormValidator;
#pod
#pod   my $profile = {
#pod     required     => [qw(password)],
#pod     optional     => [qw(password_1 password_2 email)],
#pod     constraints  => {
#pod       email => 'email',
#pod       password_1 => {
#pod         params     => [qw(password_1 password_2)],
#pod         constraint => sub { $_[0] eq $_[1] },
#pod       }
#pod     },
#pod     dependency_groups => { new_password => [qw(password_1 password_2)] }
#pod   };
#pod
#pod   my $results = Data::FormValidator->check($prefs, $profile);
#pod }
#pod
#pod =end future
#pod
#pod =cut

sub validate_prefs {
  my ($self, $prefs) = @_;
  my %errors;

  if (not $prefs->{email}) {
    $errors{email_missing} = 1;
  } elsif ($prefs->{email} and $prefs->{email} !~ $Email::Address::addr_spec) {
    undef $prefs->{email};
    $errors{email_invalid} = 1;
  }

  if (
    $prefs->{password_1} and $prefs->{password_2}
    and $prefs->{password_1} ne $prefs->{password_2}
  ) {
    undef $prefs->{password_1};
    undef $prefs->{password_2};
    $errors{password_mismatch} = 1;
  }

  unless ($prefs->{password}) {
    $errors{password_missing} = 1;
  } elsif (
    md5_hex($prefs->{password}) ne $self->param('current_user')->password
  ) {
    $errors{password_wrong} = 1;
  }

  return %errors;
}

#pod =head2 newuser
#pod
#pod If the proper form information is present, this runmode creates a new user

lib/Rubric/WebApp.pm  view on Meta::CPAN


=head2 setup_reset_code

This routine gets a reset code for the user and emails it to him.

=head2 preferences

This method displays account information for the current user.  Some account
settings may be changed.

=head2 update_user(\%prefs)

This method will update the current user object with the changes in C<%prefs>,
which is passed by the C<preferences> method.

=head2 validate_prefs(\%prefs)

Given a set of preference updates from a form submission, this method validates
them and returns a description of the validation results.  This method will
probably be redesigned (possibly with Data::FormValidator) in the future.
Don't count on its interface.

=begin future

sub validate_prefs {
  my ($self, $prefs) = @_;
  require Data::FormValidator;

  my $profile = {
    required     => [qw(password)],
    optional     => [qw(password_1 password_2 email)],
    constraints  => {
      email => 'email',
      password_1 => {
        params     => [qw(password_1 password_2)],
        constraint => sub { $_[0] eq $_[1] },
      }
    },
    dependency_groups => { new_password => [qw(password_1 password_2)] }
  };

  my $results = Data::FormValidator->check($prefs, $profile);
}

=end future

=head2 newuser

If the proper form information is present, this runmode creates a new user
account.  If not, it presents a form.

If a user is already logged in, the user is redirected to the root of the



( run in 1.411 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )