RedmineCurrent current = new Gson().fromJson(jsonString, RedmineCurrent.class);\r
String login = current.user.login;\r
\r
- if (username.equalsIgnoreCase(login)) {\r
- UserModel userModel = new UserModel(login);\r
- userModel.displayName = current.user.firstname + " " + current.user.lastname;\r
- userModel.emailAddress = current.user.mail;\r
- userModel.canAdmin = true;\r
- userModel.cookie = StringUtils.getSHA1(userModel.username + new String(password));\r
- return userModel;\r
+ boolean canAdmin = true;\r
+ // non admin user can not get login name\r
+ if (StringUtils.isEmpty(login)) {\r
+ canAdmin = false;\r
+ login = current.user.mail;\r
}\r
\r
+ UserModel userModel = new UserModel(login);\r
+ userModel.canAdmin = canAdmin;\r
+ userModel.displayName = current.user.firstname + " " + current.user.lastname;\r
+ userModel.emailAddress = current.user.mail;\r
+ userModel.cookie = StringUtils.getSHA1(userModel.username + new String(password));\r
+\r
+ return userModel;\r
} catch (IOException e) {\r
logger.error("authenticate", e);\r
}\r
+ "\"last_login_on\":\"2012-09-06T23:59:26Z\",\"firstname\":\"baz\","\r
+ "\"id\":4,\"login\":\"RedmineUserId\",\"mail\":\"baz@example.com\"}}";\r
\r
+ private static final String NOT_ADMIN_JSON = "{\"user\":{\"lastname\":\"foo\","\r
+ + "\"last_login_on\":\"2012-09-08T13:59:01Z\",\"created_on\":\"2009-03-17T14:25:50Z\","\r
+ + "\"mail\":\"baz@example.com\",\"id\":5,\"firstname\":\"baz\"}}";\r
+\r
@Test\r
public void testAuthenticate() throws Exception {\r
RedmineUserService redmineUserService = new RedmineUserService();\r
assertThat(userModel.getDisplayName(), is("baz foo"));\r
assertThat(userModel.emailAddress, is("baz@example.com"));\r
assertNotNull(userModel.cookie);\r
+ assertThat(userModel.canAdmin, is(true));\r
}\r
\r
@Test\r
- public void testAuthenticateWithWronId() throws Exception {\r
+ public void testAuthenticateNotAdminUser() throws Exception {\r
RedmineUserService redmineUserService = new RedmineUserService();\r
redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
- redmineUserService.setTestingCurrentUserAsJson(JSON);\r
- UserModel userModel = redmineUserService.authenticate("WrongRedmineUserId", "RedmineAPIKey".toCharArray());\r
- assertNull(userModel);\r
+ redmineUserService.setTestingCurrentUserAsJson(NOT_ADMIN_JSON);\r
+ UserModel userModel = redmineUserService.authenticate("RedmineUserId", "RedmineAPIKey".toCharArray());\r
+ assertThat(userModel.getName(), is("baz@example.com"));\r
+ assertThat(userModel.getDisplayName(), is("baz foo"));\r
+ assertThat(userModel.emailAddress, is("baz@example.com"));\r
+ assertNotNull(userModel.cookie);\r
+ assertThat(userModel.canAdmin, is(false));\r
}\r
\r
}\r