Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'develop' of git@github.com:MG-RAST/MG-RAST into develop

  • Loading branch information...
commit 43392298f2c402007fa7c319b9e8882616da931f 2 parents 7f014bc + bcbd707
Travis Harrison authored
Showing with 119 additions and 0 deletions.
  1. +119 −0 src/MGRAST/lib/resources2/user.pm
View
119 src/MGRAST/lib/resources2/user.pm
@@ -0,0 +1,119 @@
+package resources2::user;
+
+use strict;
+use warnings;
+no warnings('once');
+
+use Conf;
+use parent qw(resources2::resource);
+use WebApplicationDBHandle;
+use DBMaster;
+
+
+# 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} = "user";
+ $self->{attributes} = { "id" => [ 'string', 'user login' ],
+ "email" => [ 'string', 'user e-mail' ],
+ "firstname" => [ 'string', 'first name of user' ],
+ "lastname" => [ 'string', 'last name of user' ],
+ "entry_date" => [ 'date', 'date of user creation' ],
+ "active" => [ 'boolean', '' ],
+ "comment" => [ 'string', 'any comment about the user account' ],
+ "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 user resource returns information about a user.",
+ 'type' => 'object',
+ 'documentation' => $Conf::cgi_url.'/Html/api.html#'.$self->name,
+ 'requests' => [ { 'name' => "info",
+ 'request' => $self->cgi->url."/".$self->name,
+ 'description' => "Returns description of parameters and attributes.",
+ '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.",
+ 'method' => "GET" ,
+ 'type' => "synchronous" ,
+ 'attributes' => $self->attributes,
+ 'parameters' => { 'options' => {},
+ 'required' => { "id" => [ "string", "unique user login" ] },
+ 'body' => {} } },
+ ]
+ };
+
+ $self->return_data($content);
+}
+
+# the resource is called with an id parameter
+sub instance {
+ my ($self) = @_;
+
+ # check id format
+ my $rest = $self->rest;
+ my $id = $rest->[0];
+
+ if ($rest && scalar(@$rest) == 1) {
+ unless ($self->user && $self->user->has_right(undef, 'edit', 'user', $self->user->{_id})) {
+ $self->return_data( {"ERROR" => "insufficient permissions for user call"}, 400 );
+ }
+ }
+
+ use WebApplicationDBHandle;
+ use DBMaster;
+
+ my ($dbmaster, $error) = WebApplicationDBHandle->new();
+ if ($error) {
+ $self->return_data( {"ERROR" => "could not connect to user database - $error"}, 500 );
+ }
+
+ # get data
+ my $user = $dbmaster->User->get_objects( { "login" => $id } );
+
+ unless (scalar(@$user)) {
+ $self->return_data( {"ERROR" => "login $id does not exists"}, 404 );
+ }
+
+ # prepare data
+ my $data = $self->prepare_data($user->[0]);
+ $self->return_data($data);
+}
+
+# reformat the data into the requested output format
+sub prepare_data {
+ my ($self, $user) = @_;
+
+ my $url = $self->cgi->url;
+ my $obj = {};
+ $obj->{id} = $user->login;
+ $obj->{email} = $user->email;
+ $obj->{firstname} = $user->firstname;
+ $obj->{lastname} = $user->lastname;
+ $obj->{entry_date} = $user->entry_date;
+ $obj->{active} = $user->active;
+ $obj->{comment} = $user->comment;
+ $obj->{url} = $url.'/user/'.$obj->{id};
+
+ return $obj;
+}
+
+1;
Please sign in to comment.
Something went wrong with that request. Please try again.