multi_client 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cb959e3e7261ece07ebd98c3c50024e9aeeebf00
4
+ data.tar.gz: 12f046e79761540fa66f8ac99f6cd23c78e08b56
5
+ SHA512:
6
+ metadata.gz: ffd03a80b02d73e9354aea0581d0192685217591380e5d1f602f9e5aae486f2fb45c20f07d59542f04354c9c51d637d5de5a39db273a7f4afcf905c60ce08a27
7
+ data.tar.gz: 575e6f74d0533afdfe35dababf3b6a998bd5506909f3572f1367458897e34d12081c0089280b5aee5ac5216666563c1dbb68d8dbd1f0e0997f3e80f00afa1ea4
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Roberto Vasquez Angel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ = MultiClient
2
+
3
+ = Installation
4
+
5
+ Add it to your gemfile:
6
+
7
+ # Gemfile
8
+ gem 'multi_client'
9
+
10
+ Run the installer:
11
+
12
+ > rails generate multi_client:install
13
+
14
+ Add migrations and migrate
15
+
16
+ > rake multi_client:install:migrations && rake db:migrate
17
+
18
+ = How do I set the current client?
19
+
20
+ # console
21
+ MultiClient::Client.set_current_by_identifier '100'
22
+
23
+ = How do I unset the current client?
24
+
25
+ # console
26
+ MultiClient::Client.unset_current
27
+
28
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'MultiClient'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,31 @@
1
+ module MultiClient
2
+ module ControllerWithClient
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ around_action :set_current_client
7
+
8
+ helper_method :current_client
9
+ end
10
+
11
+ private
12
+
13
+ def client_class
14
+ Configuration.model_name.constantize
15
+ end
16
+
17
+ def current_client
18
+ @current_client ||= client_class.enabled.find(MultiClient::Client.current_id) if MultiClient::Client.current_id
19
+ end
20
+
21
+ def set_current_client
22
+ redirect_to(root_url(subdomain: 'www')) && return unless current_client = client_class.enabled.find_by_subdomain(request.subdomains.first)
23
+ client_class.current_id = current_client.id
24
+ begin
25
+ yield
26
+ ensure
27
+ @current_client = client_class.current_id = nil
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module MultiClient
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,2 @@
1
+ class UnscopedForbiddenError < StandardError
2
+ end
@@ -0,0 +1,4 @@
1
+ module MultiClient
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module MultiClientHelper
2
+ def render_client_navigation
3
+ clients = MultiClient::Client.all
4
+ render 'multi_client/client_navigation', clients: clients
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ module MultiClient
2
+ module ModelWithClient
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ klass = Class.new(self) do
7
+ default_scope { unscoped }
8
+
9
+ def unscoped
10
+ super
11
+ end
12
+ end
13
+ const_set 'Unscoped', klass
14
+
15
+ belongs_to MultiClient::Configuration.method_name.to_sym, class_name: MultiClient::Configuration.model_name
16
+
17
+ scope "for_current_#{MultiClient::Configuration.method_name}".to_sym, -> { where(MultiClient::Configuration.foreign_key.to_sym => MultiClient::Configuration.model_name.constantize.current_id) }
18
+ default_scope { send("for_current_#{MultiClient::Configuration.method_name}".to_sym) }
19
+
20
+ validates MultiClient::Configuration.foreign_key.to_sym, presence: true
21
+
22
+ ::MultiClient::Client.has_many name.demodulize.underscore.pluralize.to_sym, class_name: "::#{name}", foreign_key: MultiClient::Configuration.foreign_key.to_sym
23
+ end
24
+
25
+ class_methods do
26
+ def unscoped
27
+ return super if name.demodulize == 'Unscoped'
28
+ caller = caller_locations(1, 1)[0].label
29
+ return where(MultiClient::Configuration.foreign_key.to_sym => MultiClient::Configuration.model_name.constantize.current_id) if MultiClient::Configuration.force_client_scope_for_unscoped_callers.include?(caller)
30
+ return super if MultiClient::Configuration.allowed_unscoped_callers.include?(caller)
31
+ raise UnscopedForbiddenError, "Calling unscoped from #{caller} is not allowed to prevent client data leakage"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ module MultiClient
2
+ class Client < ActiveRecord::Base
3
+ validates :identifier, presence: true,
4
+ uniqueness: true
5
+
6
+ validates :subdomain, presence: true,
7
+ uniqueness: true
8
+
9
+ scope :enabled, -> { where(enabled: true) }
10
+
11
+ def self.current_id=(id)
12
+ Thread.current[:client_id] = id
13
+ end
14
+
15
+ def self.current_id
16
+ Thread.current[:client_id]
17
+ end
18
+
19
+ def human
20
+ identifier
21
+ end
22
+
23
+ def self.set_current_by_identifier(identifier)
24
+ self.current_id = where(identifier: identifier).first.try(:id)
25
+ end
26
+
27
+ def self.unset_current
28
+ self.current_id = nil
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>MultiClient</title>
5
+ <%= stylesheet_link_tag "multi_client/application", media: "all" %>
6
+ <%= javascript_include_tag "multi_client/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,9 @@
1
+ %li.dropdown.active
2
+ = link_to('#', { class: 'dropdown-toggle', 'data-toggle': 'dropdown', role: 'button', 'aria-haspopup': true, 'aria-expanded': false }) do
3
+ = t("classes.#{MultiClient::Client.name.underscore}")
4
+ %span.caret
5
+ %ul.dropdown-menu
6
+ - MultiClient::Client.enabled.all.each do |client|
7
+ %li{ class: (request.subdomain == client.subdomain ? 'active' : nil) }
8
+ = link_to(url_for(host: "#{client.subdomain}.#{request.domain}")) do
9
+ = "#{client.identifier} (#{client.subdomain})"
@@ -0,0 +1,4 @@
1
+ <% if MultiClient::Client.current_id %>
2
+ Current <%= MultiClient::Configuration.model_name.constantize.model_name.human %>: <%= current_client.human %>
3
+ <%= link_to 'Logout', sessions_url(subdomain: 'www'), method: :delete %>
4
+ <% end %>
@@ -0,0 +1,3 @@
1
+ de:
2
+ classes:
3
+ multi_client/client: Mandant
@@ -0,0 +1,3 @@
1
+ en:
2
+ classes:
3
+ multi_client/client: Mandant
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ MultiClient::Engine.routes.draw do
2
+ end
@@ -0,0 +1,11 @@
1
+ class CreateMultiClientClients < ActiveRecord::Migration
2
+ def change
3
+ create_table :multi_client_clients do |t|
4
+ t.string :identifier, null: false
5
+ t.string :subdomain, null: false
6
+ t.boolean :enabled
7
+
8
+ t.timestamps null: false
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module MultiClient
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc 'Generates the initializer'
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def generate_intializer
9
+ copy_file 'multi_client.rb', 'config/initializers/multi_client.rb'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ MultiClient.configure do |config|
2
+ # Client model name
3
+ #
4
+ # default: config.model_name = 'Client'
5
+ config.model_name = 'Client'
6
+
7
+ # Foreign key column
8
+ #
9
+ # default: config.foreign_key = 'client_id'
10
+ config.foreign_key = 'client_id'
11
+
12
+ # Method name pre/suffix
13
+ #
14
+ # default: config.method_name = 'client'
15
+ config.method_name = 'client'
16
+
17
+ # Calling unscoped is blocked to prevent data leakage. You can define
18
+ # exceptions here.
19
+ #
20
+ # default: config.allowed_unscoped_callers = %w(
21
+ # _create_record
22
+ # _update_record
23
+ # aggregate_column
24
+ # bottom_item
25
+ # eval_scope
26
+ # relation_for_destroy
27
+ # reload
28
+ # scope
29
+ # scope_for_slug_generator
30
+ # update_counters
31
+ # update_positions
32
+ # validate_each
33
+ # )
34
+ #
35
+ config.allowed_unscoped_callers = %w(
36
+ _create_record
37
+ _update_record
38
+ aggregate_column
39
+ bottom_item
40
+ eval_scope
41
+ relation_for_destroy
42
+ reload
43
+ scope
44
+ scope_for_slug_generator
45
+ update_counters
46
+ update_positions
47
+ validate_each
48
+ )
49
+
50
+ # Calling unscoped is blocked to prevent data leakage. You can override the behaviour of unscoped
51
+ # here. If the caller is in this list, it wont get the unscoped scope, but a client scoped relation.
52
+ #
53
+ # default: config.force_client_scope_for_unscoped_callers = ['aggregate_column']
54
+ #
55
+ config.force_client_scope_for_unscoped_callers = ['aggregate_column']
56
+ end
@@ -0,0 +1,27 @@
1
+ module MultiClient
2
+ module Configuration
3
+ def configure
4
+ yield self
5
+ end
6
+
7
+ mattr_accessor :model_name do
8
+ 'Client'
9
+ end
10
+
11
+ mattr_accessor :foreign_key do
12
+ 'client_id'
13
+ end
14
+
15
+ mattr_accessor :method_name do
16
+ 'client'
17
+ end
18
+
19
+ mattr_accessor(:allowed_unscoped_callers) { [] }
20
+
21
+ mattr_accessor(:force_client_scope_for_unscoped_callers) { [] }
22
+
23
+ def self.namespaced_model_name
24
+ "MultiClient::#{model_name}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+ module MultiClient
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace MultiClient
4
+
5
+ config.autoload_paths += Dir["#{config.root}/lib/**/"]
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module MultiClient
2
+ class NoSubdomain
3
+ def self.matches?(request)
4
+ case request.subdomain
5
+ when 'www', '', nil
6
+ true
7
+ else
8
+ false
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module MultiClient
2
+ class Subdomain
3
+ def self.matches?(request)
4
+ case request.subdomain
5
+ when 'www', '', nil
6
+ false
7
+ else
8
+ true
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module MultiClient
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'multi_client/engine'
2
+ require 'multi_client/configuration'
3
+
4
+ module MultiClient
5
+ extend Configuration
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :multi_client do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Vasquez Angel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jquery-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: factory_girl_rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: shoulda-matchers
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Easy MultiClient Support for Rails.
126
+ email:
127
+ - roberto@vasquez-angel.de
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - README.rdoc
134
+ - Rakefile
135
+ - app/assets/javascripts/multi_client/application.js
136
+ - app/assets/stylesheets/multi_client/application.css
137
+ - app/controllers/concerns/multi_client/controller_with_client.rb
138
+ - app/controllers/multi_client/application_controller.rb
139
+ - app/exceptions/unscoped_forbidden_error.rb
140
+ - app/helpers/multi_client/application_helper.rb
141
+ - app/helpers/multi_client_helper.rb
142
+ - app/models/concerns/multi_client/model_with_client.rb
143
+ - app/models/multi_client/client.rb
144
+ - app/views/layouts/multi_client/application.html.erb
145
+ - app/views/multi_client/_client_navigation.haml
146
+ - app/views/multi_client/_current_client.html.erb
147
+ - config/locales/de.yml
148
+ - config/locales/en.yml
149
+ - config/routes.rb
150
+ - db/migrate/001_create_multi_client_clients.rb
151
+ - lib/generators/multi_client/install/install_generator.rb
152
+ - lib/generators/multi_client/install/templates/multi_client.rb
153
+ - lib/multi_client.rb
154
+ - lib/multi_client/configuration.rb
155
+ - lib/multi_client/engine.rb
156
+ - lib/multi_client/no_subdomain.rb
157
+ - lib/multi_client/subdomain.rb
158
+ - lib/multi_client/version.rb
159
+ - lib/tasks/multi_client_tasks.rake
160
+ homepage: https://github.com/robotex82/multi_client
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.4.8
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: MultiClient Module.
184
+ test_files: []