[Rt-devel] PATCH Inherit CF rights from object. Comments sought.

Todd Chapman todd at chaka.net
Wed Jun 8 14:55:36 EDT 2005


We have losts of custom fields. I got tired of setting and
updating rights on these CFs, especially when the rights were
always the same as the ticket (or asset).

I came up with the following patch which allow you to set
a CF to inherit See/Show and Modify from the object it
is a CF of.

Looking for comments, such as:

1 This is a good idea and implementation.
2 This is a good idea but rotten implementation.
3 This is a stupid idea, what we need is a better way to
  modify the rights on a bunch of CFs in bulk.

I myself am leaning toward 3, but I implemented 1 just for fun.

The patch is attached.

-Todd

-------------- next part --------------
Index: html/Admin/CustomFields/Modify.html
===================================================================
--- html/Admin/CustomFields/Modify.html	(revision 3087)
+++ html/Admin/CustomFields/Modify.html	(working copy)
@@ -79,6 +79,15 @@
 <tr>
 <td class="label">&nbsp;</td>
 <td>
+<INPUT TYPE=CHECKBOX NAME="InheritSee" VALUE="1" <%$InheritSeeChecked%>> <&|/l&>Inherit the ability to see this custom field from the object it is assigned to</&><br>
+<INPUT TYPE=HIDDEN NAME="InheritSee-Magic" VALUE="1">
+<INPUT TYPE=CHECKBOX NAME="InheritModify" VALUE="1" <%$InheritModifyChecked%>> <&|/l&>Inherit the ability to modify this custom field from the object it is assigned to</&>
+<INPUT TYPE=HIDDEN NAME="InheritModify-Magic" VALUE="1">
+</td>
+</tr>
+<tr>
+<td class="label">&nbsp;</td>
+<td>
 <INPUT TYPE=HIDDEN NAME="SetEnabled" VALUE="1">
 <INPUT TYPE=CHECKBOX NAME="Enabled" VALUE="1" <%$EnabledChecked%>> <&|/l&>Enabled (Unchecking this box disables this custom field)</&>
 </td>
@@ -102,7 +111,7 @@
 
 
 my $CustomFieldObj = RT::CustomField->new( $session{'CurrentUser'} );
-my ( $title, @results, $EnabledChecked, $Disabled);
+my ( $title, @results, $EnabledChecked, $Disabled, $InheritSeeChecked, $InheritModifyChecked);
 $EnabledChecked = "CHECKED";
 
 if ( !$id ) {
@@ -151,7 +160,23 @@
     $id = $CustomFieldObj->id;
 }
 
+if ($ARGS{'InheritSee-Magic'}) {
+if ($InheritSee == 1) {
+    $CustomFieldObj->InheritSee(1);
+}
+else {
+    $CustomFieldObj->InheritSee(0);
+}
 
+if ($InheritModify == 1) {
+    $CustomFieldObj->InheritModify(1);
+}
+else {
+    $CustomFieldObj->InheritModify(0);
+}
+}
+$InheritSeeChecked = $CustomFieldObj->InheritSee() ? "CHECKED" : "";
+$InheritModifyChecked = $CustomFieldObj->InheritModify() ? "CHECKED" : "";
 
 
 my $paramtag = "CustomField-".$CustomFieldObj->Id."-Value-";
@@ -209,4 +234,6 @@
 $Name => undef
 $SetEnabled => undef
 $Enabled => undef
+$InheritSee => undef
+$InheritModify => undef
 </%ARGS>
Index: html/Elements/ShowCustomFields
===================================================================
--- html/Elements/ShowCustomFields	(revision 3087)
+++ html/Elements/ShowCustomFields	(working copy)
@@ -45,7 +45,7 @@
 %# END BPS TAGGED BLOCK }}}
 <table>
 % my @entry_fields;
