changeset 30:ab2fae5c5156

new: check our Chinese Local Gazetteers dataverse for login
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Fri, 11 Sep 2015 15:54:01 +0200
parents aa9b50ad64e6
children c8d367a4bbcd
files src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java src/main/java/de/mpiwg/web/jsp/SessionBean.java src/main/resources/config.properties
diffstat 3 files changed, 58 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java	Fri Sep 11 10:34:25 2015 +0200
+++ b/src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java	Fri Sep 11 15:54:01 2015 +0200
@@ -518,6 +518,7 @@
 	public static boolean isUserInGroups(String userName, List<String> userGroups) throws IOException {
 		/* Get group members for each group in userGroup list, then check if user in any of them
 		 * get group members in a group by dataverse Api: GET http://$server/api/dataverses/$id/groups?key=$apiToken
+		 * 								$id could be also an alias (string) in the input query above 
 		 */
 		
 		String apiToken = PropertiesUtils.getPropValue("dvn_apitoken");
@@ -555,6 +556,46 @@
 		return false;
 	}
 
+	public static boolean isUserInLG(String userName, String password) throws IOException {
+		String localgazetteersDvId = PropertiesUtils.getPropValue("localgazetteers_dvId");
+		
+		
+		String query = PropertiesUtils.getPropValue("dvn_server") + "/getRoleAssignments?user=" + userName + "&password=" + password + "&datasetId=" + localgazetteersDvId;
+			
+		HttpStringResponse response = null;
+		try {
+			response = HTTPUtils.getStringResponse(query);
+			JSONObject json = new JSONObject(response.content);
+			
+			if(StringUtils.equals(json.getString("state"), "ok")){
+				
+				JSONArray jsonRoleAssignees = json.getJSONArray("roleAssignments");
+				for (int i = 0; i < jsonRoleAssignees.length(); i ++) {
+					JSONObject aUser = jsonRoleAssignees.getJSONObject(i);
+					String aName = aUser.getString("identifier");
+					String aRoleName = aUser.getString("roleName");
+					
+					if (aName.compareTo("@" + userName) == 0 && 
+							( aRoleName.compareTo("Admin")==0 || aRoleName.compareTo("Curator")==0 || aRoleName.compareTo("Contributor")==0 )) {
+						
+						System.out.println("user "+ aName +" has role");
+						return true;	
+					}
+				}
+			
+			}
+		} catch (Exception e) {
+			if(response != null){
+				System.err.println(response.content);
+			}
+			e.printStackTrace();
+		}
+		
+		
+		
+		return false;
+	}
+
 
 
 }
--- a/src/main/java/de/mpiwg/web/jsp/SessionBean.java	Fri Sep 11 10:34:25 2015 +0200
+++ b/src/main/java/de/mpiwg/web/jsp/SessionBean.java	Fri Sep 11 15:54:01 2015 +0200
@@ -78,26 +78,24 @@
 			
 			if(this.user != null){
 				
-				// check userGroups
 				List<String> theUserGroups = new ArrayList<String>();
 				theUserGroups.add("mpiwg");
 				theUserGroups.add("nuist");
 				
-				// test
-				theUserGroups.add("mygroup2");
-				
-				if (DataverseUtils.isUserInGroups(this.user.getUserName(), theUserGroups)){
-					
+				// check if user has role in the "Chinese Local Gazetteers" Dataverse, rather than the following hardcoded checking
+				// use dvn/getRoleAssignments?user=&password=&datasetId=
+				if (DataverseUtils.isUserInLG(userName, password) || DataverseUtils.isUserInGroups(this.user.getUserName(), theUserGroups)){
 					this.user.setPassword(this.password);
 					this.homePage.loadParameters(request, response);
 					this.homePage.reloadBranches();	
-
+					
 				} else {					
 					this.user = null;
 					
 					addMsg("Please contact/email us to activate your account!");
 				}
 				
+				
 				// -----
 			}else{
 				addMsg("User account not found, or username and password do not match!");
--- a/src/main/resources/config.properties	Fri Sep 11 10:34:25 2015 +0200
+++ b/src/main/resources/config.properties	Fri Sep 11 15:54:01 2015 +0200
@@ -1,9 +1,9 @@
-db_gazetter_username=root
-db_gazetter_password=root
 db_gazetter_name=Gazetteer
 files_root=/gazetteer-server/data
 
 #Local
+db_gazetter_username=root
+db_gazetter_password=root
 #dvn_server=http://localhost/dvn
 dvn_server=http://localhost:8082
 dvn_apitoken=9dd1f749-8c42-49ab-a2ba-fbb963c2ff90
@@ -11,10 +11,12 @@
 root_server=http://localhost:8080/LGServices
 toc_interface=http://localhost:1080/localgazetteers-dev/LGToc
 extraction_interface=http://localhost:1080/localgazetteers-dev/extraction-interface
-
+localgazetteers_dvId=185
 
 # Production one:
 #localgazetteers-dev -> localgazetteers
+#db_gazetter_username=root
+#db_gazetter_password=admin
 #dvn_server=http://localgazetteers.mpiwg-berlin.mpg.de/dvn
 #root_server=http://localgazetteers.mpiwg-berlin.mpg.de/LGServices
 #toc_interface=http://localgazetteers.mpiwg-berlin.mpg.de/LGToc
@@ -22,7 +24,13 @@
 
 # Development one:
 #localgazetteers -> localgazetteers-test
+#db_gazetter_username=root
+#db_gazetter_password=admin
 #dvn_server=http://localgazetteers-test.mpiwg-berlin.mpg.de/dvn
+#dvn_server=http://localgazetteers-test.mpiwg-berlin.mpg.de:8081
+#dvn_apitoken=
+#dv_id=1
 #root_server=http://localgazetteers-test.mpiwg-berlin.mpg.de/LGServices
 #toc_interface=http://localgazetteers-test.mpiwg-berlin.mpg.de/LGToc
-#extraction_interface=http://localgazetteers-test.mpiwg-berlin.mpg.de/extraction-interface
\ No newline at end of file
+#extraction_interface=http://localgazetteers-test.mpiwg-berlin.mpg.de/extraction-interface
+#localgazetteers_dvId=
\ No newline at end of file