1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # CONTRIBUTION SUBMISSION POLICY:
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
45 # END BPS TAGGED BLOCK }}}
46 package RT::Attribute;
49 no warnings qw(redefine);
50 use Storable qw/nfreeze thaw/;
62 # the acl map is a map of "name of attribute" and "what right the user must have on the associated object to see/edit it
65 SavedSearch => { create => 'EditSavedSearches',
66 update => 'EditSavedSearches',
67 delete => 'EditSavedSearches',
68 display => 'ShowSavedSearches' },
72 # There are a number of attributes that users should be able to modify for themselves, such as saved searches
73 # we could do this with a different set of "modify" rights, but that gets very hacky very fast. this is even faster and even
74 # hackier. we're hardcoding that a different set of rights are needed for attributes on oneself
75 our $PERSONAL_ACL_MAP = {
76 SavedSearch => { create => 'ModifySelf',
77 update => 'ModifySelf',
78 delete => 'ModifySelf',
83 =head2 LookupObjectRight { ObjectType => undef, ObjectId => undef, Name => undef, Right => { create, update, delete, display } }
85 Returns the right that the user needs to have on this attribute's object to perform the related attribute operation. Returns "allow" if the right is otherwise unspecified.
89 sub LookupObjectRight {
91 my %args = ( ObjectType => undef,
97 # if it's an attribute on oneself, check the personal acl map
98 if (($args{'ObjectType'} eq 'RT::User') && ($args{'ObjectId'} eq $self->CurrentUser->Id)) {
99 return('allow') unless ($PERSONAL_ACL_MAP->{$args{'Name'}});
100 return('allow') unless ($PERSONAL_ACL_MAP->{$args{'Name'}}->{$args{'Right'}});
101 return($PERSONAL_ACL_MAP->{$args{'Name'}}->{$args{'Right'}});
104 # otherwise check the main ACL map
106 return('allow') unless ($ACL_MAP->{$args{'Name'}});
107 return('allow') unless ($ACL_MAP->{$args{'Name'}}->{$args{'Right'}});
108 return($ACL_MAP->{$args{'Name'}}->{$args{'Right'}});
115 =head2 Create PARAMHASH
117 Create takes a hash of values and creates a row in the database:
120 varchar(255) 'Content'.
121 varchar(16) 'ContentType',
122 varchar(64) 'ObjectType'.
125 You may pass a C<Object> instead of C<ObjectType> and C<ObjectId>.
142 if ($args{Object} and UNIVERSAL::can($args{Object}, 'Id')) {
143 $args{ObjectType} = ref($args{Object});
144 $args{ObjectId} = $args{Object}->Id;
146 return(0, $self->loc("Required parameter '[_1]' not specified", 'Object'));
150 # object_right is the right that the user has to have on the object for them to have $right on this attribute
151 my $object_right = $self->LookupObjectRight(
153 ObjectId => $args{'ObjectId'},
154 ObjectType => $args{'ObjectType'},
155 Name => $args{'Name'}
157 if ($object_right eq 'deny') {
158 return (0, $self->loc('Permission Denied'));
160 elsif ($object_right eq 'allow') {
161 # do nothing, we're ok
163 elsif (!$self->CurrentUser->HasRight( Object => $args{Object}, Right => $object_right)) {
164 return (0, $self->loc('Permission Denied'));
168 if (ref ($args{'Content'}) ) {
169 eval {$args{'Content'} = $self->_SerializeContent($args{'Content'}); };
173 $args{'ContentType'} = 'storable';
177 $self->SUPER::Create(
178 Name => $args{'Name'},
179 Content => $args{'Content'},
180 ContentType => $args{'ContentType'},
181 Description => $args{'Description'},
182 ObjectType => $args{'ObjectType'},
183 ObjectId => $args{'ObjectId'},
189 # {{{ sub LoadByNameAndObject
191 =head2 LoadByNameAndObject (Object => OBJECT, Name => NAME)
193 Loads the Attribute named NAME for Object OBJECT.
197 sub LoadByNameAndObject {
207 Name => $args{'Name'},
208 ObjectType => ref($args{'Object'}),
209 ObjectId => $args{'Object'}->Id,
218 =head2 _DeserializeContent
220 DeserializeContent returns this Attribute's "Content" as a hashref.
225 sub _DeserializeContent {
230 eval {$hashref = thaw(decode_base64($content))} ;
232 $RT::Logger->error("Deserialization of attribute ".$self->Id. " failed");
242 Returns this attribute's content. If it's a scalar, returns a scalar
243 If it's data structure returns a ref to that data structure.
249 # Here we call _Value to get the ACL check.
250 my $content = $self->_Value('Content');
251 if ($self->__Value('ContentType') eq 'storable') {
252 eval {$content = $self->_DeserializeContent($content); };
254 $RT::Logger->error("Deserialization of content for attribute ".$self->Id. " failed. Attribute was: ".$content);
262 sub _SerializeContent {
265 return( encode_base64(nfreeze($content)));
273 # Call __Value to avoid ACL check.
274 if ($self->__Value('ContentType') eq 'storable') {
275 # We eval the serialization because it will lose on a coderef.
276 eval {$content = $self->_SerializeContent($content); };
278 $RT::Logger->error("For some reason, content couldn't be frozen");
282 return ($self->SUPER::SetContent($content));
287 Returns the subvalue for $key.
291 my $user = $RT::SystemUser;
292 my ($id, $msg) = $user->AddAttribute(Name => 'SavedSearch', Content => { Query => 'Foo'} );
294 my $attr = RT::Attribute->new($RT::SystemUser);
296 ok($attr->Name eq 'SavedSearch');
297 $attr->SetSubValues( Format => 'baz');
299 my $format = $attr->SubValue('Format');
300 is ($format , 'baz');
302 $attr->SetSubValues( Format => 'bar');
303 $format = $attr->SubValue('Format');
304 is ($format , 'bar');
306 $attr->DeleteAllSubValues();
307 $format = $attr->SubValue('Format');
310 $attr->SetSubValues(Format => 'This is a format');
312 my $attr2 = RT::Attribute->new($RT::SystemUser);
314 is ($attr2->SubValue('Format'), 'This is a format');
324 my $values = $self->Content();
325 return undef unless ref($values);
326 return($values->{$key});
329 =head2 DeleteSubValue NAME
331 Deletes the subvalue with the key NAME
338 my %values = $self->Content();
339 delete $values{$key};
340 $self->SetContent(%values);
347 =head2 DeleteAllSubValues
349 Deletes all subvalues for this attribute
354 sub DeleteAllSubValues {
356 $self->SetContent({});
359 =head2 SetSubValues { }
361 Takes a hash of keys and values and stores them in the content of this attribute.
363 Each key B<replaces> the existing key with the same name
365 Returns a tuple of (status, message)
373 my $values = ($self->Content() || {} );
374 foreach my $key (keys %args) {
375 $values->{$key} = $args{$key};
378 $self->SetContent($values);
385 my $object_type = $self->__Value('ObjectType');
387 eval { $object = $object_type->new($self->CurrentUser) };
388 unless(UNIVERSAL::isa($object, $object_type)) {
389 $RT::Logger->error("Attribute ".$self->Id." has a bogus object type - $object_type (".$@.")");
392 $object->Load($self->__Value('ObjectId'));
401 unless ($self->CurrentUserHasRight('delete')) {
402 return (0,$self->loc('Permission Denied'));
404 return($self->SUPER::Delete(@_));
410 unless ($self->CurrentUserHasRight('display')) {
411 return (0,$self->loc('Permission Denied'));
414 return($self->SUPER::_Value(@_));
422 unless ($self->CurrentUserHasRight('modify')) {
424 return (0,$self->loc('Permission Denied'));
426 return($self->SUPER::_Set(@_));
431 =head2 CurrentUserHasRight
433 One of "display" "modify" "delete" or "create" and returns 1 if the user has that right for attributes of this name for this object.Returns undef otherwise.
437 sub CurrentUserHasRight {
441 # object_right is the right that the user has to have on the object for them to have $right on this attribute
442 my $object_right = $self->LookupObjectRight(
444 ObjectId => $self->__Value('ObjectId'),
445 ObjectType => $self->__Value('ObjectType'),
446 Name => $self->__Value('Name')
449 return (1) if ($object_right eq 'allow');
450 return (0) if ($object_right eq 'deny');
451 return(1) if ($self->CurrentUser->HasRight( Object => $self->Object, Right => $object_right));
459 We should be deserializing the content on load and then enver again, rather than at every access