-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add role to users #87 Role can be either `admin`, `regular` or `api`. The first two represent the two existing roles while the third is added in preparation for API access. * First steps towards an API #87 Allows to query for environments, nodes and keys for now. Returned JSON structure might not yet be final. * Allow querying key data w/o environment #87 This will simply use the node's environment.
- Loading branch information
Showing
31 changed files
with
1,789 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Api | ||
module V1 | ||
class ApiController < ApplicationController | ||
attr_reader :current_user | ||
|
||
rescue_from Hdm::Error, with: :return_error_json | ||
rescue_from CanCan::AccessDenied, with: :access_denied | ||
|
||
before_action :authentication_required | ||
|
||
helper_method :current_user | ||
|
||
private | ||
|
||
def authentication_required | ||
@current_user = | ||
if Rails.configuration.hdm.authentication_disabled | ||
DummyUser.new | ||
else | ||
authenticate_with_http_basic do |email, password| | ||
u = User.api.find_by(email: email.downcase) | ||
u&.authenticate(password) | ||
end | ||
end | ||
access_denied unless @current_user | ||
end | ||
|
||
def load_environments | ||
@environments = Environment.all | ||
@environments.select! { |e| current_user.may_access?(e) } | ||
@environment = Environment.find(params[:environment_id]) | ||
authorize! :show, @environment | ||
end | ||
|
||
def return_error_json(error) | ||
@error = error | ||
render json: error.message, status: :internal_server_error | ||
end | ||
|
||
def access_denied | ||
render json: "forbidden", status: :forbidden | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Api | ||
module V1 | ||
class EnvironmentsController < Api::V1::ApiController | ||
def index | ||
@environments = Environment.all | ||
@environments.select! { |e| current_user.may_access?(e) } | ||
|
||
respond_to do |format| | ||
format.json do | ||
render json: @environments | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Api | ||
module V1 | ||
class KeysController < Api::V1::ApiController | ||
before_action :load_environment_and_node | ||
|
||
def index | ||
@keys = Key.all_for(@node, environment: @environment) | ||
@keys.select! { |k| current_user.may_access?(k) } | ||
|
||
respond_to do |format| | ||
format.json do | ||
render json: @keys.to_json(except: :environment) | ||
end | ||
end | ||
end | ||
|
||
def show | ||
@key = Key.new(environment: @environment, name: params[:id]) | ||
authorize! :show, @key | ||
|
||
respond_to do |format| | ||
format.json do | ||
render json: values_per_hierarchy_and_file | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def values_per_hierarchy_and_file | ||
@environment.hierarchies.map do |hierarchy| | ||
files = hierarchy.files_for(node: @node).map do |file| | ||
{ path: file.path, value: file.value_for(key: @key).value } | ||
end | ||
{ hierarchy_name: hierarchy.name, files: } | ||
end | ||
end | ||
|
||
def load_environment_and_node | ||
@node = Node.find(params[:node_id]) | ||
authorize! :show, @node | ||
@environment = if params[:environment_id].present? | ||
Environment.find(params[:environment_id]) | ||
else | ||
@node.environment | ||
end | ||
authorize! :show, @environment | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Api | ||
module V1 | ||
class NodesController < Api::V1::ApiController | ||
def index | ||
@nodes = Node.all | ||
@nodes.select! { |n| current_user.may_access?(n) } | ||
|
||
respond_to do |format| | ||
format.json do | ||
render json: @nodes | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,10 @@ def user? | |
true | ||
end | ||
|
||
def api? | ||
true | ||
end | ||
|
||
def may_access?(_record) | ||
true | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.