Skip to content

v0.2.47..v0.2.48 changeset UserManagerImpl.java

Garret Voltz edited this page Sep 27, 2019 · 1 revision
diff --git a/hoot-services/src/main/java/hoot/services/controllers/auth/UserManagerImpl.java b/hoot-services/src/main/java/hoot/services/controllers/auth/UserManagerImpl.java
index d2f78b3..f10bd49 100644
--- a/hoot-services/src/main/java/hoot/services/controllers/auth/UserManagerImpl.java
+++ b/hoot-services/src/main/java/hoot/services/controllers/auth/UserManagerImpl.java
@@ -22,7 +22,7 @@
  * This will properly maintain the copyright information. DigitalGlobe
  * copyrights will be updated automatically.
  *
- * @copyright Copyright (C) 2018 DigitalGlobe (http://www.digitalglobe.com/)
+ * @copyright Copyright (C) 2018, 2019 DigitalGlobe (http://www.digitalglobe.com/)
  */
 package hoot.services.controllers.auth;
 
@@ -54,13 +54,17 @@ import org.xml.sax.SAXException;
 
 import hoot.services.models.db.SpringSession;
 import hoot.services.models.db.Users;
+import hoot.services.utils.DbUtils;
 import hoot.services.utils.XmlDocumentBuilder;
 
 @Component
 @Transactional(propagation = Propagation.REQUIRES_NEW) // Run inside of a new transaction.  This is intentional.
 public class UserManagerImpl implements UserManager {
+
     private static final Logger logger = LoggerFactory.getLogger(UserManagerImpl.class);
+
     public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+
     public static final ConcurrentHashMap<String, Users> userCache = new ConcurrentHashMap<String, Users>();
 
     @Override
@@ -88,18 +92,19 @@ public class UserManagerImpl implements UserManager {
 
         return null;
     }
+
     private void update(Users user) {
         createQuery().update(users).populate(user).where(users.id.eq(user.getId())).execute();
     }
+
     private void insert(Users user) {
         createQuery().insert(users).populate(user).execute();
     }
-    public Users getUser(String id) {
-        return getUser(Long.parseLong(id));
-    }
+
     public Users getUser(Long id) {
         return createQuery().select(users).from(users).where(users.id.eq(id)).fetchFirst();
     }
+
     @Override
     public Timestamp parseTimestamp(String timestamp) {
 
@@ -112,6 +117,7 @@ public class UserManagerImpl implements UserManager {
             return null;
         }
     }
+
     @Override
     public Users parseUser(String xml) throws SAXException, IOException, ParserConfigurationException, InvalidUserProfileException {
         Users user = new Users();
@@ -136,6 +142,7 @@ public class UserManagerImpl implements UserManager {
         }
         return user;
     }
+
     private void attributeSessionWithUser(String sessionId, Users u) {
         long affectedRows = createQuery()
         .update(springsessions)
@@ -147,16 +154,22 @@ public class UserManagerImpl implements UserManager {
             logger.warn("attributeSessionWithUser(): failed to attribute spring session with user.");
         }
     }
+
     @Override
     public Users upsert(String xml, OAuthConsumerToken accessToken, String sessionId) throws SAXException, IOException, ParserConfigurationException, InvalidUserProfileException {
         Users user = this.parseUser(xml);
         user.setProviderAccessToken(accessToken);
         user.setEmail(String.format("%d@hootenanny", user.getId()));
-        if (this.getUser(user.getId()) == null) {
+
+        Users existingUser = this.getUser(user.getId());
+        if (existingUser == null) {
             this.insert(user);
         } else {
+            // look in database for possible existing privileges set
+            user.setPrivileges(existingUser.getPrivileges());
             this.update(user);
         }
+
         attributeSessionWithUser(sessionId, user);
         userCache.put(sessionId, user);
         return user;
Clone this wiki locally