-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use RESTful routing for users' API key management
This commit refactors how we are using routes to use RESTful actions, moving the non-RESTful endpoint into a separate controller. This has the benefit to have a more organized codebase, but also to improve the granularity of how we can set permissions. For example, now we can have a permission set that does not allow to change or revoke users' API key but allows management of other users things. For this reason, a new ability is required and has been added in the provided UserManagement permission set. This commit also deprecates the old controller actions, printing a message in the log of users that are still using the old actions. The controller tests for the old actions have been deprecated as well, and copied over the new controller specs file but it is only checking controller related stuff now, since the real functionality is already covered by unit and feature specs elsewhere.
- Loading branch information
Showing
7 changed files
with
124 additions
and
6 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
29 changes: 29 additions & 0 deletions
29
backend/app/controllers/spree/admin/users/api_key_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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Admin | ||
module Users | ||
class ApiKeyController < Spree::Admin::BaseController | ||
def create | ||
if user.generate_spree_api_key! | ||
flash[:success] = t('spree.admin.api.key_generated') | ||
end | ||
redirect_to edit_admin_user_path(user) | ||
end | ||
|
||
def destroy | ||
if user.clear_spree_api_key! | ||
flash[:success] = t('spree.admin.api.key_cleared') | ||
end | ||
redirect_to edit_admin_user_path(user) | ||
end | ||
|
||
private | ||
|
||
def user | ||
@user ||= Spree.user_class.find(params[:user_id]) | ||
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
64 changes: 64 additions & 0 deletions
64
backend/spec/controllers/spree/admin/users/api_key_controller_spec.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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe Spree::Admin::Users::ApiKeyController, type: :controller do | ||
let(:user) { create(:user) } | ||
|
||
describe '#create' do | ||
context "with ability to manage users and API keys" do | ||
stub_authorization! do |_user| | ||
can [:manage], Spree.user_class | ||
can [:manage], :api_key | ||
end | ||
|
||
it "allows admins to create a new user's API key" do | ||
post :create, params: { user_id: user.id } | ||
|
||
expect(flash[:success]).to eq I18n.t('spree.admin.api.key_generated') | ||
expect(response).to redirect_to(spree.edit_admin_user_path(user)) | ||
end | ||
end | ||
|
||
context "without ability to manage users and API keys" do | ||
stub_authorization! do |_user| | ||
end | ||
|
||
it 'denies access' do | ||
delete :destroy, params: { user_id: user.id } | ||
|
||
expect(flash[:error]).to eq I18n.t('spree.authorization_failure') | ||
expect(response).to redirect_to '/unauthorized' | ||
end | ||
end | ||
end | ||
|
||
describe '#destroy' do | ||
context "with ability to manage users and API keys" do | ||
stub_authorization! do |_user| | ||
can [:manage], Spree.user_class | ||
can [:manage], :api_key | ||
end | ||
|
||
it "allows admins to clear an existing user's API key" do | ||
user.generate_spree_api_key! | ||
delete :destroy, params: { user_id: user.id } | ||
|
||
expect(flash[:success]).to eq I18n.t('spree.admin.api.key_cleared') | ||
expect(response).to redirect_to(spree.edit_admin_user_path(user)) | ||
end | ||
end | ||
|
||
context "without ability to manage users and API keys" do | ||
stub_authorization! do |_user| | ||
end | ||
|
||
it 'denies access' do | ||
delete :destroy, params: { user_id: user.id } | ||
|
||
expect(flash[:error]).to eq I18n.t('spree.authorization_failure') | ||
expect(response).to redirect_to '/unauthorized' | ||
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