Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.66 KB | None | 0 0
  1. package RTK::RPAN::Controller::Users;
  2. use Moose;
  3. use namespace::autoclean;
  4.  
  5. use RTK::RPAN::Model::User;
  6. use Digest::MD5 qw/md5_hex/;
  7.  
  8. BEGIN {extends 'Catalyst::Controller::REST'; }
  9.  
  10. =head1 NAME
  11.  
  12. RTK::RPAN::Controller::Users - Catalyst Controller
  13.  
  14. =cut
  15.  
  16. sub index :Path :Args(0) : ActionClass('REST') {}
  17.  
  18. sub index_GET {
  19.     my ( $self, $c ) = @_;
  20.  
  21.     my $base = $c->req->base . "users/";
  22.  
  23.     $self->status_ok(
  24.         $c,
  25.         entity => { map { $_ => "${base}$_" } 'a' .. 'z' },
  26.     );
  27. }
  28.  
  29. for my $char ( 'a' .. 'z' ) {
  30.     eval "sub $char :Local :ActionClass('REST') {}; 1" || die( $@ );
  31.     no strict 'refs';
  32.     *{"${char}_GET"}    = sub { shift->char_GET( $char, @_ )   };
  33.     *{"${char}_POST"}   = sub { shift->char_POST( $char, @_ )  };
  34.     *{"${char}_PUT"}    = sub { shift->char_PUT( $char, @_ )   };
  35.     *{"${char}_DELETE"} = sub { shift->char_DELETE( $char, @_ )};
  36. }
  37.  
  38. sub content_type {
  39.     my ( $self, $c ) = @_;
  40.  
  41.     return $c->req->query_params->{'content-type'}
  42.         || $c->req->content_type
  43.         || 'text/html';
  44. }
  45.  
  46. sub char_GET {
  47.     my ( $self, $char, $c, $arg ) = @_;
  48.     my $base = $c->req->base . "users/$char/";
  49.  
  50.     return $c->go( 'user_view', [ $arg ])
  51.         if $arg && $self->content_type( $c ) =~ m{^text/html$}i;
  52.  
  53.     my $user = $self->get_user( $arg );
  54.  
  55.     return $self->status_not_found(
  56.         $c,
  57.         message => "No such user '$arg'",
  58.     ) unless $user;
  59.  
  60.     $self->status_ok(
  61.         $c,
  62.         entity => $arg
  63.             ? { %$user } # Unbless (essentially)
  64.             : $self->list_users( $c, $base )
  65.     );
  66. }
  67.  
  68. sub list_users {
  69.     my ( $self, $c, $base, $char ) = @_;
  70.  
  71.     return {
  72.         map { $_->name => $base . $_->name }
  73.             RTK::RPAN::Model::User->get_starting_with( $char )
  74.     };
  75. }
  76.  
  77. sub get_user {
  78.     my ( $self, $arg ) = @_;
  79.     return RTK::RPAN::Model::User->new( name => $arg );
  80. }
  81.  
  82. sub user_view :Action :ActionClass('RenderView') {
  83.     my ( $self, $c, $arg ) = @_;
  84.     $c->stash->{ arg } = $arg;
  85.     $c->stash->{ user } = $self->get_user( $arg ) || undef;
  86.     $c->stash->{ request } = $c->req;
  87. }
  88.  
  89. sub char_POST {
  90.     my ( $self, $char, $c, $arg ) = @_;
  91.     return $self->invalid_method( 'POST' ) unless $arg;
  92.     my $req = $c->req;
  93.  
  94.     #XXX HTML forms cannot do PUT or DELETE
  95.     return $self->char_PUT( $char, $c, $arg )
  96.         if $req->param('op') eq 'create';
  97.     return $self->char_DELETE( $char, $c, $arg )
  98.         if $req->param('op') eq 'delete';
  99.  
  100.     my $user = $self->get_user( $arg )
  101.             || return $self->invalid_user( $arg );
  102.  
  103.     my $password = $self->validate_password( $req );
  104.  
  105.     $user->update(
  106.         $password ? ( password => $password ) : (),
  107.         disabled => $req->param('disabled') ? 1 : 0,
  108.     );
  109.    
  110.     # XXX This doesn't work, the text/html not supported message is already in body.
  111.     if ( $self->content_type( $c ) =~ m{application/x-www-form-urlencoded}i ) {
  112.         return $c->go( 'user_view', [ $arg ]);
  113.     };
  114.    
  115.     $self->status_accepted(
  116.         $c,
  117.         entity => $c->req->uri->as_string,
  118.     );
  119. }
  120.  
  121. sub validate_password {
  122.     my ( $self, $req ) = @_;
  123.     return unless $req->param( 'password1' )
  124.                || $req->param( 'password2' );
  125.  
  126.     return md5_hex( $req->param( 'password1' ))
  127.         if ( $req->param( 'password1' ) eq $req->param( 'password2' ));
  128.  
  129.     die( "Handle this better!" );
  130. }
  131.  
  132. sub char_PUT {
  133.     my ( $self, $char, $c, $arg ) = @_;
  134.     return $self->invalid_method( 'PUT' ) unless $arg;
  135.  
  136. }
  137.  
  138. sub char_DELETE {
  139.     my ( $self, $char, $c, $arg ) = @_;
  140.     return $self->invalid_method( 'DELETE' ) unless $arg;
  141.  
  142. }
  143.  
  144. __PACKAGE__->meta->make_immutable;
  145.  
  146. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement