@Override @Cacheable(value = "userSearchLocales") public List<Locale> getSearchLocales(Locale currentLocale, User user) throws APIException { Set<Locale> locales = new LinkedHashSet<>(); locales.add(currentLocale); //the currently used full locale locales.add(new Locale(currentLocale.getLanguage())); if (user != null) { List<Locale> proficientLocales = user.getProficientLocales(); if (proficientLocales != null) { locales.addAll(proficientLocales); } } //limit locales to only allowed locales List<Locale> allowedLocales = Context.getAdministrationService().getAllowedLocales(); if (allowedLocales != null) { Set<Locale> retainLocales = new HashSet<>(); for (Locale allowedLocale : allowedLocales) { retainLocales.add(allowedLocale); retainLocales.add(new Locale(allowedLocale.getLanguage())); } locales.retainAll(retainLocales); } return new ArrayList<>(locales); }
locales.add(getDefaultLocale()); if (localesAllowedListCache == null) { localesAllowedListCache = Context.getAdministrationService().getAllowedLocales();
@Test public void getAllowedLocales_shouldReturnAtLeastOneLocaleIfNoLocalesDefinedInDatabaseYet() { assertTrue(adminService.getAllowedLocales().size() > 0); }
@Test public void getAllowedLocales_shouldNotFailIfNotGlobalPropertyForLocalesAllowedDefinedYet() { adminService.purgeGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST)); adminService.getAllowedLocales(); }
@Test public void getAllowedLocales_shouldNotReturnDuplicatesEvenIfTheGlobalPropertyHasThem() { adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB,fr,es,en_GB")); assertEquals(3, adminService.getAllowedLocales().size()); }
@RequestMapping(method = RequestMethod.POST) @ResponseBody @ResponseStatus(value = HttpStatus.OK) public void post(@RequestBody Map<String, String> body) { String localeStr = body.get("locale"); Locale locale = null; try { locale = LocaleUtils.toLocale(localeStr); } catch (IllegalArgumentException e) { throw new APIException(" '" + localeStr + "' does not represent a valid locale."); } Set<Locale> allowedLocales = new HashSet<Locale>(Context.getAdministrationService().getAllowedLocales()); if (allowedLocales.contains(locale)) { Context.setLocale(locale); } else { throw new APIException(" '" + localeStr + "' is not in the list of allowed locales."); } }
@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()); }
/** * 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; }