/** * @see org.openmrs.notification.AlertService#getAlertsByUser(org.openmrs.User) */ @Override @Transactional(readOnly = true) public List<Alert> getAlertsByUser(User user) throws APIException { log.debug("Getting unread alerts for user " + user); if (user == null) { if (Context.isAuthenticated()) { user = Context.getAuthenticatedUser(); } else { user = new User(); } } return Context.getAlertService().getAlerts(user, false, false); }
/** * Make sure the "@Before" method on {@link BaseContextSensitiveTest} is not authenticating the * user when told to skip * * @throws Exception */ @Test @SkipBaseSetup public void shouldSkipAuthentication() { // this depends on Context.logout() being in the previous test method Assert.assertFalse(Context.isAuthenticated()); }
} else if (attributes.hasAuthorizedAnnotation(method) && !Context.isAuthenticated()) { throwUnauthorized(Context.getAuthenticatedUser(), method);
/** * Check if user is authenticated * * @return true/false whether user is authenticated */ public boolean checkIfLoggedIn() { return Context.isAuthenticated(); }
/** * Make sure the "@Before" method on {@link BaseContextSensitiveTest} is authenticating the user * * @throws Exception */ @Test public void shouldCallBaseSetupMethod() { // make sure we're authenticated Assert.assertTrue(Context.isAuthenticated()); // make sure we have some data from the EXAMPLE_XML_DATASET_PACKAGE_PATH Assert.assertTrue(Context.getEncounterService().getAllEncounterTypes().size() > 0); // make sure we have the data from the INITIAL_DATA_SET_XML_FILENAME Context.authenticate("admin", "test"); // this is put here for the next test method to check that authentication is // not happening when told not to Context.logout(); }
if (Context.isAuthenticated() && Context.getAuthenticatedUser().equals(authenticatedUser)) { return; while (!Context.isAuthenticated() && attempts < 3) {
@Test public void saveUser_shouldFailToCreateTheUserWithAWeakPassword() { assertTrue("The context needs to be correctly authenticated to by a user", Context.isAuthenticated()); UserService us = userService; User u = new User(); u.setPerson(new Person()); u.addName(new PersonName("Benjamin", "A", "Wolfe")); u.setUsername("bwolfe"); u.getPerson().setGender("M"); expectedException.expect(PasswordException.class); us.createUser(u, "short"); }
/** * @should return unauthorized if not logged in * @should return forbidden if logged in */ @ExceptionHandler(APIAuthenticationException.class) @ResponseBody public SimpleObject apiAuthenticationExceptionHandler(Exception ex, HttpServletRequest request, HttpServletResponse response) throws Exception { int errorCode; String errorDetail; if (Context.isAuthenticated()) { // user is logged in but doesn't have the relevant privilege -> 403 FORBIDDEN errorCode = HttpServletResponse.SC_FORBIDDEN; errorDetail = "User is logged in but doesn't have the relevant privilege"; } else { // user is not logged in -> 401 UNAUTHORIZED errorCode = HttpServletResponse.SC_UNAUTHORIZED; errorDetail = "User is not logged in"; if (shouldAddWWWAuthHeader(request)) { response.addHeader("WWW-Authenticate", "Basic realm=\"OpenMRS at " + RestConstants.URI_PREFIX + "\""); } } response.setStatus(errorCode); return RestUtil.wrapErrorResponse(ex, errorDetail); }
@RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) public void changeOwnPassword(@RequestBody Map<String, String> body) { String oldPassword = body.get("oldPassword"); String newPassword = body.get("newPassword"); if (!Context.isAuthenticated()) { throw new APIAuthenticationException("Must be authenticated to change your own password"); } try { userService.changePassword(oldPassword, newPassword); } catch (APIException ex) { // this happens if they give the wrong oldPassword throw new ValidationException(ex.getMessage()); } }
@Test public void createUser_shouldCreateNewUserWithBasicElements() { assertTrue("The context needs to be correctly authenticated to by a user", Context.isAuthenticated()); User u = new User(); u.setPerson(new Person()); u.addName(new PersonName("Benjamin", "A", "Wolfe")); u.setUsername("bwolfe"); u.getPerson().setGender("M"); User createdUser = userService.createUser(u, "Openmr5xy"); // if we're returning the object from create methods, check validity assertTrue("The user returned by the create user method should equal the passed in user", createdUser.equals(u)); createdUser = userService.getUserByUsername("bwolfe"); assertTrue("The created user should equal the passed in user", createdUser.equals(u)); }
assertEquals(expectedSize + 1, patientEncounters.size()); if (Context.isAuthenticated()) { Context.logout();
assertTrue("The context needs to be correctly authenticated to by a user", Context.isAuthenticated());
if (!Context.isAuthenticated()) { String basicAuth = httpRequest.getHeader("Authorization"); if (basicAuth != null) {
/** * @see SessionController1_8#delete() * @verifies log the client out */ @Test public void delete_shouldLogTheClientOut() throws Exception { Assert.assertTrue(Context.isAuthenticated()); controller.delete(); Assert.assertFalse(Context.isAuthenticated()); }
@Test public void get_shouldReturnLocaleInfoIfTheUserIsAuthenticated() throws Exception { Assert.assertTrue(Context.isAuthenticated()); Object ret = controller.get(request); Assert.assertEquals(Context.getLocale(), PropertyUtils.getProperty(ret, "locale")); Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(), ((List<Locale>) PropertyUtils.getProperty(ret, "allowedLocales")).toArray()); }
/** * @see SessionController1_8#get(WebRequest) * @verifies return the session id if the user is not authenticated */ @Test public void get_shouldReturnTheSessionIdIfTheUserIsNotAuthenticated() throws Exception { Context.logout(); Assert.assertFalse(Context.isAuthenticated()); Object ret = controller.get(request); Assert.assertEquals(SESSION_ID, PropertyUtils.getProperty(ret, "sessionId")); Assert.assertEquals(false, PropertyUtils.getProperty(ret, "authenticated")); }
/** * @see SessionController1_8#get(WebRequest) * @verifies return the session id if the user is authenticated */ @Test public void get_shouldReturnTheSessionIdAndUserIfTheUserIsAuthenticated() throws Exception { Assert.assertTrue(Context.isAuthenticated()); Object ret = controller.get(request); Object userProp = PropertyUtils.getProperty(ret, "user"); Assert.assertEquals(SESSION_ID, PropertyUtils.getProperty(ret, "sessionId")); Assert.assertEquals(true, PropertyUtils.getProperty(ret, "authenticated")); Assert.assertEquals(Context.getAuthenticatedUser().getUuid(), PropertyUtils.getProperty(userProp, "uuid")); Object personProp = PropertyUtils.getProperty(userProp, "person"); Assert.assertEquals(Context.getAuthenticatedUser().getPerson().getUuid(), PropertyUtils.getProperty(personProp, "uuid")); }
/** * Tells the user their sessionId, and whether or not they are authenticated. * * @param request * @return * @should return the session id if the user is authenticated * @should return the session id if the user is not authenticated */ @RequestMapping(method = RequestMethod.GET) @ResponseBody public Object get(WebRequest request) { boolean authenticated = Context.isAuthenticated(); SimpleObject session = new SimpleObject(); session.add("sessionId", request.getSessionId()).add("authenticated", authenticated); if (authenticated) { String repParam = request.getParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION); Representation rep = (repParam != null) ? restService.getRepresentation(repParam) : Representation.DEFAULT; session.add("user", ConversionUtil.convertToRepresentation(Context.getAuthenticatedUser(), rep)); session.add("locale", Context.getLocale()); session.add("allowedLocales", Context.getAdministrationService().getAllowedLocales()); } return session; }