Skip to content

Commit

Permalink
Adapt to Gerrit API 2.15
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaswolf committed Apr 30, 2018
1 parent 2b6a5dc commit d05c5f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 55 deletions.
4 changes: 2 additions & 2 deletions docu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ with full SSO through Gerrit.

* License: [Apache Public License 2.0](http://www.apache.org/licenses/LICENSE-2.0)
* [Home page](https://github.com/tomaswolf/gerrit-gitblit-plugin)
* Installed plugin version: <em id='gerrit-gitblit-current-version'>2.14.171.0-SNAPSHOT</em> &mdash; <a id='gerrit-gitblit-version-check' style='display:none;' href='#'>Check for updates</a>
* Installed plugin version: <em id='gerrit-gitblit-current-version'>2.15.171.0-SNAPSHOT</em> &mdash; <a id='gerrit-gitblit-version-check' style='display:none;' href='#'>Check for updates</a>

For a list of contributors, see at [GitHub](https://github.com/tomaswolf/gerrit-gitblit-plugin/graphs/contributors).

Expand Down Expand Up @@ -111,6 +111,6 @@ Report bugs or make feature requests at the [GitHub issue tracker](https://githu

<hr style="color: #C0C0C0; background-color: #C0C0C0; border-color: #C0C0C0; height: 2px;" />
<div style="float:right;">
<a href="https://github.com/tomaswolf/gerrit-gitblit-plugin" target="_blank">GitBlit plugin 2.14.171.0-SNAPSHOT</a>
<a href="https://github.com/tomaswolf/gerrit-gitblit-plugin" target="_blank">GitBlit plugin 2.15.171.0-SNAPSHOT</a>
</div>
<script type="text/javascript" src="version_check.js"></script>
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
<artifactId>gitblit-plugin</artifactId>
<description>GitBlit for Gerrit integrated as a plugin</description>
<name>Gerrit - GitBlit Plugin</name>
<version>2.14.171.0-SNAPSHOT</version><!-- Gerrit API version followed by collapsed GitBlit version, followed by plugin version -->
<version>2.15.171.0-SNAPSHOT</version><!-- Gerrit API version followed by collapsed GitBlit version, followed by plugin version -->
<licenses>
<license>
<name>Apache License 2.0</name>
Expand All @@ -29,7 +29,7 @@ limitations under the License.
</licenses>
<properties>
<Gerrit-ApiType>plugin</Gerrit-ApiType>
<Gerrit-ApiVersion>2.14</Gerrit-ApiVersion>
<Gerrit-ApiVersion>2.15</Gerrit-ApiVersion>
<GitBlit-Version>1.7.1</GitBlit-Version>
<Wicket-Version>1.4.22</Wicket-Version>
<Gerrit-ReloadMode>restart</Gerrit-ReloadMode>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.GetDiffPreferences;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
Expand All @@ -46,7 +46,7 @@ public class GerritGitBlitUserManager implements IUserManager {

private static final Logger log = LoggerFactory.getLogger(GerritGitBlitUserManager.class);

private final ProjectControl.GenericFactory projectControl;
private final PermissionBackend permissions;

private final Provider<CurrentUser> userProvider;

Expand All @@ -55,9 +55,9 @@ public class GerritGitBlitUserManager implements IUserManager {
private final GetDiffPreferences getDiffPreferences;

@Inject
public GerritGitBlitUserManager(final ProjectControl.GenericFactory projectControl, final GitBlitSettings settings,
public GerritGitBlitUserManager(final PermissionBackend permissions, final GitBlitSettings settings,
final DynamicItem<WebSession> gerritSession, final Provider<AnonymousUser> anonymousUser, final GetDiffPreferences getDiffPreferences) {
this.projectControl = projectControl;
this.permissions = permissions;
this.userProvider = new Provider<CurrentUser>() {
@Override
public CurrentUser get() {
Expand Down Expand Up @@ -88,22 +88,22 @@ public void setup(IRuntimeManager runtimeManager) {
@Override
public UserModel getUserModel(String username) {
if (username == null || GerritGitBlitUserModel.ANONYMOUS_USER.equals(username)) {
return new GerritGitBlitUserModel(projectControl, anonymousUser, getDiffPreferences);
return new GerritGitBlitUserModel(permissions, anonymousUser, getDiffPreferences);
}
return new GerritGitBlitUserModel(username, projectControl, userProvider, getDiffPreferences);
return new GerritGitBlitUserModel(username, permissions, userProvider, getDiffPreferences);
}

/**
* GitBlit assumes all users (or user accounts) have a username (account name or login name). Gerrit allows users (accounts) to not have a
* username, for instance if the account is created or logged in via Google OAuth. I such cases, we have to fake a username for GitBlit.
*
*
* @return a GitBlit {@link UserModel} for an unnamed Gerrit account.
*/
public UserModel getUnnamedGerritUser() {
CurrentUser user = userProvider.get();
if (!user.isIdentifiedUser()) {
log.warn("\"Logged-in\" user according to session is anonymous.");
return new GerritGitBlitUserModel(projectControl, anonymousUser, getDiffPreferences);
return new GerritGitBlitUserModel(permissions, anonymousUser, getDiffPreferences);
}
IdentifiedUser loggedInUser = (IdentifiedUser) user;
// We know that this user has no username. Synthesize one for GitBlit.
Expand All @@ -114,7 +114,7 @@ public UserModel getUnnamedGerritUser() {
fakeUserName = "external" + loggedInUser.getAccountId().toString();
}
}
return new GerritGitBlitUserModel(fakeUserName, projectControl, userProvider, getDiffPreferences);
return new GerritGitBlitUserModel(fakeUserName, permissions, userProvider, getDiffPreferences);
}

@Override
Expand Down Expand Up @@ -229,7 +229,7 @@ public boolean isInternalAccount(String username) {

/**
* Tries to ensure that GitBlit's "anonymous" user obeys the branch visibility defined by Gerrit.
*
*
* @return {@code true} if sucessful, {@code false} if unsuccessful
*/
private boolean fixAnonymousUser() {
Expand All @@ -255,7 +255,7 @@ private boolean fixAnonymousUser() {
modifiers.setAccessible(true);
int modifierFlags = anonymousField.getModifiers();
modifiers.set(anonymousField, modifierFlags & ~Modifier.FINAL); // Remove "final" from the "ANONYMOUS" field
anonymousField.set(null, new GerritGitBlitUserModel(projectControl, anonymousUser, getDiffPreferences));
anonymousField.set(null, new GerritGitBlitUserModel(permissions, anonymousUser, getDiffPreferences));
modifiers.set(anonymousField, modifierFlags); // Make the field "final" again.
modifiers.setAccessible(false); // Re-enable Java-language accessibility checks
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.GetDiffPreferences;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.project.RefControl;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.gerrit.server.permissions.PermissionBackend.ForProject;
import com.google.gerrit.server.permissions.PermissionBackend.ForRef;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.permissions.ProjectPermission;
import com.google.gerrit.server.permissions.RefPermission;
import com.google.inject.Provider;

/**
Expand All @@ -44,26 +47,26 @@ public class GerritGitBlitUserModel extends UserModel {

private static final long serialVersionUID = 1L;

private transient final ProjectControl.GenericFactory projectControlFactory;
private transient final Provider<? extends CurrentUser> userProvider;
private transient final GetDiffPreferences getDiffPreferences;
private transient final PermissionBackend permissions;

public GerritGitBlitUserModel(final ProjectControl.GenericFactory projectControlFactory, final Provider<? extends CurrentUser> userProvider,
public GerritGitBlitUserModel(final PermissionBackend permissions, final Provider<? extends CurrentUser> userProvider,
final GetDiffPreferences getDiffPreferences) {
super(ANONYMOUS_USER);
this.isAuthenticated = false;
this.projectControlFactory = projectControlFactory;
this.permissions = permissions;
this.userProvider = userProvider;
this.displayName = this.username;
this.getDiffPreferences = getDiffPreferences;
}

public GerritGitBlitUserModel(String username, final ProjectControl.GenericFactory projectControlFactory,
public GerritGitBlitUserModel(String username, final PermissionBackend permissions,
final Provider<? extends CurrentUser> userProvider, final GetDiffPreferences getDiffPreferences) {
super(username);
this.username = username;
this.isAuthenticated = true;
this.projectControlFactory = projectControlFactory;
this.permissions = permissions;
this.userProvider = userProvider;
this.getDiffPreferences = getDiffPreferences;
CurrentUser user = userProvider.get();
Expand All @@ -78,54 +81,42 @@ public GerritGitBlitUserModel(String username, final ProjectControl.GenericFacto

@Override
protected boolean canAccess(final RepositoryModel repository, final AccessRestrictionType ifRestriction, final AccessPermission requirePermission) {
try {
ProjectControl control = projectControlFactory.controlFor(new NameKey(StringUtils.stripDotGit(repository.name)), userProvider.get());
if (control == null) {
return false;
}
switch (ifRestriction) {
case VIEW:
return control.isVisible();
case CLONE:
return control.canRunUploadPack();
case PUSH:
return control.canRunReceivePack();
default:
return true;
}
} catch (NoSuchProjectException | IOException e) {
ForProject projectPermissions = permissions.user(userProvider).project(new NameKey(StringUtils.stripDotGit(repository.name)));
if (projectPermissions == null) {
return false;
}
switch (ifRestriction) {
case VIEW:
return projectPermissions.testOrFalse(ProjectPermission.ACCESS);
case CLONE:
return projectPermissions.testOrFalse(ProjectPermission.RUN_UPLOAD_PACK);
case PUSH:
return projectPermissions.testOrFalse(ProjectPermission.RUN_RECEIVE_PACK);
default:
return true;
}
}

@Override
public boolean hasRepositoryPermission(String name) {
try {
ProjectControl control = projectControlFactory.controlFor(new NameKey(StringUtils.stripDotGit(name)), userProvider.get());
return control != null && control.isVisible();
} catch (NoSuchProjectException | IOException e) {
return false;
}
ForProject projectPermissions = permissions.user(userProvider).project(new NameKey(StringUtils.stripDotGit(name)));
return projectPermissions != null && projectPermissions.testOrFalse(ProjectPermission.ACCESS);
}

@Override
public boolean canView(RepositoryModel repository, String ref) {
try {
ProjectControl control = projectControlFactory.controlFor(new NameKey(StringUtils.stripDotGit(repository.name)), userProvider.get());
if (control != null && control.isVisible()) {
RefControl branchCtrl = control.controlForRef(ref);
return branchCtrl != null && branchCtrl.isVisible();
}
} catch (NoSuchProjectException | IOException e) {
// Silently ignore and return false below.
ForProject projectPermissions = permissions.user(userProvider).project(new NameKey(StringUtils.stripDotGit(repository.name)));
if (projectPermissions != null) {
ForRef refPermissions = projectPermissions.ref(ref);
return refPermissions != null && refPermissions.testOrFalse(RefPermission.READ);
}
return false;
}

/**
* Retrieves the Gerrit preference setting for the number of diff context lines. A value < 0 indicates a "full file" context. If the current user
* is not logged in, returns the Gitblit (and JGit) default of 3, otherwise the setting as configured by the user in his Gerrit settings.
*
*
* @return the number of context lines to display in a diff, or < 0 if the whole file shall be shown.
*/
public int diffContext() {
Expand All @@ -137,7 +128,7 @@ public int diffContext() {
if (diffPrefs != null) {
return diffPrefs.context;
}
} catch (AuthException | ConfigInvalidException | IOException e) {
} catch (AuthException | ConfigInvalidException | PermissionBackendException | IOException e) {
// Ignore and return default below.
}
}
Expand Down

0 comments on commit d05c5f4

Please sign in to comment.