annotate Lib/IMPL/Web/Application/Resource.pm @ 393:69a1f1508696

minor security refactoring
author cin
date Fri, 14 Feb 2014 16:41:12 +0400
parents 54b1dc9e808a
children ef37ec102dab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
1 package IMPL::Web::Application::Resource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
2 use strict;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
3
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
4 use constant {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
5 ResourceClass => __PACKAGE__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
6 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
7 use Scalar::Util qw(blessed);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
8
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
9 use IMPL::lang qw(:hash :base);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
10 use IMPL::Const qw(:prop);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
11 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
12 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
13 Exception => 'IMPL::Exception',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
14 OpException => '-IMPL::InvalidOperationException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
15 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
16 ResourceInterface => '-IMPL::Web::Application',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
17 Loader => 'IMPL::Code::Loader'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
18 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
19 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
20 'IMPL::Object' => undef,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
21 'IMPL::Web::Application::ResourceBase' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
22 ],
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
23 props => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
24 access => PROP_RW,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
25 verbs => PROP_RW,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
26 children => PROP_RW
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
27 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
28 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
29
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
30 __PACKAGE__->static_accessor(verbNames => [qw(get post put delete options head)]);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
31 __PACKAGE__->static_accessor(httpMethodPrefix => 'Http');
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
32
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
33 sub CTOR {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
34 my ($this, %args) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
35
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
36 my %verbs;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
37 my $httpPrefix = $this->httpMethodPrefix;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
38
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
39 foreach my $verb (@{$this->verbNames}) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
40 my $method = exists $args{$verb} ? $args{$verb} : $this->can($httpPrefix . ucfirst($verb));
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
41 $verbs{$verb} = $method
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
42 if $method;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
43 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
44
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
45 hashApply(\%verbs,$args{verbs})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
46 if ref($args{verbs}) eq 'HASH' ;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
47
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
48 $this->children($args{children} || $this->GetChildResources());
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
49
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
50 $this->access($args{access})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
51 if $args{access};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
52
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
53 $this->verbs(\%verbs);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
54 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
55
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
56 sub _isInvokable {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
57 my ($this,$method) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
58
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
59 return
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
60 (blessed($method) and $method->can('Invoke')) ||
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
61 ref($method) eq 'CODE'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
62 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
63
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
64 sub _invoke {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
65 my ($this,$method,@args) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
66
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
67 if(blessed($method) and $method->can('Invoke')) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
68 return $method->Invoke($this,@args);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
69 } elsif(ref($method) eq 'CODE' || (not(ref($method)) and $this->can($method))) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
70 return $this->$method(@args);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
71 } else {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
72 die OpException->new("Can't invoke the specified method: $method");
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
73 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
74 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
75
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
76 sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
77 shift->model;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
78 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
79
392
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
80 sub AccessCheck {
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
81 my ($this,$verb) = @_;
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
82
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
83 $this->_invoke($this->access,$verb)
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
84 if $this->access;
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
85 }
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
86
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
87 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
88 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
89
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
90 my $children = $this->children
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
91 or die NotFoundException->new( $this->location->url, $childId );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
92
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
93 if (ref($children) eq 'HASH') {
375
cin
parents: 374
diff changeset
94 if(my $child = $children->{$childId}) {
cin
parents: 374
diff changeset
95 return $this->_isInvokable($child) ? $this->_invoke($child, $childId) : $child;
cin
parents: 374
diff changeset
96 } else {
cin
parents: 374
diff changeset
97 die NotFoundException->new( $this->location->url, $childId );
cin
parents: 374
diff changeset
98 }
374
cin
parents: 373
diff changeset
99 } elsif($this->_isInvokable($children)) {
cin
parents: 373
diff changeset
100 return $this->_invoke($children,$childId);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
101 } else {
374
cin
parents: 373
diff changeset
102 die OpException->new("Invalid resource description", $childId, $children);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
103 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
104 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
105
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
106 sub FetchChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
107 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
108
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
109 my $info = $this->Fetch($childId);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
110
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
111 return $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
112 if (is($info,ResourceInterface));
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
113
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
114 return $this->CreateChildResource($info, $childId)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
115 if ref($info) eq 'HASH';
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
116
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
117 die OpException->new("Invalid resource description", $childId, $info);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
118 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
119
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
120 sub CreateChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
121 my ($this,$info, $childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
122
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
123 my $params = hashApply(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
124 {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
125 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
126 id => $childId,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
127 request => $this->request,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
128 class => ResourceClass
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
129 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
130 $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
131 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
132
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
133 $params->{model} = $this->_invoke($params->{model})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
134 if $this->_isInvokable($params->{model});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
135
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
136 my $factory = Loader->default->Require($params->{class});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
137
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
138 return $factory->new(%$params);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
139 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
140
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
141 sub GetChildResources {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
142 return {};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
143 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
144
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
145 1;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
146
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
147 __END__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
148
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
149 =pod
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
150
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
151 =head1 NAME
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
152
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
153 C<IMPL::Web::Application::Resource> - Ресурс C<REST> веб приложения
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
154
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
155 =head1 SYNOPSIS
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
156
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
157 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
158
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
159 use IMPL::require {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
160 Resource => 'IMPL::Web::Application::Resource',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
161 Security => 'IMPL::Security',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
162 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
163 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
164 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
165
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
166 my $model = Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
167 get => sub { },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
168 verbs => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
169 # non-standart verbs placed here
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
170 myverb => sub { }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
171 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
172 #child resources can be a hash
391
2287c72f303a code cleanup
cin
parents: 375
diff changeset
173 children => {
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
174 user => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
175 # a resource class may be specified optionally
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
176 # class => Resource,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
177 model => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
178 return Security->principal
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
179 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
180 # the default get implementation is implied
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
181 # get => sub { shift->model },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
182 access => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
183 my ($this,$verb) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
184 die ForbiddenException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
185 if Security->principal->isNobody
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
186 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
187 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
188 catalog => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
189 get => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
190 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
191
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
192 return $ctx->products->find_rs({ in_stock => 1 });
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
193 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
194 # chid resource may be created dynamically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
195 children => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
196 # binds model against the parent reource and id
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
197 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
198
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
199 ($id) = ($id =~ /^(\w+)$/)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
200 or die NotFoundException->new($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
201
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
202 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
203
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
204 my $item = $ctx->products->fetch($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
205
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
206 die NotFoundException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
207 unless $item;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
208
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
209 # return parameters for the new resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
210 return {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
211 model => $item,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
212 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
213 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
214 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
215 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
216 # dynamically binds whole child resource. The result of binding is
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
217 # the new resource or a hash with arguments to create one
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
218 posts => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
219 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
220
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
221 # this approach can be used to create a dynamic resource relaying
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
222 # on the type of the model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
223
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
224 return Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
225 id => $id,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
226 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
227 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
228 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
229
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
230 # ditto
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
231 # parent and id will be mixed in automagically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
232 # return { get => sub { shift->model} }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
233 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
234 post_only => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
235 get => undef, # remove GET verb implicitly
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
236 post => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
237 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
238 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
239 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
240 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
241 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
242
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
243 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
244
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
245 Альтернативный вариант для создания класса ресурса.
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
246
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
247 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
248
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
249 package MyResource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
250
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
251 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
252 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
253 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
254 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
255 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
256 'IMPL::Web::Application::Resource' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
257 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
258 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
259
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
260 sub ds {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
261 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
262
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
263 $this->context->{ds} ||= $this->application->ConnectDb();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
264 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
265
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
266 sub InvokeHttpVerb {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
267 my $this = shift;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
268
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
269 $this->ds->Begin();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
270
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
271 my $result = $this->next::method(@_);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
272
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
273 # in case of error the data context will be disposed and the transaction
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
274 # will be reverted
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
275 $this->ds->Commit();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
276
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
277 return $result;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
278 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
279
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
280 # this method is inherited by default
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
281 # sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
282 # shift->model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
283 #
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
284 # }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
285
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
286 sub HttpPost {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
287 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
288
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
289 my %data = map {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
290 $_,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
291 $this->request->param($_)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
292 } qw(name description value);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
293
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
294 die ForbiddenException->new("The item with the scpecified name can't be created'")
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
295 if(not $data{name} or $this->ds->items->find({ name => $data{name}))
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
296
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
297 $this->ds->items->insert(\%data);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
298
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
299 return $this->NoContent();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
300 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
301
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
302 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
303 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
304
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
305 my $item = $this->ds->items->find({name => $childId})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
306 or die NotFoundException->new();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
307
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
308 # return parameters for the child resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
309 return { model => $item, role => "item food" };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
310 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
311
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
312 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
313
374
cin
parents: 373
diff changeset
314 =head1 MEMBERS
cin
parents: 373
diff changeset
315
375
cin
parents: 374
diff changeset
316 =head2 C<[get,set]verbs>
cin
parents: 374
diff changeset
317
cin
parents: 374
diff changeset
318 Хеш с C<HTTP> методами. При попытке вызова C<HTTP> метода, которого нет в этом
cin
parents: 374
diff changeset
319 хеше приводит к исключению C<IMPL::Web::NotAllowedException>.
cin
parents: 374
diff changeset
320
cin
parents: 374
diff changeset
321 =head2 C<[get,set]access>
cin
parents: 374
diff changeset
322
cin
parents: 374
diff changeset
323 Метод для проверки прав доступа. Если не задан, то доспуп возможен для всех.
cin
parents: 374
diff changeset
324
cin
parents: 374
diff changeset
325 =head2 C<[get,set]children>
cin
parents: 374
diff changeset
326
cin
parents: 374
diff changeset
327 Дочерние ресурсы. Дочерние ресурсы могут быть описаны либо в виде хеша, либо
cin
parents: 374
diff changeset
328 в виде метода.
cin
parents: 374
diff changeset
329
cin
parents: 374
diff changeset
330 =head3 C<HASH>
cin
parents: 374
diff changeset
331
cin
parents: 374
diff changeset
332 Данный хещ содержит в себе таблицу идентификаторов дочерних ресурсов и их
cin
parents: 374
diff changeset
333 описаний.
cin
parents: 374
diff changeset
334
cin
parents: 374
diff changeset
335 Описание каждого ресурса представляет собой либо функцию, либо параметры для
cin
parents: 374
diff changeset
336 создания ресурса C<CraeteChildResource>. Если описание в виде функции, то она
cin
parents: 374
diff changeset
337 должна возвращать либо объект типа ресурс либо параметры для его создания.
cin
parents: 374
diff changeset
338
cin
parents: 374
diff changeset
339 =head3 C<CODE>
cin
parents: 374
diff changeset
340
cin
parents: 374
diff changeset
341 Если дочерние ресурсы описаны в виде функции (возможно использовать имя метода
cin
parents: 374
diff changeset
342 класса текущего ресурса), то для получения дочернего ресурса будет вызвана
cin
parents: 374
diff changeset
343 функция с параметрами C<($this,$childId)>, где C<$this> - текущий ресурс,
cin
parents: 374
diff changeset
344 C<$childId> - идентификатор дочернего ресурса, который нужно вернуть.
cin
parents: 374
diff changeset
345
cin
parents: 374
diff changeset
346 Данная функция должна возвратить либо объект типа ресурс, либо ссылку на хеш с
cin
parents: 374
diff changeset
347 параметрами для создания оного при помощи метода
cin
parents: 374
diff changeset
348 C<CreateChildResource($params,$childId)>.
cin
parents: 374
diff changeset
349
374
cin
parents: 373
diff changeset
350 =head2 C<[virtual]Fetch($childId)>
cin
parents: 373
diff changeset
351
cin
parents: 373
diff changeset
352 Метод для получения дочернего ресурса.
cin
parents: 373
diff changeset
353
cin
parents: 373
diff changeset
354 Возвращает параметры для создания дочернего ресурса, либо уже созданный ресурс.
cin
parents: 373
diff changeset
355 Создание дочернего ресурса происходит при помощи метода C<CreateChildResource()>
cin
parents: 373
diff changeset
356 который добавляет недостающие параметры к возвращенным в данным методом и
cin
parents: 373
diff changeset
357 создает новый ресурс
cin
parents: 373
diff changeset
358
375
cin
parents: 374
diff changeset
359 =head2 C<CreateChildResource($params,$id)>
cin
parents: 374
diff changeset
360
cin
parents: 374
diff changeset
361 Создает новый дочерний ресурс с указанным идентификатором и параметрами.
cin
parents: 374
diff changeset
362 Автоматически заполняет параметры
cin
parents: 374
diff changeset
363
cin
parents: 374
diff changeset
364 =over
cin
parents: 374
diff changeset
365
cin
parents: 374
diff changeset
366 =item * C<parent>
cin
parents: 374
diff changeset
367
cin
parents: 374
diff changeset
368 =item * C<id>
cin
parents: 374
diff changeset
369
cin
parents: 374
diff changeset
370 =item * C<request>
cin
parents: 374
diff changeset
371
cin
parents: 374
diff changeset
372 =back
cin
parents: 374
diff changeset
373
cin
parents: 374
diff changeset
374 Тип создаваемого ресурса C<IMPL::Web::Application::Resource>, либо указывается
cin
parents: 374
diff changeset
375 в параметре C<class>.
cin
parents: 374
diff changeset
376
cin
parents: 374
diff changeset
377 =head2 C<[virtual]HttpGet()>
cin
parents: 374
diff changeset
378
cin
parents: 374
diff changeset
379 Реализует C<HTTP> метод C<GET>. По-умолчанию возвращает модель.
cin
parents: 374
diff changeset
380
cin
parents: 374
diff changeset
381 Данный метод нужен для того, чтобы ресурс по-умолчанию поддерживал метод C<GET>,
cin
parents: 374
diff changeset
382 что является самым частым случаем, если нужно изменить данное поведение, нужно:
cin
parents: 374
diff changeset
383
cin
parents: 374
diff changeset
384 =over
cin
parents: 374
diff changeset
385
cin
parents: 374
diff changeset
386 =item * Передать в параметр конструктора C<get> значение undef
cin
parents: 374
diff changeset
387
cin
parents: 374
diff changeset
388 =item * Переопределить метод C<HttpGet>
cin
parents: 374
diff changeset
389
cin
parents: 374
diff changeset
390 =item * При проверке прав доступа выдать исключение
cin
parents: 374
diff changeset
391
cin
parents: 374
diff changeset
392 =back
374
cin
parents: 373
diff changeset
393
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
394 =cut
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
395