Refactor CurrentUser to always be request scoped
This permits us to cache the user's effective groups and other account related information within the CurrentUser instance. By caching at the request scope we ensure we reload any effective group membership changes on each new SSH command, even if they are coming in over an existing connection. Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
@@ -15,14 +15,11 @@
|
||||
package com.google.gerrit.server;
|
||||
|
||||
import com.google.inject.servlet.RequestScoped;
|
||||
import com.google.inject.servlet.SessionScoped;
|
||||
|
||||
/**
|
||||
* Information about the currently logged in user.
|
||||
* <p>
|
||||
* This is a {@link RequestScoped} or {@link SessionScoped} property managed by
|
||||
* Guice, depending on the environment. Application code should just assume it
|
||||
* is {@code RequestScoped}.
|
||||
* This is a {@link RequestScoped} property managed by Guice.
|
||||
*
|
||||
* @see IdentifiedUser
|
||||
*/
|
||||
|
@@ -22,15 +22,17 @@ import com.google.inject.servlet.RequestScoped;
|
||||
@RequestScoped
|
||||
class HttpCurrentUserProvider implements Provider<CurrentUser> {
|
||||
private final GerritCall call;
|
||||
private final IdentifiedUser.Factory factory;
|
||||
|
||||
@Inject
|
||||
HttpCurrentUserProvider(final GerritCall c) {
|
||||
HttpCurrentUserProvider(final GerritCall c, final IdentifiedUser.Factory f) {
|
||||
call = c;
|
||||
factory = f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CurrentUser get() {
|
||||
final Account.Id id = call.getAccountId();
|
||||
return id != null ? new IdentifiedUser(id) : CurrentUser.ANONYMOUS;
|
||||
return id != null ? factory.create(id) : CurrentUser.ANONYMOUS;
|
||||
}
|
||||
}
|
||||
|
@@ -1,14 +1,37 @@
|
||||
// Copyright 2009 Google Inc. All Rights Reserved.
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server;
|
||||
|
||||
import com.google.gerrit.client.reviewdb.Account;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
import com.google.inject.servlet.RequestScoped;
|
||||
|
||||
/** An authenticated user. */
|
||||
@RequestScoped
|
||||
public class IdentifiedUser extends CurrentUser {
|
||||
public static final class Factory {
|
||||
public IdentifiedUser create(final Account.Id id) {
|
||||
return new IdentifiedUser(id);
|
||||
}
|
||||
}
|
||||
|
||||
private final Account.Id accountId;
|
||||
|
||||
public IdentifiedUser(final Account.Id i) {
|
||||
@Inject
|
||||
IdentifiedUser(@Assisted final Account.Id i) {
|
||||
accountId = i;
|
||||
}
|
||||
|
||||
@@ -21,4 +44,4 @@ public class IdentifiedUser extends CurrentUser {
|
||||
public String toString() {
|
||||
return "CurrentUser[" + getAccountId() + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -28,6 +28,7 @@ import com.google.gerrit.server.ContactStore;
|
||||
import com.google.gerrit.server.EncryptedContactStoreProvider;
|
||||
import com.google.gerrit.server.FileTypeRegistry;
|
||||
import com.google.gerrit.server.GerritServer;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.MimeUtilFileTypeRegistry;
|
||||
import com.google.gerrit.server.mail.AbandonedSender;
|
||||
import com.google.gerrit.server.mail.AddReviewerSender;
|
||||
@@ -81,6 +82,7 @@ public class GerritServerModule extends FactoryModule {
|
||||
|
||||
bind(EmailSender.class).to(SmtpEmailSender.class).in(SINGLETON);
|
||||
bind(PatchSetInfoFactory.class);
|
||||
bind(IdentifiedUser.Factory.class);
|
||||
|
||||
factory(AbandonedSender.Factory.class);
|
||||
factory(AddReviewerSender.Factory.class);
|
||||
|
@@ -0,0 +1,41 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.ssh;
|
||||
|
||||
import com.google.gerrit.client.reviewdb.Account;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.ssh.SshScopes.Context;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.ProvisionException;
|
||||
|
||||
class SshCurrentUserProvider implements Provider<IdentifiedUser> {
|
||||
private final IdentifiedUser.Factory factory;
|
||||
|
||||
@Inject
|
||||
SshCurrentUserProvider(final IdentifiedUser.Factory f) {
|
||||
factory = f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentifiedUser get() {
|
||||
final Context ctx = SshScopes.getContext();
|
||||
final Account.Id id = ctx.session.getAttribute(SshUtil.CURRENT_ACCOUNT);
|
||||
if (id == null) {
|
||||
throw new ProvisionException("User not yet authenticated");
|
||||
}
|
||||
return factory.create(id);
|
||||
}
|
||||
}
|
@@ -16,15 +16,12 @@ package com.google.gerrit.server.ssh;
|
||||
|
||||
import static com.google.inject.Scopes.SINGLETON;
|
||||
|
||||
import com.google.gerrit.client.reviewdb.Account;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.RemotePeer;
|
||||
import com.google.gerrit.server.ssh.SshScopes.Context;
|
||||
import com.google.gerrit.server.ssh.commands.DefaultCommandModule;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.ProvisionException;
|
||||
import com.google.inject.servlet.RequestScoped;
|
||||
import com.google.inject.servlet.SessionScoped;
|
||||
|
||||
@@ -73,21 +70,11 @@ public class SshDaemonModule extends AbstractModule {
|
||||
.getAttribute(SshUtil.REMOTE_PEER);
|
||||
}
|
||||
}).in(SshScopes.SESSION);
|
||||
|
||||
bind(IdentifiedUser.class).toProvider(new Provider<IdentifiedUser>() {
|
||||
@Override
|
||||
public IdentifiedUser get() {
|
||||
final Context ctx = SshScopes.getContext();
|
||||
final Account.Id id = ctx.session.getAttribute(SshUtil.CURRENT_ACCOUNT);
|
||||
if (id == null) {
|
||||
throw new ProvisionException("User not yet authenticated");
|
||||
}
|
||||
return new IdentifiedUser(id);
|
||||
}
|
||||
}).in(SshScopes.SESSION);
|
||||
bind(CurrentUser.class).to(IdentifiedUser.class);
|
||||
}
|
||||
|
||||
private void configureRequestScope() {
|
||||
bind(IdentifiedUser.class).toProvider(SshCurrentUserProvider.class).in(
|
||||
SshScopes.REQUEST);
|
||||
bind(CurrentUser.class).to(IdentifiedUser.class);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user