One-off request context on behalf of a user
Enable the invocation of Gerrit API on behalf of another user.
Example use with a ChangeApi invocation:
  try (ManualRequestContext ctx = oneOffRequestContext.openAs(userId)) {
    ChangeApi cApi = gApi.changes().id(change.getId().get());
    ...
  }
Change-Id: I48c292b24613bc32406bf1673a11430267a21d67
			
			
This commit is contained in:
		@@ -14,7 +14,9 @@
 | 
			
		||||
 | 
			
		||||
package com.google.gerrit.server.util;
 | 
			
		||||
 | 
			
		||||
import com.google.gerrit.reviewdb.client.Account;
 | 
			
		||||
import com.google.gerrit.reviewdb.server.ReviewDb;
 | 
			
		||||
import com.google.gerrit.server.IdentifiedUser;
 | 
			
		||||
import com.google.gerrit.server.InternalUser;
 | 
			
		||||
import com.google.gwtorm.server.OrmException;
 | 
			
		||||
import com.google.gwtorm.server.SchemaFactory;
 | 
			
		||||
@@ -27,25 +29,34 @@ import com.google.inject.Singleton;
 | 
			
		||||
 * Each call to {@link #open()} opens a new {@link ReviewDb}, so this class
 | 
			
		||||
 * should only be used in a bounded try/finally block.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * The user in the request context is {@link InternalUser}.
 | 
			
		||||
 * The user in the request context is {@link InternalUser} or the
 | 
			
		||||
 * {@link IdentifiedUser} associated to the userId passed as parameter.
 | 
			
		||||
 */
 | 
			
		||||
@Singleton
 | 
			
		||||
public class OneOffRequestContext {
 | 
			
		||||
  private final InternalUser.Factory userFactory;
 | 
			
		||||
  private final SchemaFactory<ReviewDb> schemaFactory;
 | 
			
		||||
  private final ThreadLocalRequestContext requestContext;
 | 
			
		||||
  private final IdentifiedUser.GenericFactory identifiedUserFactory;
 | 
			
		||||
 | 
			
		||||
  @Inject
 | 
			
		||||
  OneOffRequestContext(InternalUser.Factory userFactory,
 | 
			
		||||
      SchemaFactory<ReviewDb> schemaFactory,
 | 
			
		||||
      ThreadLocalRequestContext requestContext) {
 | 
			
		||||
      ThreadLocalRequestContext requestContext,
 | 
			
		||||
      IdentifiedUser.GenericFactory identifiedUserFactory) {
 | 
			
		||||
    this.userFactory = userFactory;
 | 
			
		||||
    this.schemaFactory = schemaFactory;
 | 
			
		||||
    this.requestContext = requestContext;
 | 
			
		||||
    this.identifiedUserFactory = identifiedUserFactory;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public ManualRequestContext open() throws OrmException {
 | 
			
		||||
    return new ManualRequestContext(userFactory.create(),
 | 
			
		||||
        schemaFactory, requestContext);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public ManualRequestContext openAs(Account.Id userId) throws OrmException {
 | 
			
		||||
    return new ManualRequestContext(identifiedUserFactory.create(userId),
 | 
			
		||||
        schemaFactory, requestContext);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user