From 97cc34eaf873ec86a3155ae683b0be8ab3997002 Mon Sep 17 00:00:00 2001 From: Dan Schultz Date: Sun, 8 May 2016 21:54:03 -0400 Subject: [PATCH] added User stack --- .../java/org/openhmis/dao/TmpUserDAO.java | 66 ++++++++++ .../java/org/openhmis/domain/TmpUser.java | 111 +++++++++++++++++ src/main/java/org/openhmis/dto/UserDTO.java | 117 ++++++++++++++++++ .../org/openhmis/manager/UserManager.java | 114 +++++++++++++++++ .../org/openhmis/util/Authentication.java | 39 ++++-- .../util/HibernateSessionFactory.java | 1 + .../org/openhmis/webservice/UserService.java | 92 ++++++++++++++ .../db/migration/V028__CREATE_TMP_USER.sql | 9 ++ 8 files changed, 536 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/openhmis/dao/TmpUserDAO.java create mode 100644 src/main/java/org/openhmis/domain/TmpUser.java create mode 100644 src/main/java/org/openhmis/dto/UserDTO.java create mode 100644 src/main/java/org/openhmis/manager/UserManager.java create mode 100644 src/main/java/org/openhmis/webservice/UserService.java create mode 100644 src/main/resources/db/migration/V028__CREATE_TMP_USER.sql diff --git a/src/main/java/org/openhmis/dao/TmpUserDAO.java b/src/main/java/org/openhmis/dao/TmpUserDAO.java new file mode 100644 index 0000000..20371af --- /dev/null +++ b/src/main/java/org/openhmis/dao/TmpUserDAO.java @@ -0,0 +1,66 @@ +package org.openhmis.dao; + + +import java.util.Date; +import java.util.List; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.openhmis.domain.TmpUser; + +public class TmpUserDAO extends BaseDAO { + + public TmpUserDAO() { + } + + public TmpUser getTmpUserById(Integer userId) { + String queryString = "select user " + + "from TmpUser as user " + + "where user.userId =:userId"; + + Session session = getSession(); + Query queryObject = session.createQuery(queryString); + queryObject.setParameter("userId", userId); + queryObject.setMaxResults(1); + + List results = queryObject.list(); + session.close(); + + if(results.size() > 0) + return (TmpUser)results.get(0); + else + return null; + } + + public TmpUser getTmpUserByExternalId(String externalId) { + String queryString = "select user " + + "from TmpUser as user " + + "where user.externalId =:externalId"; + + Session session = getSession(); + Query queryObject = session.createQuery(queryString); + queryObject.setParameter("externalId", externalId); + queryObject.setMaxResults(1); + + List results = queryObject.list(); + session.close(); + + if(results.size() > 0) + return (TmpUser)results.get(0); + else + return null; + } + + + @SuppressWarnings("unchecked") + public List getTmpUsers() { + String queryString = "select user " + + "from TmpUser as user"; + + Session session = getSession(); + Query queryObject = session.createQuery(queryString); + List results = queryObject.list(); + session.close(); + return results; + } +} \ No newline at end of file diff --git a/src/main/java/org/openhmis/domain/TmpUser.java b/src/main/java/org/openhmis/domain/TmpUser.java new file mode 100644 index 0000000..1a94480 --- /dev/null +++ b/src/main/java/org/openhmis/domain/TmpUser.java @@ -0,0 +1,111 @@ +package org.openhmis.domain; + + + +// Generated Aug 5, 2015 10:00:15 PM by Hibernate Tools 4.3.1 + +import java.util.Date; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import static javax.persistence.GenerationType.IDENTITY; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +@Entity +@Table(name = "TMP_USER") +public class TmpUser implements java.io.Serializable { + private Integer userId; + private String externalId; + private Integer canRead; + private Integer canWrite; + private Integer canAdmin; + private Date dateCreated; + private Date dateUpdated; + + public TmpUser() { + } + + public TmpUser(Integer userId, String externalId, + Integer canRead, Integer canWrite, + Integer canAdmin, + Date dateCreated, Date dateUpdated) { + this.userId = userId; + this.externalId = externalId; + this.canRead = canRead; + this.canWrite = canWrite; + this.canAdmin = canAdmin; + this.dateCreated = dateCreated; + this.dateUpdated = dateUpdated; + } + + @Id + @GeneratedValue(strategy = IDENTITY) + @Column(name = "userId", unique = true, nullable = false) + public Integer getUserId() { + return this.userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + @Column(name = "externalId") + public String getExternalId() { + return this.externalId; + } + + public void setExternalId(String externalId) { + this.externalId = externalId; + } + + @Column(name = "canRead") + public Integer getCanRead() { + return this.canRead; + } + + public void setCanRead(Integer canRead) { + this.canRead = canRead; + } + + @Column(name = "canWrite") + public Integer getCanWrite() { + return this.canWrite; + } + + public void setCanWrite(Integer canWrite) { + this.canWrite = canWrite; + } + + @Column(name = "canAdmin") + public Integer getCanAdmin() { + return this.canAdmin; + } + + public void setCanAdmin(Integer canAdmin) { + this.canAdmin = canAdmin; + } + + @Temporal(TemporalType.DATE) + @Column(name = "dateCreated", length = 10) + public Date getDateCreated() { + return this.dateCreated; + } + + public void setDateCreated(Date dateCreated) { + this.dateCreated = dateCreated; + } + + @Temporal(TemporalType.DATE) + @Column(name = "dateUpdated", length = 10) + public Date getDateUpdated() { + return this.dateUpdated; + } + + public void setDateUpdated(Date dateUpdated) { + this.dateUpdated = dateUpdated; + } + +} diff --git a/src/main/java/org/openhmis/dto/UserDTO.java b/src/main/java/org/openhmis/dto/UserDTO.java new file mode 100644 index 0000000..e793472 --- /dev/null +++ b/src/main/java/org/openhmis/dto/UserDTO.java @@ -0,0 +1,117 @@ +package org.openhmis.dto; + + +import java.util.Date; + +import javax.xml.bind.annotation.XmlRootElement; + +import org.openhmis.code.ClientDestination; +import org.openhmis.code.ClientEarlyExitReason; +import org.openhmis.code.ClientEmploymentType; +import org.openhmis.code.ClientExitAction; +import org.openhmis.code.ClientExpelledReason; +import org.openhmis.code.ClientHealthStatus; +import org.openhmis.code.ClientHousingAssessmentAtExit; +import org.openhmis.code.ClientHousingAssessmentDisposition; +import org.openhmis.code.ClientNotEmployedReason; +import org.openhmis.code.ClientProjectCompletionStatus; +import org.openhmis.code.ClientSubsidyInformation; +import org.openhmis.code.YesNoReason; + +import com.fasterxml.jackson.annotation.JsonProperty; + +@XmlRootElement +public class UserDTO { + + private String userId; + private String externalId; + private Integer canRead; + private Integer canWrite; + private Integer canAdmin; + + // Export Standard Fields + private Date dateCreated; + private Date dateUpdated; + + public UserDTO() {} + + @JsonProperty + public String getId() { + return userId; + } + + @JsonProperty + public void setId(String userId) { + this.userId = userId; + } + + @JsonProperty + public String getUserId() { + return userId; + } + + @JsonProperty + public void setUserId(String userId) { + this.userId = userId; + } + + @JsonProperty + public String getExternalId() { + return externalId; + } + + @JsonProperty + public void setExternalId(String externalId) { + this.externalId = externalId; + } + + @JsonProperty + public Integer getCanRead() { + return canRead; + } + + @JsonProperty + public void setCanRead(Integer canRead) { + this.canRead = canRead; + } + + @JsonProperty + public Integer getCanWrite() { + return canWrite; + } + + @JsonProperty + public void setCanWrite(Integer canWrite) { + this.canWrite = canWrite; + } + + @JsonProperty + public Integer getCanAdmin() { + return canAdmin; + } + + @JsonProperty + public void setCanAdmin(Integer canAdmin) { + this.canAdmin = canAdmin; + } + + @JsonProperty + public Date getDateCreated() { + return dateCreated; + } + + @JsonProperty + public void setDateCreated(Date dateCreated) { + this.dateCreated = dateCreated; + } + + @JsonProperty + public Date getDateUpdated() { + return dateUpdated; + } + + @JsonProperty + public void setDateUpdated(Date dateUpdated) { + this.dateUpdated = dateUpdated; + } +} \ No newline at end of file diff --git a/src/main/java/org/openhmis/manager/UserManager.java b/src/main/java/org/openhmis/manager/UserManager.java new file mode 100644 index 0000000..0b0a987 --- /dev/null +++ b/src/main/java/org/openhmis/manager/UserManager.java @@ -0,0 +1,114 @@ +package org.openhmis.manager; + +import java.util.ArrayList; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +import org.openhmis.dao.TmpUserDAO; +import org.openhmis.domain.TmpUser; +import org.openhmis.dto.UserDTO; + +public class UserManager { + private static final TmpUserDAO tmpUserDAO = new TmpUserDAO(); + + public UserManager() {} + + public static UserDTO getUserById(String userId) { + UserDTO userDTO = UserManager.generateUserDTO(tmpUserDAO.getTmpUserById(Integer.parseInt(userId))); + return userDTO; + } + + public static List getUsers() { + List userDTOs = new ArrayList(); + + // Collect the users + List tmpUsers = tmpUserDAO.getTmpUsers(); + + // For each financialAssistance, collect and map the data + for (Iterator iterator = tmpUsers.iterator(); iterator.hasNext();) { + TmpUser tmpUser = iterator.next(); + UserDTO userDTO = UserManager.generateUserDTO(tmpUser); + userDTOs.add(userDTO); + } + return userDTOs; + } + + public static UserDTO getUserByExternalId(String externalId) { + + // Collect the users + TmpUser tmpUser = tmpUserDAO.getTmpUserByExternalId(externalId); + + UserDTO userDTO = UserManager.generateUserDTO(tmpUser); + return userDTO; + + } + + public static UserDTO addUser(UserDTO inputDTO) { + // Generate a PathClient from the input + TmpUser tmpUser = UserManager.generateTmpUser(inputDTO); + + // Set Export fields + tmpUser.setDateCreated(new Date()); + tmpUser.setDateUpdated(new Date()); + + // Save the client to allow secondary object generation + tmpUserDAO.save(tmpUser); + inputDTO.setUserId(tmpUser.getUserId().toString()); + + // Return the resulting DTO + return UserManager.generateUserDTO(tmpUser); + } + + public static UserDTO updateUser(UserDTO inputDTO) { + // Generate a Exit from the input + TmpUser tmpUser = UserManager.generateTmpUser(inputDTO); + tmpUser.setUserId(Integer.parseInt(inputDTO.getUserId())); + tmpUser.setDateUpdated(new Date()); + + // Update the object + tmpUserDAO.update(tmpUser); + + // Return the resulting DTO + return UserManager.generateUserDTO(tmpUser); + + } + + public static boolean deleteUser(String userId) { + TmpUser tmpUser = tmpUserDAO.getTmpUserById(Integer.parseInt(userId)); + tmpUserDAO.delete(tmpUser); + return true; + } + + public static UserDTO generateUserDTO(TmpUser tmpUser) { + UserDTO userDTO = new UserDTO(); + + userDTO.setUserId(tmpUser.getUserId().toString()); + userDTO.setExternalId(tmpUser.getExternalId()); + userDTO.setCanRead(tmpUser.getCanRead()); + userDTO.setCanWrite(tmpUser.getCanWrite()); + userDTO.setCanAdmin(tmpUser.getCanAdmin()); + + // Export Standard Fields + userDTO.setDateCreated(tmpUser.getDateCreated()); + userDTO.setDateUpdated(tmpUser.getDateUpdated()); + + return userDTO; + } + + public static TmpUser generateTmpUser(UserDTO inputDTO) { + TmpUser tmpUser = new TmpUser(); + + tmpUser.setExternalId(inputDTO.getExternalId()); + tmpUser.setCanRead(inputDTO.getCanRead()); + tmpUser.setCanWrite(inputDTO.getCanWrite()); + tmpUser.setCanAdmin(inputDTO.getCanAdmin()); + + // Export Standard Fields + tmpUser.setDateCreated(inputDTO.getDateCreated()); + tmpUser.setDateUpdated(inputDTO.getDateUpdated()); + + return tmpUser; + } + +} \ No newline at end of file diff --git a/src/main/java/org/openhmis/util/Authentication.java b/src/main/java/org/openhmis/util/Authentication.java index 83c11df..1f30d2b 100644 --- a/src/main/java/org/openhmis/util/Authentication.java +++ b/src/main/java/org/openhmis/util/Authentication.java @@ -36,6 +36,11 @@ import com.google.api.client.json.jackson2.JacksonFactory; public class Authentication { + public static final String EXISTS = "EXISTS"; + public static final String READ = "READ"; + public static final String WRITE = "WRITE"; + + private static final HttpTransport TRANSPORT = new NetHttpTransport(); private static final JacksonFactory JSON_FACTORY = new JacksonFactory(); @@ -67,20 +72,28 @@ public static String getGoogleToken(String code) { } public static Boolean googleAuthenticate(String tokenString) { - if(tokenString == null) - return false; + return googleAuthenticate(tokenString, Authentication.EXISTS); - try { - // Verify that the token is a legitimate google token - GoogleIdToken token = GoogleIdToken.parse(JSON_FACTORY, tokenString); - GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier(TRANSPORT, JSON_FACTORY); - verifier.verify(token); - return true; - } catch (IOException e) { - return false; - } catch (GeneralSecurityException e) { - return false; - } + } + + public static Boolean googleAuthenticate(String tokenString, String authType) { + if(tokenString == null) + return false; + try { + // Verify that the token is a legitimate google token + GoogleIdToken token = GoogleIdToken.parse(JSON_FACTORY, tokenString); + GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier(TRANSPORT, JSON_FACTORY); + verifier.verify(token); + + // If we get here then this is a valid google item + String userId = token.getPayload().getSubject(); + + return true; + } catch (IOException e) { + return false; + } catch (GeneralSecurityException e) { + return false; + } } } diff --git a/src/main/java/org/openhmis/util/HibernateSessionFactory.java b/src/main/java/org/openhmis/util/HibernateSessionFactory.java index a141402..a425c5f 100644 --- a/src/main/java/org/openhmis/util/HibernateSessionFactory.java +++ b/src/main/java/org/openhmis/util/HibernateSessionFactory.java @@ -103,6 +103,7 @@ public static void rebuildSessionFactory() { configuration.addAnnotatedClass(org.openhmis.domain.TmpIncomeSource.class); configuration.addAnnotatedClass(org.openhmis.domain.PathClientRace.class); configuration.addAnnotatedClass(org.openhmis.domain.TmpDevelopmentalDisability.class); + configuration.addAnnotatedClass(org.openhmis.domain.TmpUser.class); // Load the application properties based on the current context ApplicationPropertyUtil propertyUtil = new ApplicationPropertyUtil(); diff --git a/src/main/java/org/openhmis/webservice/UserService.java b/src/main/java/org/openhmis/webservice/UserService.java new file mode 100644 index 0000000..0428c05 --- /dev/null +++ b/src/main/java/org/openhmis/webservice/UserService.java @@ -0,0 +1,92 @@ + + + + +package org.openhmis.webservice; + +import java.io.IOException; +import java.util.List; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import org.apache.log4j.Logger; +import org.openhmis.dto.UserDTO; +import org.openhmis.manager.UserManager; +import org.openhmis.util.Authentication; +import org.openhmis.util.DateParser; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; + + +@Path("/users") +public class UserService { + private static final Logger log = Logger.getLogger(ExitService.class); + public UserService() {} + + @GET + @Path("/") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public List getUsers(@HeaderParam("Authorization") String authorization) throws JsonProcessingException { + if(!Authentication.googleAuthenticate(authorization)) + throw new Error("You are not authorized to access this content"); + + List userDTOs = UserManager.getUsers(); + return userDTOs; + } + + @POST + @Path("/") + @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public UserDTO createUser(@HeaderParam("Authorization") String authorization, UserDTO inputDTO) throws JsonParseException, JsonMappingException, IOException { + if(!Authentication.googleAuthenticate(authorization)) + throw new Error("You are not authorized to access this content"); + UserDTO outputDTO = UserManager.addUser(inputDTO); + return outputDTO; + } + + @GET + @Path("/{userId}") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public UserDTO getUser(@HeaderParam("Authorization") String authorization, @PathParam("userId") String userId) throws JsonProcessingException { + if(!Authentication.googleAuthenticate(authorization)) + throw new Error("You are not authorized to access this content"); + UserDTO outputDTO = UserManager.getUserById(userId); + return outputDTO; + } + + @PUT + @Path("/{userId}") + @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public UserDTO updateUser(@HeaderParam("Authorization") String authorization, @PathParam("userId") String userId, UserDTO inputDTO) throws JsonParseException, JsonMappingException, IOException { + if(!Authentication.googleAuthenticate(authorization)) + throw new Error("You are not authorized to access this content"); + inputDTO.setUserId(userId); + + UserDTO outputDTO = UserManager.updateUser(inputDTO); + return outputDTO; + } + + @DELETE + @Path("/{userId}") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public String deleteUser(@HeaderParam("Authorization") String authorization, @PathParam("userId") String userId) throws JsonParseException, JsonMappingException, IOException { + if(!Authentication.googleAuthenticate(authorization)) + throw new Error("You are not authorized to access this content"); + UserManager.deleteUser(userId); + return "true"; + } +} \ No newline at end of file diff --git a/src/main/resources/db/migration/V028__CREATE_TMP_USER.sql b/src/main/resources/db/migration/V028__CREATE_TMP_USER.sql new file mode 100644 index 0000000..1de814d --- /dev/null +++ b/src/main/resources/db/migration/V028__CREATE_TMP_USER.sql @@ -0,0 +1,9 @@ +CREATE TABLE `TMP_USER` ( + userId INT AUTO_INCREMENT PRIMARY KEY, + externalID VARCHAR(255) UNIQUE, + canRead INT, + canWrite INT, + canAdmin INT, + dateCreated DATE, + dateUpdated DATE +);