Reorg and more methods for RolesApi

Jens Pelzetter 2020-05-29 20:30:49 +02:00
parent 211467eb4e
commit 8fead43ead
17 changed files with 651 additions and 42 deletions

View File

@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.core.api;
package org.libreccm.api;
/**
*

View File

@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.core.api;
package org.libreccm.api;
/**
*

View File

@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.core.api;
package org.libreccm.api;
import java.util.Objects;

View File

@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.core.api;
package org.libreccm.api;
/**
*

View File

@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.core.api;
package org.libreccm.api;
import com.google.common.collect.Sets;

View File

@ -22,8 +22,8 @@ import org.libreccm.api.admin.security.dto.GroupUserMembership;
import org.libreccm.api.admin.security.dto.PartyRoleMembership;
import org.libreccm.api.dto.ListView;
import org.libreccm.core.CoreConstants;
import org.libreccm.core.api.ExtractedIdentifier;
import org.libreccm.core.api.IdentifierExtractor;
import org.libreccm.api.ExtractedIdentifier;
import org.libreccm.api.IdentifierExtractor;
import java.net.URI;
import java.util.List;

View File

@ -5,18 +5,17 @@
*/
package org.libreccm.api.admin.security;
import org.libreccm.core.CcmObject;
import org.libreccm.api.ExtractedIdentifier;
import org.libreccm.core.CcmObjectRepository;
import org.libreccm.core.CoreConstants;
import org.libreccm.core.api.ExtractedIdentifier;
import org.libreccm.core.api.IdentifierExtractor;
import org.libreccm.core.api.JsonArrayCollector;
import org.libreccm.api.IdentifierExtractor;
import org.libreccm.api.admin.security.dto.RoleData;
import org.libreccm.api.dto.ListView;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.transaction.Transactional;
@ -30,7 +29,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@ -42,6 +40,14 @@ import org.libreccm.security.Role;
import org.libreccm.security.RoleManager;
import org.libreccm.security.RoleRepository;
import java.net.URI;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.ws.rs.WebApplicationException;
import static com.arsdigita.bebop.Component.*;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
@ -71,26 +77,19 @@ public class RolesApi {
@AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
@Transactional(Transactional.TxType.REQUIRED)
public JsonObject getRoles(
public ListView<RoleData> getRoles(
@QueryParam("limit") @DefaultValue("20") final int limit,
@QueryParam("offset") @DefaultValue("0") final int offset
) {
final long count = roleRepository.countAll();
final List<Role> roles = roleRepository.findAll(limit, offset);
return Json
.createObjectBuilder()
.add("count", count)
.add("limit", limit)
.add("offset", offset)
.add(
"roles",
roles
.stream()
.map(Role::toJson)
.collect(new JsonArrayCollector())
)
.build();
return new ListView<>(
roles.stream().map(RoleData::new).collect(Collectors.toList()),
count,
limit,
offset
);
}
@GET
@ -99,10 +98,10 @@ public class RolesApi {
@AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
@Transactional(Transactional.TxType.REQUIRED)
public JsonObject getRole(
public RoleData getRole(
@PathParam("roleIdentifier") final String roleIdentifier
) {
return findRole(roleIdentifier).toJson();
return new RoleData(findRole(roleIdentifier));
}
@POST
@ -111,8 +110,17 @@ public class RolesApi {
@AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
@Transactional(Transactional.TxType.REQUIRED)
public Response addRole(final JsonObject roleData) {
throw new UnsupportedOperationException();
public Response addRole(final RoleData roleData) {
final Role role = new Role();
role.setName(roleData.getName());
role.setDescription(roleData.getDescription());
roleRepository.save(role);
return Response.created(
URI.create(String.format("/api/admin/roles/%s", role.getName()))
).build();
}
@PUT
@ -213,8 +221,6 @@ public class RolesApi {
throw new UnsupportedOperationException();
}
private Party findParty(final String partyIdentifier) {
final ExtractedIdentifier identifier = identifierExtractor
.extractIdentifier(partyIdentifier);

View File

@ -23,8 +23,8 @@ import org.libreccm.api.admin.security.dto.UserGroupMembership;
import org.libreccm.api.admin.security.dto.PartyRoleMembership;
import org.libreccm.api.dto.ListView;
import org.libreccm.core.CoreConstants;
import org.libreccm.core.api.ExtractedIdentifier;
import org.libreccm.core.api.IdentifierExtractor;
import org.libreccm.api.ExtractedIdentifier;
import org.libreccm.api.IdentifierExtractor;
import java.net.URI;
import java.util.List;

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2020 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.api.admin.security.dto;
import org.libreccm.api.admin.workflow.dto.TaskId;
import org.libreccm.workflow.TaskAssignment;
import java.util.Objects;
/**
* A DTO for a {@link TaskAssignment} from the viewpoint of a {@link Role}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class RoleAssignedTask {
private long taskAssignmentId;
private String uuid;
private TaskId task;
/**
* Parameterless constructor for generating empty instances.
*/
public RoleAssignedTask() {
// Nothing
}
/**
* Creates {@code RoleAssignedTask} DTO from a {@link TaskAssignment}.
*
* @param taskAssignment The source object.
*/
public RoleAssignedTask(final TaskAssignment taskAssignment) {
Objects.requireNonNull(
taskAssignment, "Can't create a RoleAssignedTask DTO from null."
);
taskAssignmentId = taskAssignment.getTaskAssignmentId();
uuid = taskAssignment.getUuid();
task = new TaskId(taskAssignment.getTask());
}
}

View File

@ -0,0 +1,142 @@
/*
* Copyright (C) 2020 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.api.admin.security.dto;
import org.libreccm.l10n.LocalizedString;
import org.libreccm.security.Role;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* A DTO for {@link Role}s.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class RoleData {
private long roleId;
private String uuid;
private String name;
private LocalizedString description;
private List<RolePartyMembership> memberships;
private List<RolePermission> permissions;
private List<RoleAssignedTask> assignedTasks;
/**
* Parameterless constructor for creating empty instances.
*/
public RoleData() {
memberships = new ArrayList<>();
permissions = new ArrayList<>();
assignedTasks = new ArrayList<>();
}
public RoleData(final Role role) {
Objects.requireNonNull(role, "Can't create a RoleData DTO from null.");
roleId = role.getRoleId();
uuid = role.getUuid();
name = role.getName();
description = role.getDescription();
memberships = role
.getMemberships()
.stream()
.map(RolePartyMembership::new)
.collect(Collectors.toList());
permissions = role
.getPermissions()
.stream()
.map(RolePermission::new)
.collect(Collectors.toList());
assignedTasks = role
.getAssignedTasks()
.stream()
.map(RoleAssignedTask::new)
.collect(Collectors.toList());
}
public long getRoleId() {
return roleId;
}
public void setRoleId(final long roleId) {
this.roleId = roleId;
}
public String getUuid() {
return uuid;
}
public void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public List<RolePartyMembership> getMemberships() {
return new ArrayList<>(memberships);
}
public void setMemberships(final List<RolePartyMembership> memberships) {
this.memberships = new ArrayList<>(memberships);
}
public List<RoleAssignedTask> getAssignedTasks() {
return new ArrayList<>(assignedTasks);
}
public void setAssignedTasks(final List<RoleAssignedTask> assignedTasks) {
this.assignedTasks = new ArrayList<>(assignedTasks);
}
public LocalizedString getDescription() {
return description;
}
public void setDescription(final LocalizedString description) {
this.description = description;
}
public List<RolePermission> getPermissions() {
return new ArrayList<>(permissions);
}
public void setPermissions(final List<RolePermission> permissions) {
this.permissions = new ArrayList<>(permissions);
}
}

View File

@ -0,0 +1,85 @@
/*
* Copyright (C) 2020 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.api.admin.security.dto;
import org.libreccm.security.RoleMembership;
import java.util.Objects;
/**
* Data Transfer Object for data about the members of a role.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class RolePartyMembership {
private long membershipId;
private String uuid;
private PartyId party;
/**
* Parameterless constructor for generating empty instances.
*/
public RolePartyMembership() {
// Nothing
}
/**
* Creates a {@code RolePartyMembership} DTO from a {@link RoleMembership}.
*
* @param membership The source object from which the instance is created.
*/
public RolePartyMembership(final RoleMembership membership) {
Objects.requireNonNull(
membership,
"Can't create a RolePartyMembership from null."
);
membershipId = membership.getMembershipId();
uuid = membership.getUuid();
party = new PartyId(membership.getMember());
}
public long getMembershipId() {
return membershipId;
}
public void setMembershipId(final long membershipId) {
this.membershipId = membershipId;
}
public String getUuid() {
return uuid;
}
public void setUuid(final String uuid) {
this.uuid = uuid;
}
public PartyId getParty() {
return party;
}
public void setParty(final PartyId party) {
this.party = party;
}
}

View File

@ -0,0 +1,153 @@
/*
* Copyright (C) 2020 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.api.admin.security.dto;
import org.libreccm.api.core.dto.CcmObjectId;
import org.libreccm.security.Permission;
import java.util.Date;
import java.util.Objects;
/**
* A DTO for a {@link Permission} assigned to a role.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class RolePermission {
private long permissionId;
private String uuid;
private String grantedPrivilege;
private boolean inherited;
private CcmObjectId object;
private PartyId creationUser;
private Date creationDate;
private String creationIp;
private CcmObjectId inheritedFrom;
/**
* Parameterless constructor for creating empty instances.
*/
public RolePermission() {
// Nothing
}
public RolePermission(final Permission permission) {
Objects.requireNonNull(
permission, "Can't create a RolePermission DTO from null."
);
permissionId = permission.getPermissionId();
uuid = permission.getUuid();
grantedPrivilege = permission.getGrantedPrivilege();
inherited = permission.isInherited();
if (permission.getObject() != null) {
object = new CcmObjectId(permission.getObject());
}
creationUser = new PartyId(permission.getCreationUser());
creationDate = permission.getCreationDate();
creationIp = permission.getCreationIp();
if (inherited && permission.getInheritedFrom() != null) {
inheritedFrom = new CcmObjectId(permission.getInheritedFrom());
}
}
public long getPermissionId() {
return permissionId;
}
public void setPermissionId(final long permissionId) {
this.permissionId = permissionId;
}
public String getUuid() {
return uuid;
}
public void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getGrantedPrivilege() {
return grantedPrivilege;
}
public void setGrantedPrivilege(final String grantedPrivilege) {
this.grantedPrivilege = grantedPrivilege;
}
public boolean isInherited() {
return inherited;
}
public void setInherited(final boolean inherited) {
this.inherited = inherited;
}
public CcmObjectId getObject() {
return object;
}
public void setObject(final CcmObjectId object) {
this.object = object;
}
public PartyId getCreationUser() {
return creationUser;
}
public void setCreationUser(final PartyId creationUser) {
this.creationUser = creationUser;
}
public Date getCreationDate() {
return new Date(creationDate.getTime());
}
public void setCreationDate(Date creationDate) {
this.creationDate = new Date(creationDate.getTime());
}
public String getCreationIp() {
return creationIp;
}
public void setCreationIp(final String creationIp) {
this.creationIp = creationIp;
}
public CcmObjectId getInheritedFrom() {
return inheritedFrom;
}
public void setInheritedFrom(final CcmObjectId inheritedFrom) {
this.inheritedFrom = inheritedFrom;
}
}

View File

@ -0,0 +1,83 @@
/*
* Copyright (C) 2020 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.api.admin.workflow.dto;
import org.libreccm.l10n.LocalizedString;
import org.libreccm.workflow.Task;
import java.util.Objects;
/**
* A DTO for the identifing information of a tasks.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class TaskId {
private long taskId;
private String uuid;
private LocalizedString label;
/**
* Parameterless contructor for creating empty instances.
*/
public TaskId() {
// Nothing
}
/**
* Creates a {@code TaskId} DTO for a {@link Task}.
*
* @param task The source object.
*/
public TaskId(final Task task) {
Objects.requireNonNull(task, "Can't create a TaskId from null.");
taskId = task.getTaskId();
uuid = task.getUuid();
label = task.getLabel();
}
public long getTaskId() {
return taskId;
}
public void setTaskId(long taskId) {
this.taskId = taskId;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public LocalizedString getLabel() {
return label;
}
public void setLabel(LocalizedString label) {
this.label = label;
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (C) 2020 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.api.core.dto;
import org.libreccm.core.CcmObject;
import java.util.Objects;
/**
* A DTO containing the basic information about a {@link CcmObject}
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class CcmObjectId {
private long objectId;
private String uuid;
private String displayName;
/**
* Parameterless constructor for creating empty instances.
*/
public CcmObjectId() {
// Nothing
}
public CcmObjectId(final CcmObject object) {
Objects.requireNonNull(
object, "Can't create a CcmObjectId DTO from null."
);
objectId = object.getObjectId();
uuid = object.getUuid();
displayName = object.getDisplayName();
}
public long getObjectId() {
return objectId;
}
public void setObjectId(final long objectId) {
this.objectId = objectId;
}
public String getUuid() {
return uuid;
}
public void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(final String displayName) {
this.displayName = displayName;
}
}

View File

@ -23,7 +23,7 @@ import static org.libreccm.core.CoreConstants.*;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.libreccm.core.api.JsonArrayCollector;
import org.libreccm.api.JsonArrayCollector;
import javax.persistence.FetchType;
import javax.validation.constraints.NotNull;

View File

@ -28,7 +28,7 @@ import org.libreccm.workflow.TaskAssignment;
import static org.libreccm.core.CoreConstants.CORE_XML_NS;
import static org.libreccm.core.CoreConstants.DB_SCHEMA;
import org.libreccm.core.api.JsonArrayCollector;
import org.libreccm.api.JsonArrayCollector;
import org.libreccm.imexport.Exportable;
import java.io.Serializable;

View File

@ -30,7 +30,7 @@ import java.io.Serializable;
import static org.libreccm.core.CoreConstants.CORE_XML_NS;
import static org.libreccm.core.CoreConstants.DB_SCHEMA;
import org.libreccm.core.api.JsonArrayCollector;
import org.libreccm.api.JsonArrayCollector;
import org.libreccm.imexport.Exportable;
import java.util.ArrayList;