-% while (my $CustomField = $CustomFields->Next()) {
+% while (my $CustomField = $CustomFields->Next($Object)) {
 % my $Values = $Object->CustomFieldValues($CustomField->Id);
   <tr>
     <td class="label"><%$CustomField->Name%>:</td>
Index: lib/RT/CustomField_Overlay.pm
===================================================================
--- lib/RT/CustomField_Overlay.pm	(revision 3087)
+++ lib/RT/CustomField_Overlay.pm	(working copy)
@@ -668,11 +668,30 @@
 sub CurrentUserHasRight {
     my $self  = shift;
     my $right = shift;
+    my $inheritObj = shift;
 
-    return $self->CurrentUser->HasRight(
+    return 1 if  $self->CurrentUser->HasRight(
 	Object => $self,
 	Right  => $right,
     );
+
+    if (ref $inheritObj) { $self->{_CacheInheritObject} = $inheritObj; }
+    if ( $right eq 'ModifyCustomField' and $self->{_CacheInheritObject} ) {
+        if ( $self->InheritModify() ) {
+            if ( $self->{_CacheInheritObject} =~ /^RTx?.*::(.+)$/ ) {
+                return $self->{_CacheInheritObject}->CurrentUserHasRight("Modify$1");
+            }
+        }
+    }
+    elsif ( $right eq 'SeeCustomField' and $self->{_CacheInheritObject} ) {
+        if ( $self->InheritSee() ) {
+            if ( $self->{_CacheInheritObject} =~ /^RTx?.*::(.+)$/ ) {
+                return $self->{_CacheInheritObject}->CurrentUserHasRight("Show$1");
+            }
+        }
+    }
+
+    return 0;
 }
 
 # }}}
@@ -706,7 +725,7 @@
     my $field = shift;
 
     # we need to do the rights check
-    unless ( $self->id && $self->CurrentUserHasRight( 'SeeCustomField') ) {
+    unless ( $self->id && $self->CurrentUserHasRight( 'SeeCustomField', $self->{_CacheInheritObject}) ) {
 	    return (undef);
     }
     return ( $self->__Value($field) );
@@ -935,7 +954,7 @@
     );
     my $obj = $args{'Object'} or return;
 
-    unless ( $self->CurrentUserHasRight('ModifyCustomField') ) {
+    unless ( $self->CurrentUserHasRight( 'ModifyCustomField', $args{'Object'} ) ) {
         return ( 0, $self->loc('Permission Denied') );
     }
 
@@ -1007,7 +1026,7 @@
 		     @_ );
 
 
-    unless ($self->CurrentUserHasRight('ModifyCustomField')) {
+    unless ($self->CurrentUserHasRight( 'ModifyCustomField', $args{'Object'} )) {
         return (0, $self->loc('Permission Denied'));
     }
 
@@ -1050,7 +1069,7 @@
     my $object = shift;
 
 	my $values = new RT::ObjectCustomFieldValues($self->CurrentUser);
-	unless ($self->CurrentUserHasRight('SeeCustomField')) {
+	unless ($self->CurrentUserHasRight('SeeCustomField', $object)) {
         # Return an empty object if they have no rights to see
         return ($values);
     }
@@ -1090,4 +1109,58 @@
 
 # }}}
 
+sub InheritSee {
+
+    my $self = shift;
+    return $self->_Inherit("InheritSee", @_);
+
+}
+
+sub InheritModify {
+
+    my $self = shift;
+    return $self->_Inherit("InheritModify", @_);
+
+}
+
+=head2 _Inherit
+
+Set special attribues for this CF
+
+=begin testing
+
+ok(my $cf = RT::CustomField->new($RT::SystemUser));
+$cf->Load(1);
+ok($cf->Id == 1);
+ok(! defined $cf->_Inherit("InheritSee"));
+ok($cf->_Inherit("InheritSee", 1));
+ok($cf->_Inherit("InheritSee") == 1);
+$cf->_Inherit("InheritSee", 0);
+ok(! defined $cf->_Inherit("InheritSee"));
+
+=end testing
+
+=cut
+
+sub _Inherit {
+
+    my $self = shift;
+    my $right = shift;
+
+    my $content = $self->FirstAttribute($right);
+
+    unless (@_) { return $content; }
+
+    if ($_[0]) {
+        #$self->DeleteAttribute($right);
+        #return $self->AddAttribute(Name => $right, Content => 1, Description => "Inherit see/modify from object");
+        return $self->SetAttribute(Name => $right, Content => 1, Description => "Inherit see/modify from object");
+    }
+    else {
+        return $self->DeleteAttribute($right);
+    }
+    
+
+}
+
 1;
Index: lib/RT/Transaction_Overlay.pm
===================================================================
--- lib/RT/Transaction_Overlay.pm	(revision 3087)
+++ lib/RT/Transaction_Overlay.pm	(working copy)
@@ -882,7 +882,7 @@
     elsif ( ( $self->__Value('Type') eq 'CustomField' ) && $self->__Value('Field') ) {
         my $cf = RT::CustomField->new( $self->CurrentUser );
         $cf->Load( $self->__Value('Field') );
-        return (undef) unless ( $cf->CurrentUserHasRight('SeeCustomField') );
+        return (undef) unless ( $cf->CurrentUserHasRight('SeeCustomField'), $self->Object );
     }
 
 
Index: lib/RT/CustomFields_Overlay.pm
===================================================================
--- lib/RT/CustomFields_Overlay.pm	(revision 3087)
+++ lib/RT/CustomFields_Overlay.pm	(working copy)
@@ -184,18 +184,19 @@
   
 sub Next {
     my $self = shift;
+    my $obj = shift;
     
     
     my $CF = $self->SUPER::Next();
     if ((defined($CF)) and (ref($CF))) {
 
-	if ($CF->CurrentUserHasRight('SeeCustomField')) {
+	if ($CF->CurrentUserHasRight('SeeCustomField', $obj)) {
 	    return($CF);
 	}
 	
 	#If the user doesn't have the right to show this queue
 	else {	
-	    return($self->Next());
+	    return($self->Next($obj));
 	}
     }
     #if there never was any queue


More information about the Rt-devel mailing list