Users.pm 1.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#Written by Keith Jolley
#Copyright (c) 2014, University of Oxford
#E-mail: keith.jolley@zoo.ox.ac.uk
#
#This file is part of Bacterial Isolate Genome Sequence Database (BIGSdb).
#
#BIGSdb is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#BIGSdb is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with BIGSdb.  If not, see <http://www.gnu.org/licenses/>.
package BIGSdb::REST::Routes::Users;
use strict;
use warnings;
use 5.010;
use Dancer2 appname => 'BIGSdb::REST::Interface';
use Log::Log4perl qw(get_logger);
use BIGSdb::Utils;
my $logger = get_logger('BIGSdb.Rest');

#User routes
get '/db/:db/users/:user' => sub {
	my $self    = setting('self');
	my $user_id = param('user');
	if ( !BIGSdb::Utils::is_int($user_id) ) {
		status(400);
34
		return { error => 'User id must be an integer.' };
35
36
37
38
	}
	my $user = $self->{'datastore'}->run_query( "SELECT * FROM users WHERE id=?", $user_id, { fetch => 'row_hashref' } );
	if ( !defined $user->{'id'} ) {
		status(404);
39
		return { error => "User $user_id does not exist." };
40
41
42
43
44
45
46
47
48
49
50
	}
	my $values = [];
	foreach my $field (qw(id first_name surname affiliation email)) {

		#Only include E-mail for curators/admins
		next if $field eq 'email' && !$self->{'system'}->{'privacy'} && $user->{'status'} eq 'user';
		push @$values, { $field => $user->{$field} };
	}
	return $values;
};
1;