UserSettingService implements IAnyUserSettingService, IUserSettingService { public IUserSetting getUserSetting( String settingName, String defaultValue ) { boolean hasAuth = PentahoSessionHolder.getSession().getAttribute( "SPRING_SECURITY_CONTEXT" ) != null; if ( hasAuth ) { try { String homePath = ClientRepositoryPaths.getUserHomeFolderPath( PentahoSessionHolder.getSession().getName() ); Serializable userHomeId = repository.getFile( homePath ).getId(); Map<String, Serializable> userMetadata = repository.getFileMetadata( userHomeId ); for ( Map.Entry<String, Serializable> entry : userMetadata.entrySet() ) { String key = entry.getKey(); if ( key.startsWith( SETTING_PREFIX ) ) { String settingFromKey = key.substring( SETTING_PREFIX.length() ); if ( settingFromKey.equals( settingName ) ) { return createSetting( settingFromKey, entry.getValue() ); } } } String tentantHomePath = ClientRepositoryPaths.getEtcFolderPath(); Serializable tenantHomeId = repository.getFile( tentantHomePath ).getId(); Map<String, Serializable> tenantMetadata = repository.getFileMetadata( tenantHomeId ); for ( Map.Entry<String, Serializable> entry : tenantMetadata.entrySet() ) { String key = entry.getKey(); if ( key.startsWith( SETTING_PREFIX ) ) { String settingFromKey = key.substring( SETTING_PREFIX.length() ); if ( settingFromKey.equals( settingName ) ) { return createSetting( settingFromKey, entry.getValue() ); } } } } catch ( Throwable ignored ) { } } return createSetting( settingName, defaultValue ); } UserSettingService( IUnifiedRepository repository ); void init( IPentahoSession session ); void deleteUserSettings(); List<IUserSetting> getUserSettings(); IUserSetting getUserSetting( String settingName, String defaultValue ); void setUserSetting( String settingName, String settingValue ); @Override void deleteUserSettings( String username ); @Override List<IUserSetting> getUserSettings( String username ); @Override IUserSetting getUserSetting( String username, String settingName, String defaultValue ); @Override void setUserSetting( String username, String settingName, String settingValue ); IUserSetting getGlobalUserSetting( String settingName, String defaultValue ); List<IUserSetting> getGlobalUserSettings(); void setGlobalUserSetting( String settingName, String settingValue ); static final String SETTING_PREFIX; }
@Test public void testGetUserSettingByName() throws Exception { final String settingName = USER_SETTING_NAME_3; final String defaultValue = "defaultValue"; IAuthorizationPolicy policy = mock( IAuthorizationPolicy.class ); when( policy.isAllowed( anyString() ) ).thenReturn( true ); PentahoSystem.registerObject( policy ); final IUserSetting userSetting = userSettingService.getUserSetting( "test", settingName, defaultValue ); assertEquals( settingName, userSetting.getSettingName() ); assertEquals( USER_SETTING_VALUE_3, userSetting.getSettingValue() ); }
@Test public void testGetUserSettingNoAuth() throws Exception { final String settingName = "settingName"; final String defaultValue = "defaultValue"; when( session.getAttribute( eq( "SPRING_SECURITY_CONTEXT" ) ) ).thenReturn( null ); final IUserSetting userSetting = userSettingService.getUserSetting( settingName, defaultValue ); assertEquals( settingName, userSetting.getSettingName() ); assertEquals( defaultValue, userSetting.getSettingValue() ); }
@Test public void testGetUserSettingUnset() throws Exception { final String settingName = "settingName"; final String defaultValue = "defaultValue"; when( session.getAttribute( eq( "SPRING_SECURITY_CONTEXT" ) ) ).thenReturn( 1 ); final IUserSetting userSetting = userSettingService.getUserSetting( settingName, defaultValue ); assertEquals( settingName, userSetting.getSettingName() ); assertEquals( defaultValue, userSetting.getSettingValue() ); }
@Test public void testGetUserSetting() throws Exception { final String settingName = USER_SETTING_NAME_3; final String defaultValue = "defaultValue"; when( session.getAttribute( eq( "SPRING_SECURITY_CONTEXT" ) ) ).thenReturn( 1 ); final IUserSetting userSetting = userSettingService.getUserSetting( settingName, defaultValue ); assertEquals( settingName, userSetting.getSettingName() ); assertEquals( USER_SETTING_VALUE_3, userSetting.getSettingValue() ); }
@Test public void testGetUserSettingOnlyGlobalExist() throws Exception { final String settingName = GLOBAL_SETTING_NAME_3; final String defaultValue = "defaultValue"; when( session.getAttribute( eq( "SPRING_SECURITY_CONTEXT" ) ) ).thenReturn( 1 ); final IUserSetting userSetting = userSettingService.getUserSetting( settingName, defaultValue ); assertEquals( settingName, userSetting.getSettingName() ); assertEquals( GLOBAL_SETTING_VALUE_3, userSetting.getSettingValue() ); }