[Rt-commit] r8504 - in rt/branches/3.7-EXPERIMENTAL-TUNIS: lib/RT

thayes at bestpractical.com thayes at bestpractical.com
Fri Aug 10 18:17:14 EDT 2007


Author: thayes
Date: Fri Aug 10 18:16:56 2007
New Revision: 8504

Modified:
   rt/branches/3.7-EXPERIMENTAL-TUNIS/   (props changed)
   rt/branches/3.7-EXPERIMENTAL-TUNIS/lib/RT/TicketLocking.pm
   rt/branches/3.7-EXPERIMENTAL-TUNIS/lib/RT/User_Overlay.pm

Log:
 r8710 at toth:  toth | 2007-08-10 18:14:34 -0400
 * Moved RemoveUserLocks and GetUserLocks to be methods of the User class
 * RT::Ticket::Unlock() now returns a tuple consisting of the duration of the lock (or undef if the ticket was not unlocked) and a message regarding the success or failure of the Unlock operation
 


Modified: rt/branches/3.7-EXPERIMENTAL-TUNIS/lib/RT/TicketLocking.pm
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL-TUNIS/lib/RT/TicketLocking.pm	(original)
+++ rt/branches/3.7-EXPERIMENTAL-TUNIS/lib/RT/TicketLocking.pm	Fri Aug 10 18:16:56 2007
@@ -48,7 +48,7 @@
 
 package RT::Ticket;
 
