-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #435 from UffizziCloud/feature/434_bearer_authenti…
…cation Feature/434 bearer authentication
- Loading branch information
Showing
10 changed files
with
147 additions
and
11 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
45 changes: 45 additions & 0 deletions
45
core/app/controllers/uffizzi_core/api/cli/v1/accounts_controller.rb
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 @@ | ||
# frozen_string_literal: true | ||
|
||
# @resource Project | ||
|
||
class UffizziCore::Api::Cli::V1::AccountsController < UffizziCore::Api::Cli::V1::ApplicationController | ||
before_action :authorize_uffizzi_core_api_cli_v1_accounts | ||
|
||
# Get accounts of current user | ||
# | ||
# @path [GET] /api/cli/v1/accounts | ||
# | ||
# @response [object<accounts: Array<object<id: integer, name: string>> >] 200 OK | ||
# @response 401 Not authorized | ||
def index | ||
accounts = current_user.accounts.order(name: :desc) | ||
|
||
respond_with accounts | ||
end | ||
|
||
# Get account by name | ||
# | ||
# @path [GET] /api/cli/v1/accounts/{name} | ||
# | ||
# @response [object<account: <object<id: integer, name: string, projects: Array<object<id: integer, slug: string>>>> >] 200 OK | ||
# @response 401 Not authorized | ||
def show | ||
respond_with resource_account | ||
end | ||
|
||
private | ||
|
||
def policy_context | ||
account = resource_account || current_user.default_account | ||
|
||
UffizziCore::AccountContext.new(current_user, user_access_module, account, params) | ||
end | ||
|
||
def resource_account | ||
@resource_account ||= if params[:name] | ||
current_user.accounts.find_by!(name: params[:name]) | ||
else | ||
current_user.default_account | ||
end | ||
end | ||
end |
4 changes: 4 additions & 0 deletions
4
core/app/policies/uffizzi_core/api/cli/v1/accounts/projects_policy.rb
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
11 changes: 11 additions & 0 deletions
11
core/app/policies/uffizzi_core/api/cli/v1/accounts_policy.rb
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Api::Cli::V1::AccountsPolicy < UffizziCore::ApplicationPolicy | ||
def index? | ||
context.user.present? | ||
end | ||
|
||
def show? | ||
context.user_access_module.any_access_to_account?(context.user, context.account) | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
core/app/serializers/uffizzi_core/api/cli/v1/account_serializer.rb
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Api::Cli::V1::AccountSerializer < UffizziCore::BaseSerializer | ||
type :account | ||
|
||
has_many :projects | ||
|
||
attributes :id, :name | ||
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
22 changes: 22 additions & 0 deletions
22
core/test/controllers/uffizzi_core/api/cli/v1/accounts_controller_test.rb
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class UffizziCore::Api::Cli::V1::AccountsControllerTest < ActionController::TestCase | ||
setup do | ||
@user = create(:user, :with_personal_account) | ||
sign_in(@user) | ||
end | ||
|
||
test '#index' do | ||
get :index, format: :json | ||
|
||
assert_response(:success) | ||
end | ||
|
||
test '#show' do | ||
get :show, params: { name: 'wrong' }, format: :json | ||
|
||
assert_response(:not_found) | ||
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