From 2c50880dbd5de292e37d2533fccfcb62f6be9f95 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 29 May 2014 21:47:56 -0400 Subject: [PATCH] Extract authorization/access selection panel to re-usable class --- .../wicket/pages/NewRepositoryPage.html | 13 +- .../wicket/pages/NewRepositoryPage.java | 101 +---------- .../panels/RepositoryPermissionPanel.html | 25 +++ .../panels/RepositoryPermissionPanel.java | 171 ++++++++++++++++++ 4 files changed, 205 insertions(+), 105 deletions(-) create mode 100644 src/main/java/com/gitblit/wicket/panels/RepositoryPermissionPanel.html create mode 100644 src/main/java/com/gitblit/wicket/panels/RepositoryPermissionPanel.java diff --git a/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.html b/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.html index e9f12027..c59f29c8 100644 --- a/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.html +++ b/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.html @@ -32,18 +32,7 @@
- -
-
- - -
-
-
- -
-
-
+
diff --git a/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java index a423ae62..cd0cef61 100644 --- a/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java @@ -17,7 +17,6 @@ package com.gitblit.wicket.pages; import java.io.File; import java.io.IOException; -import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.text.MessageFormat; import java.util.ArrayList; @@ -27,16 +26,11 @@ import java.util.List; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; import org.apache.wicket.behavior.SimpleAttributeModifier; -import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.CheckBox; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.markup.html.form.Radio; -import org.apache.wicket.markup.html.form.RadioGroup; import org.apache.wicket.markup.html.form.TextField; -import org.apache.wicket.markup.html.list.ListItem; -import org.apache.wicket.markup.html.list.ListView; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.Model; @@ -67,15 +61,16 @@ import com.gitblit.utils.FileUtils; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.WicketUtils; +import com.gitblit.wicket.panels.RepositoryPermissionPanel; public class NewRepositoryPage extends RootSubPage { private final RepositoryModel repositoryModel; - private RadioGroup permissionGroup; private IModel addReadmeModel; private Model gitignoreModel; private IModel addGitflowModel; private IModel addGitignoreModel; + private RepositoryPermissionPanel permissionPanel; public NewRepositoryPage() { // create constructor @@ -148,9 +143,7 @@ public class NewRepositoryPage extends RootSubPage { repositoryModel.name = fullName; repositoryModel.projectPath = null; - Permission permission = permissionGroup.getModelObject(); - repositoryModel.authorizationControl = permission.control; - repositoryModel.accessRestriction = permission.type; + permissionPanel.setPermission(repositoryModel); repositoryModel.owners = new ArrayList(); repositoryModel.owners.add(GitBlitWebSession.get().getUsername()); @@ -229,46 +222,7 @@ public class NewRepositoryPage extends RootSubPage { form.add(new TextField("name")); form.add(new TextField("description")); - Permission anonymousPermission = new Permission(getString("gb.anonymousPush"), - getString("gb.anonymousPushDescription"), - "blank.png", - AuthorizationControl.AUTHENTICATED, - AccessRestrictionType.NONE); - - Permission authenticatedPermission = new Permission(getString("gb.pushRestrictedAuthenticated"), - getString("gb.pushRestrictedAuthenticatedDescription"), - "lock_go_16x16.png", - AuthorizationControl.AUTHENTICATED, - AccessRestrictionType.PUSH); - - Permission publicPermission = new Permission(getString("gb.pushRestrictedNamed"), - getString("gb.pushRestrictedNamedDescription"), - "lock_go_16x16.png", - AuthorizationControl.NAMED, - AccessRestrictionType.PUSH); - - Permission protectedPermission = new Permission(getString("gb.cloneRestricted"), - getString("gb.cloneRestrictedDescription"), - "lock_pull_16x16.png", - AuthorizationControl.NAMED, - AccessRestrictionType.CLONE); - - Permission privatePermission = new Permission(getString("gb.private"), - getString("gb.privateRepoDescription"), - "shield_16x16.png", - AuthorizationControl.NAMED, - AccessRestrictionType.VIEW); - - List permissions = new ArrayList(); - if (app().settings().getBoolean(Keys.git.allowAnonymousPushes, false)) { - permissions.add(anonymousPermission); - } - permissions.add(authenticatedPermission); - permissions.add(publicPermission); - permissions.add(protectedPermission); - permissions.add(privatePermission); - - // determine default permission selection + // prepare the default access controls AccessRestrictionType defaultRestriction = AccessRestrictionType.fromName( app().settings().getString(Keys.git.defaultAccessRestriction, AccessRestrictionType.PUSH.name())); if (AccessRestrictionType.NONE == defaultRestriction) { @@ -281,31 +235,11 @@ public class NewRepositoryPage extends RootSubPage { defaultRestriction = AccessRestrictionType.PUSH; } - Permission defaultPermission = publicPermission; - for (Permission permission : permissions) { - if (permission.type == defaultRestriction - && permission.control == defaultControl) { - defaultPermission = permission; - } - } - - permissionGroup = new RadioGroup<>("permissionsGroup", new Model(defaultPermission)); - form.add(permissionGroup); + repositoryModel.authorizationControl = defaultControl; + repositoryModel.accessRestriction = defaultRestriction; - ListView permissionsList = new ListView("permissions", permissions) { - - private static final long serialVersionUID = 1L; - - @Override - protected void populateItem(ListItem item) { - Permission p = item.getModelObject(); - item.add(new Radio("radio", item.getModel())); - item.add(WicketUtils.newImage("image", p.image)); - item.add(new Label("name", p.name)); - item.add(new Label("description", p.description)); - } - }; - permissionGroup.add(permissionsList); + permissionPanel = new RepositoryPermissionPanel("permissionPanel", repositoryModel); + form.add(permissionPanel); // // initial commit options @@ -506,23 +440,4 @@ public class NewRepositoryPage extends RootSubPage { } return success; } - - private static class Permission implements Serializable { - - private static final long serialVersionUID = 1L; - - final String name; - final String description; - final String image; - final AuthorizationControl control; - final AccessRestrictionType type; - - Permission(String name, String description, String img, AuthorizationControl control, AccessRestrictionType type) { - this.name = name; - this.description = description; - this.image = img; - this.control = control; - this.type = type; - } - } } diff --git a/src/main/java/com/gitblit/wicket/panels/RepositoryPermissionPanel.html b/src/main/java/com/gitblit/wicket/panels/RepositoryPermissionPanel.html new file mode 100644 index 00000000..6a4d5cef --- /dev/null +++ b/src/main/java/com/gitblit/wicket/panels/RepositoryPermissionPanel.html @@ -0,0 +1,25 @@ + + + + + + + +
+
+ + +
+
+
+ +
+
+
+ +
+ + \ No newline at end of file diff --git a/src/main/java/com/gitblit/wicket/panels/RepositoryPermissionPanel.java b/src/main/java/com/gitblit/wicket/panels/RepositoryPermissionPanel.java new file mode 100644 index 00000000..7c416b1b --- /dev/null +++ b/src/main/java/com/gitblit/wicket/panels/RepositoryPermissionPanel.java @@ -0,0 +1,171 @@ +/* + * Copyright 2014 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.wicket.panels; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.form.Radio; +import org.apache.wicket.markup.html.form.RadioGroup; +import org.apache.wicket.markup.html.list.ListItem; +import org.apache.wicket.markup.html.list.ListView; +import org.apache.wicket.model.Model; + +import com.gitblit.Constants.AccessRestrictionType; +import com.gitblit.Constants.AuthorizationControl; +import com.gitblit.Keys; +import com.gitblit.models.RepositoryModel; +import com.gitblit.wicket.WicketUtils; + +/** + * A radio group panel of the 5 available authorization/access restriction combinations. + * + * @author James Moger + * + */ +public class RepositoryPermissionPanel extends BasePanel { + + private static final long serialVersionUID = 1L; + + private final RepositoryModel repository; + + private RadioGroup permissionGroup; + + public RepositoryPermissionPanel(String wicketId, RepositoryModel repository) { + super(wicketId); + this.repository = repository; + } + + @Override + protected void onInitialize() { + super.onInitialize(); + + Permission anonymousPermission = new Permission(getString("gb.anonymousPush"), + getString("gb.anonymousPushDescription"), + "blank.png", + AuthorizationControl.AUTHENTICATED, + AccessRestrictionType.NONE); + + Permission authenticatedPermission = new Permission(getString("gb.pushRestrictedAuthenticated"), + getString("gb.pushRestrictedAuthenticatedDescription"), + "lock_go_16x16.png", + AuthorizationControl.AUTHENTICATED, + AccessRestrictionType.PUSH); + + Permission publicPermission = new Permission(getString("gb.pushRestrictedNamed"), + getString("gb.pushRestrictedNamedDescription"), + "lock_go_16x16.png", + AuthorizationControl.NAMED, + AccessRestrictionType.PUSH); + + Permission protectedPermission = new Permission(getString("gb.cloneRestricted"), + getString("gb.cloneRestrictedDescription"), + "lock_pull_16x16.png", + AuthorizationControl.NAMED, + AccessRestrictionType.CLONE); + + Permission privatePermission = new Permission(getString("gb.private"), + getString("gb.privateRepoDescription"), + "shield_16x16.png", + AuthorizationControl.NAMED, + AccessRestrictionType.VIEW); + + List permissions = new ArrayList(); + if (app().settings().getBoolean(Keys.git.allowAnonymousPushes, false)) { + permissions.add(anonymousPermission); + } + permissions.add(authenticatedPermission); + permissions.add(publicPermission); + permissions.add(protectedPermission); + permissions.add(privatePermission); + + AccessRestrictionType defaultRestriction = repository.accessRestriction; + if (defaultRestriction == null) { + defaultRestriction = AccessRestrictionType.fromName(app().settings().getString(Keys.git.defaultAccessRestriction, + AccessRestrictionType.PUSH.name())); + } + + AuthorizationControl defaultControl = repository.authorizationControl; + if (defaultControl == null) { + defaultControl = AuthorizationControl.fromName(app().settings().getString(Keys.git.defaultAuthorizationControl, + AuthorizationControl.NAMED.name())); + } + + Permission defaultPermission = publicPermission; + for (Permission permission : permissions) { + if (permission.type == defaultRestriction && permission.control == defaultControl) { + defaultPermission = permission; + } + } + + permissionGroup = new RadioGroup<>("permissionsGroup", new Model(defaultPermission)); + ListView permissionsList = new ListView("permissions", permissions) { + + private static final long serialVersionUID = 1L; + + @Override + protected void populateItem(ListItem item) { + Permission p = item.getModelObject(); + item.add(new Radio("radio", item.getModel())); + item.add(WicketUtils.newImage("image", p.image)); + item.add(new Label("name", p.name)); + item.add(new Label("description", p.description)); + } + }; + permissionGroup.add(permissionsList); + + setOutputMarkupId(true); + + add(permissionGroup); + } + + public void setPermission(RepositoryModel repository) { + Permission permission = permissionGroup.getModelObject(); + repository.authorizationControl = permission.control; + repository.accessRestriction = permission.type; + } + + @Override + protected boolean getStatelessHint() { + return false; + } + + private static class Permission implements Serializable { + + private static final long serialVersionUID = 1L; + + final String name; + final String description; + final String image; + final AuthorizationControl control; + final AccessRestrictionType type; + + Permission(String name, String description, String img, AuthorizationControl control, AccessRestrictionType type) { + this.name = name; + this.description = description; + this.image = img; + this.control = control; + this.type = type; + } + + @Override + public String toString() { + return name; + } + } +} -- 2.39.5