Skip to content

Commit

Permalink
Refs #37936 - Correct success method for jwt api
Browse files Browse the repository at this point in the history
Refs #37936 - Remove dead code, fix not_found message
  • Loading branch information
girijaasoni authored and stejskalleos committed Jan 22, 2025
1 parent 2b7e57a commit 1ad2139
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions app/controllers/api/v2/registration_tokens_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module Api
module V2
class RegistrationTokensController < V2::BaseController
include Foreman::Controller::UsersMixin
include Foreman::Controller::UserSelfEditing

include Foreman::Controller::Parameters::User
include Foreman::Controller::AutoCompleteSearch
before_action :authenticate, :only => [:invalidate_jwt_tokens, :invalidate_jwt]

def resource_class
User
end

def resource_name(resource = resource_class.model_name.name.downcase)
resource
end

def find_resource(permission = :view_users)
editing_self? ? User.find(User.current.id) : User.authorized(permission).except_hidden.find(params[:id])
end
Expand All @@ -24,22 +26,20 @@ def action_permission
end
end

api :DELETE, '/users/:id/registration_tokens', N_("Invalidate all registration tokens for a specific user.")
api :DELETE, '/users/:user_id/registration_tokens', N_("Invalidate all registration tokens for a specific user")
description <<-DOC
The user you specify will no longer be able to register hosts by using their JWTs.
DOC
param :id, String, :desc => N_("ID of the user"), :required => true
param :user_id, String, :desc => N_("ID of the user"), :required => true

def invalidate_jwt
@user = find_resource(:edit_users)
unless @user
raise ::Foreman::Exception.new(N_("No record found for %s"), params[:id])
end
@user.jwt_secret&.destroy
process_success _("Successfully invalidated registration tokens for %s.\n" % @user.login)
login = @user.login
render :json => { :message => _("Successfully invalidated registration tokens."), :user => login}, :status => :ok
end

api :DELETE, "/registration_tokens", N_("Invalidate all registration tokens for multiple users.")
api :DELETE, "/registration_tokens", N_("Invalidate all registration tokens for multiple users")
param :search, String, :desc => N_("URL-encoded search query that selects users for which registration tokens will be invalidated. Search query example: id ^ (2, 4, 6)"), :required => true
description <<-DOC
The users you specify will no longer be able to register hosts by using their JWTs.
Expand All @@ -51,7 +51,8 @@ def invalidate_jwt_tokens
if @users.blank?
raise ::Foreman::Exception.new(N_("No record found for search '%s'"), params[:search]) end
JwtSecret.where(user_id: @users).destroy_all
process_success _("Successfully invalidated registration tokens for %s.\n" % @users.pluck(:login).to_sentence)
login = @users.pluck(:login).to_sentence
render :json => { :message => _("Successfully invalidated registration tokens."), :users => login}, :status => :ok
end
end
end
Expand Down

0 comments on commit 1ad2139

Please sign in to comment.