1 # {{{ BEGIN BPS TAGGED BLOCK
5 # This software is Copyright (c) 1996-2004 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
47 no warnings qw(redefine);
48 use Storable qw/nfreeze thaw/;
60 # 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
63 SavedSearch => { create => 'EditSavedSearches',
64 update => 'EditSavedSearches',
65 delete => 'EditSavedSearches',
66 display => 'ShowSavedSearches' },
70 # There are a number of attributes that users should be able to modify for themselves, such as saved searches
71 # we could do this with a different set of "modify" rights, but that gets very hacky very fast. this is even faster and even
72 # hackier. we're hardcoding that a different set of rights are needed for attributes on oneself
73 our $PERSONAL_ACL_MAP = {
74 SavedSearch => { create => 'ModifySelf',
75 update => 'ModifySelf',
76 delete => 'ModifySelf',
81 =head2 LookupObjectRight { ObjectType => undef, ObjectId => undef, Name => undef, Right => { create, update, delete, display } }
83 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.
87 sub LookupObjectRight {
89 my %args = ( ObjectType => undef,
95 # if it's an attribute on oneself, check the personal acl map
96 if (($args{'ObjectType'} eq 'RT::User') && ($args{'ObjectId'} eq $self->CurrentUser->Id)) {
97 return('allow') unless ($PERSONAL_ACL_MAP->{$args{'Name'}});
98 return('allow') unless ($PERSONAL_ACL_MAP->{$args{'Name'}}->{$args{'Right'}});
99 return($PERSONAL_ACL_MAP->{$args{'Name'}}->{$args{'Right'}});
102 # otherwise check the main ACL map
104 return('allow') unless ($ACL_MAP->{$args{'Name'}});
105 return('allow') unless ($ACL_MAP->{$args{'Name'}}->{$args{'Right'}});
106 return($ACL_MAP->{$args{'Name'}}->{$args{'Right'}});
113 =head2 Create PARAMHASH
115 Create takes a hash of values and creates a row in the database:
118 varchar(255) 'Content'.
119 varchar(16) 'ContentType',
120 varchar(64) 'ObjectType'.
123 You may pass a C<Object> instead of C<ObjectType> and C<ObjectId>.
140 if ($args{Object} and UNIVERSAL::can($args{Object}, 'Id')) {
141 $args{ObjectType} = ref($args{Object});
142 $args{ObjectId} = $args{Object}->Id;
144 return(0, $self->loc("Required parameter '[_1]' not specified", 'Object'));
148 # object_right is the right that the user has to have on the object for them to have $right on this attribute
149 my $object_right = $self->LookupObjectRight(
151 ObjectId => $args{'ObjectId'},
152 ObjectType => $args{'ObjectType'},
153 Name => $args{'Name'}
155 if ($object_right eq 'deny') {
156 return (0, $self->loc('Permission Denied'));
158 elsif ($object_right eq 'allow') {
159 # do nothing, we're ok
161 elsif (!$self->CurrentUser->HasRight( Object => $args{Object}, Right => $object_right)) {
162 return (0, $self->loc('Permission Denied'));
166 if (ref ($args{'Content'}) ) {
167 eval {$args{'Content'} = $self->_SerializeContent($args{'Content'}); };
171 $args{'ContentType'} = 'storable';
175 $self->SUPER::Create(
176 Name => $args{'Name'},
177 Content => $args{'Content'},
178 ContentType => $args{'ContentType'},
179 Description => $args{'Description'},
180 ObjectType => $args{'ObjectType'},
181 ObjectId => $args{'ObjectId'},
187 # {{{ sub LoadByNameAndObject
189 =head2 LoadByNameAndObject (Object => OBJECT, Name => NAME)
191 Loads the Attribute named NAME for Object OBJECT.
195 sub LoadByNameAndObject {
205 Name => $args{'Name'},
206 ObjectType => ref($args{'Object'}),
207 ObjectId => $args{'Object'}->Id,
216 =head2 _DeserializeContent
218 DeserializeContent returns this Attribute's "Content" as a hashref.
223 sub _DeserializeContent {
228 eval {$hashref = thaw(decode_base64($content))} ;
230 $RT::Logger->error("Deserialization of attribute ".$self->Id. " failed");
240 Returns this attribute's content. If it's a scalar, returns a scalar
241 If it's data structure returns a ref to that data structure.
247 # Here we call _Value to get the ACL check.
248 my $content = $self->_Value('Content');
249 if ($self->__Value('ContentType') eq 'storable') {
250 eval {$content = $self->_DeserializeContent($content); };
252 $RT::Logger->error("Deserialization of content for attribute ".$self->Id. " failed. Attribute was: ".$content);
260 sub _SerializeContent {
263 return( encode_base64(nfreeze($content)));
271 # Call __Value to avoid ACL check.
272 if ($self->__Value('ContentType') eq 'storable') {
273 # We eval the serialization because it will lose on a coderef.
274 eval {$content = $self->_SerializeContent($content); };
276 $RT::Logger->error("For some reason, content couldn't be frozen");
280 return ($self->SUPER::SetContent($content));
285 Returns the subvalue for $key.
289 my $user = $RT::SystemUser;
290 my ($id, $msg) = $user->AddAttribute(Name => 'SavedSearch', Content => { Query => 'Foo'} );
292 my $attr = RT::Attribute->new($RT::SystemUser);
294 ok($attr->Name eq 'SavedSearch');
295 $attr->SetSubValues( Format => 'baz');
297 my $format = $attr->SubValue('Format');
298 is ($format , 'baz');
300 $attr->SetSubValues( Format => 'bar');
301 $format = $attr->SubValue('Format');
302 is ($format , 'bar');
304 $attr->DeleteAllSubValues();
305 $format = $attr->SubValue('Format');
308 $attr->SetSubValues(Format => 'This is a format');
310 my $attr2 = RT::Attribute->new($RT::SystemUser);
312 is ($attr2->SubValue('Format'), 'This is a format');
322 my $values = $self->Content();
323 return undef unless ref($values);
324 return($values->{$key});
327 =head2 DeleteSubValue NAME
329 Deletes the subvalue with the key NAME
336 my %values = $self->Content();
337 delete $values{$key};
338 $self->SetContent(%values);
345 =head2 DeleteAllSubValues
347 Deletes all subvalues for this attribute
352 sub DeleteAllSubValues {
354 $self->SetContent({});
357 =head2 SetSubValues { }
359 Takes a hash of keys and values and stores them in the content of this attribute.
361 Each key B<replaces> the existing key with the same name
363 Returns a tuple of (status, message)
371 my $values = ($self->Content() || {} );
372 foreach my $key (keys %args) {
373 $values->{$key} = $args{$key};
376 $self->SetContent($values);
383 my $object_type = $self->__Value('ObjectType');
385 eval { $object = $object_type->new($self->CurrentUser) };
386 unless(UNIVERSAL::isa($object, $object_type)) {
387 $RT::Logger->error("Attribute ".$self->Id." has a bogus object type - $object_type (".$@.")");
390 $object->Load($self->__Value('ObjectId'));
399 unless ($self->CurrentUserHasRight('delete')) {
400 return (0,$self->loc('Permission Denied'));
402 return($self->SUPER::Delete(@_));
408 unless ($self->CurrentUserHasRight('display')) {
409 return (0,$self->loc('Permission Denied'));
412 return($self->SUPER::_Value(@_));
420 unless ($self->CurrentUserHasRight('modify')) {
422 return (0,$self->loc('Permission Denied'));
424 return($self->SUPER::_Set(@_));
429 =head2 CurrentUserHasRight
431 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.
435 sub CurrentUserHasRight {
439 # object_right is the right that the user has to have on the object for them to have $right on this attribute
440 my $object_right = $self->LookupObjectRight(
442 ObjectId => $self->__Value('ObjectId'),
443 ObjectType => $self->__Value('ObjectType'),
444 Name => $self->__Value('Name')
447 return (1) if ($object_right eq 'allow');
448 return (0) if ($object_right eq 'deny');
449 return(1) if ($self->CurrentUser->HasRight( Object => $self->Object, Right => $object_right));
457 We should be deserializing the content on load and then enver again, rather than at every access