annotate Lib/IMPL/Code/BasePropertyImplementor.pm @ 393:69a1f1508696

minor security refactoring
author cin
date Fri, 14 Feb 2014 16:41:12 +0400
parents 0d63f5273307
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
1 package IMPL::Code::BasePropertyImplementor;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
2 use strict;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
3
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
4 use IMPL::Const qw(:prop :access);
381
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
5 use Scalar::Util qw(looks_like_number);
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
6
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
7 use constant {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
8 CodeNoGetAccessor => 'die new IMPL::Exception(\'The property is write only\',$name,$class) unless $get;',
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
9 CodeNoSetAccessor => 'die new IMPL::Exception(\'The property is read only\',$name,$class) unless $set;',
381
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
10 CodeCustomGetAccessor => '$this->$get(@_);',
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
11 CodeCustomSetAccessor => '$this->$set(@_);',
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
12 CodeValidator => '$this->$validator(@_);',
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
13 CodeOwnerCheck => "die new IMPL::Exception('Set accessor is restricted to the owner',\$name,\$class,scalar caller) unless caller eq \$class;"
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
14 };
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
15
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
16 sub CodeSetAccessor {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
17 die new IMPL::Exception("Standard accessors not supported",'Set');
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
18 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
19
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
20 sub CodeGetAccessor {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
21 die new IMPL::Exception("Standard accessors not supported",'Get');
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
22 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
23
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
24 sub CodeGetListAccessor {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
25 die new IMPL::Exception("Standard accessors not supported",'GetList');
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
26 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
27
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
28 sub CodeSetListAccessor {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
29 die new IMPL::Exception("Standard accessors not supported",'SetList');
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
30 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
31
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
32 sub factoryParams { qw($class $name $set $get $validator) };
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
33
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
34 our %ACCESS_CODE = (
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
35 ACCESS_PUBLIC , "",
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
36 ACCESS_PROTECTED, "die new IMPL::Exception('Can\\'t access the protected member',\$name,\$class,scalar caller) unless UNIVERSAL::isa(scalar caller,\$class);",
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
37 ACCESS_PRIVATE, "die new IMPL::Exception('Can\\'t access the private member',\$name,\$class,scalar caller) unless caller eq \$class;"
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
38 );
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
39
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
40 sub NormalizeSpecification {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
41 my ($this,$spec) = @_;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
42
385
sergey
parents: 381
diff changeset
43 return $spec if ref($spec);
sergey
parents: 381
diff changeset
44
sergey
parents: 381
diff changeset
45 if (looks_like_number($spec)) {
sergey
parents: 381
diff changeset
46 return {
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
47 get => $spec & PROP_GET,
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
48 set => $spec & PROP_SET,
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
49 isList => $spec & PROP_LIST,
278
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
50 ownerSet => (($spec & PROP_OWNERSET) == PROP_OWNERSET),
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
51 direct => $spec & PROP_DIRECT
385
sergey
parents: 381
diff changeset
52 };
sergey
parents: 381
diff changeset
53 } else {
sergey
parents: 381
diff changeset
54 return {};
sergey
parents: 381
diff changeset
55 }
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
56 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
57
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
58 sub CreateFactoryId {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
59 my ($self, $spec) = @_;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
60
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
61 join( '',
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
62 map(
278
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
63 ($_
381
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
64 ? ( _isCustom($_)
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
65 ? 'x'
278
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
66 : 's')
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
67 : '_'),
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
68 @$spec{qw(get set)}
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
69 ),
278
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
70 $spec->{access} || ACCESS_PUBLIC,
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
71 $spec->{validator} ? 'v' : '_',
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
72 $spec->{isList} ? 'l' : '_',
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
73 $spec->{ownerSet} ? 'o' : '_'
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
74 );
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
75 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
76
381
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
77 sub _isCustom {
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
78 ref($_[0]) eq 'CODE' || not(ref($_[0]) || looks_like_number($_[0]));
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
79 }
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
80
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
81 sub CreateFactory {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
82 my ($self,$spec) = @_;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
83
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
84 return $self->CreateFactoryImpl(
278
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
85 ($spec->{get}
381
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
86 ? ( _isCustom($spec->{get})
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
87 ? $self->CodeCustomGetAccessor
278
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
88 : ($spec->{isList}
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
89 ? $self->CodeGetListAccessor
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
90 : $self->CodeGetAccessor
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
91 )
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
92 )
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
93 : $self->CodeNoGetAccessor
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
94 ),
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
95 ($spec->{set}
381
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
96 ? ( _isCustom($spec->{set})
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
97 ? $self->CodeCustomSetAccessor
278
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
98 : ($spec->{isList}
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
99 ? $self->CodeSetListAccessor
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
100 : $self->CodeSetAccessor
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
101 )
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
102 )
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
103 : $self->CodeNoSetAccessor
4ddb27ff4a0b core refactoring
cin
parents: 277
diff changeset
104 ),
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
105 $ACCESS_CODE{$spec->{access} || ACCESS_PUBLIC} || '',
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
106 $spec->{validator} ? $self->CodeValidator : '',
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
107 $spec->{ownerSet} ? $self->CodeOwnerCheck : ''
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
108 );
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
109 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
110
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
111 sub CreateFactoryImpl {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
112 my ($self,$codeGet,$codeSet,$codeAccessCheck,$codeValidator,$codeOwnerCheck) = @_;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
113
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
114 my $strParams = join(',',$self->factoryParams);
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
115
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
116 my $factory = <<FACTORY;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
117
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
118 sub {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
119 my ($strParams) = \@_;
381
ced5937ff21a Custom getters/setters support method names in theirs definitions
cin
parents: 278
diff changeset
120 return sub {
277
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
121 my \$this = shift;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
122 $codeAccessCheck
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
123 if (\@_) {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
124 $codeOwnerCheck
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
125 $codeValidator
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
126 $codeSet
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
127 } else {
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
128 $codeGet
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
129 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
130 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
131 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
132 FACTORY
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
133
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
134 return ( eval $factory or die new IMPL::Exception("Syntax error due compiling the factory","$@") );
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
135 }
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
136
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
137
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
138 1;
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
139
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
140 __END__
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
141
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
142 =pod
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
143
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
144 =head1 NAME
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
145
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
146 C<IMPL::Code::BasePropertyImplementor> набор впомогательныйх статических методов
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
147 для генерации свойств.
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
148
6585464c4664 sync (unstable)
sergey
parents:
diff changeset
149 =cut