activeadmin-refinerycms-authentication 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 164ccaf2c38de9e195e0b073f2fc5a90cc24dd4a
4
+ data.tar.gz: e0a639f835e86dd7faa07927bfe05d71686230e6
5
+ SHA512:
6
+ metadata.gz: ee56f47b688dd18ca803a690110ff672661512bef7b8321d3e3dea3e67f42d6e927141a73cba6495271fcecb815c9bec6d7c707dd8cd015d905af46919069e6e
7
+ data.tar.gz: 4844b0e8db4a5e07bd762019285552841a23ccf010120e76e5da6f6aad0fc2fe627d6dc98b8606019fc2999432c11c78576fac4cded44187bb2d7f40bfe266a2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activeadmin-refinerycms-authentication.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Appmospheres, and other contributors. All rights reserved.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # Add to Gemfile
2
+ gem 'refinerycms', '~> 3.0'
3
+ gem 'activeadmin', '~> 1.0.0pre'
4
+ gem 'devise'
5
+
6
+ gem 'activeadmin-refinerycms-authentication'
7
+ gem 'deface', '~> 1.0.0'
8
+
9
+ Run ActiveAdmin generator, then the refinery:cms generator.
10
+
11
+ # Add to routes
12
+ Make sure refinery is mounted in a subpath, not directly at root.
13
+
14
+ mount Refinery::Core::Engine, at: Refinery::Core.mounted_path
15
+
16
+ # Configure refinery authentication
17
+ `rails g activeadmin_refinery_authentication:install MODELNAME` where MODELNAME is the name of the ActiveAdmin user.
18
+
19
+ Add `plugins: []` to `permit_params` in the ActiveAdmin user model.
20
+
21
+ Add the plugins partial to the edit form of the ActiveAdmin user model:
22
+
23
+ panel t('panels.user_plugins') do
24
+ render 'admin/users/plugins_form', f: f
25
+ end
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "activeadmin-refinerycms-authentication"
5
+ s.version = "0.0.2"
6
+ s.authors = ["eugen neagoe"]
7
+ s.description = "A Refinery CMS and activeadmin connector"
8
+ s.summary = "This will tell Refinery CMS to use ActiveAdmin for authentication."
9
+ s.homepage = "https://github.com/appmospheres/activeadmin-refinery-authentication"
10
+
11
+ s.files = `git ls-files`.split($/)
12
+ s.test_files = s.files.grep(%r{^spec/})
13
+ s.require_paths = ["lib"]
14
+
15
+ s.add_runtime_dependency 'activeadmin', '~> 1.0.0pre'
16
+
17
+ s.add_runtime_dependency 'refinerycms-core', '~> 3.0.0'
18
+ s.add_runtime_dependency 'zilch-authorisation', '>= 0.0.1'
19
+ s.add_runtime_dependency 'decorators', '~> 2.0.0'
20
+ s.add_runtime_dependency 'deface', '~> 1.0.0'
21
+ end
@@ -0,0 +1,26 @@
1
+ require "activeadmin_refinery_authentication/authorisation_manager"
2
+
3
+ module ActiveAdminAuthenticationActionControllerBaseDecoration
4
+ def self.prepended(base)
5
+ base.prepend_before_action :detect_activeadmin_sign_on!
6
+ end
7
+
8
+ protected
9
+ def refinery_users_exist?
10
+ raise not_yet_implemented
11
+ end
12
+
13
+ private
14
+ def refinery_authorisation_manager
15
+ @refinery_authorisation_manager ||= ActiveAdminRefineryAuthentication::AuthorisationManager.new
16
+ end
17
+
18
+ def detect_activeadmin_sign_on!
19
+ current_user = send Rails.application.config.x.aa_refinery.current_user_method
20
+ if current_user
21
+ refinery_authorisation_manager.set_user!(current_user)
22
+ end
23
+ end
24
+ end
25
+
26
+ ActionController::Base.send :prepend, ActiveAdminAuthenticationActionControllerBaseDecoration
@@ -0,0 +1,24 @@
1
+ module ActiveAdminRefineryAuthenticationAdminControllerDecorator
2
+ protected
3
+ def authenticate_refinery_user!
4
+ begin
5
+ super
6
+ rescue Zilch::Authorisation::NotAuthorisedException
7
+ session["user_return_to"] = request.path
8
+ current_user = send(Rails.application.config.x.aa_refinery.current_user_method)
9
+ if current_user.present?
10
+ # avoid redirect loop
11
+ redirect_to main_app.admin_root_path and return
12
+ else
13
+ redirect_to main_app.send(Rails.application.config.x.aa_refinery.admin_login_path) and return
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+ def authorisation_manager
20
+ refinery_authorisation_manager
21
+ end
22
+ end
23
+
24
+ Refinery::AdminController.send :prepend, ActiveAdminRefineryAuthenticationAdminControllerDecorator
@@ -0,0 +1,6 @@
1
+ Refinery::ApplicationController.module_eval do
2
+ private
3
+ def authorisation_manager
4
+ refinery_authorisation_manager
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ Devise::SessionsController.class_eval do
2
+ skip_before_action :detect_activeadmin_sign_on!, only: [:create]
3
+ after_action :detect_activeadmin_sign_on!, only: [:create]
4
+
5
+ private
6
+ def after_sign_in_path_for(resource)
7
+ session["user_return_to"] || super
8
+ end
9
+ end
@@ -0,0 +1,76 @@
1
+ unless Rails.application.config.x.aa_refinery.empty?
2
+ Rails.application.config.x.aa_refinery.admin_user_class.constantize.class_eval do
3
+ has_many :plugins, -> { order('position ASC') },
4
+ class_name: "::UserPlugin", foreign_key: 'user_id', dependent: :destroy
5
+
6
+ # Should be overridden in authentication solutions.
7
+ def has_role?(role)
8
+ case
9
+ when role == :superuser
10
+ send Rails.application.config.x.aa_refinery.refinery_role_method
11
+ when role == :refinery
12
+ send Rails.application.config.x.aa_refinery.refinery_role_method
13
+ else
14
+ false
15
+ end
16
+ end
17
+
18
+ def self.available_plugins
19
+ Refinery::Plugins.registered.in_menu.map { |a|
20
+ { :name => a.name, :title => a.title }
21
+ }.sort_by { |a| a[:title] }
22
+ end
23
+
24
+ def plugins=(plugin_names)
25
+ filtered_names = filter_existing_plugins_for(string_plugin_names(plugin_names))
26
+ create_plugins_for(filtered_names)
27
+ end
28
+
29
+ def active_plugins
30
+ @active_plugins ||= Refinery::Plugins.new(
31
+ Refinery::Plugins.registered.select do |plugin|
32
+ authorised_plugins.include?(plugin.name)
33
+ end
34
+ )
35
+ end
36
+
37
+ def has_plugin?(name)
38
+ active_plugins.names.include?(name)
39
+ end
40
+
41
+ def authorised_plugins
42
+ plugins.collect(&:name) | ::Refinery::Plugins.always_allowed.names
43
+ end
44
+ alias_method :authorized_plugins, :authorised_plugins
45
+
46
+ def landing_url
47
+ active_plugins.in_menu.first_url_in_menu
48
+ end
49
+
50
+ private
51
+
52
+ def string_plugin_names(plugin_names)
53
+ plugin_names.select{ |plugin_name| plugin_name.is_a?(String) }
54
+ end
55
+
56
+ def create_plugins_for(plugin_names)
57
+ plugin_names.each { |plugin_name| plugins.create name: plugin_name, position: plugin_position}
58
+ end
59
+
60
+ def plugin_position
61
+ plugins.select(:position).map{ |p| p.position.to_i}.max.to_i + 1
62
+ end
63
+
64
+ def filter_existing_plugins_for(plugin_names)
65
+ assigned_plugins = plugins.load
66
+ assigned_plugins.each do |assigned_plugin|
67
+ if plugin_names.include?(assigned_plugin.name)
68
+ plugin_names.delete(assigned_plugin.name)
69
+ else
70
+ assigned_plugin.destroy
71
+ end
72
+ end
73
+ plugin_names
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ class UserPlugin < Refinery::Core::BaseModel
2
+ self.table_name = 'active_admin_refinery_user_plugins'
3
+ belongs_to Rails.application.config.x.aa_refinery.admin_user_class.singularize.underscore.to_sym,
4
+ foreign_key: 'user_id'
5
+ end
@@ -0,0 +1,7 @@
1
+ Deface::Override.new(
2
+ virtual_path: 'refinery/admin/_menu',
3
+ original: '71468c0788f57fd03c9c6c2ebe3c6c026be5aeb5',
4
+ name: 'active_admin_link',
5
+ insert_top: '#menu',
6
+ text: "<%= link_to I18n.t('active_admin.title', default: 'ActiveAdmin'), main_app.admin_root_path %>"
7
+ )
@@ -0,0 +1 @@
1
+ require "activeadmin_refinery_authentication/engine"
@@ -0,0 +1,29 @@
1
+ require "refinery/core/authorisation_adapter"
2
+
3
+ module ActiveAdminRefineryAuthentication
4
+ class AuthorisationAdapter < Refinery::Core::AuthorisationAdapter
5
+
6
+ def current_user
7
+ @current_user ||= Rails.application.config.x.aa_refinery.admin_user_class.constantize.new
8
+ end
9
+
10
+ def current_user=(user)
11
+ @current_user = user
12
+ end
13
+
14
+ def allow?(operation, resource)
15
+ case
16
+ when resource == :site_bar
17
+ current_user.has_role?(:refinery)
18
+ when operation == :plugin
19
+ current_user.active_plugins.names.include?(resource)
20
+ when operation == :controller
21
+ current_user.active_plugins.any? do |plugin|
22
+ Regexp.new(plugin.menu_match) === resource
23
+ end
24
+ else
25
+ false
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require "refinery/core/authorisation_manager"
2
+ require "activeadmin_refinery_authentication/authorisation_adapter"
3
+
4
+ module ActiveAdminRefineryAuthentication
5
+ class AuthorisationManager < Refinery::Core::AuthorisationManager
6
+
7
+ def authenticate!
8
+ unless adapter.current_user.send(config.refinery_role_method)
9
+ raise Zilch::Authorisation::NotAuthorisedException
10
+ end
11
+
12
+ adapter.current_user
13
+ end
14
+
15
+ def default_adapter
16
+ @default_adapter ||= ActiveAdminRefineryAuthentication::AuthorisationAdapter.new
17
+ end
18
+
19
+ def set_user!(user)
20
+ adapter.current_user = user
21
+ end
22
+
23
+ private
24
+
25
+ def config
26
+ Rails.application.config.x.aa_refinery
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,56 @@
1
+ require 'refinery/engine'
2
+ require 'zilch/authorisation'
3
+
4
+ module ActiveAdminRefineryAuthentication
5
+ class Engine < Rails::Engine
6
+
7
+ include Refinery::Engine
8
+ engine_name "activeadmin_refinery_authentication"
9
+
10
+ config.autoload_paths += %W( #{config.root}/lib )
11
+
12
+ before_inclusion do
13
+ Refinery::Plugin.register do |plugin|
14
+ plugin.name = 'activeadmin_refinery_authentication'
15
+ plugin.pathname = root
16
+ plugin.hide_from_menu = true
17
+ plugin.always_allow_access = true
18
+ end
19
+ end
20
+
21
+ config.to_prepare do
22
+ if defined?(WillPaginate)
23
+ ::WillPaginate::ActiveRecord::RelationMethods.module_eval do
24
+ def per_page(num)
25
+ if (n = num.to_i) <= 0
26
+ self
27
+ else
28
+ limit(n).offset(offset_value / limit_value * n)
29
+ end
30
+ end
31
+
32
+ def total_pages
33
+ (total_count.to_f / limit_value).ceil
34
+ end
35
+
36
+ alias_method :per, :per_page
37
+ alias_method :num_pages, :total_pages
38
+ alias_method :total_count, :total_entries
39
+ alias_method :prev_page, :previous_page
40
+ end
41
+ end
42
+ end
43
+
44
+ config.after_initialize do
45
+ Rails.application.reload_routes!
46
+
47
+ if Rails.application.config.x.aa_refinery.show_refinery_in_active_admin
48
+ ::ActiveAdmin.register_page 'Refinery CMS' do
49
+ menu label: I18n.t('refinery.plugins.refinery_core.title'),
50
+ url: Refinery::Core.backend_path,
51
+ priority: Rails.application.config.x.aa_refinery.refinery_active_admin_menu_priority
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActiveadminRefineryAuthentication
4
+ module Generators
5
+ class InstallGenerator < ActiveRecord::Generators::Base
6
+ desc "Installs Refinery Authentication via Active Admin and generates the necessary migrations"
7
+ argument :name, type: :string, default: 'AdminUser'
8
+
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ def copy_initializer
12
+ @user_class = name
13
+ @formatted_user_name = name.underscore.gsub('/', '_')
14
+ template 'activeadmin-refinery-authentication.rb.erb', 'config/initializers/activeadmin_refinery_authentication.rb'
15
+ end
16
+
17
+ def copy_views
18
+ @user_class = name
19
+ @formatted_user_name = name.underscore.gsub('/', '_')
20
+ template '_plugins_form.html.erb', 'app/views/admin/users/_plugins_form.html.erb'
21
+ end
22
+
23
+ def create_migrations
24
+ migration_template 'migrations/create_user_plugins.rb', 'db/migrate/create_user_plugins.rb'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ <div class='field plugin_access'>
2
+ <ul id='plugins' class='checkboxes'>
3
+ <%% <%= @user_class %>.available_plugins.each do |plugin| -%>
4
+ <%% if Refinery::Plugins.always_allowed.names.include?(plugin[:name]) %>
5
+ <%%= hidden_field_tag '<%= @formatted_user_name %>[plugins][]', plugin[:name], :id => "plugins_#{plugin[:name]}" %>
6
+ <%% else %>
7
+ <li>
8
+ <%%= check_box_tag '<%= @formatted_user_name %>[plugins][]', plugin[:name],
9
+ @<%= @formatted_user_name %>.plugins.map(&:name).include?(plugin[:name]),
10
+ :id => "plugins_#{plugin[:name]}" %>
11
+ <%%= f.label '<%= @formatted_user_name %>[plugins][]',
12
+ t('title', :scope => "refinery.plugins.#{plugin[:name].downcase}", :default => plugin[:title]),
13
+ :class => "stripped",
14
+ :for => "plugins_#{plugin[:name]}" %>
15
+ </li>
16
+ <%% end %>
17
+ <%% end %>
18
+ </ul>
19
+ </div>
@@ -0,0 +1,8 @@
1
+ Rails.application.config.x.aa_refinery.show_refinery_in_active_admin = true
2
+ Rails.application.config.x.aa_refinery.refinery_active_admin_menu_priority = 1
3
+
4
+ Rails.application.config.x.aa_refinery.current_user_method = :current_<%= @formatted_user_name %>
5
+ Rails.application.config.x.aa_refinery.admin_login_path = :new_<%= @formatted_user_name %>_session_path
6
+ Rails.application.config.x.aa_refinery.admin_user_class = '<%= @user_class %>'
7
+ Rails.application.config.x.aa_refinery.refinery_role_method = :admin?
8
+
@@ -0,0 +1,12 @@
1
+ class CreateUserPlugins < ActiveRecord::Migration
2
+ def change
3
+ create_table :active_admin_refinery_user_plugins do |t|
4
+ t.integer :user_id
5
+ t.string :name
6
+ t.integer :position
7
+ end
8
+
9
+ add_index :active_admin_refinery_user_plugins, :name
10
+ add_index :active_admin_refinery_user_plugins, [:user_id, :name], unique: true
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeadmin-refinerycms-authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - eugen neagoe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeadmin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0pre
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0pre
27
+ - !ruby/object:Gem::Dependency
28
+ name: refinerycms-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: zilch-authorisation
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: decorators
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: deface
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0
83
+ description: A Refinery CMS and activeadmin connector
84
+ email:
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - Gemfile
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - activeadmin-refinerycms-authentication.gemspec
94
+ - app/decorators/controllers/action_controller_base_decorator.rb
95
+ - app/decorators/controllers/refinery/admin_controller_decorator.rb
96
+ - app/decorators/controllers/refinery/application_controller_decorator.rb
97
+ - app/decorators/controllers/sessions_controller_decorator.rb
98
+ - app/decorators/models/user_decorator.rb
99
+ - app/models/user_plugin.rb
100
+ - app/overrides/active_admin_link.rb
101
+ - lib/activeadmin-refinerycms-authentication.rb
102
+ - lib/activeadmin_refinery_authentication/authorisation_adapter.rb
103
+ - lib/activeadmin_refinery_authentication/authorisation_manager.rb
104
+ - lib/activeadmin_refinery_authentication/engine.rb
105
+ - lib/generators/activeadmin_refinery_authentication/install/install_generator.rb
106
+ - lib/generators/activeadmin_refinery_authentication/install/templates/_plugins_form.html.erb
107
+ - lib/generators/activeadmin_refinery_authentication/install/templates/activeadmin-refinery-authentication.rb.erb
108
+ - lib/generators/activeadmin_refinery_authentication/install/templates/migrations/create_user_plugins.rb
109
+ - pkg/activeadmin-refinerycms-authentication-0.0.2.gem
110
+ homepage: https://github.com/appmospheres/activeadmin-refinery-authentication
111
+ licenses: []
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.7
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: This will tell Refinery CMS to use ActiveAdmin for authentication.
133
+ test_files: []