-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/perms' into 'main'
✨ (perms) Permissions system to access endpoints See merge request decidim/decidim-chatbot/decidim-module-rest_full!6
- Loading branch information
Showing
25 changed files
with
298 additions
and
66 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
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
27 changes: 27 additions & 0 deletions
27
app/controllers/decidim/rest_full/system/permissions_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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module RestFull | ||
module System | ||
class PermissionsController < Decidim::System::ApplicationController | ||
helper Decidim::Admin::AttributesDisplayHelper | ||
helper Decidim::Core::Engine.routes.url_helpers | ||
helper_method :destroy_admin_session_path | ||
|
||
def core_engine_routes | ||
Decidim::Core::Engine.routes.url_helpers | ||
end | ||
|
||
def create | ||
@form = form(ApiPermissions).from_params(params) | ||
api_client = Decidim::RestFull::ApiClient.find(@form.api_client_id) | ||
api_client.permissions = @form.permissions.map do |perm_string| | ||
api_client.permissions.build(permission: perm_string) | ||
end | ||
api_client.save! | ||
redirect_to core_engine_routes.edit_system_api_client_path(api_client) | ||
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module RestFull | ||
# The form that validates the data to construct a valid OAuthApplication. | ||
class ApiPermissions < Decidim::Form | ||
mimic :system_api_client | ||
attribute :permissions, [String] | ||
attribute :api_client_id, Integer | ||
|
||
def organization | ||
current_organization || Decidim::Organization.find_by(id: decidim_organization_id) | ||
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cancan" | ||
|
||
module Decidim | ||
module RestFull | ||
class Ability | ||
include ::CanCan::Ability | ||
attr_reader :api_client, :permissions | ||
|
||
def initialize(api_client) | ||
return unless api_client | ||
|
||
@api_client = api_client | ||
@permissions = api_client.permission_strings | ||
|
||
can :impersonate, Decidim::RestFull::ApiClient if permissions.include? "oauth.impersonate" | ||
can :login, Decidim::RestFull::ApiClient if permissions.include? "oauth.login" | ||
# Switch scopes and compose permissions | ||
scopes = api_client.scopes.to_a | ||
perms_for_public if scopes.include? "public" | ||
perms_for_system if scopes.include? "system" | ||
perms_for_proposals if scopes.include? "proposals" | ||
end | ||
|
||
def self.from_doorkeeper_token(doorkeeper_token) | ||
return Decidim::RestFull::Ability.new(nil) unless doorkeeper_token && doorkeeper_token.valid? | ||
return Decidim::RestFull::Ability.new(nil) unless doorkeeper_token.application.is_a? Decidim::RestFull::ApiClient | ||
|
||
Decidim::RestFull::Ability.new(doorkeeper_token.application) | ||
end | ||
|
||
private | ||
|
||
def perms_for_public; end | ||
|
||
def perms_for_system | ||
can :read, ::Decidim::Organization if permissions.include? "system.organizations.read" | ||
can :read, ::Decidim::User if permissions.include? "system.users.read" | ||
end | ||
|
||
def perms_for_proposals; 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module RestFull | ||
class Permission < ::ApplicationRecord | ||
self.table_name = "decidim_rest_full_api_client_permissions" | ||
|
||
belongs_to :api_client, class_name: "Decidim::RestFull::ApiClient" | ||
|
||
validates :permission, presence: true | ||
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
Oops, something went wrong.