-my @Types = qw(Auto Take Hard);
+my @LockTypes = qw(Auto Take Hard);
 
 sub Locked {
     my $ticket = shift;
@@ -56,9 +56,11 @@
     if($lock) {
         my $duration = time() - $lock->Content->{'Timestamp'};
         my $expiry = RT->Config->Get('LockExpiry');
-        unless($duration < $expiry) {
-            $ticket->DeleteAttribute('RT_Lock');
-            undef $lock;
+        if($expiry) {
+            unless($duration < $expiry) {
+                $ticket->DeleteAttribute('RT_Lock');
+                undef $lock;
+            }
         }
     }
     return $lock;
@@ -73,22 +75,22 @@
         my $LockType = $lock->Content->{'Type'};
         my $priority;
         my $LockPriority;
-        for(my $i = 0; $i < scalar @Types; $i++) {
-            $priority = $i if (lc $Types[$i]) eq (lc $type);
-            $LockPriority = $i if (lc $Types[$i]) eq (lc $LockType);
+        for(my $i = 0; $i < scalar @LockTypes; $i++) {
+            $priority = $i if (lc $LockTypes[$i]) eq (lc $type);
+            $LockPriority = $i if (lc $LockTypes[$i]) eq (lc $LockType);
         }
         return undef if $priority <= $LockPriority;
-    } else {
-        $ticket->Unlock($type);    #Remove any existing locks (because this one has greater priority)
-        $ticket->SetAttribute(
-            Name    => 'RT_Lock',
-            Content => {
-                User      => $ticket->CurrentUser->id,
-                Timestamp => time(),
-                Type => $type
-            }
-        );
     }
+    $ticket->Unlock($type);    #Remove any existing locks (because this one has greater priority)
+    $ticket->SetAttribute(
+        Name    => 'RT_Lock',
+        Content => {
+            User      => $ticket->CurrentUser->id,
+            Timestamp => time(),
+            Type => $type,
+            Ticket => $ticket->id
+        }
+    );
 }
 
 
@@ -97,40 +99,26 @@
     my $type = shift || 'Auto';
 
     my $lock = $ticket->RT::Ticket::Locked();
-    return undef unless $lock;
-    return undef unless $lock->Content->{User} ==  $ticket->CurrentUser->id;
+    return (undef, "This ticket was not locked.") unless $lock;
+    return (undef, "You cannot unlock a ticket locked by another user.") unless $lock->Content->{User} ==  $ticket->CurrentUser->id;
     
     my $LockType = $lock->Content->{'Type'};
     my $priority;
     my $LockPriority;
-    for(my $i = 0; $i < scalar @Types; $i++) {
-        $priority = $i if (lc $Types[$i]) eq (lc $type);
-        $LockPriority = $i if (lc $Types[$i]) eq (lc $LockType);
+    for(my $i = 0; $i < scalar @LockTypes; $i++) {
+        $priority = $i if (lc $LockTypes[$i]) eq (lc $type);
+        $LockPriority = $i if (lc $LockTypes[$i]) eq (lc $LockType);
     }
-    return undef if $priority < $LockPriority;
+    return (undef, "There is a lock with a higher priority on this ticket.") if $priority < $LockPriority;
+    my $duration = time() - $lock->Content->{'Timestamp'};
     $ticket->DeleteAttribute('RT_Lock');
-    return $lock;
+    return ($duration, "You have unlocked this ticket.");
 }
 
 
 sub BreakLock {
     my $ticket = shift;
-    my $lock = $ticket->RT::Ticket::Locked();
-     return undef unless $lock;
-    $ticket->DeleteAttribute('RT_Lock');
+    return $ticket->DeleteAttribute('RT_Lock');
 }
 
 
-
-sub RemoveUserLocks {
-    my $user = shift;
-
-    return undef unless $user;
-    
-    my $attribs = RT::Attributes->new($user);
-    $attribs->Limit(FIELD => 'Creator', OPERATOR=> '=', VALUE => $user->id(), ENTRYAGGREGATOR => 'AND');
-    my @attributes = $attribs->Named('RT_Lock');
-    foreach my $lock (@attributes) {
-        $lock->Delete();
-    }
-}

Modified: rt/branches/3.7-EXPERIMENTAL-TUNIS/lib/RT/User_Overlay.pm
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL-TUNIS/lib/RT/User_Overlay.pm	(original)
+++ rt/branches/3.7-EXPERIMENTAL-TUNIS/lib/RT/User_Overlay.pm	Fri Aug 10 18:16:56 2007
@@ -264,7 +264,7 @@
 
 
     if ( $record_transaction ) {
-	$self->_NewTransaction( Type => "Create" );
+    $self->_NewTransaction( Type => "Create" );
     }
 
     $RT::Handle->Commit;
@@ -519,14 +519,14 @@
     if ( UNIVERSAL::isa( $email => 'Mail::Address' ) ) {
         ($email, $name) = ($email->address, $email->phrase);
     } else {
-	    ($email, $name) = RT::Interface::Email::ParseAddressFromHeader( $email );
+        ($email, $name) = RT::Interface::Email::ParseAddressFromHeader( $email );
     }
 
     $self->LoadByEmail( $email );
-	$self->Load( $email ) unless $self->Id;
+    $self->Load( $email ) unless $self->Id;
     $message = $self->loc('User loaded');
 
-	unless( $self->Id ) {
+    unless( $self->Id ) {
         my $val;
         ($val, $message) = $self->Create(
             Name         => $email,
@@ -710,12 +710,12 @@
     }
 
     my $ret = RT::Interface::Email::SendEmailUsingTemplate(
-		To        => $self->EmailAddress,
-		Template  => 'PasswordChange',
-		Arguments => {
-		    NewPassword => $pass,
-		},
-	    );
+        To        => $self->EmailAddress,
+        Template  => 'PasswordChange',
+        Arguments => {
+            NewPassword => $pass,
+        },
+        );
 
     if ($ret) {
         return ( 1, $self->loc('New password notification sent') );
@@ -1198,7 +1198,7 @@
     my $groups = RT::Groups->new($self->CurrentUser);
     $groups->LimitToUserDefinedGroups;
     $groups->WithMember(PrincipalId => $self->Id, 
-			Recursively => 1);
+            Recursively => 1);
     return $groups;
 }
 
@@ -1452,45 +1452,45 @@
 sub _CleanupInvalidDelegations {
     my $self = shift;
     my %args = ( InsideTransaction => undef,
-		  @_ );
+          @_ );
 
     unless ( $self->Id ) {
-	$RT::Logger->warning("User not loaded.");
-	return (undef);
+    $RT::Logger->warning("User not loaded.");
+    return (undef);
     }
 
     my $in_trans = $args{InsideTransaction};
 
     return(1) if ($self->HasRight(Right => 'DelegateRights',
-				  Object => $RT::System));
+                  Object => $RT::System));
 
     # Look up all delegation rights currently posessed by this user.
     my $deleg_acl = RT::ACL->new($RT::SystemUser);
     $deleg_acl->LimitToPrincipal(Type => 'User',
-				 Id => $self->PrincipalId,
-				 IncludeGroupMembership => 1);
+                 Id => $self->PrincipalId,
+                 IncludeGroupMembership => 1);
     $deleg_acl->Limit( FIELD => 'RightName',
-		       OPERATOR => '=',
-		       VALUE => 'DelegateRights' );
+               OPERATOR => '=',
+               VALUE => 'DelegateRights' );
     my @allowed_deleg_objects = map {$_->Object()}
