123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- /*
- * 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.manager;
-
- import java.io.File;
- import java.io.IOException;
- import java.text.MessageFormat;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import com.gitblit.ConfigUserService;
- import com.gitblit.Constants;
- import com.gitblit.IStoredSettings;
- import com.gitblit.IUserService;
- import com.gitblit.Keys;
- import com.gitblit.models.TeamModel;
- import com.gitblit.models.UserModel;
- import com.gitblit.utils.StringUtils;
-
- /**
- * The user manager manages persistence and retrieval of users and teams.
- *
- * @author James Moger
- *
- */
- public class UserManager implements IUserManager {
-
- private final Logger logger = LoggerFactory.getLogger(getClass());
-
- private final IStoredSettings settings;
-
- private final IRuntimeManager runtimeManager;
-
- private final Map<String, String> legacyBackingServices;
-
- private IUserService userService;
-
- public UserManager(IRuntimeManager runtimeManager) {
- this.settings = runtimeManager.getSettings();
- this.runtimeManager = runtimeManager;
-
- // map of legacy realm backing user services
- legacyBackingServices = new HashMap<String, String>();
- legacyBackingServices.put("com.gitblit.HtpasswdUserService", "realm.htpasswd.backingUserService");
- legacyBackingServices.put("com.gitblit.LdapUserService", "realm.ldap.backingUserService");
- legacyBackingServices.put("com.gitblit.PAMUserService", "realm.pam.backingUserService");
- legacyBackingServices.put("com.gitblit.RedmineUserService", "realm.redmine.backingUserService");
- legacyBackingServices.put("com.gitblit.SalesforceUserService", "realm.salesforce.backingUserService");
- legacyBackingServices.put("com.gitblit.WindowsUserService", "realm.windows.backingUserService");
- }
-
- /**
- * Set the user service. The user service authenticates *local* users and is
- * responsible for persisting and retrieving all users and all teams.
- *
- * @param userService
- */
- public void setUserService(IUserService userService) {
- logger.info(userService.toString());
- this.userService = userService;
- this.userService.setup(runtimeManager);
- }
-
- @Override
- public void setup(IRuntimeManager runtimeManager) {
- // NOOP
- }
-
- @Override
- public UserManager start() {
- if (this.userService == null) {
- String realm = settings.getString(Keys.realm.userService, "${baseFolder}/users.conf");
- IUserService service = null;
- if (legacyBackingServices.containsKey(realm)) {
- // create the user service from the legacy config
- String realmKey = legacyBackingServices.get(realm);
- logger.warn("");
- logger.warn("#################################################################");
- logger.warn(" Key '{}' is obsolete!", realmKey);
- logger.warn(" Please set '{}={}'", Keys.realm.userService, settings.getString(realmKey, "${baseFolder}/users.conf"));
- logger.warn("#################################################################");
- logger.warn("");
- File realmFile = runtimeManager.getFileOrFolder(realmKey, "${baseFolder}/users.conf");
- service = createUserService(realmFile);
- } else {
- // either a file path OR a custom user service
- try {
- // check to see if this "file" is a custom user service class
- Class<?> realmClass = Class.forName(realm);
- service = (IUserService) realmClass.newInstance();
- } catch (Throwable t) {
- // typical file path configuration
- File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf");
- service = createUserService(realmFile);
- }
- }
- setUserService(service);
- }
- return this;
- }
-
- protected IUserService createUserService(File realmFile) {
- IUserService service = null;
- if (realmFile.getName().toLowerCase().endsWith(".conf")) {
- // config-based realm file
- service = new ConfigUserService(realmFile);
- }
-
- assert service != null;
-
- if (!realmFile.exists()) {
- // Create the Administrator account for a new realm file
- try {
- realmFile.createNewFile();
- } catch (IOException x) {
- logger.error(MessageFormat.format("COULD NOT CREATE REALM FILE {0}!", realmFile), x);
- }
- UserModel admin = new UserModel("admin");
- admin.password = "admin";
- admin.canAdmin = true;
- admin.excludeFromFederation = true;
- service.updateUserModel(admin);
- }
-
- return service;
- }
-
- @Override
- public UserManager stop() {
- return this;
- }
-
- /**
- * Returns true if the username represents an internal account
- *
- * @param username
- * @return true if the specified username represents an internal account
- */
- @Override
- public boolean isInternalAccount(String username) {
- return !StringUtils.isEmpty(username)
- && (username.equalsIgnoreCase(Constants.FEDERATION_USER)
- || username.equalsIgnoreCase(UserModel.ANONYMOUS.username));
- }
-
- /**
- * Returns the cookie value for the specified user.
- *
- * @param model
- * @return cookie value
- */
- @Override
- public String getCookie(UserModel model) {
- return userService.getCookie(model);
- }
-
- /**
- * Retrieve the user object for the specified cookie.
- *
- * @param cookie
- * @return a user object or null
- */
- @Override
- public UserModel getUserModel(char[] cookie) {
- UserModel user = userService.getUserModel(cookie);
- return user;
- }
-
- /**
- * Retrieve the user object for the specified username.
- *
- * @param username
- * @return a user object or null
- */
- @Override
- public UserModel getUserModel(String username) {
- if (StringUtils.isEmpty(username)) {
- return null;
- }
- String usernameDecoded = StringUtils.decodeUsername(username);
- UserModel user = userService.getUserModel(usernameDecoded);
- return user;
- }
-
- /**
- * Updates/writes a complete user object.
- *
- * @param model
- * @return true if update is successful
- */
- @Override
- public boolean updateUserModel(UserModel model) {
- return userService.updateUserModel(model);
- }
-
- /**
- * Updates/writes all specified user objects.
- *
- * @param models a list of user models
- * @return true if update is successful
- * @since 1.2.0
- */
- @Override
- public boolean updateUserModels(Collection<UserModel> models) {
- return userService.updateUserModels(models);
- }
-
- /**
- * Adds/updates a user object keyed by username. This method allows for
- * renaming a user.
- *
- * @param username
- * the old username
- * @param model
- * the user object to use for username
- * @return true if update is successful
- */
- @Override
- public boolean updateUserModel(String username, UserModel model) {
- return userService.updateUserModel(username, model);
- }
-
- /**
- * Deletes the user object from the user service.
- *
- * @param model
- * @return true if successful
- */
- @Override
- public boolean deleteUserModel(UserModel model) {
- return userService.deleteUserModel(model);
- }
-
- /**
- * Delete the user object with the specified username
- *
- * @param username
- * @return true if successful
- */
- @Override
- public boolean deleteUser(String username) {
- if (StringUtils.isEmpty(username)) {
- return false;
- }
- String usernameDecoded = StringUtils.decodeUsername(username);
- return userService.deleteUser(usernameDecoded);
- }
-
- /**
- * Returns the list of all users available to the login service.
- *
- * @return list of all usernames
- */
- @Override
- public List<String> getAllUsernames() {
- List<String> names = new ArrayList<String>(userService.getAllUsernames());
- return names;
- }
-
- /**
- * Returns the list of all users available to the login service.
- *
- * @return list of all users
- * @since 0.8.0
- */
- @Override
- public List<UserModel> getAllUsers() {
- List<UserModel> users = userService.getAllUsers();
- return users;
- }
-
- /**
- * Returns the list of all teams available to the login service.
- *
- * @return list of all teams
- * @since 0.8.0
- */
- @Override
- public List<String> getAllTeamNames() {
- List<String> teams = userService.getAllTeamNames();
- return teams;
- }
-
- /**
- * Returns the list of all teams available to the login service.
- *
- * @return list of all teams
- * @since 0.8.0
- */
- @Override
- public List<TeamModel> getAllTeams() {
- List<TeamModel> teams = userService.getAllTeams();
- return teams;
- }
-
- /**
- * Returns the list of all teams who are allowed to bypass the access
- * restriction placed on the specified repository.
- *
- * @param role
- * the repository name
- * @return list of all teams that can bypass the access restriction
- * @since 0.8.0
- */
- @Override
- public List<String> getTeamNamesForRepositoryRole(String role) {
- List<String> teams = userService.getTeamNamesForRepositoryRole(role);
- return teams;
- }
-
- /**
- * Retrieve the team object for the specified team name.
- *
- * @param teamname
- * @return a team object or null
- * @since 0.8.0
- */
- @Override
- public TeamModel getTeamModel(String teamname) {
- TeamModel team = userService.getTeamModel(teamname);
- return team;
- }
-
- /**
- * Updates/writes a complete team object.
- *
- * @param model
- * @return true if update is successful
- * @since 0.8.0
- */
- @Override
- public boolean updateTeamModel(TeamModel model) {
- return userService.updateTeamModel(model);
- }
-
- /**
- * Updates/writes all specified team objects.
- *
- * @param models a list of team models
- * @return true if update is successful
- * @since 1.2.0
- */
- @Override
- public boolean updateTeamModels(Collection<TeamModel> models) {
- return userService.updateTeamModels(models);
- }
-
- /**
- * Updates/writes and replaces a complete team object keyed by teamname.
- * This method allows for renaming a team.
- *
- * @param teamname
- * the old teamname
- * @param model
- * the team object to use for teamname
- * @return true if update is successful
- * @since 0.8.0
- */
- @Override
- public boolean updateTeamModel(String teamname, TeamModel model) {
- return userService.updateTeamModel(teamname, model);
- }
-
- /**
- * Deletes the team object from the user service.
- *
- * @param model
- * @return true if successful
- * @since 0.8.0
- */
- @Override
- public boolean deleteTeamModel(TeamModel model) {
- return userService.deleteTeamModel(model);
- }
-
- /**
- * Delete the team object with the specified teamname
- *
- * @param teamname
- * @return true if successful
- * @since 0.8.0
- */
- @Override
- public boolean deleteTeam(String teamname) {
- return userService.deleteTeam(teamname);
- }
-
- /**
- * Returns the list of all users who are allowed to bypass the access
- * restriction placed on the specified repository.
- *
- * @param role
- * the repository name
- * @return list of all usernames that can bypass the access restriction
- * @since 0.8.0
- */
- @Override
- public List<String> getUsernamesForRepositoryRole(String role) {
- return userService.getUsernamesForRepositoryRole(role);
- }
-
- /**
- * Renames a repository role.
- *
- * @param oldRole
- * @param newRole
- * @return true if successful
- */
- @Override
- public boolean renameRepositoryRole(String oldRole, String newRole) {
- return userService.renameRepositoryRole(oldRole, newRole);
- }
-
- /**
- * Removes a repository role from all users.
- *
- * @param role
- * @return true if successful
- */
- @Override
- public boolean deleteRepositoryRole(String role) {
- return userService.deleteRepositoryRole(role);
- }
- }
|