/
user.pm
120 lines (100 loc) · 4.46 KB
/
user.pm
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package resources2::user;
use strict;
use warnings;
no warnings('once');
use Conf;
use Data::Dumper;
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', 'user is active' ],
"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' => $self->cgi->url.'/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;