diff --git a/src/main/java/com/google/gerrit/server/CurrentUser.java b/src/main/java/com/google/gerrit/server/CurrentUser.java index 1c11ba649d..a923497722 100644 --- a/src/main/java/com/google/gerrit/server/CurrentUser.java +++ b/src/main/java/com/google/gerrit/server/CurrentUser.java @@ -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. *

- * 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 */ diff --git a/src/main/java/com/google/gerrit/server/HttpCurrentUserProvider.java b/src/main/java/com/google/gerrit/server/HttpCurrentUserProvider.java index ec026b507a..69ba3f180d 100644 --- a/src/main/java/com/google/gerrit/server/HttpCurrentUserProvider.java +++ b/src/main/java/com/google/gerrit/server/HttpCurrentUserProvider.java @@ -22,15 +22,17 @@ import com.google.inject.servlet.RequestScoped; @RequestScoped class HttpCurrentUserProvider implements Provider { 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; } } diff --git a/src/main/java/com/google/gerrit/server/IdentifiedUser.java b/src/main/java/com/google/gerrit/server/IdentifiedUser.java index 67723c4784..b75022da15 100644 --- a/src/main/java/com/google/gerrit/server/IdentifiedUser.java +++ b/src/main/java/com/google/gerrit/server/IdentifiedUser.java @@ -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() + "]"; } -} \ No newline at end of file +} diff --git a/src/main/java/com/google/gerrit/server/config/GerritServerModule.java b/src/main/java/com/google/gerrit/server/config/GerritServerModule.java index edf0cd2b15..19fae281e8 100644 --- a/src/main/java/com/google/gerrit/server/config/GerritServerModule.java +++ b/src/main/java/com/google/gerrit/server/config/GerritServerModule.java @@ -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); diff --git a/src/main/java/com/google/gerrit/server/ssh/SshCurrentUserProvider.java b/src/main/java/com/google/gerrit/server/ssh/SshCurrentUserProvider.java new file mode 100644 index 0000000000..efcde68422 --- /dev/null +++ b/src/main/java/com/google/gerrit/server/ssh/SshCurrentUserProvider.java @@ -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 { + 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); + } +} diff --git a/src/main/java/com/google/gerrit/server/ssh/SshDaemonModule.java b/src/main/java/com/google/gerrit/server/ssh/SshDaemonModule.java index ef2c5d7199..0330ecded8 100644 --- a/src/main/java/com/google/gerrit/server/ssh/SshDaemonModule.java +++ b/src/main/java/com/google/gerrit/server/ssh/SshDaemonModule.java @@ -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() { - @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); } }