You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserManager.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.manager;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.text.MessageFormat;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import com.gitblit.ConfigUserService;
  28. import com.gitblit.Constants;
  29. import com.gitblit.IStoredSettings;
  30. import com.gitblit.IUserService;
  31. import com.gitblit.Keys;
  32. import com.gitblit.models.TeamModel;
  33. import com.gitblit.models.UserModel;
  34. import com.gitblit.utils.StringUtils;
  35. /**
  36. * The user manager manages persistence and retrieval of users and teams.
  37. *
  38. * @author James Moger
  39. *
  40. */
  41. public class UserManager implements IUserManager {
  42. private final Logger logger = LoggerFactory.getLogger(getClass());
  43. private final IStoredSettings settings;
  44. private final IRuntimeManager runtimeManager;
  45. private final Map<String, String> legacyBackingServices;
  46. private IUserService userService;
  47. public UserManager(IRuntimeManager runtimeManager) {
  48. this.settings = runtimeManager.getSettings();
  49. this.runtimeManager = runtimeManager;
  50. // map of legacy realm backing user services
  51. legacyBackingServices = new HashMap<String, String>();
  52. legacyBackingServices.put("com.gitblit.HtpasswdUserService", "realm.htpasswd.backingUserService");
  53. legacyBackingServices.put("com.gitblit.LdapUserService", "realm.ldap.backingUserService");
  54. legacyBackingServices.put("com.gitblit.PAMUserService", "realm.pam.backingUserService");
  55. legacyBackingServices.put("com.gitblit.RedmineUserService", "realm.redmine.backingUserService");
  56. legacyBackingServices.put("com.gitblit.SalesforceUserService", "realm.salesforce.backingUserService");
  57. legacyBackingServices.put("com.gitblit.WindowsUserService", "realm.windows.backingUserService");
  58. }
  59. /**
  60. * Set the user service. The user service authenticates *local* users and is
  61. * responsible for persisting and retrieving all users and all teams.
  62. *
  63. * @param userService
  64. */
  65. public void setUserService(IUserService userService) {
  66. logger.info(userService.toString());
  67. this.userService = userService;
  68. this.userService.setup(runtimeManager);
  69. }
  70. @Override
  71. public void setup(IRuntimeManager runtimeManager) {
  72. // NOOP
  73. }
  74. @Override
  75. public UserManager start() {
  76. if (this.userService == null) {
  77. String realm = settings.getString(Keys.realm.userService, "${baseFolder}/users.conf");
  78. IUserService service = null;
  79. if (legacyBackingServices.containsKey(realm)) {
  80. // create the user service from the legacy config
  81. String realmKey = legacyBackingServices.get(realm);
  82. logger.warn("");
  83. logger.warn("#################################################################");
  84. logger.warn(" Key '{}' is obsolete!", realmKey);
  85. logger.warn(" Please set '{}={}'", Keys.realm.userService, settings.getString(realmKey, "${baseFolder}/users.conf"));
  86. logger.warn("#################################################################");
  87. logger.warn("");
  88. File realmFile = runtimeManager.getFileOrFolder(realmKey, "${baseFolder}/users.conf");
  89. service = createUserService(realmFile);
  90. } else {
  91. // either a file path OR a custom user service
  92. try {
  93. // check to see if this "file" is a custom user service class
  94. Class<?> realmClass = Class.forName(realm);
  95. service = (IUserService) realmClass.newInstance();
  96. } catch (Throwable t) {
  97. // typical file path configuration
  98. File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf");
  99. service = createUserService(realmFile);
  100. }
  101. }
  102. setUserService(service);
  103. }
  104. return this;
  105. }
  106. protected IUserService createUserService(File realmFile) {
  107. IUserService service = null;
  108. if (realmFile.getName().toLowerCase().endsWith(".conf")) {
  109. // config-based realm file
  110. service = new ConfigUserService(realmFile);
  111. }
  112. assert service != null;
  113. if (!realmFile.exists()) {
  114. // Create the Administrator account for a new realm file
  115. try {
  116. realmFile.createNewFile();
  117. } catch (IOException x) {
  118. logger.error(MessageFormat.format("COULD NOT CREATE REALM FILE {0}!", realmFile), x);
  119. }
  120. UserModel admin = new UserModel("admin");
  121. admin.password = "admin";
  122. admin.canAdmin = true;
  123. admin.excludeFromFederation = true;
  124. service.updateUserModel(admin);
  125. }
  126. return service;
  127. }
  128. @Override
  129. public UserManager stop() {
  130. return this;
  131. }
  132. /**
  133. * Returns true if the username represents an internal account
  134. *
  135. * @param username
  136. * @return true if the specified username represents an internal account
  137. */
  138. @Override
  139. public boolean isInternalAccount(String username) {
  140. return !StringUtils.isEmpty(username)
  141. && (username.equalsIgnoreCase(Constants.FEDERATION_USER)
  142. || username.equalsIgnoreCase(UserModel.ANONYMOUS.username));
  143. }
  144. /**
  145. * Returns the cookie value for the specified user.
  146. *
  147. * @param model
  148. * @return cookie value
  149. */
  150. @Override
  151. public String getCookie(UserModel model) {
  152. return userService.getCookie(model);
  153. }
  154. /**
  155. * Retrieve the user object for the specified cookie.
  156. *
  157. * @param cookie
  158. * @return a user object or null
  159. */
  160. @Override
  161. public UserModel getUserModel(char[] cookie) {
  162. UserModel user = userService.getUserModel(cookie);
  163. return user;
  164. }
  165. /**
  166. * Retrieve the user object for the specified username.
  167. *
  168. * @param username
  169. * @return a user object or null
  170. */
  171. @Override
  172. public UserModel getUserModel(String username) {
  173. if (StringUtils.isEmpty(username)) {
  174. return null;
  175. }
  176. String usernameDecoded = StringUtils.decodeUsername(username);
  177. UserModel user = userService.getUserModel(usernameDecoded);
  178. return user;
  179. }
  180. /**
  181. * Updates/writes a complete user object.
  182. *
  183. * @param model
  184. * @return true if update is successful
  185. */
  186. @Override
  187. public boolean updateUserModel(UserModel model) {
  188. return userService.updateUserModel(model);
  189. }
  190. /**
  191. * Updates/writes all specified user objects.
  192. *
  193. * @param models a list of user models
  194. * @return true if update is successful
  195. * @since 1.2.0
  196. */
  197. @Override
  198. public boolean updateUserModels(Collection<UserModel> models) {
  199. return userService.updateUserModels(models);
  200. }
  201. /**
  202. * Adds/updates a user object keyed by username. This method allows for
  203. * renaming a user.
  204. *
  205. * @param username
  206. * the old username
  207. * @param model
  208. * the user object to use for username
  209. * @return true if update is successful
  210. */
  211. @Override
  212. public boolean updateUserModel(String username, UserModel model) {
  213. return userService.updateUserModel(username, model);
  214. }
  215. /**
  216. * Deletes the user object from the user service.
  217. *
  218. * @param model
  219. * @return true if successful
  220. */
  221. @Override
  222. public boolean deleteUserModel(UserModel model) {
  223. return userService.deleteUserModel(model);
  224. }
  225. /**
  226. * Delete the user object with the specified username
  227. *
  228. * @param username
  229. * @return true if successful
  230. */
  231. @Override
  232. public boolean deleteUser(String username) {
  233. if (StringUtils.isEmpty(username)) {
  234. return false;
  235. }
  236. String usernameDecoded = StringUtils.decodeUsername(username);
  237. return userService.deleteUser(usernameDecoded);
  238. }
  239. /**
  240. * Returns the list of all users available to the login service.
  241. *
  242. * @return list of all usernames
  243. */
  244. @Override
  245. public List<String> getAllUsernames() {
  246. List<String> names = new ArrayList<String>(userService.getAllUsernames());
  247. return names;
  248. }
  249. /**
  250. * Returns the list of all users available to the login service.
  251. *
  252. * @return list of all users
  253. * @since 0.8.0
  254. */
  255. @Override
  256. public List<UserModel> getAllUsers() {
  257. List<UserModel> users = userService.getAllUsers();
  258. return users;
  259. }
  260. /**
  261. * Returns the list of all teams available to the login service.
  262. *
  263. * @return list of all teams
  264. * @since 0.8.0
  265. */
  266. @Override
  267. public List<String> getAllTeamNames() {
  268. List<String> teams = userService.getAllTeamNames();
  269. return teams;
  270. }
  271. /**
  272. * Returns the list of all teams available to the login service.
  273. *
  274. * @return list of all teams
  275. * @since 0.8.0
  276. */
  277. @Override
  278. public List<TeamModel> getAllTeams() {
  279. List<TeamModel> teams = userService.getAllTeams();
  280. return teams;
  281. }
  282. /**
  283. * Returns the list of all teams who are allowed to bypass the access
  284. * restriction placed on the specified repository.
  285. *
  286. * @param role
  287. * the repository name
  288. * @return list of all teams that can bypass the access restriction
  289. * @since 0.8.0
  290. */
  291. @Override
  292. public List<String> getTeamNamesForRepositoryRole(String role) {
  293. List<String> teams = userService.getTeamNamesForRepositoryRole(role);
  294. return teams;
  295. }
  296. /**
  297. * Retrieve the team object for the specified team name.
  298. *
  299. * @param teamname
  300. * @return a team object or null
  301. * @since 0.8.0
  302. */
  303. @Override
  304. public TeamModel getTeamModel(String teamname) {
  305. TeamModel team = userService.getTeamModel(teamname);
  306. return team;
  307. }
  308. /**
  309. * Updates/writes a complete team object.
  310. *
  311. * @param model
  312. * @return true if update is successful
  313. * @since 0.8.0
  314. */
  315. @Override
  316. public boolean updateTeamModel(TeamModel model) {
  317. return userService.updateTeamModel(model);
  318. }
  319. /**
  320. * Updates/writes all specified team objects.
  321. *
  322. * @param models a list of team models
  323. * @return true if update is successful
  324. * @since 1.2.0
  325. */
  326. @Override
  327. public boolean updateTeamModels(Collection<TeamModel> models) {
  328. return userService.updateTeamModels(models);
  329. }
  330. /**
  331. * Updates/writes and replaces a complete team object keyed by teamname.
  332. * This method allows for renaming a team.
  333. *
  334. * @param teamname
  335. * the old teamname
  336. * @param model
  337. * the team object to use for teamname
  338. * @return true if update is successful
  339. * @since 0.8.0
  340. */
  341. @Override
  342. public boolean updateTeamModel(String teamname, TeamModel model) {
  343. return userService.updateTeamModel(teamname, model);
  344. }
  345. /**
  346. * Deletes the team object from the user service.
  347. *
  348. * @param model
  349. * @return true if successful
  350. * @since 0.8.0
  351. */
  352. @Override
  353. public boolean deleteTeamModel(TeamModel model) {
  354. return userService.deleteTeamModel(model);
  355. }
  356. /**
  357. * Delete the team object with the specified teamname
  358. *
  359. * @param teamname
  360. * @return true if successful
  361. * @since 0.8.0
  362. */
  363. @Override
  364. public boolean deleteTeam(String teamname) {
  365. return userService.deleteTeam(teamname);
  366. }
  367. /**
  368. * Returns the list of all users who are allowed to bypass the access
  369. * restriction placed on the specified repository.
  370. *
  371. * @param role
  372. * the repository name
  373. * @return list of all usernames that can bypass the access restriction
  374. * @since 0.8.0
  375. */
  376. @Override
  377. public List<String> getUsernamesForRepositoryRole(String role) {
  378. return userService.getUsernamesForRepositoryRole(role);
  379. }
  380. /**
  381. * Renames a repository role.
  382. *
  383. * @param oldRole
  384. * @param newRole
  385. * @return true if successful
  386. */
  387. @Override
  388. public boolean renameRepositoryRole(String oldRole, String newRole) {
  389. return userService.renameRepositoryRole(oldRole, newRole);
  390. }
  391. /**
  392. * Removes a repository role from all users.
  393. *
  394. * @param role
  395. * @return true if successful
  396. */
  397. @Override
  398. public boolean deleteRepositoryRole(String role) {
  399. return userService.deleteRepositoryRole(role);
  400. }
  401. }