From b384a9923314ef0ab7ec0e9f506c8d52cc31f5fc Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 27 Sep 2013 21:33:34 -0400
Subject: [PATCH] Fix width calculation in branch graph when there are 0 or 1 commits

---
 src/main/java/com/gitblit/ConfigUserService.java |   49 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/src/main/java/com/gitblit/ConfigUserService.java b/src/main/java/com/gitblit/ConfigUserService.java
index f2bd7b8..44687b4 100644
--- a/src/main/java/com/gitblit/ConfigUserService.java
+++ b/src/main/java/com/gitblit/ConfigUserService.java
@@ -92,6 +92,8 @@
 	
 	private static final String STARRED = "starred";
 	
+	private static final String LOCALE = "locale";
+
 	private final File realmFile;
 
 	private final Logger logger = LoggerFactory.getLogger(ConfigUserService.class);
@@ -184,8 +186,10 @@
 		if (!StringUtils.isEmpty(model.cookie)) {
 			return model.cookie;
 		}
-		read();
-		UserModel storedModel = users.get(model.username.toLowerCase());
+		UserModel storedModel = getUserModel(model.username);
+		if (storedModel == null) {
+			return null;
+		}
 		return storedModel.cookie;
 	}
 
@@ -196,7 +200,7 @@
 	 * @return a user object or null
 	 */
 	@Override
-	public UserModel authenticate(char[] cookie) {
+	public synchronized UserModel authenticate(char[] cookie) {
 		String hash = new String(cookie);
 		if (StringUtils.isEmpty(hash)) {
 			return null;
@@ -205,6 +209,12 @@
 		UserModel model = null;
 		if (cookies.containsKey(hash)) {
 			model = cookies.get(hash);
+		}
+		
+		if (model != null) {
+			// clone the model, otherwise all changes to this object are
+			// live and unpersisted
+			model = DeepCopier.copy(model);
 		}
 		return model;
 	}
@@ -218,7 +228,6 @@
 	 */
 	@Override
 	public UserModel authenticate(String username, char[] password) {
-		read();
 		UserModel returnedUser = null;
 		UserModel user = getUserModel(username);
 		if (user == null) {
@@ -260,7 +269,7 @@
 	 * @return a user object or null
 	 */
 	@Override
-	public UserModel getUserModel(String username) {
+	public synchronized UserModel getUserModel(String username) {
 		read();
 		UserModel model = users.get(username.toLowerCase());
 		if (model != null) {
@@ -290,7 +299,7 @@
 	 * @since 1.2.0
 	 */
 	@Override
-	public boolean updateUserModels(Collection<UserModel> models) {
+	public synchronized boolean updateUserModels(Collection<UserModel> models) {
 		try {
 			read();
 			for (UserModel model : models) {
@@ -342,7 +351,7 @@
 	 * @return true if update is successful
 	 */
 	@Override
-	public boolean updateUserModel(String username, UserModel model) {
+	public synchronized boolean updateUserModel(String username, UserModel model) {
 		UserModel originalUser = null;
 		try {
 			read();
@@ -408,7 +417,7 @@
 	 * @return true if successful
 	 */
 	@Override
-	public boolean deleteUser(String username) {
+	public synchronized boolean deleteUser(String username) {
 		try {
 			// Read realm file
 			read();
@@ -458,7 +467,7 @@
 	 * @since 0.8.0
 	 */
 	@Override
-	public List<TeamModel> getAllTeams() {
+	public synchronized List<TeamModel> getAllTeams() {
 		read();
 		List<TeamModel> list = new ArrayList<TeamModel>(teams.values());
 		list = DeepCopier.copy(list);
@@ -475,7 +484,7 @@
 	 * @return list of all usernames that can bypass the access restriction
 	 */
 	@Override
-	public List<String> getTeamnamesForRepositoryRole(String role) {
+	public synchronized List<String> getTeamnamesForRepositoryRole(String role) {
 		List<String> list = new ArrayList<String>();
 		try {
 			read();
@@ -502,7 +511,7 @@
 	 * @return true if successful
 	 */
 	@Override
-	public boolean setTeamnamesForRepositoryRole(String role, List<String> teamnames) {
+	public synchronized boolean setTeamnamesForRepositoryRole(String role, List<String> teamnames) {
 		try {
 			Set<String> specifiedTeams = new HashSet<String>();
 			for (String teamname : teamnames) {
@@ -539,7 +548,7 @@
 	 * @since 0.8.0
 	 */
 	@Override
-	public TeamModel getTeamModel(String teamname) {
+	public synchronized TeamModel getTeamModel(String teamname) {
 		read();
 		TeamModel model = teams.get(teamname.toLowerCase());
 		if (model != null) {
@@ -669,7 +678,7 @@
 	 * @return list of all usernames
 	 */
 	@Override
-	public List<UserModel> getAllUsers() {
+	public synchronized List<UserModel> getAllUsers() {
 		read();
 		List<UserModel> list = new ArrayList<UserModel>(users.values());
 		list = DeepCopier.copy(list);
@@ -686,7 +695,7 @@
 	 * @return list of all usernames that can bypass the access restriction
 	 */
 	@Override
-	public List<String> getUsernamesForRepositoryRole(String role) {
+	public synchronized List<String> getUsernamesForRepositoryRole(String role) {
 		List<String> list = new ArrayList<String>();
 		try {
 			read();
@@ -714,7 +723,7 @@
 	 */
 	@Override
 	@Deprecated
-	public boolean setUsernamesForRepositoryRole(String role, List<String> usernames) {
+	public synchronized boolean setUsernamesForRepositoryRole(String role, List<String> usernames) {
 		try {
 			Set<String> specifiedUsers = new HashSet<String>();
 			for (String username : usernames) {
@@ -751,7 +760,7 @@
 	 * @return true if successful
 	 */
 	@Override
-	public boolean renameRepositoryRole(String oldRole, String newRole) {
+	public synchronized boolean renameRepositoryRole(String oldRole, String newRole) {
 		try {
 			read();
 			// identify users which require role rename
@@ -786,7 +795,7 @@
 	 * @return true if successful
 	 */
 	@Override
-	public boolean deleteRepositoryRole(String role) {
+	public synchronized boolean deleteRepositoryRole(String role) {
 		try {
 			read();
 
@@ -848,6 +857,11 @@
 			}
 			if (!StringUtils.isEmpty(model.countryCode)) {
 				config.setString(USER, model.username, COUNTRYCODE, model.countryCode);
+			}
+			if (model.getPreferences() != null) {
+				if (!StringUtils.isEmpty(model.getPreferences().locale)) {
+					config.setString(USER, model.username, LOCALE, model.getPreferences().locale);
+				}
 			}
 
 			// user roles
@@ -1010,6 +1024,7 @@
 					user.stateProvince = config.getString(USER, username, STATEPROVINCE);
 					user.countryCode = config.getString(USER, username, COUNTRYCODE);
 					user.cookie = config.getString(USER, username, COOKIE);
+					user.getPreferences().locale = config.getString(USER, username, LOCALE);	
 					if (StringUtils.isEmpty(user.cookie) && !StringUtils.isEmpty(user.password)) {
 						user.cookie = StringUtils.getSHA1(user.username + user.password);
 					}

--
Gitblit v1.9.1