package LATMOS::Accounts::Web::Controller::Rpc;
use Moose;
use namespace::autoclean;
$RPC::XML::ENCODING = 'utf-8';

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

LATMOS::Accounts::Web::Controller::Rpc - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

sub _begin : Private {
    my ($self, $c) = @_;
}

sub index : Public {
    my ($self, $c) = @_;
    $c->xmlrpc;
}

sub ech : XMLRPC('ech.ech') {
    my ( $self, $c, @args ) = @_;
    return RPC::XML::fault->new( 400, "No input!" ) unless
    @args;
    return join ' ', @args;
}
sub user_change_password : XMLRPC('user.change_password') {
    my ( $self, $c, $username, $oldpasswd, $newpasswd ) = @_;
    return RPC::XML::fault->new( 400, "No input!" ) unless $newpasswd;

    $c->authenticate({
        username => $username,
        password => $oldpasswd,
    }) or return RPC::XML::fault->new( 400, "Cannot authenticate" );

    my $msg = $c->forward('/users/change_password', [ $username, $newpasswd ]);
    if ($msg) {
        return RPC::XML::fault->new( 400, "Error: $msg" );
    } else {
        return "Password changed";
    }
}


sub end : Private {}

=head1 AUTHOR

Olivier Thauvin

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
