From 27ae9095639bb228a1b7ff86a3ebe4264abf05be Mon Sep 17 00:00:00 2001
From: mschaefers <mschaefers@scoop-gmbh.de>
Date: Thu, 29 Nov 2012 12:33:09 -0500
Subject: [PATCH] feature: when using LdapUserService one can configure Gitblit to fetch all users from ldap that can possibly login. This allows to see newly generated LDAP users instantly in Gitblit. By now an LDAP user had to log in once to appear in GitBlit.

---
 src/com/gitblit/wicket/pages/EditRepositoryPage.java |  182 ++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 154 insertions(+), 28 deletions(-)

diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/com/gitblit/wicket/pages/EditRepositoryPage.java
index 1a2e63c..dead34a 100644
--- a/src/com/gitblit/wicket/pages/EditRepositoryPage.java
+++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.java
@@ -27,6 +27,9 @@
 import java.util.Set;
 
 import org.apache.wicket.PageParameters;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
+import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
 import org.apache.wicket.behavior.SimpleAttributeModifier;
 import org.apache.wicket.extensions.markup.html.form.palette.Palette;
 import org.apache.wicket.markup.html.WebMarkupContainer;
@@ -36,8 +39,7 @@
 import org.apache.wicket.markup.html.form.DropDownChoice;
 import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.form.IChoiceRenderer;
-import org.apache.wicket.markup.html.form.Radio;
-import org.apache.wicket.markup.html.form.RadioGroup;
+import org.apache.wicket.markup.html.form.RadioChoice;
 import org.apache.wicket.markup.html.form.TextField;
 import org.apache.wicket.markup.html.list.ListItem;
 import org.apache.wicket.markup.html.list.ListView;
@@ -51,6 +53,7 @@
 import com.gitblit.Constants.AccessRestrictionType;
 import com.gitblit.Constants.AuthorizationControl;
 import com.gitblit.Constants.FederationStrategy;
+import com.gitblit.Constants.RegistrantType;
 import com.gitblit.GitBlit;
 import com.gitblit.GitBlitException;
 import com.gitblit.Keys;
@@ -70,6 +73,8 @@
 	private final boolean isCreate;
 
 	private boolean isAdmin;
+	
+	RepositoryModel repositoryModel;
 
 	private IModel<String> mailingLists;
 
@@ -97,6 +102,7 @@
 		
 		setupPage(model);
 		setStatelessHint(false);
+		setOutputMarkupId(true);
 	}
 
 	public EditRepositoryPage(PageParameters params) {
@@ -107,9 +113,17 @@
 		RepositoryModel model = GitBlit.self().getRepositoryModel(name);
 		setupPage(model);
 		setStatelessHint(false);
+		setOutputMarkupId(true);
+	}
+	
+	@Override
+	protected boolean requiresPageMap() {
+		return true;
 	}
 
-	protected void setupPage(final RepositoryModel repositoryModel) {
+	protected void setupPage(RepositoryModel model) {
+		this.repositoryModel = model;
+		
 		// ensure this user can create or edit this repository
 		checkPermissions(repositoryModel);
 
@@ -132,11 +146,11 @@
 			}
 		} else {
 			super.setupPage(getString("gb.edit"), repositoryModel.name);
-			if (repositoryModel.accessRestriction.exceeds(AccessRestrictionType.NONE)) {
-				repositoryUsers.addAll(GitBlit.self().getUserAccessPermissions(repositoryModel));
-				repositoryTeams.addAll(GitBlit.self().getTeamAccessPermissions(repositoryModel));
-				Collections.sort(repositoryUsers);
-			}
+			repositoryUsers.addAll(GitBlit.self().getUserAccessPermissions(repositoryModel));
+			repositoryTeams.addAll(GitBlit.self().getTeamAccessPermissions(repositoryModel));
+			Collections.sort(repositoryUsers);
+			Collections.sort(repositoryTeams);
+			
 			federationSets.addAll(repositoryModel.federationSets);
 			if (!ArrayUtils.isEmpty(repositoryModel.indexedBranches)) {
 				indexedBranches.addAll(repositoryModel.indexedBranches);
@@ -145,10 +159,10 @@
 
 		final String oldName = repositoryModel.name;
 
-		RegistrantPermissionsPanel usersPalette = new RegistrantPermissionsPanel("users", 
-				GitBlit.self().getAllUsernames(), repositoryUsers, getAccessPermissions());
-		RegistrantPermissionsPanel teamsPalette = new RegistrantPermissionsPanel("teams", 
-				GitBlit.self().getAllTeamnames(), repositoryTeams, getAccessPermissions());
+		final RegistrantPermissionsPanel usersPalette = new RegistrantPermissionsPanel("users", 
+				RegistrantType.USER, GitBlit.self().getAllUsernames(), repositoryUsers, getAccessPermissions());
+		final RegistrantPermissionsPanel teamsPalette = new RegistrantPermissionsPanel("teams", 
+				RegistrantType.TEAM, GitBlit.self().getAllTeamnames(), repositoryTeams, getAccessPermissions());
 
 		// indexed local branches palette
 		List<String> allLocalBranches = new ArrayList<String>();
@@ -206,9 +220,9 @@
 		};
 		customFieldsListView.setReuseItems(true);
 
-		CompoundPropertyModel<RepositoryModel> model = new CompoundPropertyModel<RepositoryModel>(
+		CompoundPropertyModel<RepositoryModel> rModel = new CompoundPropertyModel<RepositoryModel>(
 				repositoryModel);
-		Form<RepositoryModel> form = new Form<RepositoryModel>("editForm", model) {
+		Form<RepositoryModel> form = new Form<RepositoryModel>("editForm", rModel) {
 
 			private static final long serialVersionUID = 1L;
 
@@ -364,10 +378,11 @@
 		form.add(new TextField<String>("name").setEnabled(allowEditName));
 		form.add(new TextField<String>("description"));
 		form.add(new DropDownChoice<String>("owner", GitBlit.self().getAllUsernames())
-				.setEnabled(GitBlitWebSession.get().canAdmin()));
+				.setEnabled(GitBlitWebSession.get().canAdmin() && !repositoryModel.isPersonalRepository()));
 		form.add(new CheckBox("allowForks"));
-		form.add(new DropDownChoice<AccessRestrictionType>("accessRestriction", Arrays
-				.asList(AccessRestrictionType.values()), new AccessRestrictionRenderer()));
+		DropDownChoice<AccessRestrictionType> accessRestriction = new DropDownChoice<AccessRestrictionType>("accessRestriction", Arrays
+				.asList(AccessRestrictionType.values()), new AccessRestrictionRenderer());
+		form.add(accessRestriction);
 		form.add(new CheckBox("isFrozen"));
 		// TODO enable origin definition
 		form.add(new TextField<String>("origin").setEnabled(false/* isCreate */));
@@ -379,9 +394,10 @@
 		}
 		form.add(new DropDownChoice<String>("HEAD", availableRefs).setEnabled(availableRefs.size() > 0));
 
-		List<String> gcPeriods = Arrays.asList("1 day", "2 days", "3 days", "4 days", "5 days", "7 days", "10 days", "14 days");
-		form.add(new DropDownChoice<String>("gcPeriod", gcPeriods));
-		form.add(new TextField<String>("gcThreshold"));
+		boolean gcEnabled = GitBlit.getBoolean(Keys.git.enableGarbageCollection, false); 
+		List<Integer> gcPeriods = Arrays.asList(1, 2, 3, 4, 5, 7, 10, 14 );
+		form.add(new DropDownChoice<Integer>("gcPeriod", gcPeriods, new GCPeriodRenderer()).setEnabled(gcEnabled));
+		form.add(new TextField<String>("gcThreshold").setEnabled(gcEnabled));
 
 		// federation strategies - remove ORIGIN choice if this repository has
 		// no origin.
@@ -403,14 +419,14 @@
 		form.add(new TextField<String>("mailingLists", mailingLists));
 		form.add(indexedBranchesPalette);
 		
-		RadioGroup<AuthorizationControl> group = new RadioGroup<AuthorizationControl>("authorizationControl");
-		Radio<AuthorizationControl> allowAuthenticated = new Radio<AuthorizationControl>("allowAuthenticated", new Model<AuthorizationControl>(AuthorizationControl.AUTHENTICATED));		
-		Radio<AuthorizationControl> allowNamed = new Radio<AuthorizationControl>("allowNamed", new Model<AuthorizationControl>(AuthorizationControl.NAMED));
-		group.add(allowAuthenticated);
-		group.add(allowNamed);
-		form.add(group);
-				
-		form.add(new CheckBox("verifyCommitter"));
+		List<AuthorizationControl> acList = Arrays.asList(AuthorizationControl.values());
+		final RadioChoice<AuthorizationControl> authorizationControl = new RadioChoice<Constants.AuthorizationControl>(
+				"authorizationControl", acList, new AuthorizationControlRenderer());
+		form.add(authorizationControl);
+		
+		final CheckBox verifyCommitter = new CheckBox("verifyCommitter");
+		verifyCommitter.setOutputMarkupId(true);
+		form.add(verifyCommitter);
 
 		form.add(usersPalette);
 		form.add(teamsPalette);
@@ -425,7 +441,73 @@
 		WebMarkupContainer customFieldsSection = new WebMarkupContainer("customFieldsSection");
 		customFieldsSection.add(customFieldsListView);
 		form.add(customFieldsSection.setVisible(!GitBlit.getString(Keys.groovy.customFields, "").isEmpty()));
+		
+		// initial enable/disable of permission controls
+		if (repositoryModel.accessRestriction.equals(AccessRestrictionType.NONE)) {
+			// anonymous everything, disable all controls
+			usersPalette.setEnabled(false);
+			teamsPalette.setEnabled(false);
+			authorizationControl.setEnabled(false);
+			verifyCommitter.setEnabled(false);
+		} else {
+			// authenticated something
+			// enable authorization controls
+			authorizationControl.setEnabled(true);
+			verifyCommitter.setEnabled(true);
+			
+			boolean allowFineGrainedControls = repositoryModel.authorizationControl.equals(AuthorizationControl.NAMED);
+			usersPalette.setEnabled(allowFineGrainedControls);
+			teamsPalette.setEnabled(allowFineGrainedControls);
+		}
+		
+		accessRestriction.add(new AjaxFormComponentUpdatingBehavior("onchange") {
+	           
+			private static final long serialVersionUID = 1L;
 
+			protected void onUpdate(AjaxRequestTarget target) {
+				// enable/disable permissions panel based on access restriction
+				boolean allowAuthorizationControl = repositoryModel.accessRestriction.exceeds(AccessRestrictionType.NONE);
+				authorizationControl.setEnabled(allowAuthorizationControl);
+				verifyCommitter.setEnabled(allowAuthorizationControl);
+				
+				boolean allowFineGrainedControls = allowAuthorizationControl && repositoryModel.authorizationControl.equals(AuthorizationControl.NAMED);
+				usersPalette.setEnabled(allowFineGrainedControls);
+				teamsPalette.setEnabled(allowFineGrainedControls);
+				
+				if (allowFineGrainedControls) {
+					repositoryModel.authorizationControl = AuthorizationControl.NAMED;
+				}
+				
+				target.addComponent(authorizationControl);
+				target.addComponent(verifyCommitter);
+				target.addComponent(usersPalette);
+				target.addComponent(teamsPalette);
+			}
+		});
+		
+		authorizationControl.add(new AjaxFormChoiceComponentUpdatingBehavior() {
+	           
+			private static final long serialVersionUID = 1L;
+
+			protected void onUpdate(AjaxRequestTarget target) {
+				// enable/disable permissions panel based on access restriction
+				boolean allowAuthorizationControl = repositoryModel.accessRestriction.exceeds(AccessRestrictionType.NONE);
+				authorizationControl.setEnabled(allowAuthorizationControl);
+				
+				boolean allowFineGrainedControls = allowAuthorizationControl && repositoryModel.authorizationControl.equals(AuthorizationControl.NAMED);
+				usersPalette.setEnabled(allowFineGrainedControls);
+				teamsPalette.setEnabled(allowFineGrainedControls);
+				
+				if (allowFineGrainedControls) {
+					repositoryModel.authorizationControl = AuthorizationControl.NAMED;
+				}
+				
+				target.addComponent(authorizationControl);
+				target.addComponent(usersPalette);
+				target.addComponent(teamsPalette);
+			}
+		});
+		
 		form.add(new Button("save"));
 		Button cancel = new Button("cancel") {
 			private static final long serialVersionUID = 1L;
@@ -528,4 +610,48 @@
 			return Integer.toString(index);
 		}
 	}
+	
+	private class AuthorizationControlRenderer implements IChoiceRenderer<AuthorizationControl> {
+
+		private static final long serialVersionUID = 1L;
+
+		private final Map<AuthorizationControl, String> map;
+
+		public AuthorizationControlRenderer() {
+			map = getAuthorizationControls();
+		}
+
+		@Override
+		public String getDisplayValue(AuthorizationControl type) {
+			return map.get(type);
+		}
+
+		@Override
+		public String getIdValue(AuthorizationControl type, int index) {
+			return Integer.toString(index);
+		}
+	}
+	
+	private class GCPeriodRenderer implements IChoiceRenderer<Integer> {
+
+		private static final long serialVersionUID = 1L;
+
+		public GCPeriodRenderer() {
+		}
+
+		@Override
+		public String getDisplayValue(Integer value) {
+			if (value == 1) {
+				return getString("gb.duration.oneDay");
+			} else {
+				return MessageFormat.format(getString("gb.duration.days"), value);
+			}
+		}
+
+		@Override
+		public String getIdValue(Integer value, int index) {
+			return Integer.toString(index);
+		}
+	}
+	
 }

--
Gitblit v1.9.1