lib/VUser/SOAP/Dispatcher.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 151 CC 14 SCAR 23.9
CC
14
Cov
0%
get_options
20.0
AUTOLOAD
20.0
get_keywords
12.0
CRAP 11.0
1 package VUser::SOAP::Dispatcher;
2 use warnings;
3 use strict;
4  
5 # Copyright (c) 2006 Randy Smith
6 # $Id: Dispatcher.pm,v 1.9 2007/07/03 21:10:57 perlstalker Exp $
7  
8 use Data::Dumper;
9  
10 use SOAP::Lite;
11 use VUser::SOAP;
12 use VUser::ExtLib qw(:config);
13 use VUser::Log qw(:levels);
14  
15 our @ISA = qw(Exporter SOAP::Server::Parameters);
16  
17 my $c_sec = 'vsoapd';
18  
19 sub login {
20 my $self = shift;
21 my $user = shift;
22 my $password = shift;
23 my $envelope = shift; # SOAP::SOM object
24
25 VUser::SOAP::Log(LOG_DEBUG, "In login");
26
27 # Is there a way to get the IP from a SOAP::SOM object?
28 my $ip = '127.0.0.1';
29
30 # Check auth
31 my $authinfo = VUser::SOAP::login($user, $password, $ip);
32
33 VUser::SOAP::Log(LOG_DEBUG, "Mid login");
34
35 if (not defined $authinfo) {
36 # auth failed FAULT
37 VUser::SOAP::Log(LOG_INFO, "Login failed for $user\@$ip");
38 die $self->SOAP::Fault->faultcode('Server.Custom')->faultstring("Failed login");
39 } else {
40 VUser::SOAP::Log(LOG_INFO, "Login successful for $user\@$ip");
41 my $soap_info = SOAP::Data->name('authinfo' => $authinfo);
42 return $soap_info;
43 }
44 }
45  
46 sub get_keywords {
47 my $self = shift;
48 my $envelope = pop; # SOAP::SOM object
49 my $authinfo = $envelope->valueof ("//authinfo");
50
51 # authenticate here
52 if (check_bool(VUser::SOAP::conf($c_sec, 'require authentication'))) {
53 if (not VUser::SOAP::check_ticket($authinfo)) {
54 # error: invalid or expired ticket: FAULT
55 die SOAP::Failt
56 ->faultcode('Server.Custom')
57 ->faultstring('Authentication failed');
58 }
59 }
60
61 #return VUser::SOAP::get_keywords($authinfo);
62 my @keywords = VUser::SOAP::get_keywords($authinfo);
63 return SOAP::Data->name('keywords' => @keywords);
64 }
65  
66 sub get_actions {
67 my $self = shift;
68 my $env = pop; # SOAP::SOM object
69 my $authinfo = $env->valueof ("//authinfo");
70
71 VUser::SOAP::Log(LOG_DEBUG, "get_actions()");
72
73 # authenticate here
74 if (check_bool(VUser::SOAP::conf($c_sec, 'require authentication'))) {
75 if (not VUser::SOAP::check_ticket($authinfo)) {
76 # error: invalid or expired ticket: FAULT
77 die SOAP::Failt
78 ->faultcode('Server.Custom')
79 ->faultstring('Authentication failed');
80 }
81 }
82
83 my $keyword = $env->valueof('//keyword');
84
85 my @actions = VUser::SOAP::get_actions ($authinfo, $keyword);
86 #use Data::Dumper; print Dumper \@actions;
87 VUser::SOAP::Log(LOG_DEBUG, 'Actions: '.Dumper(\@actions));
88 return SOAP::Data->name('actions' => @actions);
89 }
90  
91 sub get_options {
92 my $self = shift;
93 my $envelop = pop; # SOAP::SOM object
94 my $authinfo = $envelop->valueof ("//authinfo");
95
96 # authenticate here
97 if (check_bool(VUser::SOAP::conf($c_sec, 'require authentication'))) {
98 if (not VUser::SOAP::check_ticket($authinfo)) {
99 # error: invalid or expired ticket: FAULT
100 die SOAP::Failt
101 ->faultcode('Server.Custom')
102 ->faultstring('Authentication failed');
103 }
104 }
105
106 #print STDERR "get_options() Passed options: ";
107 #use Data::Dumper; print Dumper [@_];
108
109 my $keyword = $envelop->valueof('//keyword');
110 my $action = $envelop->valueof('//action');
111
112 VUser::SOAP::Log (LOG_DEBUG, "Dispatch: get options for %s | %s", $keyword, $action);
113
114 if (not defined $keyword or not defined $action) {
115 die SOAP::Fault->faultcode('Server.Custom')->faultstring("Missing keyword or action");
116 }
117
118 my @options = VUser::SOAP::get_options ($authinfo, $keyword, $action);
119 return SOAP::Data->name('options' => @options);
120 }
121  
122 # SOAP Param order: keyword, action, @params
123  
124 # This might be hairy from a WSDL perspective since the options change
125 # based on the keyword/action pair that's used. It might be better if, instead,
126 # a hash is used (as per the original vsoapd) in this case. Or it might be
127 # be better if it's not here at all.
128 # For now, I'll leave it here as an undocumented feature.
129 sub run_tasks {
130 my $self = shift;
131
132 my $env = $_[-1]; # SOAP::SOM object
133  
134 my $keyword = shift;
135 my $action = shift;
136 my @params = @_;
137
138 my $authinfo = $env->valueof ("//authinfo");
139
140 # authenticate here
141 if (check_bool(VUser::SOAP::conf($c_sec, 'require authentication'))) {
142 if (not VUser::SOAP::check_ticket($authinfo)) {
143 # error: invalid or expired ticket: FAULT
144 die SOAP::Fault
145 ->faultcode('Server.Custom')
146 ->faultstring('Authentication failed');
147 }
148 }
149
150 # We've successfully gotten passed the authentication.
151 # Let's do some work.
152 #print "Authinfo: "; use Data::Dumper; print Dumper $authinfo;
153 VUser::SOAP::Log (LOG_DEBUG, "Authinfo $authinfo");
154 return VUser::SOAP::rs2soap(VUser::SOAP::run_tasks($authinfo,
155 $keyword->value,
156 $action->value,
157 @params));
158 }
159  
160 #sub handle {
161 # my $self = shift;
162 # print STDERR "Getting a handle\n";
163 # $self->SUPER::handle(@_);
164 #}
165  
166 sub AUTOLOAD {
167 use vars '$AUTOLOAD';
168 my $self = shift;
169
170 my $envelope = $_[-1]; # SOAP::SOM object
171
172 my @params = @_;
173  
174 my $name = $AUTOLOAD;
175 $name =~ s/.*://;
176 #print "name: $name\n";
177 if ($name =~ /^([^_]+)_([^_]+)$/) {
178 my $keyword = $1;
179 my $action = $2;
180
181 my $authinfo = $envelope->valueof ("//authinfo");
182
183 # authenticate here
184 if (check_bool(VUser::SOAP::conf($c_sec, 'require authentication'))) {
185 if (not VUser::SOAP::check_ticket($authinfo)) {
186 # error: invalid or expired ticket: FAULT
187 die SOAP::Fault
188 ->faultcode('Server.Custom')
189 ->faultstring('Authentication failed');
190 }
191 }
192
193 # We've successfully gotten passed the authentication.
194 # Let's do some work.
195 my $results = VUser::SOAP::rs2soap(VUser::SOAP::run_tasks($authinfo,
196 $keyword, $action, @params));
197 #print "results: "; use Data::Dumper; print Dumper $results;
198 return $results;
199 } else {
200 VUser::SOAP::Log(LOG_INFO, "Unknown method called: $name");
201 die SOAP::Fault->faultcode("Server.Custom")
202 ->faultstring("Unknown method");
203 }
204 }
205  
206 1;
207  
208 __END__