Browse Source

Remove obsolete and deprecated FileUserService

Change-Id: I92d1d742e286643e1e1ab47a410b3fda146d1741
tags/v1.4.0
James Moger 10 years ago
parent
commit
578319a659

+ 0
- 1146
src/main/java/com/gitblit/FileUserService.java
File diff suppressed because it is too large
View File


+ 16
- 36
src/main/java/com/gitblit/GitblitUserService.java View File

/** /**
* This class wraps the default user service and is recommended as the starting * This class wraps the default user service and is recommended as the starting
* point for custom user service implementations. * point for custom user service implementations.
*
*
* This does seem a little convoluted, but the idea is to allow IUserService to * This does seem a little convoluted, but the idea is to allow IUserService to
* evolve with new methods and implementations without breaking custom * evolve with new methods and implementations without breaking custom
* authentication implementations. * authentication implementations.
*
*
* The most common implementation of a custom IUserService is to only override * The most common implementation of a custom IUserService is to only override
* authentication and then delegate all other functionality to one of Gitblit's * authentication and then delegate all other functionality to one of Gitblit's
* user services. This class optimizes that use-case. * user services. This class optimizes that use-case.
*
*
* Extending GitblitUserService allows for authentication customization without * Extending GitblitUserService allows for authentication customization without
* having to keep-up-with IUSerService API changes. * having to keep-up-with IUSerService API changes.
*
*
* @author James Moger * @author James Moger
*
*
*/ */
public class GitblitUserService implements IUserService { public class GitblitUserService implements IUserService {
protected IUserService serviceImpl; protected IUserService serviceImpl;
private final Logger logger = LoggerFactory.getLogger(GitblitUserService.class); private final Logger logger = LoggerFactory.getLogger(GitblitUserService.class);
public GitblitUserService() { public GitblitUserService() {
logger.info("GUS delegating to " + serviceImpl.toString()); logger.info("GUS delegating to " + serviceImpl.toString());
} }
@SuppressWarnings("deprecation")
protected IUserService createUserService(File realmFile) { protected IUserService createUserService(File realmFile) {
IUserService service = null; IUserService service = null;
if (realmFile.getName().toLowerCase().endsWith(".properties")) {
// v0.5.0 - v0.7.0 properties-based realm file
service = new FileUserService(realmFile);
} else if (realmFile.getName().toLowerCase().endsWith(".conf")) {
if (realmFile.getName().toLowerCase().endsWith(".conf")) {
// v0.8.0+ config-based realm file // v0.8.0+ config-based realm file
service = new ConfigUserService(realmFile); service = new ConfigUserService(realmFile);
} }
service.updateUserModel(admin); service.updateUserModel(admin);
} }
if (service instanceof FileUserService) {
// automatically create a users.conf realm file from the original
// users.properties file
File usersConfig = new File(realmFile.getParentFile(), "users.conf");
if (!usersConfig.exists()) {
logger.info(MessageFormat.format("Automatically creating {0} based on {1}",
usersConfig.getAbsolutePath(), realmFile.getAbsolutePath()));
ConfigUserService configService = new ConfigUserService(usersConfig);
for (String username : service.getAllUsernames()) {
UserModel userModel = service.getUserModel(username);
configService.updateUserModel(userModel);
}
}
// issue suggestion about switching to users.conf
logger.warn("Please consider using \"users.conf\" instead of the deprecated \"users.properties\" file");
}
return service; return service;
} }
@Override @Override
public String toString() { public String toString() {
return getClass().getSimpleName(); return getClass().getSimpleName();
setAccountType(user); setAccountType(user);
return user; return user;
} }
@Override @Override
public void logout(UserModel user) { public void logout(UserModel user) {
serviceImpl.logout(user); serviceImpl.logout(user);
if (!model.isLocalAccount() && !supportsTeamMembershipChanges()) { if (!model.isLocalAccount() && !supportsTeamMembershipChanges()) {
// teams are externally controlled - copy from original model // teams are externally controlled - copy from original model
UserModel existingModel = getUserModel(username); UserModel existingModel = getUserModel(username);
model = DeepCopier.copy(model); model = DeepCopier.copy(model);
model.teams.clear(); model.teams.clear();
model.teams.addAll(existingModel.teams); model.teams.addAll(existingModel.teams);
if (!model.isLocalAccount() && !supportsTeamMembershipChanges()) { if (!model.isLocalAccount() && !supportsTeamMembershipChanges()) {
// teams are externally controlled- copy from original model // teams are externally controlled- copy from original model
UserModel existingModel = getUserModel(username); UserModel existingModel = getUserModel(username);
model = DeepCopier.copy(model); model = DeepCopier.copy(model);
model.teams.clear(); model.teams.clear();
model.teams.addAll(existingModel.teams); model.teams.addAll(existingModel.teams);
for (UserModel user : users) { for (UserModel user : users) {
setAccountType(user); setAccountType(user);
} }
return users;
return users;
} }
@Override @Override
if (!supportsTeamMembershipChanges()) { if (!supportsTeamMembershipChanges()) {
// teams are externally controlled - copy from original model // teams are externally controlled - copy from original model
TeamModel existingModel = getTeamModel(teamname); TeamModel existingModel = getTeamModel(teamname);
model = DeepCopier.copy(model); model = DeepCopier.copy(model);
model.users.clear(); model.users.clear();
model.users.addAll(existingModel.users); model.users.addAll(existingModel.users);
public boolean deleteRepositoryRole(String role) { public boolean deleteRepositoryRole(String role) {
return serviceImpl.deleteRepositoryRole(role); return serviceImpl.deleteRepositoryRole(role);
} }
protected boolean isLocalAccount(String username) { protected boolean isLocalAccount(String username) {
UserModel user = getUserModel(username); UserModel user = getUserModel(username);
return user != null && user.isLocalAccount(); return user != null && user.isLocalAccount();
} }
protected void setAccountType(UserModel user) { protected void setAccountType(UserModel user) {
if (user != null) { if (user != null) {
if (!StringUtils.isEmpty(user.password) if (!StringUtils.isEmpty(user.password)
} }
} }
} }
protected AccountType getAccountType() { protected AccountType getAccountType() {
return AccountType.LOCAL; return AccountType.LOCAL;
} }

+ 6
- 17
src/test/java/com/gitblit/tests/UserServiceTest.java View File

import com.gitblit.ConfigUserService; import com.gitblit.ConfigUserService;
import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.FileUserService;
import com.gitblit.IUserService; import com.gitblit.IUserService;
import com.gitblit.models.RepositoryModel; import com.gitblit.models.RepositoryModel;
import com.gitblit.models.TeamModel; import com.gitblit.models.TeamModel;
public class UserServiceTest { public class UserServiceTest {
@Test
public void testFileUserService() throws IOException {
File file = new File("us-test.properties");
file.delete();
IUserService service = new FileUserService(file);
testUsers(service);
testTeams(service);
file.delete();
}
@Test @Test
public void testConfigUserService() throws IOException { public void testConfigUserService() throws IOException {
File file = new File("us-test.conf"); File file = new File("us-test.conf");
// add admin and admins team // add admin and admins team
TeamModel admins = new TeamModel("admins"); TeamModel admins = new TeamModel("admins");
admins.mailingLists.add("admins@localhost.com"); admins.mailingLists.add("admins@localhost.com");
admin = new UserModel("admin"); admin = new UserModel("admin");
admin.password = "password"; admin.password = "password";
admin.canAdmin = true; admin.canAdmin = true;
admin.excludeFromFederation = true; admin.excludeFromFederation = true;
admin.teams.add(admins); admin.teams.add(admins);
service.updateUserModel(admin); service.updateUserModel(admin);
admin = null; admin = null;
admins = null; admins = null;
// confirm we have 1 team (admins) // confirm we have 1 team (admins)
assertEquals(1, service.getAllTeamNames().size()); assertEquals(1, service.getAllTeamNames().size());
assertEquals("admins", service.getAllTeamNames().get(0)); assertEquals("admins", service.getAllTeamNames().get(0));
RepositoryModel newrepo1 = new RepositoryModel("newrepo1", null, null, null); RepositoryModel newrepo1 = new RepositoryModel("newrepo1", null, null, null);
newrepo1.accessRestriction = AccessRestrictionType.VIEW; newrepo1.accessRestriction = AccessRestrictionType.VIEW;
RepositoryModel NEWREPO1 = new RepositoryModel("NEWREPO1", null, null, null); RepositoryModel NEWREPO1 = new RepositoryModel("NEWREPO1", null, null, null);
newrepo2.accessRestriction = AccessRestrictionType.VIEW; newrepo2.accessRestriction = AccessRestrictionType.VIEW;
RepositoryModel NEWREPO2 = new RepositoryModel("NEWREPO2", null, null, null); RepositoryModel NEWREPO2 = new RepositoryModel("NEWREPO2", null, null, null);
NEWREPO2.accessRestriction = AccessRestrictionType.VIEW; NEWREPO2.accessRestriction = AccessRestrictionType.VIEW;
team.addRepositoryPermission(newrepo2.name); team.addRepositoryPermission(newrepo2.name);
team.name = "testteam2"; team.name = "testteam2";
service.updateTeamModel("testteam", team); service.updateTeamModel("testteam", team);
// delete both teams // delete both teams
service.deleteTeam("testteam"); service.deleteTeam("testteam");
service.deleteTeam("nextteam"); service.deleteTeam("nextteam");
// assert we still have the admins team // assert we still have the admins team
assertEquals(1, service.getAllTeamNames().size()); assertEquals(1, service.getAllTeamNames().size());
assertEquals("admins", service.getAllTeamNames().get(0)); assertEquals("admins", service.getAllTeamNames().get(0));
team = service.getTeamModel("admins"); team = service.getTeamModel("admins");
assertEquals(1, team.mailingLists.size()); assertEquals(1, team.mailingLists.size());
assertTrue(team.mailingLists.contains("admins@localhost.com")); assertTrue(team.mailingLists.contains("admins@localhost.com"));

Loading…
Cancel
Save