Skip to content

Commit

Permalink
Fixes #37976 - API and rabl views for flatpak remotes
Browse files Browse the repository at this point in the history
  • Loading branch information
sjha4 committed Nov 6, 2024
1 parent da40587 commit 8b2e20d
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 0 deletions.
76 changes: 76 additions & 0 deletions app/controllers/katello/api/v2/flatpak_remotes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module Katello
class Api::V2::FlatpakRemotesController < Katello::Api::V2::ApiController
include Katello::Concerns::FilteredAutoCompleteSearch

before_action :find_authorized_katello_resource, :except => [:index, :create, :copy, :auto_complete_search]
before_action :find_optional_organization, :only => [:index, :auto_complete_search]

resource_description do
name 'Flatpak Remotes'
end

def_param_group :flatpak_remote do
param :description, String, :desc => N_("Description of the flatpak remote"), :required => false
param :username, String, :desc => N_("Username for the flatpak remote"), :required => false
param :token, String, :desc => N_("Token/password for the flatpak remote"), :required => false
end

api :GET, "/organizations/:organization_id/flatpak_remotes", N_("List flatpak remotes")
api :GET, "/flatpak_remotes", N_("List flatpak remotes")
param :organization_id, :number, :desc => N_("organization identifier"), :required => false
param :name, String, :desc => N_("Name of the flatpak remote"), :required => false
param :label, String, :desc => N_("Label of the flatpak remote"), :required => false
param_group :search, Api::V2::ApiController
add_scoped_search_description_for(FlatpakRemote)
def index
respond(:collection => scoped_search(index_relation, :name, :asc))
end

def index_relation
remotes = FlatpakRemote.readable
remotes = remotes.where(organization_id: @organization.id) if @organization
remotes = remotes.where(name: params[:name]) if params[:name]
remotes = remotes.where(label: params[:label]) if params[:label]
remotes
end

api :GET, "/flatpak_remotes/:id", N_("Show a content view")
param :id, :number, :desc => N_("Flatpak remote numeric identifier"), :required => true
def show
respond :resource => @flatpak_remote
end

api :POST, "/flatpak_remote", N_("Create a flatpak remote")
param :name, String, :desc => N_("name"), :required => true
param :url, String, :desc => N_("url"), :required => true
param :organization_id, :number, :desc => N_("organization identifier"), :required => true
param_group :flatpak_remote
def create
flatpak_remote = FlatpakRemote.new(flatpak_remote_params)
respond_for_create(resource: flatpak_remote)
end

api :PUT, "/flatpak_remotes/:id", N_("Update a flatpak remote")
param_group :flatpak_remote
param :id, :number, :desc => N_("Flatpak remote numeric identifier"), :required => true
param :name, String, :desc => N_("name")
param :url, String, :desc => N_("url")
def update
@flatpak_remote.update!(flatpak_remote_params)
end

api :DELETE, "/flatpak_remotes/:id", N_("Delete a flatpak remote")
param :id, :number, :desc => N_("Flatpak remote numeric identifier"), :required => true
def destroy
@flatpak_remote.destroy
end

def default_sort
%w(name asc)
end

def flatpak_remote_params
params.require(:flatpak_remote).permit(:name, :url, :organization_id, :description, :username, :token)
end
end
end
33 changes: 33 additions & 0 deletions app/models/katello/authorization/flatpak_remote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Katello
module Authorization::FlatpakRemote
extend ActiveSupport::Concern

include Authorizable

def readable?
authorized?(:view_flatpak_remotes)
end

def editable?
authorized?(:edit_flatpak_remotes)
end

def deletable?
authorized?(:destroy_flatpak_remotes)
end

module ClassMethods
def readable
authorized(:view_flatpak_remotes)
end

def editable
authorized(:edit_flatpak_remotes)
end

def deletable
authorized(:destroy_flatpak_remotes)
end
end
end
end
10 changes: 10 additions & 0 deletions app/models/katello/flatpak_remote.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
module Katello
class FlatpakRemote < Katello::Model
include Authorization::FlatpakRemote
include ForemanTasks::Concerns::ActionSubject

has_many :remote_repositories, dependent: :destroy, class_name: 'Katello::FlatpakRemoteRepository'
has_many :remote_repository_manifests, through: :remote_repositories, source: :remote_repository_manifests
belongs_to :organization, inverse_of: :flatpak_remotes

validates :name, presence: true
validates :url, presence: true
validates :organization_id, presence: true

scope :seeded, -> { where(:seeded => true) }

scoped_search :on => :name, :complete_value => true
scoped_search :on => :organization_id, :complete_value => true, :only_explicit => true, :validator => ScopedSearch::Validators::INTEGER
scoped_search :on => :url, :complete_value => true
scoped_search :on => :seeded, :complete_value => true
end
end
5 changes: 5 additions & 0 deletions app/views/katello/api/v2/flatpak_remotes/base.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extends 'katello/api/v2/common/identifier'
extends 'katello/api/v2/common/org_reference'

