Allow user to creates named auth keys with token
This commit is contained in:
parent
7d1274cbb3
commit
763c9789ec
51
app/controllers/keys_controller.rb
Normal file
51
app/controllers/keys_controller.rb
Normal file
@ -0,0 +1,51 @@
|
||||
class KeysController < ApplicationController
|
||||
before_filter :set_key, only: %i[show edit update destroy]
|
||||
|
||||
def index
|
||||
@keys = current_user.keys
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def new
|
||||
@key = current_user.keys.new
|
||||
end
|
||||
|
||||
def create
|
||||
@key = current_user.keys.build key_params
|
||||
|
||||
if @key.save
|
||||
redirect_to action: :index
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
if @key.update_attributes key_params
|
||||
redirect_to action: :index
|
||||
else
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@key.destroy
|
||||
redirect_to keys_path
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def set_key
|
||||
@key = current_user.keys.find(params[:id])
|
||||
end
|
||||
|
||||
def key_params
|
||||
params.require(:key).permit :name
|
||||
end
|
||||
end
|
7
app/models/key.rb
Normal file
7
app/models/key.rb
Normal file
@ -0,0 +1,7 @@
|
||||
class Key < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
|
||||
has_secure_token :token
|
||||
|
||||
validates :name, presence: true
|
||||
end
|
@ -4,6 +4,7 @@ class User < ActiveRecord::Base
|
||||
attr_reader :password
|
||||
|
||||
has_many :playlists
|
||||
has_many :keys
|
||||
|
||||
validates :email,
|
||||
presence: true,
|
||||
|
19
app/views/keys/_form.haml
Normal file
19
app/views/keys/_form.haml
Normal file
@ -0,0 +1,19 @@
|
||||
= form_for @key do |f|
|
||||
%fieldset
|
||||
%p
|
||||
= f.label :name
|
||||
%br
|
||||
= f.text_field :name
|
||||
|
||||
- if @key.errors.any?
|
||||
.errors
|
||||
%p
|
||||
= pluralize @key.errors.count, 'error'
|
||||
prohibited this post from being saved:
|
||||
%ul
|
||||
- @key.errors.full_messages.each do |error|
|
||||
%li= error
|
||||
|
||||
%ul
|
||||
%li
|
||||
= f.button
|
6
app/views/keys/edit.haml
Normal file
6
app/views/keys/edit.haml
Normal file
@ -0,0 +1,6 @@
|
||||
%h1
|
||||
Edit
|
||||
key
|
||||
|
||||
|
||||
= render 'form'
|
42
app/views/keys/index.haml
Normal file
42
app/views/keys/index.haml
Normal file
@ -0,0 +1,42 @@
|
||||
%h1
|
||||
Listing
|
||||
keys
|
||||
|
||||
|
||||
- if @keys.empty?
|
||||
%p No record found.
|
||||
|
||||
- else
|
||||
%table
|
||||
%thead
|
||||
%tr
|
||||
%th
|
||||
Name
|
||||
%th
|
||||
Created at
|
||||
%th
|
||||
Updated at
|
||||
%th
|
||||
Actions
|
||||
|
||||
%tbody
|
||||
- @keys.each do |e|
|
||||
%tr
|
||||
%td
|
||||
= link_to e.name, e
|
||||
%td
|
||||
= e.created_at
|
||||
%td
|
||||
= e.updated_at
|
||||
%td
|
||||
%ul
|
||||
%li
|
||||
= link_to 'Edit', edit_key_path(e)
|
||||
%li
|
||||
= link_to 'Destroy', e, method: :delete,
|
||||
data: { confirm: 'Are you sure?' }
|
||||
|
||||
|
||||
%ul
|
||||
%li
|
||||
= link_to 'New key', new_key_path
|
6
app/views/keys/new.haml
Normal file
6
app/views/keys/new.haml
Normal file
@ -0,0 +1,6 @@
|
||||
%h1
|
||||
New
|
||||
key
|
||||
|
||||
|
||||
= render 'form'
|
13
app/views/keys/show.haml
Normal file
13
app/views/keys/show.haml
Normal file
@ -0,0 +1,13 @@
|
||||
%h1
|
||||
Showing
|
||||
key
|
||||
|
||||
|
||||
%pre= @key.attributes.to_yaml
|
||||
|
||||
|
||||
%ul.actions
|
||||
%li
|
||||
= link_to 'Edit', edit_key_path(@key)
|
||||
%li
|
||||
= link_to 'Destroy', e, method: :delete, data: { confirm: 'Are you sure?' }
|
@ -12,6 +12,8 @@
|
||||
- if current_user?
|
||||
%nav
|
||||
%ul
|
||||
%li
|
||||
= link_to 'Keys', keys_path
|
||||
%li
|
||||
= link_to 'Playlists', playlists_path
|
||||
%li
|
||||
|
@ -22,5 +22,7 @@ Rails.application.routes.draw do
|
||||
get 'download', on: :member
|
||||
end
|
||||
|
||||
resources :keys
|
||||
|
||||
resources :users, only: %i[new create]
|
||||
end
|
||||
|
12
db/migrate/20150504043016_create_keys.rb
Normal file
12
db/migrate/20150504043016_create_keys.rb
Normal file
@ -0,0 +1,12 @@
|
||||
class CreateKeys < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :keys do |t|
|
||||
t.timestamps null: false
|
||||
t.references :user, index: true, foreign_key: true, null: false
|
||||
t.string :name
|
||||
t.string :token
|
||||
end
|
||||
|
||||
add_index :keys, :token, unique: true
|
||||
end
|
||||
end
|
14
db/schema.rb
14
db/schema.rb
@ -11,11 +11,22 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20110916003929) do
|
||||
ActiveRecord::Schema.define(version: 20150504043016) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
create_table "keys", force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "user_id", null: false
|
||||
t.string "name"
|
||||
t.string "token"
|
||||
end
|
||||
|
||||
add_index "keys", ["token"], name: "index_keys_on_token", unique: true, using: :btree
|
||||
add_index "keys", ["user_id"], name: "index_keys_on_user_id", using: :btree
|
||||
|
||||
create_table "playlists", force: :cascade do |t|
|
||||
t.integer "user_id"
|
||||
t.string "name"
|
||||
@ -56,4 +67,5 @@ ActiveRecord::Schema.define(version: 20110916003929) do
|
||||
|
||||
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
|
||||
|
||||
add_foreign_key "keys", "users"
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user