User management changes

svoorakk svoorakk 2014-06-26

changed src/main/java/net/timbusproject/dpes/data/DataAccessOperations.java
changed src/main/java/net/timbusproject/dpes/registry/DataBaseSchema.java
changed src/main/java/net/timbusproject/dpes/service/api/Login.java
changed src/main/java/net/timbusproject/dpes/servlets/LoginServlet.java
changed src/main/java/net/timbusproject/dpes/servlets/LookupServlet.java
copied src/main/java/net/timbusproject/dpes/registry/Login.java -> src/main/java/net/timbusproject/dpes/registry/UserAccess.java
src/main/java/net/timbusproject/dpes/data/DataAccessOperations.java Diff Switch to side-by-side view
Loading...
src/main/java/net/timbusproject/dpes/registry/DataBaseSchema.java Diff Switch to side-by-side view
Loading...
src/main/java/net/timbusproject/dpes/service/api/Login.java Diff Switch to side-by-side view
Loading...
src/main/java/net/timbusproject/dpes/servlets/LoginServlet.java Diff Switch to side-by-side view
Loading...
src/main/java/net/timbusproject/dpes/servlets/LookupServlet.java Diff Switch to side-by-side view
Loading...
src/main/java/net/timbusproject/dpes/registry/Login.java to src/main/java/net/timbusproject/dpes/registry/UserAccess.java
--- a/src/main/java/net/timbusproject/dpes/registry/Login.java
+++ b/src/main/java/net/timbusproject/dpes/registry/UserAccess.java
@@ -26,14 +26,31 @@
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.HashMap;
-
-public class Login {
+import java.util.List;
+
+import net.timbusproject.dpes.data.DataAccessOperations;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class UserAccess {
 
 	HashMap tokens; 
-	
-    public Login(){
-	
+
+	public class UserPrivilege {
+		public String userId;
+		public String groupId;
+		public String role;
+	}
+	
+    public UserAccess() throws SQLException{
+    	ResultSet rs = DataAccessOperations.getRoles();
+    	if (!rs.next()) { //if roles table is empty
+    		H2DBUtility.runStatement(DataBaseSchema.getSQLInsertRoles());
+    	}
 	}
 	
 	public String login(String userID, String password){
@@ -70,14 +87,122 @@
 			return "registration error, retry error\n error : "+ e.getMessage();
 		}		
 	}
+	
+	public void unRegisterUser(String userId) {
+		DataAccessOperations.deleteUser(userId);
+	}
 
 	public String logoutUser(String userid) { 
 		// TODO Auto-generated method stub
 		return "success";
 	}	
 	
-	
-	
-         
+	public void setUserAccess(JSONObject user) throws JSONException, SQLException {
+		String userId = user.getString("userId");
+		boolean approved = user.getBoolean("accessApproved");
+		DataAccessOperations.setUserApproval(userId, approved);
+		JSONArray access = user.optJSONArray("access");
+		if (access != null  &&  access.length() > 0) {
+			int count = access.length();
+			List<UserPrivilege> li = new ArrayList<UserPrivilege>();
+			for (int i = 0; i < count; i++) {
+				JSONObject jo = access.getJSONObject(i);
+				UserPrivilege up = new UserPrivilege();
+				up.groupId = jo.getString("groupId");
+				up.userId = jo.getString("userId");
+				up.role = jo.getString("role");
+				li.add(up);
+			}
+			DataAccessOperations.setUserAccess(li);
+		}
+	}
+	
+	public JSONObject getUserAccess(String userId) throws SQLException, JSONException {
+		ResultSet rsUser = DataAccessOperations.getUser(userId);
+		JSONObject jUser = new JSONObject();
+		if (rsUser.next()) {
+			jUser = getUserObject(rsUser);
+			JSONArray jAccess = new JSONArray();
+			ResultSet rsAccess = DataAccessOperations.getUserAccess(userId);
+			while (rsAccess.next()) {
+				JSONObject jo = getUserAccessObject(rsAccess);
+				jAccess.put(jo);
+			}
+			jUser.put("access", jAccess);
+		}
+		return jUser;
+	}
+	
+	public int addSecurityGroup(String groupName, String orgName) throws Exception {
+		ResultSet rs = DataAccessOperations.getSecurityGroup(groupName, orgName);
+		if (rs.next()) {
+			throw new Exception("Group already exists");
+		}
+		int groupId = DataAccessOperations.insertSecurityGroup(groupName, orgName);
+		return groupId;
+	}
+	
+	public void editSecurityGroup(String groupId, String groupName, String orgName) {
+		DataAccessOperations.updateSecurityGroup(groupId, groupName, orgName);
+	}
+	
+	public void deleteSecurityGroup (String groupId) {
+		DataAccessOperations.deleteSecurityGroup(groupId);
+	}
+	
+    public JSONArray getRoles() throws SQLException, JSONException {
+    	JSONArray roles = new JSONArray();
+    	ResultSet rsRoles = DataAccessOperations.getRoles();
+    	while (rsRoles.next()) {
+    		JSONObject role = new JSONObject();
+    		role.put("role", rsRoles.getString("security_role"));
+    		role.put("description", rsRoles.getString("description"));
+    		roles.put(role);
+    	}
+    	return roles;
+    }
     
+    public JSONArray getUsers() throws SQLException, JSONException {
+    	JSONArray users = new JSONArray();
+    	ResultSet rsUsers = DataAccessOperations.getUsers();
+    	String prvUserId = "";
+    	JSONArray privs = new JSONArray();
+    	JSONObject user = new JSONObject();
+    	while (rsUsers.next()) {
+    		String userId = rsUsers.getString("userId");
+    		if (prvUserId != userId) {
+    			if (prvUserId.length() != 0) {
+    				user.put("access", privs);
+    				users.put(user);
+    			}
+    			privs = new JSONArray();
+    			user = getUserObject(rsUsers);
+    		}
+    		if (rsUsers.getString("group_id") != null) {
+        		JSONObject priv = getUserAccessObject(rsUsers);
+        		privs.put(priv);
+    		}
+    		prvUserId = userId;
+    	}
+		user.put("access", privs);
+		users.put(user);
+		return users;
+    }
+    
+    private JSONObject getUserObject(ResultSet rsUser) throws JSONException, SQLException {
+    	JSONObject jUser = new JSONObject();
+		jUser.put("userId", rsUser.getString("userid"));
+		jUser.put("email", rsUser.getString("email"));
+		jUser.put("accessApproved", rsUser.getBoolean("access_approved"));
+		return jUser;
+    }
+    
+    private JSONObject getUserAccessObject(ResultSet rsAccess) throws JSONException, SQLException {
+		JSONObject jo = new JSONObject();
+		jo.put("groupId", rsAccess.getInt("group_id"));
+		jo.put("groupName", rsAccess.getString("security_group"));
+		jo.put("orgName", rsAccess.getString("org_name"));
+		jo.put("role", rsAccess.getInt("role"));
+		return jo;
+    }
 }