Commit 3a609038 authored by Timothy Andrew's avatar Timothy Andrew

Allow creating Personal Access Tokens through the website.

parent 973b948b
class Profiles::PersonalAccessTokensController < ApplicationController
def index
@user = current_user
@personal_access_token = current_user.personal_access_tokens.new
end
def create
@personal_access_token = current_user.personal_access_tokens.generate(personal_access_token_params)
if @personal_access_token.save
redirect_to profile_personal_access_tokens_path, notice: "Created personal access token!"
else
render :index
end
end
private
def personal_access_token_params
params.require(:personal_access_token).permit(:name)
end
end
class PersonalAccessToken < ActiveRecord::Base
belongs_to :user
def self.generate(params)
personal_access_token = self.new(params)
personal_access_token.token = Devise.friendly_token(50)
personal_access_token
end
end
...@@ -109,6 +109,7 @@ class User < ActiveRecord::Base ...@@ -109,6 +109,7 @@ class User < ActiveRecord::Base
# Profile # Profile
has_many :keys, dependent: :destroy has_many :keys, dependent: :destroy
has_many :emails, dependent: :destroy has_many :emails, dependent: :destroy
has_many :personal_access_tokens, dependent: :destroy
has_many :identities, dependent: :destroy, autosave: true has_many :identities, dependent: :destroy, autosave: true
# Groups # Groups
......
%h1 Profiles::PersonalAccessTokens#create
%p Find me in app/views/profiles/personal_access_tokens/create.html.haml
- page_title "Personal Access Tokens"
- header_title page_title, profile_personal_access_tokens_path
.row.prepend-top-default
.col-lg-3.profile-settings-sidebar
%h4.prepend-top-0
= page_title
%p
You can generate a personal access token for each application you use that needs access to GitLab.
.col-lg-9
%h5.prepend-top-0
Add a Personal Access Token
%p.profile-settings-content
Pick a name for the application, and we'll give you a unique token.
= form_for [:profile, @personal_access_token], method: :post, html: { class: 'js-requires-input' } do |f|
.form-group
= f.label :name, class: 'label-light'
= f.text_field :name, class: "form-control", required: true
.prepend-top-default
= f.submit 'Add Personal Access Token', class: "btn btn-create"
%hr
%h5
Active Personal Access Tokens
- if @user.personal_access_tokens.exists?
.table-responsive
%table.table.table-striped
%thead
%tr
%th Name
%th Token
%th Created At
%tbody
- @user.personal_access_tokens.each do |token|
%tr
%td= token.name
%td= token.token
%td= token.created_at
- else
%span You don't have any tokens yet.
\ No newline at end of file
...@@ -333,6 +333,7 @@ Rails.application.routes.draw do ...@@ -333,6 +333,7 @@ Rails.application.routes.draw do
resources :keys resources :keys
resources :emails, only: [:index, :create, :destroy] resources :emails, only: [:index, :create, :destroy]
resource :avatar, only: [:destroy] resource :avatar, only: [:destroy]
resources :personal_access_tokens, only: [:index, :create]
resource :two_factor_auth, only: [:new, :create, :destroy] do resource :two_factor_auth, only: [:new, :create, :destroy] do
member do member do
post :codes post :codes
......
class CreatePersonalAccessTokens < ActiveRecord::Migration
def change
create_table :personal_access_tokens do |t|
t.references :user, index: true, foreign_key: true, null: false
t.string :token, index: {unique: true}, null: false
t.string :name, null: false
t.timestamps null: false
end
end
end
...@@ -704,6 +704,19 @@ ActiveRecord::Schema.define(version: 20160421130527) do ...@@ -704,6 +704,19 @@ ActiveRecord::Schema.define(version: 20160421130527) do
add_index "oauth_applications", ["owner_id", "owner_type"], name: "index_oauth_applications_on_owner_id_and_owner_type", using: :btree add_index "oauth_applications", ["owner_id", "owner_type"], name: "index_oauth_applications_on_owner_id_and_owner_type", using: :btree
add_index "oauth_applications", ["uid"], name: "index_oauth_applications_on_uid", unique: true, using: :btree add_index "oauth_applications", ["uid"], name: "index_oauth_applications_on_uid", unique: true, using: :btree
create_table "personal_access_tokens", force: :cascade do |t|
t.integer "user_id", null: false
t.string "token", null: false
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "revoked", default: false
t.datetime "expires_at"
end
add_index "personal_access_tokens", ["token"], name: "index_personal_access_tokens_on_token", unique: true, using: :btree
add_index "personal_access_tokens", ["user_id"], name: "index_personal_access_tokens_on_user_id", using: :btree
create_table "project_group_links", force: :cascade do |t| create_table "project_group_links", force: :cascade do |t|
t.integer "project_id", null: false t.integer "project_id", null: false
t.integer "group_id", null: false t.integer "group_id", null: false
...@@ -1030,4 +1043,5 @@ ActiveRecord::Schema.define(version: 20160421130527) do ...@@ -1030,4 +1043,5 @@ ActiveRecord::Schema.define(version: 20160421130527) do
add_index "web_hooks", ["created_at", "id"], name: "index_web_hooks_on_created_at_and_id", using: :btree add_index "web_hooks", ["created_at", "id"], name: "index_web_hooks_on_created_at_and_id", using: :btree
add_index "web_hooks", ["project_id"], name: "index_web_hooks_on_project_id", using: :btree add_index "web_hooks", ["project_id"], name: "index_web_hooks_on_project_id", using: :btree
add_foreign_key "personal_access_tokens", "users"
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment