Skip to content

Commit

Permalink
feat: Ads permissions to api Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Gepardgame committed Oct 14, 2024
1 parent aa99025 commit 5940550
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
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());
}
}

0 comments on commit 5940550

Please sign in to comment.