authist 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +32 -0
  4. data/lib/authist.rb +16 -0
  5. data/lib/authist/authism.rb +14 -0
  6. data/lib/authist/controller_helpers.rb +31 -0
  7. data/lib/authist/models.rb +6 -0
  8. data/lib/authist/models/role.rb +25 -0
  9. data/lib/authist/models/role_subscription.rb +8 -0
  10. data/lib/authist/version.rb +3 -0
  11. data/lib/generators/active_record/access_type_generator.rb +15 -0
  12. data/lib/generators/active_record/authist_generator.rb +21 -0
  13. data/lib/generators/active_record/install_generator.rb +17 -0
  14. data/lib/generators/active_record/templates/access_type_migration.rb +7 -0
  15. data/lib/generators/authist/access_type_generator.rb +11 -0
  16. data/lib/generators/authist/authist_generator.rb +11 -0
  17. data/lib/generators/authist/install_generator.rb +15 -0
  18. data/lib/generators/templates/authist.rb +4 -0
  19. data/lib/generators/templates/core_migration.rb +17 -0
  20. data/test/dummy/README.rdoc +28 -0
  21. data/test/dummy/Rakefile +6 -0
  22. data/test/dummy/app/assets/javascripts/application.js +13 -0
  23. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  24. data/test/dummy/app/controllers/application_controller.rb +5 -0
  25. data/test/dummy/app/helpers/application_helper.rb +2 -0
  26. data/test/dummy/app/models/user.rb +3 -0
  27. data/test/dummy/app/models/user_group.rb +3 -0
  28. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/test/dummy/bin/bundle +3 -0
  30. data/test/dummy/bin/rails +4 -0
  31. data/test/dummy/bin/rake +4 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/config/application.rb +23 -0
  34. data/test/dummy/config/boot.rb +5 -0
  35. data/test/dummy/config/database.yml +25 -0
  36. data/test/dummy/config/environment.rb +5 -0
  37. data/test/dummy/config/environments/development.rb +29 -0
  38. data/test/dummy/config/environments/production.rb +80 -0
  39. data/test/dummy/config/environments/test.rb +36 -0
  40. data/test/dummy/config/initializers/authist.rb +4 -0
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  43. data/test/dummy/config/initializers/inflections.rb +16 -0
  44. data/test/dummy/config/initializers/mime_types.rb +5 -0
  45. data/test/dummy/config/initializers/secret_token.rb +12 -0
  46. data/test/dummy/config/initializers/session_store.rb +3 -0
  47. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/test/dummy/config/locales/en.yml +23 -0
  49. data/test/dummy/config/routes.rb +56 -0
  50. data/test/dummy/db/development.sqlite3 +0 -0
  51. data/test/dummy/db/migrate/20140315092237_create_users.rb +7 -0
  52. data/test/dummy/db/migrate/20141021153614_create_authistic_behaviour.rb +17 -0
  53. data/test/dummy/db/migrate/20141021160014_add_access_type_shoe_lacing.rb +7 -0
  54. data/test/dummy/db/migrate/20141021200700_create_user_groups.rb +9 -0
  55. data/test/dummy/db/schema.rb +42 -0
  56. data/test/dummy/db/test.sqlite3 +0 -0
  57. data/test/dummy/log/development.log +420 -0
  58. data/test/dummy/log/test.log +10 -0
  59. data/test/dummy/public/404.html +58 -0
  60. data/test/dummy/public/422.html +58 -0
  61. data/test/dummy/public/500.html +57 -0
  62. data/test/dummy/public/favicon.ico +0 -0
  63. data/test/test_helper.rb +15 -0
  64. metadata +201 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c7422a9ad57d23dd5eeb3f96bdfcac245c17321
