您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SshKeysPanel.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. private final boolean canWriteKeys;
  45. public SshKeysPanel(String wicketId, UserModel user) {
  46. super(wicketId);
  47. this.user = user;
  48. this.canWriteKeys = app().keys().supportsWritingKeys(user);
  49. }
  50. @Override
  51. protected void onInitialize() {
  52. super.onInitialize();
  53. setOutputMarkupId(true);
  54. final List<SshKey> keys = new ArrayList<SshKey>(app().keys().getKeys(user.username));
  55. final ListDataProvider<SshKey> dp = new ListDataProvider<SshKey>(keys);
  56. final DataView<SshKey> keysView = new DataView<SshKey>("keys", dp) {
  57. private static final long serialVersionUID = 1L;
  58. @Override
  59. public void populateItem(final Item<SshKey> item) {
  60. final SshKey key = item.getModelObject();
  61. item.add(new Label("comment", key.getComment()));
  62. item.add(new Label("fingerprint", key.getFingerprint()));
  63. item.add(new Label("permission", key.getPermission().toString()));
  64. item.add(new Label("algorithm", key.getAlgorithm()));
  65. AjaxLink<Void> delete = new AjaxLink<Void>("delete") {
  66. private static final long serialVersionUID = 1L;
  67. @Override
  68. public void onClick(AjaxRequestTarget target) {
  69. if (app().keys().removeKey(user.username, key)) {
  70. // reset the keys list
  71. keys.clear();
  72. keys.addAll(app().keys().getKeys(user.username));
  73. // update the panel
  74. target.addComponent(SshKeysPanel.this);
  75. }
  76. }
  77. };
  78. if (!canWriteKeys) {
  79. delete.setVisibilityAllowed(false);
  80. }
  81. item.add(delete);
  82. }
  83. };
  84. add(keysView);
  85. Form<Void> addKeyForm = new Form<Void>("addKeyForm");
  86. final IModel<String> keyData = Model.of("");
  87. addKeyForm.add(new TextAreaOption("addKeyData",
  88. getString("gb.key"),
  89. null,
  90. "span5",
  91. keyData));
  92. final IModel<AccessPermission> keyPermission = Model.of(AccessPermission.PUSH);
  93. addKeyForm.add(new ChoiceOption<AccessPermission>("addKeyPermission",
  94. getString("gb.permission"),
  95. getString("gb.sshKeyPermissionDescription"),
  96. keyPermission,
  97. Arrays.asList(AccessPermission.SSHPERMISSIONS)));
  98. final IModel<String> keyComment = Model.of("");
  99. addKeyForm.add(new TextOption("addKeyComment",
  100. getString("gb.sshKeyComment"),
  101. getString("gb.sshKeyCommentDescription"),
  102. "span5",
  103. keyComment));
  104. addKeyForm.add(new AjaxButton("addKeyButton") {
  105. private static final long serialVersionUID = 1L;
  106. @Override
  107. protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  108. UserModel user = GitBlitWebSession.get().getUser();
  109. String data = keyData.getObject();
  110. if (StringUtils.isEmpty(data)) {
  111. // do not submit empty key
  112. return;
  113. }
  114. SshKey key = new SshKey(data);
  115. try {
  116. key.getPublicKey();
  117. } catch (Exception e) {
  118. // failed to parse the key
  119. return;
  120. }
  121. AccessPermission permission = keyPermission.getObject();
  122. key.setPermission(permission);
  123. String comment = keyComment.getObject();
  124. if (!StringUtils.isEmpty(comment)) {
  125. key.setComment(comment);
  126. }
  127. if (app().keys().addKey(user.username, key)) {
  128. // reset add key fields
  129. keyData.setObject("");
  130. keyPermission.setObject(AccessPermission.PUSH);
  131. keyComment.setObject("");
  132. // reset the keys list
  133. keys.clear();
  134. keys.addAll(app().keys().getKeys(user.username));
  135. // update the panel
  136. target.addComponent(SshKeysPanel.this);
  137. }
  138. }
  139. });
  140. if (! canWriteKeys) {
  141. addKeyForm.setVisibilityAllowed(false);
  142. }
  143. add(addKeyForm);
  144. }
  145. }