Change roles to MSRead, MSWrite

This commit is contained in:
karl 2019-12-10 15:50:13 +01:00
parent 56d8f7fca7
commit 16041ef050
5 changed files with 24 additions and 10 deletions

View File

@ -11,7 +11,6 @@ import java.net.URI;
import java.util.List;
@Path("/movie")
@RolesAllowed("MoviesUserRole")
public class MovieResource {
@Inject
private MoviesService moviesService;
@ -24,6 +23,7 @@ public class MovieResource {
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
@RolesAllowed("MSRead")
public List<Movie> retrieveAll() {
return moviesService.findAll();
}
@ -33,6 +33,7 @@ public class MovieResource {
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
@RolesAllowed("MSRead")
@Path("/{id}")
public Movie retrieve(@PathParam("id") long id) {
return moviesService.findById(id);
@ -40,6 +41,7 @@ public class MovieResource {
@DELETE
@Path("/{id}")
@RolesAllowed("MSWrite")
public void delete(@PathParam("id") long id) {
moviesService.removeById(id);
}
@ -49,6 +51,7 @@ public class MovieResource {
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
@RolesAllowed("MSWrite")
public Response create(Movie movie) {
movie.setId(null); // Make sure that a new movie is added, not overwriting existing one
List<Movie> newMovies = moviesService.save(List.of(movie));
@ -64,6 +67,7 @@ public class MovieResource {
MediaType.APPLICATION_XML
})
@Path("/{id}")
@RolesAllowed("MSWrite")
public void update(@PathParam("id") long id, Movie movie) {
movie.setId(id); // Make sure that a new movie is added, not overwriting existing one
moviesService.save(List.of(movie));

View File

@ -7,8 +7,8 @@ import javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition;
@BasicAuthenticationMechanismDefinition(realmName = "MoviesWebApp")
@DeclareRoles({
"MoviesAdminRole",
"MoviesUerRole"
"MSRead",
"MSWrite"
})
@DatabaseIdentityStoreDefinition(
dataSourceLookup = "java:jboss/datasources/MoviesDS",

View File

@ -14,7 +14,6 @@ import java.util.logging.Logger;
@Stateless
@TransactionManagement(value=TransactionManagementType.CONTAINER)
@RolesAllowed("MoviesUserRole")
public class MoviesService {
private static final Logger LOGGER = Logger.getLogger(MoviesService.class.getName());
@ -27,6 +26,7 @@ public class MoviesService {
@Inject
private SecurityContext securityContext;
@RolesAllowed("MSRead")
public Movie findById(long id) {
LOGGER.info("findById() >> id=" + id);
@ -38,6 +38,7 @@ public class MoviesService {
return movie;
}
@RolesAllowed("MSRead")
public List<Movie> findByTitle(String title) {
LOGGER.info("findByTitle() >> title=" + title);
@ -46,6 +47,7 @@ public class MoviesService {
.getResultList();
}
@RolesAllowed("MSRead")
public List<Movie> findAll() {
LOGGER.info("findAll()");
@ -56,6 +58,7 @@ public class MoviesService {
.getResultList();
}
@RolesAllowed("MSWrite")
public void removeById(long id) {
LOGGER.info("removeById() >> id=" + id);
@ -63,6 +66,7 @@ public class MoviesService {
em.remove(movie); //managed news required
}
@RolesAllowed("MSRead")
public List<Actor> findAllActors() {
LOGGER.info("findAllActors)");
@ -70,6 +74,7 @@ public class MoviesService {
.getResultList();
}
@RolesAllowed("MSRead")
public List<Studio> findAllStudios() {
LOGGER.info("findAllStudios)");
@ -77,6 +82,7 @@ public class MoviesService {
.getResultList();
}
@RolesAllowed("MSRead")
public Studio findStudioById(long id) {
LOGGER.info("findStudioById() >> id=" + id);
@ -88,8 +94,8 @@ public class MoviesService {
return studio;
}
// TODO maybe check if the movie already exists?
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@RolesAllowed("MSWrite")
public List<Movie> save(List<Movie> movies) {
try {

View File

@ -11,7 +11,6 @@ import java.net.URI;
import java.util.List;
@Path("/studio")
@RolesAllowed("MoviesUserRole")
public class StudioResource {
@Inject
private MoviesService moviesService;
@ -24,6 +23,7 @@ public class StudioResource {
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
@RolesAllowed("MSRead")
public List<Studio> retrieveAll() {
return moviesService.findAllStudios();
}
@ -34,6 +34,7 @@ public class StudioResource {
MediaType.APPLICATION_XML
})
@Path("/{id}")
@RolesAllowed("MSRead")
public Studio retrieve(@PathParam("id") long id) {
return moviesService.findStudioById(id);
}

View File

@ -24,8 +24,11 @@ CREATE TABLE t_user_role (
INSERT INTO t_user (id, username, password) VALUES (1, 'moviesadmin', SHA2('topsecret', 512));
INSERT INTO t_user (id, username, password) VALUES (2, 'moviesuser', SHA2('topsecret', 512));
INSERT INTO t_role (id, rolename) VALUES (1, 'MoviesAdminRole');
INSERT INTO t_role (id, rolename) VALUES (2, 'MoviesUserRole');
INSERT INTO t_role (id, rolename) VALUES (1, 'MSWrite');
INSERT INTO t_role (id, rolename) VALUES (2, 'MSRead');
INSERT INTO t_user_role (id, userid, roleid) VALUES (1, 1, 1);
INSERT INTO t_user_role (id, userid, roleid) VALUES (2, 2, 2);
-- TODO: Would be nice to add a trigger which automatically adds MSWrite users into MSRead
INSERT INTO t_user_role (id, userid, roleid) VALUES (1, 1, 1); -- Admin can write
INSERT INTO t_user_role (id, userid, roleid) VALUES (2, 1, 2); -- Admin can read
INSERT INTO t_user_role (id, userid, roleid) VALUES (3, 2, 2); -- User can read