4
+ data.tar.gz: f11023381203387eeb30549b892f5410c6ed00b0
5
+ SHA512:
6
+ metadata.gz: bca1e2f4f60485bee97b0c2c259ef42eca59c7c11e80dd2d91e34c9b6da1c60e369cf46b7d489da89896878094f679f8317701f82c108b87267bba09cb5e1a3b
7
+ data.tar.gz: ff5bdebd10f6ebf16227e11e84048145ce53dfb42c16d06c84ee7a82e23272ab2c31b858a7c48594647b20a31123cbd610b96f71acd688684a4e471f7cb2c7d3
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Ruben 'angryzor' Tytgat
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.
@@ -0,0 +1,32 @@
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 = 'Authist'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,16 @@
1
+ module Authist
2
+ autoload :Authism, 'authist/authism'
3
+ autoload :Models, 'authist/models'
4
+ autoload :ControllerHelpers, 'authist/controller_helpers'
5
+
6
+ @@disable_controller_extensions = false
7
+ mattr_accessor :disable_controller_extensions
8
+
9
+ def self.setup
10
+ yield self
11
+
12
+ unless disable_controller_extensions
13
+ ApplicationController.include ControllerHelpers
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module Authist
2
+ module Authism
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :role_subscriptions, as: :authorizable, class_name: '::Authist::Models::RoleSubscription'
7
+ has_many :roles, through: :role_subscriptions, class_name: '::Authist::Models::Role'
8
+ end
9
+
10
+ def has_access? access_type
11
+ roles.any? { |role| role.allows_access? access_type }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,31 @@
1
+ module Authist
2
+ module ControllerHelpers
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ def access_required type
9
+ truth_required current_user.has_access?(type)
10
+ end
11
+
12
+ def user_or_access_required user, type
13
+ truth_required current_user == user || current_user.has_access?(type)
14
+ end
15
+
16
+ def user_required user
17
+ truth_required current_user == user
18
+ end
19
+
20
+ def truth_required clause
21
+ unless clause
22
+ unauthorized_access
23
+ false
24
+ end
25
+ end
26
+
27
+ def unauthorized_access
28
+ redirect root_path
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ module Authist
2
+ module Models
3
+ autoload :Role, 'authist/models/role'
4
+ autoload :RoleSubscription, 'authist/models/role_subscription'
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ module Authist
2
+ module Models
3
+ class Role < ActiveRecord::Base
4
+ has_and_belongs_to_many :included_roles, class_name: "Role", foreign_key: "includer_id"
5
+ has_many :role_subscriptions
6
+
7
+ def allows_access? access_type
8
+ allows_access_directly? access_type or allows_access_transitively? access_type
9
+ end
10
+
11
+ private
12
+ def allows_access_directly? access_type
13
+ begin
14
+ send access_type
15
+ rescue NoMethodError => e
16
+ raise "No such access type: #{access_type}"
17
+ end
18
+ end
19
+
20
+ def allows_access_transitively? access_type
21
+ included_roles.any? { |role| role.allows_access? access_type }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ module Authist
2
+ module Models
3
+ class RoleSubscription < ActiveRecord::Base
4
+ belongs_to :role
5
+ belongs_to :authorizable, polymorphic: true
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Authist
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActiveRecord
4
+ module Generators
5
+ class AccessTypeGenerator < ActiveRecord::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ desc "Add access type."
9
+
10
+ def generate_migrations
11
+ migration_template "access_type_migration.rb", "db/migrate/add_access_type_#{singular_name}.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActiveRecord
4
+ module Generators
5
+ class AuthistGenerator < ActiveRecord::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ desc "Turns an ActiveModel model into an authist."
9
+
10
+ def include_authism_in_model
11
+ class_path = if namespaced?
12
+ class_name.to_s.split("::")
13
+ else
14
+ [class_name]
15
+ end
16
+
17
+ inject_into_class File.join("app", "models", "#{file_path}.rb"), class_path.last, " include Authist::Authism\n"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActiveRecord
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include ActiveRecord::Generators::Migration
7
+
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+
10
+ desc "Creates the core Authist ActiveRecord migrations."
11
+
12
+ def generate_migrations
13
+ migration_template "core_migration.rb", "db/migrate/create_authistic_behaviour.rb"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ class AddAccessType<%= singular_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ change_table :roles do |t|
4
+ t.boolean :<%= singular_name %>
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Authist
2
+ module Generators
3
+ class AccessTypeGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Creates a new access type."
7
+
8
+ hook_for :orm
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Authist
2
+ module Generators
3
+ class AuthistGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Turns a model into an Authist."
7
+
8
+ hook_for :orm
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Authist
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Creates an authist initializer and core migrations."
7
+
8
+ hook_for :orm
9
+
10
+ def copy_initializer
11
+ template "authist.rb", "config/initializers/authist.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ Authist.setup do |config|
2
+ # Uncomment this if you don't want the controller helpers.
3
+ # config.disable_controller_extensions
4
+ end
@@ -0,0 +1,17 @@
1
+ class CreateAuthisticBehaviour < ActiveRecord::Migration
2
+ def change
3
+ create_table :roles do |t|
4
+ t.string :name
5
+ end
6
+
7
+ create_table :role_subscriptions do |t|
8
+ t.references :role, index: true
9
+ t.references :authorizable, index: { name: 'index_role_subs_on_auth_id_and_auth_type_authist' }, polymorphic: true
10
+ end
11
+
12
+ create_table :roles_roles do |t|
13
+ t.references :role, index: true
14
+ t.references :includer, index: true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_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 vendor/assets/javascripts of plugins, if any, 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/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
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 vendor/assets/stylesheets of plugins, if any, 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 top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ include Authist::Authism
3
+ end
@@ -0,0 +1,3 @@
1
+ class UserGroup < ActiveRecord::Base
2
+ include Authist::Authism
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require "authist"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
+ # config.time_zone = 'Central Time (US & Canada)'
17
+
18
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
+ # config.i18n.default_locale = :de
21
+ end
22
+ end
23
+