From f31a79c5bc3678949b7334486daa196a17a5db92 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Tue, 19 Dec 2017 19:42:52 +0100 Subject: Create Organisation#features and FeatureChecker controller concerns. Refs #5339 --- app/controllers/application_controller.rb | 1 + app/controllers/concerns/feature_checker.rb | 41 +++++++++++++++++++++++++++++ app/controllers/workbenches_controller.rb | 3 +++ app/models/organisation.rb | 5 ++++ 4 files changed, 50 insertions(+) create mode 100644 app/controllers/concerns/feature_checker.rb (limited to 'app') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 97f5548ae..474277da1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,7 @@ class ApplicationController < ActionController::Base include PaperTrailSupport include Pundit + include FeatureChecker rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized diff --git a/app/controllers/concerns/feature_checker.rb b/app/controllers/concerns/feature_checker.rb new file mode 100644 index 000000000..c23e672cf --- /dev/null +++ b/app/controllers/concerns/feature_checker.rb @@ -0,0 +1,41 @@ +# Check availability of optional features +# +# In your controller, use : +# +# requires_feature :test +# requires_feature :test, only: [:show] +# +# In your view, use : +# +# has_feature? :test +# +module FeatureChecker + extend ActiveSupport::Concern + + module ClassMethods + def requires_feature(feature, options = {}) + before_action options do + check_feature! feature + end + end + end + + included do + helper_method :has_feature? + end + + protected + + def has_feature?(*features) + features.all? do |feature| + current_organisation.has_feature? feature + end + end + + def check_feature!(*features) + authorized = has_feature? *features + raise NotAuthorizedError, "Feature not autorized" unless authorized + end + + class NotAuthorizedError < StandardError; end +end diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index b2dac9e67..4da95df7a 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -5,6 +5,9 @@ class WorkbenchesController < ChouetteController defaults resource_class: Workbench respond_to :html, only: [:show, :index] + include FeatureChecker + requires_feature :test, only: :index + def index redirect_to dashboard_path end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 4343c87af..da7d1fcf3 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -1,3 +1,4 @@ +# coding: utf-8 class Organisation < ActiveRecord::Base include DataFormatEnumerations @@ -75,4 +76,8 @@ class Organisation < ActiveRecord::Base STIF::CodifligneLineId.lines_set_from_functional_scope( functional_scope ) end + def has_feature?(feature) + features && features.include?(feature.to_s) + end + end -- cgit v1.2.3 From 3305dca57e2e0a46b583f183040a82819ffd6efb Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Wed, 20 Dec 2017 21:06:41 +0100 Subject: Improve styles in FeatureCheck code. Refs #5339 --- app/controllers/concerns/feature_checker.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/controllers/concerns/feature_checker.rb b/app/controllers/concerns/feature_checker.rb index c23e672cf..9ca5ed0a7 100644 --- a/app/controllers/concerns/feature_checker.rb +++ b/app/controllers/concerns/feature_checker.rb @@ -33,8 +33,9 @@ module FeatureChecker end def check_feature!(*features) - authorized = has_feature? *features - raise NotAuthorizedError, "Feature not autorized" unless authorized + unless has_feature?(*features) + raise NotAuthorizedError, "Feature not autorized" + end end class NotAuthorizedError < StandardError; end -- cgit v1.2.3