lib/Rapi/Blog/DB/Result/PreauthAction.pm untested

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 337 CC 25 SCAR 29.7
CC
25
Cov
0%
_handle_insert_update_columns_arg
90.0
not_expired
12.0
action_data
12.0
CRAP 19.4
1 use utf8;
2 package Rapi::Blog::DB::Result::PreauthAction;
3  
4 # Created by DBIx::Class::Schema::Loader
5 # DO NOT MODIFY THE FIRST PART OF THIS FILE
6  
7 use strict;
8 use warnings;
9  
10 use Moose;
11 use MooseX::NonMoose;
12 use MooseX::MarkAsMethods autoclean => 1;
13 extends 'DBIx::Class::Core';
14 __PACKAGE__->load_components("InflateColumn::DateTime");
15 __PACKAGE__->table("preauth_action");
16 __PACKAGE__->add_columns(
17 "id",
18 { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
19 "type",
20 { data_type => "varchar", is_foreign_key => 1, is_nullable => 0, size => 16 },
21 "active",
22 { data_type => "boolean", default_value => 1, is_nullable => 0 },
23 "sealed",
24 { data_type => "boolean", default_value => 0, is_nullable => 0 },
25 "create_ts",
26 { data_type => "datetime", is_nullable => 0 },
27 "expire_ts",
28 { data_type => "datetime", is_nullable => 0 },
29 "user_id",
30 { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
31 "auth_key",
32 { data_type => "varchar", is_nullable => 0, size => 128 },
33 "json_data",
34 { data_type => "text", is_nullable => 1 },
35 );
36 __PACKAGE__->set_primary_key("id");
37 __PACKAGE__->add_unique_constraint("auth_key_unique", ["auth_key"]);
38 __PACKAGE__->has_many(
39 "preauth_action_events",
40 "Rapi::Blog::DB::Result::PreauthActionEvent",
41 { "foreign.action_id" => "self.id" },
42 { cascade_copy => 0, cascade_delete => 0 },
43 );
44 __PACKAGE__->belongs_to(
45 "type",
46 "Rapi::Blog::DB::Result::PreauthActionType",
47 { name => "type" },
48 { is_deferrable => 0, on_delete => "CASCADE", on_update => "CASCADE" },
49 );
50 __PACKAGE__->belongs_to(
51 "user",
52 "Rapi::Blog::DB::Result::User",
53 { id => "user_id" },
54 {
55 is_deferrable => 0,
56 join_type => "LEFT",
57 on_delete => "CASCADE",
58 on_update => "CASCADE",
59 },
60 );
61  
62  
63 # Created by DBIx::Class::Schema::Loader v0.07049 @ 2019-01-27 12:50:16
64 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:43iysyBK1Y/m5ydH2fLefQ
65  
66  
67 __PACKAGE__->load_components('+Rapi::Blog::DB::Component::SafeResult');
68  
69 use RapidApp::Util ':all';
70 use Rapi::Blog::Util;
71 use Scalar::Util qw(looks_like_number);
72  
73 sub _dtf { (shift)->result_source->schema->storage->datetime_parser }
74  
75  
76 sub active_request_Hit {
77 my ($self, $new) = @_;
78 $self->{_active_request_Hit} = $new if ($new);
79 $self->{_active_request_Hit}
80 }
81  
82  
83 sub create_event {
84 my ($self, $pkt) = @_;
85
86 $pkt->{action_id} = $self->get_column('id');
87
88 my $Hit = $self->active_request_Hit;
89
90 $Hit
91 ? $self->evRsCmeth( create_with_hit => $Hit, $pkt )
92 : $self->evRsCmeth( create => $pkt )
93 }
94  
95 sub evRsCmeth {
96 my ($self, $meth, @args) = @_;
97
98 my $evRow = $self->preauth_action_events->$meth(@args);
99
100 my $trk = $self->{_track_created_Events}; # <-- This is not currently being set and may be removed
101 push @$trk, $evRow if (ref($trk)||'' eq 'ARRAY');
102  
103 $evRow
104 }
105  
106 sub _handle_insert_update_columns_arg {
107 my $self = shift;
108 my $columns = shift;
109
110 if ($columns) {
111 if(exists $columns->{action_data}) {
112 $self->action_data( delete $columns->{action_data} );
113 }
114 if (exists $columns->{ttl}) {
115 die "Do not supply both 'expire_ts' and 'ttl' - one of the other" if (exists $columns->{expire_ts});
116 die "Do not supply both 'ttl_minutes' and 'ttl' - one of the other" if (exists $columns->{ttl_minutes});
117 $self->ttl( delete $columns->{ttl} );
118 }
119 if (exists $columns->{ttl_minutes}) {
120 die "Do not supply both 'expire_ts' and 'ttl_minutes' - one of the other" if (exists $columns->{expire_ts});
121 die "Do not supply both 'ttl' and 'ttl_minutes' - one of the other" if (exists $columns->{ttl});
122 $self->ttl_minutes( delete $columns->{ttl_minutes} );
123 }
124
125 $self->set_inflated_columns($columns)
126 }
127 }
128  
129  
130 sub insert {
131 my $self = shift;
132 $self->_handle_insert_update_columns_arg(shift);
133
134 my $now_dt = Rapi::Blog::Util->now_dt;
135  
136 $self->create_ts( Rapi::Blog::Util->dt_to_ts($now_dt) );
137
138 $self->expire_ts( Rapi::Blog::Util->dt_to_ts(
139 $now_dt->clone->add( hours => 1 )
140 )) unless $self->expire_ts;
141
142 $self->next::method;
143
144 return $self;
145 }
146  
147 sub update {
148 my $self = shift;
149 $self->_handle_insert_update_columns_arg(shift);
150
151 $self->next::method;
152
153 return $self;
154 }
155  
156  
157 our @virtuals = qw/ttl ttl_minutes action_data/;
158 our %virtuals = map {$_=>1} @virtuals;
159  
160 sub store_column {
161 my ($self, $col, $val) = @_;
162  
163 $virtuals{$col} ? $self->$col($val) : $self->next::method($col, $val)
164 }
165  
166  
167  
168  
169 sub deactivate {
170 my ($self, $info) = @_;
171
172 $self->active or die "Already inactive!";
173
174 $self->create_event({
175 type_id => 3, # Deactivate
176 info => $info
177 });
178
179 $self->active(0);
180 $self->update;
181
182 $self
183 }
184  
185  
186  
187 sub not_expired {
188 my ($self, $test_dt) = shift;
189 $test_dt ||= Rapi::Blog::Util->now_dt;
190
191 return 1 if (
192 Rapi::Blog::Util->dt_to_ts($self->expire_ts)
193 gt Rapi::Blog::Util->dt_to_ts($test_dt)
194 );
195
196 $self->deactivate('Expired') if ($self->active);
197  
198 return 0
199 }
200  
201  
202 sub enforce_valid {
203 my $self = shift;
204 $self->active && $self->not_expired
205 }
206  
207  
208 sub request_validate {
209 my ($self, $Hit) = @_;
210
211 $self->active_request_Hit($Hit);
212
213 if($self->enforce_valid) {
214 $self->create_event({ type_id => 1 }); # Valid
215 return 1;
216 }
217 else {
218 $self->create_event({ type_id => 2 }); # Invalid
219 return 0;
220 }
221 }
222  
223  
224  
225 sub _new_actor_instance {
226 my ($self, $c) = @_;
227 $self->type
228 ->actor_class
229 ->new( ctx => $c, PreauthAction => $self );
230 }
231  
232  
233  
234 # This is called automatically by the actor:
235 sub _record_executed {
236 my ($self, $info) = @_;
237  
238 $self->create_event({
239 type_id => 4, # Executed
240 info => $info
241 });
242
243 $self->deactivate('Executed')
244 }
245  
246  
247  
248  
249  
250 sub seal {
251 my ($self, $info) = @_;
252
253 $self->sealed and die "Already sealed!";
254
255 $self->deactivate('Action was active but is being Sealed') if ($self->active);
256
257 $self->create_event({
258 type_id => 5, # Sealed
259 info => $info
260 });
261
262 $self->sealed(1);
263 $self->update;
264
265 $self
266 }
267  
268  
269 sub _serialize_set_new_action_data {
270 my $self = shift;
271 my $data = shift or die "_serialize_new_action_data(): no data supplied";
272 (ref($data)||'') eq 'HASH'
273 or die "_serialize_new_action_data(): invalid data supplied - must be a HashRef";
274
275 my $json = encode_json_ascii($data) or die "unknown error serializing to json";
276 $self->json_data($json)
277 }
278  
279 sub _deserialize_action_data {
280 my $self = shift;
281 my $json = $self->json_data or return {};
282
283 my $data = decode_json_ascii($json) or die "unknown error occured deserializing json_data";
284 (ref($data)||'') eq 'HASH' or die "Bad action data - did not deserialize to a HashRef";
285
286 $data
287 }
288  
289 # emulate simple column-like accessor:
290 sub action_data {
291 my $self = shift;
292 if(scalar(@_) > 0) {
293 my $new = shift;
294 $new = {} unless defined $new;
295 $self->_serialize_set_new_action_data($new);
296 }
297 $self->_deserialize_action_data
298 }
299  
300 sub ttl {
301 my $self = shift;
302 my $now_dt = Rapi::Blog::Util->now_dt;
303 if(scalar(@_) > 0) {
304 my $new = shift;
305 die "ttl must be a whole number of seconds greater than 0" unless (
306 $new && ($new =~ /^\d+$/)
307 );
308
309 $self->expire_ts( Rapi::Blog::Util->dt_to_ts(
310 $now_dt->clone->add( seconds => $new )
311 ))
312 }
313
314 # just being cautious here because of past bad expierences fully trusting
315 # the DateTime inflate/deflate across different versions and environments,
316 # and I know this is reliable across the widest range of scenarios when we
317 # don't want to consider time zones (since whatever it is, its the same in
318 # all the places this logic needs to consider:
319 my $expire_dt = Rapi::Blog::Util->ts_to_dt( $self->get_column('expire_ts') );
320 return $expire_dt->epoch - $now_dt->epoch;
321 }
322  
323  
324 sub ttl_minutes {
325 my $self = shift;
326 require POSIX;
327 if(scalar(@_) > 0) {
328 my $new = shift;
329 die "ttl_minutes must be a number greater than 0" unless (
330 $new && looks_like_number($new) && $new > 0
331 );
332 $self->ttl( POSIX::ceil($new) * 60 );
333 }
334 my $ttl = $self->ttl or return 0;
335 POSIX::ceil($ttl/60)
336 }
337  
338  
339 sub action_data_set {
340 my $self = shift;
341 my @kv_pairs = (ref($_[0]) eq 'HASH') ? %{ $_[0] } : @_; # <-- arg as hash or hashref
342
343 my $n = scalar(@kv_pairs);
344 ($n == 0) and die "no key/values supplied";
345 ($n % 2 == 1) and die "Odd number of args - must be even key/values pairs (either LIST or HashRef)";
346
347 my $data = $self->_deserialize_action_data;
348 my %new = ( %$data, @kv_pairs );
349
350 $self->_serialize_set_new_action_data(\%new)
351 }
352  
353 sub action_data_get {
354 my $self = shift;
355 my $key = shift or die "No key supplied.";
356 $self->_deserialize_action_data->{$key}
357 }
358  
359  
360 # You can replace this text with custom code or comments, and it will be preserved on regeneration
361 __PACKAGE__->meta->make_immutable;
362 1;