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.

FileUserService.java 27KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Copyright 2011 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;
  17. import java.io.File;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.text.MessageFormat;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Properties;
  27. import java.util.Set;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import com.gitblit.models.TeamModel;
  32. import com.gitblit.models.UserModel;
  33. import com.gitblit.utils.ArrayUtils;
  34. import com.gitblit.utils.DeepCopier;
  35. import com.gitblit.utils.StringUtils;
  36. /**
  37. * FileUserService is Gitblit's original default user service implementation.
  38. *
  39. * Users and their repository memberships are stored in a simple properties file
  40. * which is cached and dynamically reloaded when modified.
  41. *
  42. * This class was deprecated in Gitblit 0.8.0 in favor of ConfigUserService
  43. * which is still a human-readable, editable, plain-text file but it is more
  44. * flexible for storing additional fields.
  45. *
  46. * @author James Moger
  47. *
  48. */
  49. @Deprecated
  50. public class FileUserService extends FileSettings implements IUserService {
  51. private final Logger logger = LoggerFactory.getLogger(FileUserService.class);
  52. private final Map<String, String> cookies = new ConcurrentHashMap<String, String>();
  53. private final Map<String, TeamModel> teams = new ConcurrentHashMap<String, TeamModel>();
  54. public FileUserService(File realmFile) {
  55. super(realmFile.getAbsolutePath());
  56. }
  57. /**
  58. * Setup the user service.
  59. *
  60. * @param settings
  61. * @since 0.7.0
  62. */
  63. @Override
  64. public void setup(IStoredSettings settings) {
  65. }
  66. /**
  67. * Does the user service support changes to credentials?
  68. *
  69. * @return true or false
  70. * @since 1.0.0
  71. */
  72. @Override
  73. public boolean supportsCredentialChanges() {
  74. return true;
  75. }
  76. /**
  77. * Does the user service support changes to user display name?
  78. *
  79. * @return true or false
  80. * @since 1.0.0
  81. */
  82. @Override
  83. public boolean supportsDisplayNameChanges() {
  84. return false;
  85. }
  86. /**
  87. * Does the user service support changes to user email address?
  88. *
  89. * @return true or false
  90. * @since 1.0.0
  91. */
  92. @Override
  93. public boolean supportsEmailAddressChanges() {
  94. return false;
  95. }
  96. /**
  97. * Does the user service support changes to team memberships?
  98. *
  99. * @return true or false
  100. * @since 1.0.0
  101. */
  102. public boolean supportsTeamMembershipChanges() {
  103. return true;
  104. }
  105. /**
  106. * Does the user service support cookie authentication?
  107. *
  108. * @return true or false
  109. */
  110. @Override
  111. public boolean supportsCookies() {
  112. return true;
  113. }
  114. /**
  115. * Returns the cookie value for the specified user.
  116. *
  117. * @param model
  118. * @return cookie value
  119. */
  120. @Override
  121. public String getCookie(UserModel model) {
  122. if (!StringUtils.isEmpty(model.cookie)) {
  123. return model.cookie;
  124. }
  125. Properties allUsers = super.read();
  126. String value = allUsers.getProperty(model.username);
  127. String[] roles = value.split(",");
  128. String password = roles[0];
  129. String cookie = StringUtils.getSHA1(model.username + password);
  130. return cookie;
  131. }
  132. /**
  133. * Authenticate a user based on their cookie.
  134. *
  135. * @param cookie
  136. * @return a user object or null
  137. */
  138. @Override
  139. public UserModel authenticate(char[] cookie) {
  140. String hash = new String(cookie);
  141. if (StringUtils.isEmpty(hash)) {
  142. return null;
  143. }
  144. read();
  145. UserModel model = null;
  146. if (cookies.containsKey(hash)) {
  147. String username = cookies.get(hash);
  148. model = getUserModel(username);
  149. }
  150. return model;
  151. }
  152. /**
  153. * Authenticate a user based on a username and password.
  154. *
  155. * @param username
  156. * @param password
  157. * @return a user object or null
  158. */
  159. @Override
  160. public UserModel authenticate(String username, char[] password) {
  161. Properties allUsers = read();
  162. String userInfo = allUsers.getProperty(username);
  163. if (StringUtils.isEmpty(userInfo)) {
  164. return null;
  165. }
  166. UserModel returnedUser = null;
  167. UserModel user = getUserModel(username);
  168. if (user.password.startsWith(StringUtils.MD5_TYPE)) {
  169. // password digest
  170. String md5 = StringUtils.MD5_TYPE + StringUtils.getMD5(new String(password));
  171. if (user.password.equalsIgnoreCase(md5)) {
  172. returnedUser = user;
  173. }
  174. } else if (user.password.startsWith(StringUtils.COMBINED_MD5_TYPE)) {
  175. // username+password digest
  176. String md5 = StringUtils.COMBINED_MD5_TYPE
  177. + StringUtils.getMD5(username.toLowerCase() + new String(password));
  178. if (user.password.equalsIgnoreCase(md5)) {
  179. returnedUser = user;
  180. }
  181. } else if (user.password.equals(new String(password))) {
  182. // plain-text password
  183. returnedUser = user;
  184. }
  185. return returnedUser;
  186. }
  187. /**
  188. * Logout a user.
  189. *
  190. * @param user
  191. */
  192. @Override
  193. public void logout(UserModel user) {
  194. }
  195. /**
  196. * Retrieve the user object for the specified username.
  197. *
  198. * @param username
  199. * @return a user object or null
  200. */
  201. @Override
  202. public UserModel getUserModel(String username) {
  203. Properties allUsers = read();
  204. String userInfo = allUsers.getProperty(username.toLowerCase());
  205. if (userInfo == null) {
  206. return null;
  207. }
  208. UserModel model = new UserModel(username.toLowerCase());
  209. String[] userValues = userInfo.split(",");
  210. model.password = userValues[0];
  211. for (int i = 1; i < userValues.length; i++) {
  212. String role = userValues[i];
  213. switch (role.charAt(0)) {
  214. case '#':
  215. // Permissions
  216. if (role.equalsIgnoreCase(Constants.ADMIN_ROLE)) {
  217. model.canAdmin = true;
  218. } else if (role.equalsIgnoreCase(Constants.FORK_ROLE)) {
  219. model.canFork = true;
  220. } else if (role.equalsIgnoreCase(Constants.NOT_FEDERATED_ROLE)) {
  221. model.excludeFromFederation = true;
  222. }
  223. break;
  224. default:
  225. model.addRepository(role);
  226. }
  227. }
  228. // set the teams for the user
  229. for (TeamModel team : teams.values()) {
  230. if (team.hasUser(username)) {
  231. model.teams.add(DeepCopier.copy(team));
  232. }
  233. }
  234. return model;
  235. }
  236. /**
  237. * Updates/writes a complete user object.
  238. *
  239. * @param model
  240. * @return true if update is successful
  241. */
  242. @Override
  243. public boolean updateUserModel(UserModel model) {
  244. return updateUserModel(model.username, model);
  245. }
  246. /**
  247. * Updates/writes and replaces a complete user object keyed by username.
  248. * This method allows for renaming a user.
  249. *
  250. * @param username
  251. * the old username
  252. * @param model
  253. * the user object to use for username
  254. * @return true if update is successful
  255. */
  256. @Override
  257. public boolean updateUserModel(String username, UserModel model) {
  258. try {
  259. Properties allUsers = read();
  260. UserModel oldUser = getUserModel(username);
  261. ArrayList<String> roles = new ArrayList<String>(model.repositories);
  262. // Permissions
  263. if (model.canAdmin) {
  264. roles.add(Constants.ADMIN_ROLE);
  265. }
  266. if (model.canFork) {
  267. roles.add(Constants.FORK_ROLE);
  268. }
  269. if (model.excludeFromFederation) {
  270. roles.add(Constants.NOT_FEDERATED_ROLE);
  271. }
  272. StringBuilder sb = new StringBuilder();
  273. if (!StringUtils.isEmpty(model.password)) {
  274. sb.append(model.password);
  275. }
  276. sb.append(',');
  277. for (String role : roles) {
  278. sb.append(role);
  279. sb.append(',');
  280. }
  281. // trim trailing comma
  282. sb.setLength(sb.length() - 1);
  283. allUsers.remove(username.toLowerCase());
  284. allUsers.put(model.username.toLowerCase(), sb.toString());
  285. // null check on "final" teams because JSON-sourced UserModel
  286. // can have a null teams object
  287. if (model.teams != null) {
  288. // update team cache
  289. for (TeamModel team : model.teams) {
  290. TeamModel t = getTeamModel(team.name);
  291. if (t == null) {
  292. // new team
  293. t = team;
  294. }
  295. t.removeUser(username);
  296. t.addUser(model.username);
  297. updateTeamCache(allUsers, t.name, t);
  298. }
  299. // check for implicit team removal
  300. if (oldUser != null) {
  301. for (TeamModel team : oldUser.teams) {
  302. if (!model.isTeamMember(team.name)) {
  303. team.removeUser(username);
  304. updateTeamCache(allUsers, team.name, team);
  305. }
  306. }
  307. }
  308. }
  309. write(allUsers);
  310. return true;
  311. } catch (Throwable t) {
  312. logger.error(MessageFormat.format("Failed to update user model {0}!", model.username),
  313. t);
  314. }
  315. return false;
  316. }
  317. /**
  318. * Deletes the user object from the user service.
  319. *
  320. * @param model
  321. * @return true if successful
  322. */
  323. @Override
  324. public boolean deleteUserModel(UserModel model) {
  325. return deleteUser(model.username);
  326. }
  327. /**
  328. * Delete the user object with the specified username
  329. *
  330. * @param username
  331. * @return true if successful
  332. */
  333. @Override
  334. public boolean deleteUser(String username) {
  335. try {
  336. // Read realm file
  337. Properties allUsers = read();
  338. UserModel user = getUserModel(username);
  339. allUsers.remove(username);
  340. for (TeamModel team : user.teams) {
  341. TeamModel t = getTeamModel(team.name);
  342. if (t == null) {
  343. // new team
  344. t = team;
  345. }
  346. t.removeUser(username);
  347. updateTeamCache(allUsers, t.name, t);
  348. }
  349. write(allUsers);
  350. return true;
  351. } catch (Throwable t) {
  352. logger.error(MessageFormat.format("Failed to delete user {0}!", username), t);
  353. }
  354. return false;
  355. }
  356. /**
  357. * Returns the list of all users available to the login service.
  358. *
  359. * @return list of all usernames
  360. */
  361. @Override
  362. public List<String> getAllUsernames() {
  363. Properties allUsers = read();
  364. List<String> list = new ArrayList<String>();
  365. for (String user : allUsers.stringPropertyNames()) {
  366. if (user.charAt(0) == '@') {
  367. // skip team user definitions
  368. continue;
  369. }
  370. list.add(user);
  371. }
  372. Collections.sort(list);
  373. return list;
  374. }
  375. /**
  376. * Returns the list of all users available to the login service.
  377. *
  378. * @return list of all usernames
  379. */
  380. @Override
  381. public List<UserModel> getAllUsers() {
  382. read();
  383. List<UserModel> list = new ArrayList<UserModel>();
  384. for (String username : getAllUsernames()) {
  385. list.add(getUserModel(username));
  386. }
  387. Collections.sort(list);
  388. return list;
  389. }
  390. /**
  391. * Returns the list of all users who are allowed to bypass the access
  392. * restriction placed on the specified repository.
  393. *
  394. * @param role
  395. * the repository name
  396. * @return list of all usernames that can bypass the access restriction
  397. */
  398. @Override
  399. public List<String> getUsernamesForRepositoryRole(String role) {
  400. List<String> list = new ArrayList<String>();
  401. try {
  402. Properties allUsers = read();
  403. for (String username : allUsers.stringPropertyNames()) {
  404. if (username.charAt(0) == '@') {
  405. continue;
  406. }
  407. String value = allUsers.getProperty(username);
  408. String[] values = value.split(",");
  409. // skip first value (password)
  410. for (int i = 1; i < values.length; i++) {
  411. String r = values[i];
  412. if (r.equalsIgnoreCase(role)) {
  413. list.add(username);
  414. break;
  415. }
  416. }
  417. }
  418. } catch (Throwable t) {
  419. logger.error(MessageFormat.format("Failed to get usernames for role {0}!", role), t);
  420. }
  421. Collections.sort(list);
  422. return list;
  423. }
  424. /**
  425. * Sets the list of all users who are allowed to bypass the access
  426. * restriction placed on the specified repository.
  427. *
  428. * @param role
  429. * the repository name
  430. * @param usernames
  431. * @return true if successful
  432. */
  433. @Override
  434. public boolean setUsernamesForRepositoryRole(String role, List<String> usernames) {
  435. try {
  436. Set<String> specifiedUsers = new HashSet<String>(usernames);
  437. Set<String> needsAddRole = new HashSet<String>(specifiedUsers);
  438. Set<String> needsRemoveRole = new HashSet<String>();
  439. // identify users which require add and remove role
  440. Properties allUsers = read();
  441. for (String username : allUsers.stringPropertyNames()) {
  442. String value = allUsers.getProperty(username);
  443. String[] values = value.split(",");
  444. // skip first value (password)
  445. for (int i = 1; i < values.length; i++) {
  446. String r = values[i];
  447. if (r.equalsIgnoreCase(role)) {
  448. // user has role, check against revised user list
  449. if (specifiedUsers.contains(username)) {
  450. needsAddRole.remove(username);
  451. } else {
  452. // remove role from user
  453. needsRemoveRole.add(username);
  454. }
  455. break;
  456. }
  457. }
  458. }
  459. // add roles to users
  460. for (String user : needsAddRole) {
  461. String userValues = allUsers.getProperty(user);
  462. userValues += "," + role;
  463. allUsers.put(user, userValues);
  464. }
  465. // remove role from user
  466. for (String user : needsRemoveRole) {
  467. String[] values = allUsers.getProperty(user).split(",");
  468. String password = values[0];
  469. StringBuilder sb = new StringBuilder();
  470. sb.append(password);
  471. sb.append(',');
  472. // skip first value (password)
  473. for (int i = 1; i < values.length; i++) {
  474. String value = values[i];
  475. if (!value.equalsIgnoreCase(role)) {
  476. sb.append(value);
  477. sb.append(',');
  478. }
  479. }
  480. sb.setLength(sb.length() - 1);
  481. // update properties
  482. allUsers.put(user, sb.toString());
  483. }
  484. // persist changes
  485. write(allUsers);
  486. return true;
  487. } catch (Throwable t) {
  488. logger.error(MessageFormat.format("Failed to set usernames for role {0}!", role), t);
  489. }
  490. return false;
  491. }
  492. /**
  493. * Renames a repository role.
  494. *
  495. * @param oldRole
  496. * @param newRole
  497. * @return true if successful
  498. */
  499. @Override
  500. public boolean renameRepositoryRole(String oldRole, String newRole) {
  501. try {
  502. Properties allUsers = read();
  503. Set<String> needsRenameRole = new HashSet<String>();
  504. // identify users which require role rename
  505. for (String username : allUsers.stringPropertyNames()) {
  506. String value = allUsers.getProperty(username);
  507. String[] roles = value.split(",");
  508. // skip first value (password)
  509. for (int i = 1; i < roles.length; i++) {
  510. String r = roles[i];
  511. if (r.equalsIgnoreCase(oldRole)) {
  512. needsRenameRole.add(username);
  513. break;
  514. }
  515. }
  516. }
  517. // rename role for identified users
  518. for (String user : needsRenameRole) {
  519. String userValues = allUsers.getProperty(user);
  520. String[] values = userValues.split(",");
  521. String password = values[0];
  522. StringBuilder sb = new StringBuilder();
  523. sb.append(password);
  524. sb.append(',');
  525. sb.append(newRole);
  526. sb.append(',');
  527. // skip first value (password)
  528. for (int i = 1; i < values.length; i++) {
  529. String value = values[i];
  530. if (!value.equalsIgnoreCase(oldRole)) {
  531. sb.append(value);
  532. sb.append(',');
  533. }
  534. }
  535. sb.setLength(sb.length() - 1);
  536. // update properties
  537. allUsers.put(user, sb.toString());
  538. }
  539. // persist changes
  540. write(allUsers);
  541. return true;
  542. } catch (Throwable t) {
  543. logger.error(
  544. MessageFormat.format("Failed to rename role {0} to {1}!", oldRole, newRole), t);
  545. }
  546. return false;
  547. }
  548. /**
  549. * Removes a repository role from all users.
  550. *
  551. * @param role
  552. * @return true if successful
  553. */
  554. @Override
  555. public boolean deleteRepositoryRole(String role) {
  556. try {
  557. Properties allUsers = read();
  558. Set<String> needsDeleteRole = new HashSet<String>();
  559. // identify users which require role rename
  560. for (String username : allUsers.stringPropertyNames()) {
  561. String value = allUsers.getProperty(username);
  562. String[] roles = value.split(",");
  563. // skip first value (password)
  564. for (int i = 1; i < roles.length; i++) {
  565. String r = roles[i];
  566. if (r.equalsIgnoreCase(role)) {
  567. needsDeleteRole.add(username);
  568. break;
  569. }
  570. }
  571. }
  572. // delete role for identified users
  573. for (String user : needsDeleteRole) {
  574. String userValues = allUsers.getProperty(user);
  575. String[] values = userValues.split(",");
  576. String password = values[0];
  577. StringBuilder sb = new StringBuilder();
  578. sb.append(password);
  579. sb.append(',');
  580. // skip first value (password)
  581. for (int i = 1; i < values.length; i++) {
  582. String value = values[i];
  583. if (!value.equalsIgnoreCase(role)) {
  584. sb.append(value);
  585. sb.append(',');
  586. }
  587. }
  588. sb.setLength(sb.length() - 1);
  589. // update properties
  590. allUsers.put(user, sb.toString());
  591. }
  592. // persist changes
  593. write(allUsers);
  594. return true;
  595. } catch (Throwable t) {
  596. logger.error(MessageFormat.format("Failed to delete role {0}!", role), t);
  597. }
  598. return false;
  599. }
  600. /**
  601. * Writes the properties file.
  602. *
  603. * @param properties
  604. * @throws IOException
  605. */
  606. private void write(Properties properties) throws IOException {
  607. // Write a temporary copy of the users file
  608. File realmFileCopy = new File(propertiesFile.getAbsolutePath() + ".tmp");
  609. FileWriter writer = new FileWriter(realmFileCopy);
  610. properties
  611. .store(writer,
  612. " Gitblit realm file format:\n username=password,\\#permission,repository1,repository2...\n @teamname=!username1,!username2,!username3,repository1,repository2...");
  613. writer.close();
  614. // If the write is successful, delete the current file and rename
  615. // the temporary copy to the original filename.
  616. if (realmFileCopy.exists() && realmFileCopy.length() > 0) {
  617. if (propertiesFile.exists()) {
  618. if (!propertiesFile.delete()) {
  619. throw new IOException(MessageFormat.format("Failed to delete {0}!",
  620. propertiesFile.getAbsolutePath()));
  621. }
  622. }
  623. if (!realmFileCopy.renameTo(propertiesFile)) {
  624. throw new IOException(MessageFormat.format("Failed to rename {0} to {1}!",
  625. realmFileCopy.getAbsolutePath(), propertiesFile.getAbsolutePath()));
  626. }
  627. } else {
  628. throw new IOException(MessageFormat.format("Failed to save {0}!",
  629. realmFileCopy.getAbsolutePath()));
  630. }
  631. }
  632. /**
  633. * Reads the properties file and rebuilds the in-memory cookie lookup table.
  634. */
  635. @Override
  636. protected synchronized Properties read() {
  637. long lastRead = lastModified();
  638. boolean reload = forceReload();
  639. Properties allUsers = super.read();
  640. if (reload || (lastRead != lastModified())) {
  641. // reload hash cache
  642. cookies.clear();
  643. teams.clear();
  644. for (String username : allUsers.stringPropertyNames()) {
  645. String value = allUsers.getProperty(username);
  646. String[] roles = value.split(",");
  647. if (username.charAt(0) == '@') {
  648. // team definition
  649. TeamModel team = new TeamModel(username.substring(1));
  650. List<String> repositories = new ArrayList<String>();
  651. List<String> users = new ArrayList<String>();
  652. List<String> mailingLists = new ArrayList<String>();
  653. List<String> preReceive = new ArrayList<String>();
  654. List<String> postReceive = new ArrayList<String>();
  655. for (String role : roles) {
  656. if (role.charAt(0) == '!') {
  657. users.add(role.substring(1));
  658. } else if (role.charAt(0) == '&') {
  659. mailingLists.add(role.substring(1));
  660. } else if (role.charAt(0) == '^') {
  661. preReceive.add(role.substring(1));
  662. } else if (role.charAt(0) == '%') {
  663. postReceive.add(role.substring(1));
  664. } else {
  665. repositories.add(role);
  666. }
  667. }
  668. team.addRepositories(repositories);
  669. team.addUsers(users);
  670. team.addMailingLists(mailingLists);
  671. team.preReceiveScripts.addAll(preReceive);
  672. team.postReceiveScripts.addAll(postReceive);
  673. teams.put(team.name.toLowerCase(), team);
  674. } else {
  675. // user definition
  676. String password = roles[0];
  677. cookies.put(StringUtils.getSHA1(username.toLowerCase() + password), username.toLowerCase());
  678. }
  679. }
  680. }
  681. return allUsers;
  682. }
  683. @Override
  684. public String toString() {
  685. return getClass().getSimpleName() + "(" + propertiesFile.getAbsolutePath() + ")";
  686. }
  687. /**
  688. * Returns the list of all teams available to the login service.
  689. *
  690. * @return list of all teams
  691. * @since 0.8.0
  692. */
  693. @Override
  694. public List<String> getAllTeamNames() {
  695. List<String> list = new ArrayList<String>(teams.keySet());
  696. Collections.sort(list);
  697. return list;
  698. }
  699. /**
  700. * Returns the list of all teams available to the login service.
  701. *
  702. * @return list of all teams
  703. * @since 0.8.0
  704. */
  705. @Override
  706. public List<TeamModel> getAllTeams() {
  707. List<TeamModel> list = new ArrayList<TeamModel>(teams.values());
  708. list = DeepCopier.copy(list);
  709. Collections.sort(list);
  710. return list;
  711. }
  712. /**
  713. * Returns the list of all teams who are allowed to bypass the access
  714. * restriction placed on the specified repository.
  715. *
  716. * @param role
  717. * the repository name
  718. * @return list of all teamnames that can bypass the access restriction
  719. */
  720. @Override
  721. public List<String> getTeamnamesForRepositoryRole(String role) {
  722. List<String> list = new ArrayList<String>();
  723. try {
  724. Properties allUsers = read();
  725. for (String team : allUsers.stringPropertyNames()) {
  726. if (team.charAt(0) != '@') {
  727. // skip users
  728. continue;
  729. }
  730. String value = allUsers.getProperty(team);
  731. String[] values = value.split(",");
  732. for (int i = 0; i < values.length; i++) {
  733. String r = values[i];
  734. if (r.equalsIgnoreCase(role)) {
  735. // strip leading @
  736. list.add(team.substring(1));
  737. break;
  738. }
  739. }
  740. }
  741. } catch (Throwable t) {
  742. logger.error(MessageFormat.format("Failed to get teamnames for role {0}!", role), t);
  743. }
  744. Collections.sort(list);
  745. return list;
  746. }
  747. /**
  748. * Sets the list of all teams who are allowed to bypass the access
  749. * restriction placed on the specified repository.
  750. *
  751. * @param role
  752. * the repository name
  753. * @param teamnames
  754. * @return true if successful
  755. */
  756. @Override
  757. public boolean setTeamnamesForRepositoryRole(String role, List<String> teamnames) {
  758. try {
  759. Set<String> specifiedTeams = new HashSet<String>(teamnames);
  760. Set<String> needsAddRole = new HashSet<String>(specifiedTeams);
  761. Set<String> needsRemoveRole = new HashSet<String>();
  762. // identify teams which require add and remove role
  763. Properties allUsers = read();
  764. for (String team : allUsers.stringPropertyNames()) {
  765. if (team.charAt(0) != '@') {
  766. // skip users
  767. continue;
  768. }
  769. String name = team.substring(1);
  770. String value = allUsers.getProperty(team);
  771. String[] values = value.split(",");
  772. for (int i = 0; i < values.length; i++) {
  773. String r = values[i];
  774. if (r.equalsIgnoreCase(role)) {
  775. // team has role, check against revised team list
  776. if (specifiedTeams.contains(name)) {
  777. needsAddRole.remove(name);
  778. } else {
  779. // remove role from team
  780. needsRemoveRole.add(name);
  781. }
  782. break;
  783. }
  784. }
  785. }
  786. // add roles to teams
  787. for (String name : needsAddRole) {
  788. String team = "@" + name;
  789. String teamValues = allUsers.getProperty(team);
  790. teamValues += "," + role;
  791. allUsers.put(team, teamValues);
  792. }
  793. // remove role from team
  794. for (String name : needsRemoveRole) {
  795. String team = "@" + name;
  796. String[] values = allUsers.getProperty(team).split(",");
  797. StringBuilder sb = new StringBuilder();
  798. for (int i = 0; i < values.length; i++) {
  799. String value = values[i];
  800. if (!value.equalsIgnoreCase(role)) {
  801. sb.append(value);
  802. sb.append(',');
  803. }
  804. }
  805. sb.setLength(sb.length() - 1);
  806. // update properties
  807. allUsers.put(team, sb.toString());
  808. }
  809. // persist changes
  810. write(allUsers);
  811. return true;
  812. } catch (Throwable t) {
  813. logger.error(MessageFormat.format("Failed to set teamnames for role {0}!", role), t);
  814. }
  815. return false;
  816. }
  817. /**
  818. * Retrieve the team object for the specified team name.
  819. *
  820. * @param teamname
  821. * @return a team object or null
  822. * @since 0.8.0
  823. */
  824. @Override
  825. public TeamModel getTeamModel(String teamname) {
  826. read();
  827. TeamModel team = teams.get(teamname.toLowerCase());
  828. if (team != null) {
  829. // clone the model, otherwise all changes to this object are
  830. // live and unpersisted
  831. team = DeepCopier.copy(team);
  832. }
  833. return team;
  834. }
  835. /**
  836. * Updates/writes a complete team object.
  837. *
  838. * @param model
  839. * @return true if update is successful
  840. * @since 0.8.0
  841. */
  842. @Override
  843. public boolean updateTeamModel(TeamModel model) {
  844. return updateTeamModel(model.name, model);
  845. }
  846. /**
  847. * Updates/writes and replaces a complete team object keyed by teamname.
  848. * This method allows for renaming a team.
  849. *
  850. * @param teamname
  851. * the old teamname
  852. * @param model
  853. * the team object to use for teamname
  854. * @return true if update is successful
  855. * @since 0.8.0
  856. */
  857. @Override
  858. public boolean updateTeamModel(String teamname, TeamModel model) {
  859. try {
  860. Properties allUsers = read();
  861. updateTeamCache(allUsers, teamname, model);
  862. write(allUsers);
  863. return true;
  864. } catch (Throwable t) {
  865. logger.error(MessageFormat.format("Failed to update team model {0}!", model.name), t);
  866. }
  867. return false;
  868. }
  869. private void updateTeamCache(Properties allUsers, String teamname, TeamModel model) {
  870. StringBuilder sb = new StringBuilder();
  871. if (!ArrayUtils.isEmpty(model.repositories)) {
  872. for (String repository : model.repositories) {
  873. sb.append(repository);
  874. sb.append(',');
  875. }
  876. }
  877. if (!ArrayUtils.isEmpty(model.users)) {
  878. for (String user : model.users) {
  879. sb.append('!');
  880. sb.append(user);
  881. sb.append(',');
  882. }
  883. }
  884. if (!ArrayUtils.isEmpty(model.mailingLists)) {
  885. for (String address : model.mailingLists) {
  886. sb.append('&');
  887. sb.append(address);
  888. sb.append(',');
  889. }
  890. }
  891. if (!ArrayUtils.isEmpty(model.preReceiveScripts)) {
  892. for (String script : model.preReceiveScripts) {
  893. sb.append('^');
  894. sb.append(script);
  895. sb.append(',');
  896. }
  897. }
  898. if (!ArrayUtils.isEmpty(model.postReceiveScripts)) {
  899. for (String script : model.postReceiveScripts) {
  900. sb.append('%');
  901. sb.append(script);
  902. sb.append(',');
  903. }
  904. }
  905. // trim trailing comma
  906. sb.setLength(sb.length() - 1);
  907. allUsers.remove("@" + teamname);
  908. allUsers.put("@" + model.name, sb.toString());
  909. // update team cache
  910. teams.remove(teamname.toLowerCase());
  911. teams.put(model.name.toLowerCase(), model);
  912. }
  913. /**
  914. * Deletes the team object from the user service.
  915. *
  916. * @param model
  917. * @return true if successful
  918. * @since 0.8.0
  919. */
  920. @Override
  921. public boolean deleteTeamModel(TeamModel model) {
  922. return deleteTeam(model.name);
  923. }
  924. /**
  925. * Delete the team object with the specified teamname
  926. *
  927. * @param teamname
  928. * @return true if successful
  929. * @since 0.8.0
  930. */
  931. @Override
  932. public boolean deleteTeam(String teamname) {
  933. Properties allUsers = read();
  934. teams.remove(teamname.toLowerCase());
  935. allUsers.remove("@" + teamname);
  936. try {
  937. write(allUsers);
  938. return true;
  939. } catch (Throwable t) {
  940. logger.error(MessageFormat.format("Failed to delete team {0}!", teamname), t);
  941. }
  942. return false;
  943. }
  944. }