Skip to content

Commit

Permalink
ssh-key优化 (OpenCSGs#148)
Browse files Browse the repository at this point in the history
* Delete ssk_key in rails

* Update creation of ssh-key

* Update SshKeySettings.vue

* Update ssh_keys_controller.rb

* Update ssh_keys_controller.rb

* Add delsuccess

* Use wrapped starhub api(csghub_api) to call server

---------

Co-authored-by: 朱鑫睿 <[email protected]>
Co-authored-by: Hiveer <[email protected]>
  • Loading branch information
3 people authored Apr 15, 2024
1 parent 36bdb8e commit 11c527e
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 53 deletions.
22 changes: 6 additions & 16 deletions app/controllers/internal_api/ssh_keys_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
class InternalApi::SshKeysController < InternalApi::ApplicationController
def create
@ssh_key = SshKey.new(create_params)
@ssh_key.user = current_user
if @ssh_key.save
render json: { message: '添加成功' }
else
render json: { message: @ssh_key.errors.full_messages.to_sentence }, status: :bad_request
end
res = csghub_api.create_ssh_key(current_user.name, params[:name], params[:ssh_key])
raise StarhubError, res.body unless res.success?
render json: { message: I18n.t('repo.createSuccess') }
end

def destroy
@ssh_key = SshKey.find_by(id: params[:id])
return render json: { message: "SshKey not found" }, status: :not_found unless @ssh_key
return render json: { message: "Unauthorized" }, status: :unauthorized unless @ssh_key.user.id == @ssh_key.user_id

if @ssh_key.destroy
render json: {message: "SshKey destroyed"}
else
render json: {message: "Failed to destroy SshKey"}, status: :bad_request
end
res = csghub_api.delete_ssh_key(current_user.name, params[:id])
raise StarhubError, res.body unless res.success?
render json: {message: I18n.t('repo.delSuccess')}
end

private
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ def git_token
end

def ssh_keys
@ssh_keys = SshKey.where(user_id: current_user.id)
res = csghub_api.get_ssh_key(current_user.name)
raise StarhubError, res.body unless res.success?
@ssh_keys = JSON.parse(res.body)["data"]
end

def locale
# already toggle locale in application_controller/set_default_locale
redirect_back(fallback_location: root_path)
Expand Down
6 changes: 3 additions & 3 deletions app/javascript/components/user_settings/SshKeyCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<template #footer>
<span class="dialog-footer">
<el-button @click="deleteDialogVisible = false">Cancel</el-button>
<el-button type="primary" @click="confirmDeleteSshKey(theSshKeyId)">
<el-button type="primary" @click="confirmDeleteSshKey(theSshKeyName)">
{{ $t('all.confirm') }}
</el-button>
</span>
Expand Down Expand Up @@ -70,12 +70,12 @@ export default {
},
methods: {
async confirmDeleteSshKey(theSshKeyId) {
async confirmDeleteSshKey(theSshKeyName) {
this.deleteDialogVisible = false
const option = { method: 'DELETE' }
const response = await csrfFetch(`/internal_api/ssh_keys/${theSshKeyId}`, option)
const response = await csrfFetch(`/internal_api/ssh_keys/${theSshKeyName}`, option)
if (!response.ok) {
return response.json().then((data) => {
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/components/user_settings/SshKeySettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</div>
<ssh-key-card v-for="sshkey in JSON.parse(theSshKeys)"
:ssh-key-name="sshkey.name"
:ssh-key="sshkey.ssh_key"
:ssh-key="sshkey.content"
:ssh-key-id="sshkey.id"
:create-time="sshkey.created_at">
</ssh-key-card>
Expand Down
29 changes: 0 additions & 29 deletions app/models/ssh_key.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class User < ApplicationRecord
has_many :models, as: :owner
has_many :created_models, class_name: 'Model', foreign_key: :creator_id
has_many :created_organizations, class_name: 'Organization', foreign_key: :creator_id
has_many :ssh_keys, dependent: :destroy

after_save :sync_to_starhub_server

Expand Down
7 changes: 7 additions & 0 deletions app/services/starhub/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def create_ssh_key(username, key_name, content)
@client.post("/user/#{username}/ssh_keys", options)
end

def get_ssh_key(username)
options = {
username: username
}
@client.get("/user/#{username}/ssh_keys", options)
end

def delete_ssh_key(username, key_name)
options = {
username: username,
Expand Down
2 changes: 1 addition & 1 deletion app/views/settings/ssh_keys.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
ssh-keys="<%= @ssh_keys.to_json %>"
>
</ssh-key-settings>
</div>
</div>
1 change: 1 addition & 0 deletions config/locales/en_yml/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ en:
logout: 'Logout'

repo:
delSuccess: "Delete Success"
delFailed: "Delete Failed"
updateSuccess: "Update Success"
updateFailed: "Update Failed"
Expand Down

0 comments on commit 11c527e

Please sign in to comment.