Skip to content

Commit

Permalink
Add button to set Github token (for instance, to Fine-Grained persona…
Browse files Browse the repository at this point in the history
…l access token)
  • Loading branch information
b-grablabs committed Jan 26, 2024
1 parent 70485a6 commit 784379d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
26 changes: 24 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class UsersController < ApplicationController
skip_before_action :update_persistent_announcements
before_action :set_gh_oauth_client, only: [:github_oauth, :github_oauth_callback]
before_action :set_user,
only: [:github_oauth, :github_revoke, :lti_launch_initialize,
:lti_launch_link_course]
only: [:github_oauth, :github_revoke,
:show_github_token_form, :submit_github_token_form,
:lti_launch_initialize, :lti_launch_link_course]

# GET /users
action_auth_level :index, :student
Expand Down Expand Up @@ -250,6 +251,27 @@ def lti_launch_link_course
redirect_to(course)
end

action_auth_level :github_token, :student
def show_github_token_form
@github_integration = GithubIntegration.find_by(user_id: @user.id)
render('github_token')
end

action_auth_level :github_token_form, :student
def submit_github_token_form
github_integration = GithubIntegration.find_by(user_id: @user.id)
access_token = params[:access_token]

if github_integration.nil?
github_integration = GithubIntegration.create!(access_token:, user: @user)
else
github_integration.update!(access_token:)
end

flash[:success] = "Updated Github Token"
redirect_to(user_path)
end

action_auth_level :github_oauth, :student
def github_oauth
github_integration = GithubIntegration.find_by(user_id: @user.id)
Expand Down
13 changes: 13 additions & 0 deletions app/views/users/github_token.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% @title = "Github Access Token" %>

<h2>Github Update Access Token</h2>
<% if GithubIntegration.connected %>
<%= form_with url: github_token_user_path do |form| %>
<div class="input-field">
<%= form.text_field :access_token, required: true %>
<%= form.label :access_token, "Access Token" %>
<span class="helper-text">Helpful description here</span>
</div>
<%= form.submit "Update Token", { class: "btn primary" } %>
<% end %>
<% end %>
7 changes: 7 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
</span>
<% end %>
<% if GithubIntegration.connected %>
<h4>Github Settings</h4>
<% if @user.github_integration && @user.github_integration.is_connected %>
<%= link_to github_revoke_user_path(@user), data: { method: "post" } do %>
<span class="btn primary">
Expand All @@ -75,6 +76,12 @@
</span>
<% end %>
<% end %>
<%= link_to github_token_user_path(@user) do %>
<span class="btn primary">
Use Personal access Token
</span>
<% end %>
<% end %>
<h4>API Settings</h4>
<%= link_to device_flow_activation_path do %>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
get "lti_launch_initialize", on: :member
post "lti_launch_link_course", on: :member
post "github_revoke", on: :member
get "github_token", on: :member, to: 'users#show_github_token_form'
post 'github_token', on: :member, to: 'users#submit_github_token_form'
get "github_oauth_callback", on: :collection
match "update_password_for_user", on: :member, via: [:get, :put]
post "change_password_for_user", on: :member
Expand Down

0 comments on commit 784379d

Please sign in to comment.