import org.slf4j.LoggerFactory;\r
\r
import com.gitblit.Constants.AccessPermission;\r
+import com.gitblit.manager.IRuntimeManager;\r
import com.gitblit.models.TeamModel;\r
import com.gitblit.models.UserModel;\r
import com.gitblit.models.UserRepositoryPreferences;\r
/**\r
* Setup the user service.\r
*\r
- * @param settings\r
- * @since 0.7.0\r
+ * @param runtimeManager\r
+ * @since 1.4.0\r
*/\r
@Override\r
- public void setup(IStoredSettings settings) {\r
+ public void setup(IRuntimeManager runtimeManager) {\r
}\r
\r
/**\r
public void setUserService(IUserService userService) {
logger.info("Setting up user service " + userService.toString());
this.userService = userService;
- this.userService.setup(settings);
+ this.userService.setup(getManager(IRuntimeManager.class));
}
@Override
}\r
\r
@Override\r
- public void setup(IStoredSettings settings) {\r
- IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
+ public void setup(IRuntimeManager runtimeManager) {\r
File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf");\r
serviceImpl = createUserService(realmFile);\r
logger.info("GUS delegating to " + serviceImpl.toString());\r
private final boolean SUPPORT_PLAINTEXT_PWD;
+ private IRuntimeManager runtimeManager;
private IStoredSettings settings;
private File htpasswdFile;
* In addition the setup tries to read and parse the htpasswd file to be used
* for authentication.
*
- * @param settings
- * @since 0.7.0
+ * @param runtimeManager
+ * @since 1.4.0
*/
@Override
- public void setup(IStoredSettings settings)
+ public void setup(IRuntimeManager runtimeManager)
{
- this.settings = settings;
+ this.runtimeManager = runtimeManager;
+ this.settings = runtimeManager.getSettings();
// This is done in two steps in order to avoid calling GitBlit.getFileOrFolder(String, String) which will segfault for unit tests.
- IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
String file = settings.getString(KEY_BACKING_US, DEFAULT_BACKING_US);
File realmFile = runtimeManager.getFileOrFolder(file);
serviceImpl = createUserService(realmFile);
if ( !file.equals(htpasswdFilePath) ) {
// The htpasswd file setting changed. Rediscover the file.
this.htpasswdFilePath = file;
- IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
this.htpasswdFile = runtimeManager.getFileOrFolder(file);
this.htUsers.clear();
this.forceReload = true;
import java.util.Collection;\r
import java.util.List;\r
\r
+import com.gitblit.manager.IRuntimeManager;\r
import com.gitblit.models.TeamModel;\r
import com.gitblit.models.UserModel;\r
\r
* retrieve settings from gitblit.properties or the web.xml file without\r
* relying on the GitBlit static singleton.\r
*\r
- * @param settings\r
- * @since 0.7.0\r
+ * @param runtimeManager\r
+ * @since 1.4.0\r
*/\r
- void setup(IStoredSettings settings);\r
+ void setup(IRuntimeManager runtimeManager);\r
\r
/**\r
* Does the user service support changes to credentials?\r
}\r
\r
@Override\r
- public void setup(IStoredSettings settings) {\r
- this.settings = settings;\r
+ public void setup(IRuntimeManager runtimeManager) {\r
+ this.settings = runtimeManager.getSettings();\r
String file = settings.getString(Keys.realm.ldap.backingUserService, "${baseFolder}/users.conf");\r
- IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
File realmFile = runtimeManager.getFileOrFolder(file);\r
\r
serviceImpl = createUserService(realmFile);\r
}\r
\r
@Override\r
- public void setup(IStoredSettings settings) {\r
- this.settings = settings;\r
+ public void setup(IRuntimeManager runtimeManager) {\r
+ this.settings = runtimeManager.getSettings();\r
\r
String file = settings.getString(Keys.realm.pam.backingUserService, "${baseFolder}/users.conf");\r
- IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
File realmFile = runtimeManager.getFileOrFolder(file);\r
\r
serviceImpl = createUserService(realmFile);\r
}\r
\r
@Override\r
- public void setup(IStoredSettings settings) {\r
- this.settings = settings;\r
+ public void setup(IRuntimeManager runtimeManager) {\r
+ this.settings = runtimeManager.getSettings();\r
\r
String file = settings.getString(Keys.realm.redmine.backingUserService, "${baseFolder}/users.conf");\r
- IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
File realmFile = runtimeManager.getFileOrFolder(file);\r
\r
serviceImpl = createUserService(realmFile);\r
}
@Override
- public void setup(IStoredSettings settings) {
- this.settings = settings;
+ public void setup(IRuntimeManager runtimeManager) {
+ this.settings = runtimeManager.getSettings();
String file = settings.getString(
Keys.realm.salesforce.backingUserService,
"${baseFolder}/users.conf");
- IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
File realmFile = runtimeManager.getFileOrFolder(file);
serviceImpl = createUserService(realmFile);
}\r
\r
@Override\r
- public void setup(IStoredSettings settings) {\r
- this.settings = settings;\r
+ public void setup(IRuntimeManager runtimeManager) {\r
+ this.settings = runtimeManager.getSettings();\r
\r
String file = settings.getString(Keys.realm.windows.backingUserService, "${baseFolder}/users.conf");\r
- IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
File realmFile = runtimeManager.getFileOrFolder(file);\r
\r
serviceImpl = createUserService(realmFile);\r
import com.gitblit.HtpasswdUserService;
import com.gitblit.models.UserModel;
import com.gitblit.tests.mock.MemorySettings;
+import com.gitblit.tests.mock.MockRuntimeManager;
import com.gitblit.utils.StringUtils;
/**
private void setupUS()
{
htpwdUserService = new HtpasswdUserService();
- htpwdUserService.setup(getSettings());
+ htpwdUserService.setup(new MockRuntimeManager(getSettings()));
}
private void setupUS(boolean overrideLA)
{
htpwdUserService = new HtpasswdUserService();
- htpwdUserService.setup(getSettings(overrideLA));
+ htpwdUserService.setup(new MockRuntimeManager(getSettings(overrideLA)));
}
import com.gitblit.LdapUserService;
import com.gitblit.models.UserModel;
import com.gitblit.tests.mock.MemorySettings;
+import com.gitblit.tests.mock.MockRuntimeManager;
import com.gitblit.utils.StringUtils;
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
@Before
public void createLdapUserService() {
ldapUserService = new LdapUserService();
- ldapUserService.setup(getSettings());
+ ldapUserService.setup(new MockRuntimeManager(getSettings()));
}
private MemorySettings getSettings() {
MemorySettings ms = getSettings();
ms.put("realm.ldap.displayName", "${personalTitle}. ${givenName} ${surname}");
ldapUserService = new LdapUserService();
- ldapUserService.setup(ms);
+ ldapUserService.setup(new MockRuntimeManager(ms));
userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
assertNotNull(userOneModel);
MemorySettings ms = getSettings();
ms.put("realm.ldap.email", "${givenName}.${surname}@gitblit.com");
ldapUserService = new LdapUserService();
- ldapUserService.setup(ms);
+ ldapUserService.setup(new MockRuntimeManager(ms));
userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
assertNotNull(userOneModel);
\r
import static org.hamcrest.CoreMatchers.is;\r
\r
-import java.util.HashMap;\r
-\r
import org.junit.Test;\r
\r
import com.gitblit.RedmineUserService;\r
import com.gitblit.models.UserModel;\r
-import com.gitblit.tests.mock.MemorySettings;\r
+import com.gitblit.tests.mock.MockRuntimeManager;\r
import com.gitblit.utils.StringUtils;\r
\r
public class RedmineUserServiceTest extends GitblitUnitTest {\r
@Test\r
public void testAuthenticate() throws Exception {\r
RedmineUserService redmineUserService = new RedmineUserService();\r
- redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
+ redmineUserService.setup(new MockRuntimeManager());\r
redmineUserService.setTestingCurrentUserAsJson(JSON);\r
UserModel userModel = redmineUserService.authenticate("RedmineAdminId", "RedmineAPIKey".toCharArray());\r
assertThat(userModel.getName(), is("redmineadminid"));\r
@Test\r
public void testAuthenticateNotAdminUser() throws Exception {\r
RedmineUserService redmineUserService = new RedmineUserService();\r
- redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
+ redmineUserService.setup(new MockRuntimeManager());\r
redmineUserService.setTestingCurrentUserAsJson(NOT_ADMIN_JSON);\r
UserModel userModel = redmineUserService.authenticate("RedmineUserId", "RedmineAPIKey".toCharArray());\r
assertThat(userModel.getName(), is("redmineuserid"));\r
@Test\r
public void testLocalAccount() {\r
RedmineUserService redmineUserService = new RedmineUserService();\r
- redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
+ redmineUserService.setup(new MockRuntimeManager());\r
\r
UserModel localAccount = new UserModel("bruce");\r
localAccount.displayName = "Bruce Campbell";\r
--- /dev/null
+/*
+ * Copyright 2013 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gitblit.tests.mock;
+
+import java.io.File;
+import java.util.Date;
+import java.util.Map;
+import java.util.TimeZone;
+
+import com.gitblit.Constants;
+import com.gitblit.IStoredSettings;
+import com.gitblit.manager.IRuntimeManager;
+import com.gitblit.models.ServerSettings;
+import com.gitblit.models.ServerStatus;
+import com.gitblit.models.SettingModel;
+
+public class MockRuntimeManager implements IRuntimeManager {
+
+ File baseFolder;
+
+ IStoredSettings settings;
+
+ ServerStatus serverStatus;
+
+ ServerSettings serverSettings;
+
+ public MockRuntimeManager() {
+ this(new MemorySettings());
+ }
+
+ public MockRuntimeManager(Map<String, Object> settings) {
+ this(new MemorySettings(settings));
+ }
+
+ public MockRuntimeManager(IStoredSettings settings) {
+ this.settings = settings;
+
+ this.serverStatus = new ServerStatus(true);
+ this.serverStatus.servletContainer = "MockServer";
+
+ this.serverSettings = new ServerSettings();
+ }
+
+ @Override
+ public void setBaseFolder(File folder) {
+ this.baseFolder = folder;
+ }
+
+ @Override
+ public File getBaseFolder() {
+ return baseFolder;
+ }
+
+ @Override
+ public TimeZone getTimezone() {
+ return TimeZone.getDefault();
+ }
+
+ @Override
+ public boolean isServingRepositories() {
+ return true;
+ }
+
+ @Override
+ public boolean isDebugMode() {
+ return true;
+ }
+
+ @Override
+ public Date getBootDate() {
+ return serverStatus.bootDate;
+ }
+
+ @Override
+ public ServerStatus getStatus() {
+ // update heap memory status
+ serverStatus.heapAllocated = Runtime.getRuntime().totalMemory();
+ serverStatus.heapFree = Runtime.getRuntime().freeMemory();
+ return serverStatus;
+ }
+
+ @Override
+ public ServerSettings getSettingsModel() {
+ // ensure that the current values are updated in the setting models
+ for (String key : settings.getAllKeys(null)) {
+ SettingModel setting = serverSettings.get(key);
+ if (setting == null) {
+ // unreferenced setting, create a setting model
+ setting = new SettingModel();
+ setting.name = key;
+ serverSettings.add(setting);
+ }
+ setting.currentValue = settings.getString(key, "");
+ }
+ return serverSettings;
+ }
+
+ @Override
+ public File getFileOrFolder(String key, String defaultFileOrFolder) {
+ String fileOrFolder = settings.getString(key, defaultFileOrFolder);
+ return getFileOrFolder(fileOrFolder);
+ }
+
+ @Override
+ public File getFileOrFolder(String fileOrFolder) {
+ return com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$,
+ baseFolder, fileOrFolder);
+ }
+
+ @Override
+ public IStoredSettings getSettings() {
+ return settings;
+ }
+
+ @Override
+ public boolean updateSettings(Map<String, String> updatedSettings) {
+ return settings.saveSettings(updatedSettings);
+ }
+
+}