Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/MGRAST/lib/resources/resource.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ sub new {
204 => "No Content",
400 => "Bad Request",
401 => "Unauthorized",
404 => "Not Found",
416 => "Request Range Not Satisfiable",
500 => "Internal Server Error",
501 => "Not Implemented",
503 => "Service Unavailable",
507 => "Storing object failed",
-32602 => "Invalid params",
-32603 => "Internal error"
};
404 => "Not Found",
416 => "Request Range Not Satisfiable",
500 => "Internal Server Error",
501 => "Not Implemented",
503 => "Service Unavailable",
507 => "Storing object failed",
-32602 => "Invalid params",
-32603 => "Internal error"
};
# create object
my $self = {
format => "application/json",
Expand Down
107 changes: 107 additions & 0 deletions src/MGRAST/lib/resources/server.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package resources::server;

use strict;
use warnings;
no warnings('once');

use Conf;
use Data::Dumper;
use parent qw(resources::resource);
use WebApplicationDBHandle;

# Override parent constructor
sub new {
my ($class, @args) = @_;

# Call the constructor of the parent class
my $self = $class->SUPER::new(@args);

# Add name / attributes
$self->{name} = "server";
$self->{attributes} = { "id" => [ 'string', 'unique identifier of this server' ],
"version" => [ 'string', 'version number of the server' ],
"status" => [ 'string', 'status of the server' ],
"info" => [ 'string', 'informational text, i.e. downtime warnings' ],
"metagenomes" => [ 'integer', 'total number of metagenomes' ],
"public_metagenomes" => [ 'integer', 'total number of public metagenomes' ],
"sequences" => [ 'integer', 'total number of sequences' ],
"basepairs" => [ 'integer', 'total number of basepairs' ],
"url" => [ 'uri', 'resource location of this object instance' ]
};
return $self;
}

# resource is called without any parameters
# this method must return a description of the resource
sub info {
my ($self) = @_;
my $content = { 'name' => $self->name,
'url' => $self->cgi->url."/".$self->name,
'description' => "The server resource returns information about a server.",
'type' => 'object',
'documentation' => $self->cgi->url.'/api.html#'.$self->name,
'requests' => [ { 'name' => "info",
'request' => $self->cgi->url."/".$self->name,
'description' => "Returns the server information.",
'method' => "GET" ,
'type' => "synchronous" ,
'attributes' => "self",
'parameters' => { 'options' => {},
'required' => {},
'body' => {} } },
{ 'name' => "instance",
'request' => $self->cgi->url."/".$self->name."/{ID}",
'description' => "Returns a single user object.",
'example' => [ 'curl -X GET "'.$self->cgi->url."/".$self->name.'/MG-RAST"',
"info for the MG-RAST server" ],
'method' => "GET",
'type' => "synchronous" ,
'attributes' => $self->attributes,
'parameters' => { 'options' => {},
'required' => { "id" => [ "string", "unique server ID" ] },
'body' => {} } },
]
};

$self->return_data($content);
}

# the resource is called with an id parameter
sub instance {
my ($self) = @_;

my $master = $self->connect_to_datasource();

my $dis_msg;
my $disabled = "MG-RAST.disabled";
if (-e $disabled) {
open(MSG, "$disabled");
$dis_msg = <MSG>;
close(MSG);
}

my $inf_msg;
my $motd_file = "MG-RAST.motd";
if (-f $motd_file && open(FILE, $motd_file)) {
$inf_msg = "";
while (<FILE>) {
$inf_msg .= $_;
}
close FILE;
}

# prepare data
my $data = { "id" => "MG-RAST",
"version" => $Conf::server_version,
"status" => $dis_msg ? "server down" : "ok",
"info" => $dis_msg ? $dis_msg : $inf_msg,
"metagenomes" => $master->Job->count_all(),
"public_metagenomes" => $master->Job->count_public(),
"sequences" => $master->Job->count_total_sequences(),
"basepairs" => $master->Job->count_total_bp(),
"url" => $self->cgi->url."/".$self->name."/MG-RAST" };

$self->return_data($data);
}

1;
230 changes: 227 additions & 3 deletions src/MGRAST/lib/resources/user.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Conf;
use Data::Dumper;
use parent qw(resources::resource);
use WebApplicationDBHandle;
use URI::Escape;

# Override parent constructor
sub new {
Expand Down Expand Up @@ -80,13 +81,39 @@ sub instance {
if ($error) {
$self->return_data( {"ERROR" => "could not connect to user database - $error"}, 503 );
}


# check if this is a user creation
if ($self->{method} eq 'POST') {
# users may only be created with a valid recaptcha
my $ua = $self->{agent};
$ua->env_proxy();
my $resp = $ua->post( 'http://www.google.com/recaptcha/api/verify', { privatekey => '6Lf1FL4SAAAAAIJLRoCYjkEgie7RIvfV9hQGnAOh',
remoteip => $ENV{'REMOTE_ADDR'},
challenge => $rest->[0],
response => $self->{cgi}->param('response') }
);
if ( $resp->is_success ) {
my ( $answer, $message ) = split( /\n/, $resp->content, 2 );
if ( $answer !~ /true/ ) {
$self->return_data( {"ERROR" => "recaptcha failed"}, 400 );
}
} else {
$self->return_data( {"ERROR" => "recaptcha server could not be reached"}, 400 );
}

# if we get here, recaptcha is successful
my $new_user = &create_user($self);
$self->return_data($self->prepare_data($new_user));
}

# get data
my $user = [];
if ($rest->[0] =~ /^mgu(\d+)$/) { # user id
$user = $master->User->get_objects( {"_id" => $1} );
$user = $master->User->get_objects( {"_id" => $1} );
} elsif (uri_unescape($rest->[0]) =~ /\@/) {
$user = $master->User->get_objects( { "email" => uri_unescape($rest->[0]) } );
} else { # user login
$user = $master->User->get_objects( {"login" => $rest->[0]} );
$user = $master->User->get_objects( { "login" => $rest->[0] } );
}
unless (scalar(@$user)) {
$self->return_data( {"ERROR" => "user '".$rest->[0]."' does not exists"}, 404 );
Expand All @@ -98,6 +125,25 @@ sub instance {
$self->return_data( {"ERROR" => "insufficient permissions for user call"}, 401 );
}

# check if this is a user update
if ($self->{method} eq 'PUT') {
if (defined $self->{cgi}->param('email')) {
$user->email(uri_decode($self->{cgi}->param('email')));
}
if (defined $self->{cgi}->param('firstname')) {
$user->firstname(uri_decode($self->{cgi}->param('firstname')));
}
if (defined $self->{cgi}->param('lastname')) {
$user->lastname(uri_decode($self->{cgi}->param('lastname')));
}
if (defined $self->{cgi}->param('active')) {
$user->active(uri_decode($self->{cgi}->param('active')));
}
if (defined $self->{cgi}->param('comment')) {
$user->comment(uri_decode($self->{cgi}->param('comment')));
}
}

# prepare data
my $data = $self->prepare_data($user);
$self->return_data($data);
Expand All @@ -122,4 +168,182 @@ sub prepare_data {
return $obj;
}

# create a new user
sub create_user {
my ($self) = @_;

use HTML::Strip;
use HTML::Template;

my $cgi = $self->{cgi};

# check for an email address
unless ($cgi->param('email')) {
$self->return_data( {"ERROR" => "no email address passed"}, 400 );
}

# check if email address is valid
unless ($cgi->param('email') =~ /[\d\w\.\'-]+\@[\d\w\.-]+\.[\w+]/) {
$self->return_data( {"ERROR" => "email address invalid"}, 400 );
}

# check login
unless ($cgi->param('login')) {
$self->return_data( {"ERROR" => "no login passed"}, 400 );
}

# check if the login is valid
unless ($cgi->param('login') =~ /^[\d\w]+$/) {
$self->return_data( {"ERROR" => "login is invalid"}, 400 );
}

# check if firstname and lastname are distinct
if ($cgi->param('firstname') && $cgi->param('lastname')) {
if ($cgi->param('firstname') eq $cgi->param('lastname')) {
$self->return_data( {"ERROR" => "first and last name must be distinct"}, 400 );
}
}

my $user;

# get database
my ($master, $error) = WebApplicationDBHandle->new();

# check login
my $user_by_login = $master->User->init( { login => $cgi->param('login') } );
if (ref($user_by_login)) {
$self->return_data( {"ERROR" => "login already taken"}, 400 );
}
else {

# check email
my $user_by_email = $master->User->init( { email => $cgi->param('email') } );
if (ref($user_by_email)) {
$self->return_data( {"ERROR" => "email already taken"}, 400 );
}
}

# check if scope exists
if ($master->Scope->init( { application => undef,
name => $cgi->param('login') } )) {
$self->return_data( {"ERROR" => "login already taken"}, 400 );
}

# check first name
unless ($cgi->param('firstname')) {
$self->return_data( {"ERROR" => "no firstname passed"}, 400 );
}

# check last name
unless ($cgi->param('lastname')) {
$self->return_data( {"ERROR" => "no lastname passed"}, 400 );
}

# create the user in the db
$user = $master->User->create( { email => $cgi->param('email'),
firstname => $cgi->param('firstname'),
lastname => $cgi->param('lastname'),
login => $cgi->param('login') } );

# check for success
unless (ref($user)) {
$self->return_data( {"ERROR" => "could not create user"}, 500 );
}

# check for organization information
my $user_org = "";
my $org_found = 0;
my $url = "";

if ($cgi->param('organization')) {
my $hs = HTML::Strip->new();
$user_org = $hs->parse($cgi->param('organization'));
}

if ($cgi->param('lru')) {
my $hs = HTML::Strip->new();
$url = $hs->parse($cgi->param('lru'));
}

if ($user_org) {

# check if we find this organization by name
my $existing_org = $master->Organization->init( { name => $user_org } );

# check if we have a url to compare
if ($url) {
$url =~ s/(.*)\/$/$1/;
$url = "http://".$url;
$existing_org = $master->Organization->get_objects( { url => $url } );
if (scalar($existing_org)) {
$existing_org = $existing_org->[0];
}
}

# check if we found an existing org
if ($existing_org) {
$user_org = $existing_org->name();
unless (scalar(@{$master->OrganizationUsers->get_objects( { organization => $existing_org,
user => $user } )})) {
$master->OrganizationUsers->create( { organization => $existing_org,
user => $user } );
}
$org_found = 1;
}
}

# prepare admin email
my $abody = HTML::Template->new(filename => '../../src/WebApplication/templates/EmailReviewNewAccount.tmpl',
die_on_bad_params => 0);
$abody->param('FIRSTNAME', $user->firstname);
$abody->param('LASTNAME', $user->lastname);
$abody->param('LOGIN', $user->login);
$abody->param('EMAIL_USER', $user->email);
$abody->param('APPLICATION_NAME', "MG-RAST");
$abody->param('APPLICATION_URL', "http://metagenomics.anl.gov");
$abody->param('EMAIL_ADMIN', "mg-rast\@mcs.anl.gov");
$abody->param('URL', $url);
if ($cgi->param('country')) {
my $hs = HTML::Strip->new();
my $country = $hs->parse( $cgi->param('country') );
$abody->param('COUNTRY', $country);
}
if ($user_org) {
$abody->param('ORGANIZATION', $user_org);
if ($org_found) {
$abody->param('ORG_FOUND', "This organization was already present in the database.");
} else {
$abody->param('ORG_FOUND', "This organization does not yet exist. Please create it on the Organization page.");
}
}

# add registration request (non granted login right)
$user->add_login_right( $master->Backend->init({ 'name' => "MGRAST" }) );

# check if user wants to be on the mailinglist
if ($cgi->param('mailinglist')) {
$master->Preferences->create( { user => $user,
name => 'mailinglist',
value => 'mgrast' } );
}

# send user email
my $ubody = HTML::Template->new(filename => '../../src/WebApplication/templates/EmailNewAccount.tmpl',
die_on_bad_params => 0);
$ubody->param('FIRSTNAME', $user->firstname);
$ubody->param('LASTNAME', $user->lastname);
$ubody->param('LOGIN', $user->login);
$ubody->param('EMAIL_USER', $user->email);
$ubody->param('APPLICATION_NAME', "MG-RAST");
$ubody->param('APPLICATION_URL', "http://metagenomics.anl.gov");
$ubody->param('EMAIL_ADMIN', "mg-rast\@mcs.anl.gov");

$user->send_email( "mg-rast\@mcs.anl.gov",
'MG-RAST - new account requested',
$ubody->output
);

return $user;
}

1;
Loading