-	@{$deleg_acl->ItemsArrayRef()};
+    @{$deleg_acl->ItemsArrayRef()};
 
     # Look up all rights delegated by this principal which are
     # inconsistent with the allowed delegation objects.
     my $acl_to_del = RT::ACL->new($RT::SystemUser);
     $acl_to_del->DelegatedBy(Id => $self->Id);
     foreach (@allowed_deleg_objects) {
-	$acl_to_del->LimitNotObject($_);
+    $acl_to_del->LimitNotObject($_);
     }
 
     # Delete all disallowed delegations
     while ( my $ace = $acl_to_del->Next() ) {
-	my $ret = $ace->_Delete(InsideTransaction => 1);
-	unless ($ret) {
-	    $RT::Handle->Rollback() unless $in_trans;
-	    $RT::Logger->warning("Couldn't delete delegated ACL entry ".$ace->Id);
-	    return (undef);
-	}
+    my $ret = $ace->_Delete(InsideTransaction => 1);
+    unless ($ret) {
+        $RT::Handle->Rollback() unless $in_trans;
+        $RT::Logger->warning("Couldn't delete delegated ACL entry ".$ace->Id);
+        return (undef);
+    }
     }
 
     $RT::Handle->Commit() unless $in_trans;
@@ -1507,8 +1507,8 @@
     my %args = (
         Field => undef,
         Value => undef,
-	TransactionType   => 'Set',
-	RecordTransaction => 1,
+    TransactionType   => 'Set',
+    RecordTransaction => 1,
         @_
     );
 
@@ -1525,7 +1525,7 @@
     my $Old = $self->SUPER::_Value("$args{'Field'}");
     
     my ($ret, $msg) = $self->SUPER::_Set( Field => $args{'Field'},
-					  Value => $args{'Value'} );
+                      Value => $args{'Value'} );
     
     #If we can't actually set the field to the value, don't record
     # a transaction. instead, get out of here.
@@ -1613,13 +1613,48 @@
 
 sub BasicColumns {
     (
-	[ Name => 'User Id' ],
-	[ EmailAddress => 'Email' ],
-	[ RealName => 'Name' ],
-	[ Organization => 'Organization' ],
+    [ Name => 'User Id' ],
+    [ EmailAddress => 'Email' ],
+    [ RealName => 'Name' ],
+    [ Organization => 'Organization' ],
     );
 }
 
+
+
+sub GetLocks {
+    my $self = shift;
+    
+    my $attribs = RT::Attributes->new($self);
+    $attribs->Limit(FIELD => 'Creator', OPERATOR=> '=', VALUE => $self->id(), ENTRYAGGREGATOR => 'AND');
+    
+    my $expiry = RT->Config->Get('LockExpiry');
+    return $attribs->Named('RT_Lock') unless $expiry;
+    my @locks;
+    
+    foreach my $lock ($attribs->Named('RT_Lock')) {
+        my $duration = time() - $lock->Content->{'Timestamp'};
+        if($duration < $expiry) {
+            push @locks, $lock;
+        }
+        else {
+            $lock->Delete();
+        }
+    }
+    return @locks;
+}
+
+sub RemoveLocks {
+    my $self = shift;
+    
+    my $attribs = RT::Attributes->new($self);
+    $attribs->Limit(FIELD => 'Creator', OPERATOR=> '=', VALUE => $self->id(), ENTRYAGGREGATOR => 'AND');
+    my @attributes = $attribs->Named('RT_Lock');
+    foreach my $lock (@attributes) {
+        $lock->Delete();
+    }
+}
+
 1;
 
 


More information about the Rt-commit mailing list