summaryrefslogtreecommitdiff
path: root/app/controllers/api/keys_controller.rb
blob: 32ccc197a1bc3d743071a996c2c3d7fa575a1f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Api::KeysController < ApiController

  before_filter :require_login
  before_filter :require_enabled

  # get /keys
  def index
    render json: identity.keys
  end

  def show
    render json: identity.keys[params[:id]]
  end

  def create
    keyring.create type, value
    head :no_content
  rescue Keyring::Error, ActionController::ParameterMissing => e
    render status: 422, json: {error: e.message}
  end

  def update
    keyring.update type, rev: rev, value: value
    head :no_content
  rescue Keyring::NotFound => e
    render status: 404, json: {error: e.message}
  rescue Keyring::Error, ActionController::ParameterMissing => e
    render status: 422, json: {error: e.message}
  end

  def destroy
    keyring.delete type, rev: rev
    head :no_content
  rescue Keyring::NotFound => e
    render status: 404, json: {error: e.message}
  rescue Keyring::Error, ActionController::ParameterMissing => e
    render status: 422, json: {error: e.message}
  end


  protected

  def require_enabled
    if !current_user.enabled?
      access_denied
    end
  end

  def service_level
    current_user.effective_service_level
  end

  def type
    params.require :type
  end

  def value
    params.require :value
  end

  def rev
    params.require :rev
  end

  def keyring
    @keyring ||= Keyring.new identity
  end

  def identity
    @identity ||= Identity.for(current_user)
  end
end