Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Checking privilegies for both project and repo #134
Browse files Browse the repository at this point in the history
 * So that the user may be admin in the repo, but not in the project.
  • Loading branch information
tomasbjerre committed Jul 30, 2016
1 parent 474e1fd commit 4ee1f0d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### GitHub [#134](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/134) Not able to see PR buttons
Checking privilegies for both project and repo

* So that the user may be admin in the repo, but not in the project.

[0e8243eb4d445dd](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/0e8243eb4d445dd) Tomas Bjerre *2016-07-30 06:58:02*

### GitHub [#135](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/135) Not permitted to access buttons settings as project/repo admin
Getting project and repo with sys admin permissions

[4af3fd9f87fa409](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/4af3fd9f87fa409) Tomas Bjerre *2016-07-30 06:42:38*
[474e1fd252eebf4](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/474e1fd252eebf4) Tomas Bjerre *2016-07-30 06:56:50*

## 2.32
### No issue
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/se/bjurr/prnfb/service/UserCheckService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean apply(PrnfbButton input) {
}
});
return allowedButtons;
}
}

public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug) {
boolean isAdmin = this.userManager.isAdmin(userKey);
Expand All @@ -68,12 +68,17 @@ public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug

if (projectKey != null && repositorySlug == null) {
Project project = getProject(projectKey);
return this.permissionService.hasProjectPermission(project, PROJECT_ADMIN);
} else if (repositorySlug != null) {
boolean isAllowed = this.permissionService.hasProjectPermission(project, PROJECT_ADMIN);
if (isAllowed) {
return true;
}
}

if (projectKey != null && repositorySlug != null) {
Repository repository = getRepo(projectKey, repositorySlug);
return this.permissionService.hasRepositoryPermission(repository, REPO_ADMIN);
}
return isAdmin;
return false;
}

/**
Expand Down
40 changes: 28 additions & 12 deletions src/test/java/se/bjurr/prnfb/service/UserCheckServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import static se.bjurr.prnfb.settings.USER_LEVEL.SYSTEM_ADMIN;

import java.util.List;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import se.bjurr.prnfb.presentation.dto.ON_OR_OFF;
import se.bjurr.prnfb.settings.PrnfbButton;

import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionService;
import com.atlassian.bitbucket.project.ProjectService;
import com.atlassian.bitbucket.repository.RepositoryService;
Expand All @@ -31,9 +31,33 @@
import com.atlassian.sal.api.user.UserProfile;

public class UserCheckServiceTest {
private final EscalatedSecurityContext escalatedSecurityContext = new EscalatedSecurityContext() {

@Mock
private EscalatedSecurityContext escalatedSecurityContext;
@Override
public void applyToRequest() {

}

@Override
public <T, E extends Throwable> T call(Operation<T, E> arg0) throws E {
return arg0.perform();
}

@Override
public EscalatedSecurityContext withPermission(Object arg0, Permission arg1) {
return this;
}

@Override
public EscalatedSecurityContext withPermission(Permission arg0) {
return this;
}

@Override
public EscalatedSecurityContext withPermissions(Set<Permission> arg0) {
return this;
}
};

@Mock
private PermissionService permissionService;
Expand Down Expand Up @@ -62,14 +86,6 @@ public void before() throws Exception {

when(this.securityService.withPermission(Matchers.any(), Matchers.any()))//
.thenReturn(this.escalatedSecurityContext);
when(this.escalatedSecurityContext.call(Matchers.any()))//
.thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
Operation<?, ?> op = (Operation<?, ?>) invocation.getArguments()[0];
return (Boolean) op.perform();
}
});
}

@Test
Expand Down

0 comments on commit 4ee1f0d

Please sign in to comment.