Пример #1
0
  /**
   * This test doesn't really test anything, and it should ALWAYS be the last method in this class.
   * <br>
   * <br>
   * This method just resets the current user's locale so that when things are run in batches all
   * tests still work.
   */
  @Test
  public void should_resetTheLocale() {
    // set user locale to nothing
    Context.setLocale(null);

    // clear out the caches
    GlobalProperty defaultLocale =
        new GlobalProperty(
            OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "", "blanking out default locale");
    Context.getAdministrationService().saveGlobalProperty(defaultLocale);
  }
Пример #2
0
  /** @see {@link LocaleUtility#getLocalesInOrder()} */
  @Test
  @Verifies(
      value = "should have default locale as the first element if user has no preferred locale",
      method = "getLocalesInOrder()")
  public void getLocalesInOrder_shouldHaveDefaultLocaleAsTheFirstElementIfUserHasNoPreferredLocale()
      throws Exception {
    // make sure the user doesn't have a locale
    Context.setLocale(null);

    Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder();
    Assert.assertEquals(LocaleUtility.getDefaultLocale(), localesInOrder.iterator().next());
  }
Пример #3
0
 /** @see {@link LocaleUtility#getLocalesInOrder()} */
 @Test
 @Verifies(
     value = "should have default locale as the second element if user has a preferred locale",
     method = "getLocalesInOrder()")
 public void getLocalesInOrder_shouldHaveDefaultLocaleAsTheSecondElementIfUserHasAPreferredLocale()
     throws Exception {
   Locale lu_UG = new Locale("lu", "UG");
   Context.setLocale(lu_UG);
   Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder();
   Iterator<Locale> it = localesInOrder.iterator();
   Assert.assertEquals(lu_UG, it.next());
   Assert.assertEquals(LocaleUtility.getDefaultLocale(), it.next());
 }
Пример #4
0
 /** @see {@link LocaleUtility#getLocalesInOrder()} */
 @Test
 @Verifies(
     value = "should return a set of locales with no duplicates",
     method = "getLocalesInOrder()")
 public void getLocalesInOrder_shouldReturnASetOfLocalesWithNoDuplicates() throws Exception {
   GlobalProperty gp =
       new GlobalProperty(
           OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST,
           "lu_UG, lu, sw_KE, en_US, en, en, sw_KE",
           "Test Allowed list of locales");
   Context.getAdministrationService().saveGlobalProperty(gp);
   GlobalProperty defaultLocale =
       new GlobalProperty(
           OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "lu", "Test Allowed list of locales");
   Context.getAdministrationService().saveGlobalProperty(defaultLocale);
   Locale lu_UG = new Locale("lu", "UG");
   Context.setLocale(lu_UG);
   // note that unique list of locales should be lu_UG, lu, sw_KE, en_US, en
   Assert.assertEquals(6, LocaleUtility.getLocalesInOrder().size());
 }
Пример #5
0
 /** @see {@link LocaleUtility#getLocalesInOrder()} */
 @Test
 @Verifies(
     value = "should return a set of locales with a predictable order",
     method = "getLocalesInOrder()")
 public void getLocalesInOrder_shouldReturnASetOfLocalesWithAPredictableOrder() throws Exception {
   GlobalProperty gp =
       new GlobalProperty(
           OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST,
           "lu, sw_KE, en_US, en",
           "Test Allowed list of locales");
   Context.getAdministrationService().saveGlobalProperty(gp);
   Locale lu_UG = new Locale("lu", "UG");
   Context.setLocale(lu_UG);
   Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder();
   Iterator<Locale> it = localesInOrder.iterator();
   Assert.assertEquals(new Locale("lu", "UG"), it.next());
   Assert.assertEquals(LocaleUtility.getDefaultLocale(), it.next());
   Assert.assertEquals(new Locale("lu"), it.next());
   Assert.assertEquals(new Locale("sw", "KE"), it.next());
   Assert.assertEquals(new Locale("en", "US"), it.next());
   Assert.assertEquals(new Locale("en"), it.next());
 }
  @RequestMapping(method = RequestMethod.POST)
  public String loginUser(
      ModelMap model,
      HttpSession session,
      HttpServletRequest request,
      HttpServletResponse response) {

    String redirect = null;

    try {
      String username = request.getParameter("uname");
      String password = request.getParameter("pw");

      // get the place to redirect: for touch screen this is simple
      redirect = determineRedirect(request);

      // only try to authenticate if they actually typed in a username
      if (username != null && username.length() > 0) {

        Context.authenticate(username, password);

        if (Context.isAuthenticated()) {

          User user = Context.getAuthenticatedUser();

          if (user.getUserProperties() != null
              && !user.getUserProperties().containsKey("keyboardType")) {
            user.getUserProperties().put("keyboardType", "QWERTY"); // ABC is the other option
            user = Context.getUserService().saveUser(user, null);
          }
          session.setAttribute("keyboardType", user.getUserProperty("keyboardType"));

          // load the user's default locale if possible
          if (user.getUserProperties() != null) {
            if (user.getUserProperties()
                .containsKey(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE)) {
              String localeString =
                  user.getUserProperty(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE);
              Locale locale = null;
              if (localeString.length() == 5) {
                // user's locale is language_COUNTRY (i.e. en_US)
                String lang = localeString.substring(0, 2);
                String country = localeString.substring(3, 5);
                locale = new Locale(lang, country);
              } else {
                // user's locale is only the language (language plus greater than 2 char country
                // code
                locale = new Locale(localeString);
              }
              OpenmrsCookieLocaleResolver oclr = new OpenmrsCookieLocaleResolver();
              oclr.setLocale(request, response, locale);
            }
          }

          // In case the user has no preferences, make sure that the context has some locale set
          if (Context.getLocale() == null) {
            Context.setLocale(OpenmrsConstants.GLOBAL_DEFAULT_LOCALE);
          }
        }

        if (log.isDebugEnabled()) {
          log.debug("Redirecting after login to: " + redirect);
          log.debug("Locale address: " + request.getLocalAddr());
        }

        response.sendRedirect(redirect);

        // return redirect;
      }
    } catch (ContextAuthenticationException e) {
      // set the error message for the user telling them
      // to try again
      // session.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "auth.password.invalid");
      log.error("failed to authenticate: ", e);
    } catch (Exception e) {
      log.error("Uexpected auth error", e);
    }

    // send the user back the login page because they either
    // had a bad password or are locked out

    session.setAttribute(
        WebConstants.OPENMRS_MSG_ATTR,
        Context.getMessageSourceService().getMessage("rwandaprimarycare.loginFailed"));
    session.setAttribute(WebConstants.OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR, redirect);

    return "/module/rwandaprimarycare/login";
  }