attributes :name
attributes :url, :description, :username, :token, :seeded
8 changes: 8 additions & 0 deletions app/views/katello/api/v2/flatpak_remotes/index.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object false

extends "katello/api/v2/common/index"
extends 'katello/api/v2/flatpak_remotes/permissions'

child @collection[:results] => :results do
extends "katello/api/v2/flatpak_remotes/base"
end
11 changes: 11 additions & 0 deletions app/views/katello/api/v2/flatpak_remotes/permissions.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
collection @flatpak_remotes

if ::Foreman::Cast.to_bool(params.fetch(:include_permissions, false))
user = User.current # current_user is not available here
node do
node(:can_create) { user.can?("create_flatpak_remotes") }
node(:can_edit) { user.can?("edit_flatpak_remotes") }
node(:can_delete) { user.can?("destroy_flatpak_remotes") }
node(:can_view) { user.can?("view_flatpak_remotes") }
end
end
3 changes: 3 additions & 0 deletions app/views/katello/api/v2/flatpak_remotes/show.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object @resource

extends "katello/api/v2/flatpak_remotes/base"
4 changes: 4 additions & 0 deletions config/routes/api/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class ActionDispatch::Routing::Mapper
get :auto_complete_search, :on => :collection
end

api_resources :flatpak_remotes, :only => [:index, :show, :create, :update, :destroy] do
get :auto_complete_search, :on => :collection
end

api_resources :capsules, :only => [:index, :show] do
member do
resource :content, :only => [], :controller => 'capsule_content' do
Expand Down
82 changes: 82 additions & 0 deletions lib/katello/permission_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def define
subscription_permissions
sync_plan_permissions
alternate_content_source_permissions
flatpak_remote_permissions
user_permissions
end

Expand Down Expand Up @@ -449,5 +450,86 @@ def organization_permissions
},
:resource_type => 'Organization'
end

def flatpak_remote_permissions
@plugin.permission :view_flatpak_remotes,
{
'katello/api/v2/flatpak_remotes' => [:index, :show, :auto_complete_search]
},
:resource_type => 'Katello::FlatpakRemote',
:finder_scope => :readable
@plugin.permission :create_flatpak_remotes,
{
'katello/api/v2/flatpak_remotes' => [:create]
},
:resource_type => 'Katello::FlatpakRemote',
:finder_scope => :creatable
@plugin.permission :edit_flatpak_remotes,
{
'katello/api/v2/flatpak_remotes' => [:update]
},
:resource_type => 'Katello::FlatpakRemote',
:finder_scope => :editable
@plugin.permission :destroy_flatpak_remotes,
{
'katello/api/v2/flatpak_remotes' => [:destroy]
},
:resource_type => 'Katello::FlatpakRemote',
:finder_scope => :deletable
end

def flatpak_remote_repository_permissions
@plugin.permission :view_flatpak_remote_repositories,
{
'katello/api/v2/flatpak_remote_repositories' => [:index, :show, :auto_complete_search]
},
:resource_type => 'Katello::FlatpakRemoteRepository',
:finder_scope => :readable
@plugin.permission :create_flatpak_remote_repositories,
{
'katello/api/v2/flatpak_remote_repositories' => [:create]
},
:resource_type => 'Katello::FlatpakRemoteRepository',
:finder_scope => :creatable
@plugin.permission :edit_flatpak_remote_repositories,
{
'katello/api/v2/flatpak_remote_repositories' => [:update]
},
:resource_type => 'Katello::FlatpakRemoteRepository',
:finder_scope => :editable
@plugin.permission :destroy_flatpak_remote_repositories,
{
'katello/api/v2/flatpak_remote_repositories' => [:destroy]
},
:resource_type => 'Katello::FlatpakRemoteRepository',
:finder_scope => :deletable
end

def flatpak_remote_repository_manifest_permissions
@plugin.permission :view_flatpak_remote_repository_manifests,
{
'katello/api/v2/flatpak_remote_repository_manifests' => [:index, :show, :auto_complete_search]
},
:resource_type => 'Katello::FlatpakRemoteRepositoryManifest',
:finder_scope => :readable
@plugin.permission :create_flatpak_remote_repository_manifests,
{
'katello/api/v2/flatpak_remote_repository_manifests' => [:create]
},
:resource_type => 'Katello::FlatpakRemoteRepositoryManifest',
:finder_scope => :creatable
@plugin.permission :edit_flatpak_remote_repository_manifests,
{
'katello/api/v2/flatpak_remote_repository_manifests' => [:update]
},
:resource_type => 'Katello::FlatpakRemoteRepositoryManifest',
:finder_scope => :editable
@plugin.permission :destroy_flatpak_remote_repository_manifests,
{
'katello/api/v2/flatpak_remote_repository_manifests' => [:destroy]
},
:resource_type => 'Katello::FlatpakRemoteRepositoryManifest',
:finder_scope => :deletable
end
end
end

0 comments on commit 8b2e20d

Please sign in to comment.