Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RTK::RPAN::Controller::Users;
- use Moose;
- use namespace::autoclean;
- use RTK::RPAN::Model::User;
- use Digest::MD5 qw/md5_hex/;
- BEGIN {extends 'Catalyst::Controller::REST'; }
- =head1 NAME
- RTK::RPAN::Controller::Users - Catalyst Controller
- =cut
- sub index :Path :Args(0) : ActionClass('REST') {}
- sub index_GET {
- my ( $self, $c ) = @_;
- my $base = $c->req->base . "users/";
- $self->status_ok(
- $c,
- entity => { map { $_ => "${base}$_" } 'a' .. 'z' },
- );
- }
- for my $char ( 'a' .. 'z' ) {
- eval "sub $char :Local :ActionClass('REST') {}; 1" || die( $@ );
- no strict 'refs';
- *{"${char}_GET"} = sub { shift->char_GET( $char, @_ ) };
- *{"${char}_POST"} = sub { shift->char_POST( $char, @_ ) };
- *{"${char}_PUT"} = sub { shift->char_PUT( $char, @_ ) };
- *{"${char}_DELETE"} = sub { shift->char_DELETE( $char, @_ )};
- }
- sub content_type {
- my ( $self, $c ) = @_;
- return $c->req->query_params->{'content-type'}
- || $c->req->content_type
- || 'text/html';
- }
- sub char_GET {
- my ( $self, $char, $c, $arg ) = @_;
- my $base = $c->req->base . "users/$char/";
- return $c->go( 'user_view', [ $arg ])
- if $arg && $self->content_type( $c ) =~ m{^text/html$}i;
- my $user = $self->get_user( $arg );
- return $self->status_not_found(
- $c,
- message => "No such user '$arg'",
- ) unless $user;
- $self->status_ok(
- $c,
- entity => $arg
- ? { %$user } # Unbless (essentially)
- : $self->list_users( $c, $base )
- );
- }
- sub list_users {
- my ( $self, $c, $base, $char ) = @_;
- return {
- map { $_->name => $base . $_->name }
- RTK::RPAN::Model::User->get_starting_with( $char )
- };
- }
- sub get_user {
- my ( $self, $arg ) = @_;
- return RTK::RPAN::Model::User->new( name => $arg );
- }
- sub user_view :Action :ActionClass('RenderView') {
- my ( $self, $c, $arg ) = @_;
- $c->stash->{ arg } = $arg;
- $c->stash->{ user } = $self->get_user( $arg ) || undef;
- $c->stash->{ request } = $c->req;
- }
- sub char_POST {
- my ( $self, $char, $c, $arg ) = @_;
- return $self->invalid_method( 'POST' ) unless $arg;
- my $req = $c->req;
- #XXX HTML forms cannot do PUT or DELETE
- return $self->char_PUT( $char, $c, $arg )
- if $req->param('op') eq 'create';
- return $self->char_DELETE( $char, $c, $arg )
- if $req->param('op') eq 'delete';
- my $user = $self->get_user( $arg )
- || return $self->invalid_user( $arg );
- my $password = $self->validate_password( $req );
- $user->update(
- $password ? ( password => $password ) : (),
- disabled => $req->param('disabled') ? 1 : 0,
- );
- # XXX This doesn't work, the text/html not supported message is already in body.
- if ( $self->content_type( $c ) =~ m{application/x-www-form-urlencoded}i ) {
- return $c->go( 'user_view', [ $arg ]);
- };
- $self->status_accepted(
- $c,
- entity => $c->req->uri->as_string,
- );
- }
- sub validate_password {
- my ( $self, $req ) = @_;
- return unless $req->param( 'password1' )
- || $req->param( 'password2' );
- return md5_hex( $req->param( 'password1' ))
- if ( $req->param( 'password1' ) eq $req->param( 'password2' ));
- die( "Handle this better!" );
- }
- sub char_PUT {
- my ( $self, $char, $c, $arg ) = @_;
- return $self->invalid_method( 'PUT' ) unless $arg;
- }
- sub char_DELETE {
- my ( $self, $char, $c, $arg ) = @_;
- return $self->invalid_method( 'DELETE' ) unless $arg;
- }
- __PACKAGE__->meta->make_immutable;
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement