Skip to content

Commit

Permalink
Update auth procedures to use newly introduced transaction handles.
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed Jul 31, 2016
1 parent df0a636 commit 8ac5cfd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
Expand Up @@ -19,6 +19,8 @@
*/
package org.neo4j.kernel.api;

import java.util.Optional;

import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.security.AccessMode;
import org.neo4j.kernel.impl.api.Kernel;
Expand Down Expand Up @@ -64,4 +66,21 @@ public interface KernelTransactionHandle
* @param reason the reason for termination.
*/
void markForTermination( Status reason );

/**
* Access mode of underlying transaction that transaction has when handle was created.
*
* @return underlying transaction access mode
*/
AccessMode mode();

/**
* Transaction termination reason that transaction had when handle was created.
*
* @return transaction termination reason.
*/
Optional<Status> terminationReason();

// TODO: remove
boolean isSameTransaction( KernelTransaction tx );
}
Expand Up @@ -144,7 +144,7 @@ TransactionWriteState upgradeToSchemaWrites() throws InvalidTransactionTypeKerne
private final KernelStatement currentStatement;
private final StorageStatement storageStatement;
private final List<CloseListener> closeListeners = new ArrayList<>( 2 );
private AccessMode accessMode;
private volatile AccessMode accessMode;
private volatile Locks.Client locks;
private boolean beforeHookInvoked;
private volatile boolean closing, closed;
Expand Down
Expand Up @@ -19,8 +19,12 @@
*/
package org.neo4j.kernel.impl.api;

import java.util.Optional;

import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.KernelTransactionHandle;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.security.AccessMode;

/**
* A {@link KernelTransactionHandle} that wraps the given {@link KernelTransactionImplementation}.
Expand All @@ -35,13 +39,17 @@ class KernelTransactionImplementationHandle implements KernelTransactionHandle
private final long lastTransactionTimestampWhenStarted;
private final long localStartTime;
private final KernelTransactionImplementation tx;
private final AccessMode mode;
private final Status terminationReason;

KernelTransactionImplementationHandle( KernelTransactionImplementation tx )
{
this.txReuseCount = tx.getReuseCount();
this.lastTransactionIdWhenStarted = tx.lastTransactionIdWhenStarted();
this.lastTransactionTimestampWhenStarted = tx.lastTransactionTimestampWhenStarted();
this.localStartTime = tx.localStartTime();
this.mode = tx.mode();
this.terminationReason = tx.getReasonIfTerminated();
this.tx = tx;
}

Expand Down Expand Up @@ -75,6 +83,24 @@ public void markForTermination( Status reason )
tx.markForTermination( txReuseCount, reason );
}

@Override
public AccessMode mode()
{
return mode;
}

@Override
public Optional<Status> terminationReason()
{
return Optional.ofNullable( terminationReason );
}

@Override
public boolean isSameTransaction( KernelTransaction tx )
{
return this.tx == tx;
}

@Override
public boolean equals( Object o )
{
Expand Down
Expand Up @@ -20,10 +20,12 @@
package org.neo4j.kernel.impl.api;

import java.util.Objects;
import java.util.Optional;

import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.KernelTransactionHandle;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.security.AccessMode;

/**
* A test implementation of {@link KernelTransactionHandle} that simply wraps a given {@link KernelTransaction}.
Expand Down Expand Up @@ -67,6 +69,24 @@ public void markForTermination( Status reason )
tx.markForTermination( reason );
}

@Override
public AccessMode mode()
{
return tx.mode();
}

@Override
public Optional<Status> terminationReason()
{
return Optional.ofNullable( tx.getReasonIfTerminated() );
}

@Override
public boolean isSameTransaction( KernelTransaction tx )
{
return false;
}

@Override
public boolean equals( Object o )
{
Expand Down
Expand Up @@ -29,7 +29,7 @@

import org.neo4j.graphdb.security.AuthorizationViolationException;
import org.neo4j.kernel.api.KernelTransaction;

import org.neo4j.kernel.api.KernelTransactionHandle;
import org.neo4j.kernel.api.bolt.HaltableUserSession;
import org.neo4j.kernel.api.bolt.SessionTracker;
import org.neo4j.kernel.api.exceptions.Status;
Expand Down Expand Up @@ -206,7 +206,7 @@ public Stream<TransactionResult> listTransactions()

return countTransactionByUsername(
getActiveTransactions().stream()
.filter( tx -> tx.getReasonIfTerminated() == null )
.filter( tx -> !tx.terminationReason().isPresent() )
.map( tx -> tx.mode().name() )
);
}
Expand Down Expand Up @@ -253,9 +253,9 @@ public Stream<SessionResult> terminateSessionsForUser( @Name( "username" ) Strin
private Stream<TransactionTerminationResult> terminateTransactionsForValidUser( String username )
{
Long killCount = 0L;
for ( KernelTransaction tx : getActiveTransactions() )
for ( KernelTransactionHandle tx : getActiveTransactions() )
{
if ( tx.mode().name().equals( username ) && tx != this.tx )
if ( tx.mode().name().equals( username ) && !tx.isSameTransaction( this.tx) )
{
tx.markForTermination( Status.Transaction.Terminated );
killCount += 1;
Expand All @@ -279,7 +279,7 @@ private Stream<SessionResult> terminateSessionsForValidUser( String username )
return Stream.of( new SessionResult( username, killCount ) );
}

private Set<KernelTransaction> getActiveTransactions()
private Set<KernelTransactionHandle> getActiveTransactions()
{
return graph.getDependencyResolver().resolveDependency( KernelTransactions.class ).activeTransactions();
}
Expand Down

0 comments on commit 8ac5cfd

Please sign in to comment.