Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds permissions to API Keys #674

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -798,18 +798,11 @@ public boolean hasPermission(final Team team, String permissionName) {
* @since 1.1.1
*/
public boolean hasPermission(final ApiKey apiKey, String permissionName) {
if (apiKey.getTeams() == null) {
return false;
}
for (final Team team: apiKey.getTeams()) {
final List<Permission> teamPermissions = getObjectById(Team.class, team.getId()).getPermissions();
for (final Permission permission: teamPermissions) {
if (permission.getName().equals(permissionName)) {
return true;
}
}
}
return false;
final Query<Permission> query = pm.newQuery(Permission.class, "name == :permissionName && apiKey.contains(apiKey) && apiKey.id == :apiKeyId");
query.declareVariables("alpine.model.ApiKey apiKey");
query.setParameters(permissionName, apiKey.getId());
query.setResult("count(id)");
return executeAndCloseResultUnique(query, Long.class) > 0;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion alpine-model/src/main/java/alpine/model/ApiKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public class ApiKey implements Serializable, Principal {
@JsonIgnore
private List<Team> teams;

@Persistent(table = "APIKEYS_PERMISSIONS", defaultFetchGroup = "true")
@Join(column = "APIKEY_ID")
@Element(column = "PERMISSION_ID")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "name ASC"))
private List<Permission> permissions;
public long getId() {
return id;
}
Expand Down Expand Up @@ -166,5 +171,12 @@ public void setTeams(List<Team> teams) {
this.teams = teams;
}

}
public List<Permission> getPermissions() {
return permissions;
}

public void setPermissions(List<Permission> permissions) {
this.permissions = permissions;
}

}
14 changes: 13 additions & 1 deletion alpine-model/src/main/java/alpine/model/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public class Permission implements Serializable {
@JsonIgnore
private List<ManagedUser> managedUsers;

@Persistent(mappedBy = "permissions")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "username ASC"))
@JsonIgnore
private List<ApiKey> apiKeys;

public long getId() {
return id;
}
Expand Down Expand Up @@ -140,5 +145,12 @@ public List<ManagedUser> getManagedUsers() {
public void setManagedUsers(List<ManagedUser> managedUsers) {
this.managedUsers = managedUsers;
}
}

public List<ApiKey> getApiKeys() {
return apiKeys;
}

public void setApiKeys(List<ApiKey> apiKeys) {
this.apiKeys = apiKeys;
}
}
10 changes: 10 additions & 0 deletions alpine-model/src/test/java/alpine/model/ApiKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@ public void teamsTest() {
Assertions.assertEquals(teams, key.getTeams());
Assertions.assertEquals(1, key.getTeams().size());
}

@Test
public void permissionsTest() {
List<Permission> permissions = new ArrayList<>();
permissions.add(new Permission());
ApiKey user = new ApiKey();
user.setPermissions(permissions);
Assertions.assertEquals(permissions, user.getPermissions());
Assertions.assertEquals(1, user.getPermissions().size());
}
}