Full admin page functionality is working.
authorErik Brakkee <erik@brakkee.org>
Sun, 29 Sep 2013 22:20:05 +0000 (00:20 +0200)
committerErik Brakkee <erik@brakkee.org>
Sun, 29 Sep 2013 22:20:05 +0000 (00:20 +0200)
src/main/java/org/wamblee/photos/wicket/AdminPage.html
src/main/java/org/wamblee/photos/wicket/AdminPage.java

index 5567558e0fa67a3cc77fac6b7d413525b9703dc0..ccd9907cfd6c7ce39994a682c147715fb24c9183 100644 (file)
 
         <h2>Add user to group</h2>
 
+        <form wicket:id="userToGroupForm">
+            <table>
+                <tr>
+                    <th>User</th>
+                    <td>
+                        <select wicket:id="userChoice">
+                            <option>user1</option>
+                            <option>user2</option>
+                        </select>
+                    </td>
+                </tr>
+                <tr>
+                    <th>Group</th>
+                    <td>
+                        <select wicket:id="groupChoice">
+                            <option>group1</option>
+                            <option>group2</option>
+                        </select>
+                    </td>
+                </tr>
+                <tr>
+                    <th></th>
+                    <th><input type="submit" value="Add user to group"/></th>
+                </tr>
+            </table>
+        </form>
+
+
     </div>
 </wicket:extend>
 
index 0449028594ccf7216fd60e57b805ff645eaae21c..95b25665442aca8e7475f9dd775838d07b495c5b 100644 (file)
@@ -22,7 +22,9 @@ import javax.inject.Inject;
 import org.apache.wicket.PageParameters;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.basic.Label;
+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.PasswordTextField;
 import org.apache.wicket.markup.html.form.TextField;
 import org.apache.wicket.markup.html.link.Link;
@@ -175,30 +177,7 @@ public class AdminPage extends BasePage {
         {
             final TextField newUser = new TextField("user", new Model(""));
             final PasswordTextField passwordNewUser = new PasswordTextField("password", new Model(""));
-            IModel<List<String>> groups = new LoadableDetachableModel<List<String>>() {
-                @Override
-                protected List<String> load() {
-                    return _userAdmin.getGroups();
-                }
-            };
-            /*
-            final DropDownChoice groupChoice =
-                    new DropDownChoice("group", new Model(""), groups, new IChoiceRenderer() {
 
-                        @Override
-                        public Object getDisplayValue(Object object) {
-                            return object.toString();
-                        }
-
-                        @Override
-                        public String getIdValue(Object object, int index) {
-                            if (index < 0) {
-                                return "";
-                            }
-                            return object.toString();
-                        }
-                    });
-              */
             Form createUserForm = new Form("createUserForm") {
                 @Override
                 protected void onSubmit() {
@@ -232,10 +211,77 @@ public class AdminPage extends BasePage {
             createUserForm.add(passwordNewUser);
             // createUserForm.add(groupChoice);
         }
+
+        {
+            IModel<List<String>> users = new LoadableDetachableModel<List<String>>() {
+                @Override
+                protected List<String> load() {
+                    return _userAdmin.getUsers();
+                }
+            };
+            final DropDownChoice userChoice =
+                    new DropDownChoice("userChoice", new Model(""), users, new MyChoiceRenderer());
+
+            IModel<List<String>> groups = new LoadableDetachableModel<List<String>>() {
+                @Override
+                protected List<String> load() {
+                    return _userAdmin.getGroups();
+                }
+            };
+            final DropDownChoice groupChoice =
+                    new DropDownChoice("groupChoice", new Model(""), groups, new MyChoiceRenderer());
+
+            Form userToGroupForm = new Form("userToGroupForm") {
+                @Override
+                protected void onSubmit() {
+                    String user = userChoice.getValue();
+                    String group = groupChoice.getValue();
+                    if (!_userAdmin.getUsers().contains(user)) {
+                        return;
+                    }
+                    if (!_userAdmin.getGroups().contains(group)) {
+                        return;
+                    }
+                    if (_userAdmin.isInGroup(user, group)) {
+                        info("User '" + user + "' is already in group '" + group + "'");
+                        return;
+                    }
+
+                    try {
+                        _userAdmin.addUserToGroup(user, group);
+                        info("Added user '" + user + "' to group '" + group + "'");
+                    }
+                    catch (UserMgtException e) {
+                        error("Could not add user '" + user + "' to group '" + group + "': " + e.getMessage());
+                    }
+                }
+
+                ;
+            };
+            userToGroupForm.add(userChoice);
+            userToGroupForm.add(groupChoice);
+            add(userToGroupForm);
+        }
     }
 
     @Override
     protected boolean isAdminPage() {
         return true;
     }
+
+    private static class MyChoiceRenderer implements IChoiceRenderer {
+
+        @Override
+        public Object getDisplayValue(Object object) {
+            return object.toString();
+        }
+
+        @Override
+        public String getIdValue(Object object, int index) {
+            if (index < 0) {
+                return "";
+            }
+            return object.toString();
+        }
+    }
 }
\ No newline at end of file