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.

SshKeysPanel.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2014 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.wicket.panels;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import org.apache.wicket.ajax.AjaxRequestTarget;
  21. import org.apache.wicket.ajax.markup.html.AjaxLink;
  22. import org.apache.wicket.ajax.markup.html.form.AjaxButton;
  23. import org.apache.wicket.markup.html.basic.Label;
  24. import org.apache.wicket.markup.html.form.Form;
  25. import org.apache.wicket.markup.repeater.Item;
  26. import org.apache.wicket.markup.repeater.data.DataView;
  27. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  28. import org.apache.wicket.model.IModel;
  29. import org.apache.wicket.model.Model;
  30. import com.gitblit.Constants.AccessPermission;
  31. import com.gitblit.models.UserModel;
  32. import com.gitblit.transport.ssh.SshKey;
  33. import com.gitblit.utils.StringUtils;
  34. import com.gitblit.wicket.GitBlitWebSession;
  35. /**
  36. * A panel that enumerates and manages SSH public keys using AJAX.
  37. *
  38. * @author James Moger
  39. *
  40. */
  41. public class SshKeysPanel extends BasePanel {
  42. private static final long serialVersionUID = 1L;
  43. private final UserModel user;
  44. public SshKeysPanel(String wicketId, UserModel user) {
  45. super(wicketId);
  46. this.user = user;
  47. }
  48. @Override
  49. protected void onInitialize() {
  50. super.onInitialize();
  51. setOutputMarkupId(true);
  52. final List<SshKey> keys = new ArrayList<SshKey>(app().keys().getKeys(user.username));
  53. final ListDataProvider<SshKey> dp = new ListDataProvider<SshKey>(keys);
  54. final DataView<SshKey> keysView = new DataView<SshKey>("keys", dp) {
  55. private static final long serialVersionUID = 1L;
  56. @Override
  57. public void populateItem(final Item<SshKey> item) {
  58. final SshKey key = item.getModelObject();
  59. item.add(new Label("comment", key.getComment()));
  60. item.add(new Label("fingerprint", key.getFingerprint()));
  61. item.add(new Label("permission", key.getPermission().toString()));
  62. item.add(new Label("algorithm", key.getAlgorithm()));
  63. AjaxLink<Void> delete = new AjaxLink<Void>("delete") {
  64. private static final long serialVersionUID = 1L;
  65. @Override
  66. public void onClick(AjaxRequestTarget target) {
  67. if (app().keys().removeKey(user.username, key)) {
  68. // reset the keys list
  69. keys.clear();
  70. keys.addAll(app().keys().getKeys(user.username));
  71. // update the panel
  72. target.addComponent(SshKeysPanel.this);
  73. }
  74. }
  75. };
  76. item.add(delete);
  77. }
  78. };
  79. add(keysView);
  80. Form<Void> addKeyForm = new Form<Void>("addKeyForm");
  81. final IModel<String> keyData = Model.of("");
  82. addKeyForm.add(new TextAreaOption("addKeyData",
  83. getString("gb.key"),
  84. null,
  85. "span5",
  86. keyData));
  87. final IModel<AccessPermission> keyPermission = Model.of(AccessPermission.PUSH);
  88. addKeyForm.add(new ChoiceOption<AccessPermission>("addKeyPermission",
  89. getString("gb.permission"),
  90. getString("gb.sshKeyPermissionDescription"),
  91. keyPermission,
  92. Arrays.asList(AccessPermission.SSHPERMISSIONS)));
  93. final IModel<String> keyComment = Model.of("");
  94. addKeyForm.add(new TextOption("addKeyComment",
  95. getString("gb.comment"),
  96. getString("gb.sshKeyCommentDescription"),
  97. "span5",
  98. keyComment));
  99. addKeyForm.add(new AjaxButton("addKeyButton") {
  100. private static final long serialVersionUID = 1L;
  101. @Override
  102. protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  103. UserModel user = GitBlitWebSession.get().getUser();
  104. String data = keyData.getObject();
  105. if (StringUtils.isEmpty(data)) {
  106. // do not submit empty key
  107. return;
  108. }
  109. SshKey key = new SshKey(data);
  110. try {
  111. key.getPublicKey();
  112. } catch (Exception e) {
  113. // failed to parse the key
  114. return;
  115. }
  116. AccessPermission permission = keyPermission.getObject();
  117. key.setPermission(permission);
  118. String comment = keyComment.getObject();
  119. if (!StringUtils.isEmpty(comment)) {
  120. key.setComment(comment);
  121. }
  122. if (app().keys().addKey(user.username, key)) {
  123. // reset add key fields
  124. keyData.setObject("");
  125. keyPermission.setObject(AccessPermission.PUSH);
  126. keyComment.setObject("");
  127. // reset the keys list
  128. keys.clear();
  129. keys.addAll(app().keys().getKeys(user.username));
  130. // update the panel
  131. target.addComponent(SshKeysPanel.this);
  132. }
  133. }
  134. });
  135. add(addKeyForm);
  136. }
  137. }