auth-assistant 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.DS_Store +0 -0
  2. data/.document +5 -0
  3. data/.gitignore +39 -0
  4. data/Changelog.txt +30 -0
  5. data/LICENSE +20 -0
  6. data/README.markdown +308 -0
  7. data/Rakefile +20 -0
  8. data/VERSION +1 -0
  9. data/app/.DS_Store +0 -0
  10. data/app/views/.DS_Store +0 -0
  11. data/app/views/auth_assist/menu/_admin_login_items.html.erb +11 -0
  12. data/app/views/auth_assist/menu/_login_items.html.erb +11 -0
  13. data/app/views/auth_assist/menu/_registration_items.html.erb +10 -0
  14. data/auth-assistant.gemspec +115 -0
  15. data/config/locales/en.yml +14 -0
  16. data/init.rb +1 -0
  17. data/lib/.DS_Store +0 -0
  18. data/lib/auth-assistant.rb +24 -0
  19. data/lib/auth_assistant/configure.rb +16 -0
  20. data/lib/auth_assistant/helpers/admin_role.rb +59 -0
  21. data/lib/auth_assistant/helpers/all.rb +4 -0
  22. data/lib/auth_assistant/helpers/localhost.rb +22 -0
  23. data/lib/auth_assistant/helpers/roles.rb +52 -0
  24. data/lib/auth_assistant/helpers/user_role.rb +47 -0
  25. data/lib/auth_assistant/model/user_config.rb +42 -0
  26. data/lib/auth_assistant/role_strategies/admin_field.rb +37 -0
  27. data/lib/auth_assistant/role_strategies/all.rb +7 -0
  28. data/lib/auth_assistant/role_strategies/multi_role_assignment.rb +34 -0
  29. data/lib/auth_assistant/role_strategies/role_assignment.rb +41 -0
  30. data/lib/auth_assistant/role_strategies/role_field.rb +32 -0
  31. data/lib/auth_assistant/role_strategies/roles_field.rb +31 -0
  32. data/lib/auth_assistant/role_strategies/roles_mask.rb +35 -0
  33. data/lib/auth_assistant/role_strategies/shared.rb +25 -0
  34. data/lib/auth_assistant/translate/authlabels.rb +23 -0
  35. data/lib/auth_assistant/view/all.rb +4 -0
  36. data/lib/auth_assistant/view/auth_menu_item.rb +27 -0
  37. data/lib/auth_assistant/view/registration_link.rb +30 -0
  38. data/lib/auth_assistant/view/rest_link.rb +70 -0
  39. data/lib/auth_assistant/view/session_link.rb +31 -0
  40. data/lib/generators/.DS_Store +0 -0
  41. data/lib/generators/auth_assist/.DS_Store +0 -0
  42. data/lib/generators/auth_assist/clear/clear_generator.rb +30 -0
  43. data/lib/generators/auth_assist/config/.DS_Store +0 -0
  44. data/lib/generators/auth_assist/config/config_generator.rb +72 -0
  45. data/lib/generators/auth_assist/templates/ability.rb +22 -0
  46. data/lib/generators/auth_assist/templates/auth_assistant.rb +6 -0
  47. data/lib/generators/auth_assist/templates/permits.rb +91 -0
  48. data/lib/generators/auth_assist/templates/remove_multi_role_assignments_migration.rb +24 -0
  49. data/lib/generators/auth_assist/templates/remove_role_assignments_migration.rb +17 -0
  50. data/lib/generators/auth_assist/templates/role_assignments_migration.rb +14 -0
  51. data/lib/generators/auth_assist/templates/roles_migration.rb +13 -0
  52. data/lib/generators/auth_assist/test.rb +40 -0
  53. data/lib/generators/auth_assist/views/views_generator.rb +66 -0
  54. data/lib/generators/auth_code_refactor.rb +71 -0
  55. data/lib/generators/migration_helper.rb +81 -0
  56. data/lib/generators/reverse_migrations.rb +48 -0
  57. data/lib/generators/role_migrations.rb +167 -0
  58. data/lib/permits.rb +92 -0
  59. data/spec/auth-assistant_spec.rb +7 -0
  60. data/spec/generators/ability_gen_spec.rb +9 -0
  61. data/spec/sandbox.rb +9 -0
  62. data/spec/spec.opts +1 -0
  63. data/spec/spec_helper.rb +11 -0
  64. metadata +167 -0
@@ -0,0 +1,81 @@
1
+ require 'rails/generators/migration'
2
+ require 'generators/auth_code_refactor'
3
+
4
+ module AuthAssist
5
+ module MigrationHelper
6
+ include Rails::Generators::Migration
7
+
8
+ module ClassMethods
9
+ def migration_lookup_at(dirname) #:nodoc:
10
+ Dir.glob("#{dirname}/[0-9]*_*.rb")
11
+ end
12
+
13
+ def migration_exists?(dirname, file_name) #:nodoc:
14
+ migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first
15
+ end
16
+
17
+ def current_migration_number(dirname) #:nodoc:
18
+ migration_lookup_at(dirname).collect do |file|
19
+ File.basename(file).split("_").first.to_i
20
+ end.max.to_i
21
+ end
22
+
23
+ def next_migration_number(dirname) #:nodoc:
24
+ orm = Rails.configuration.generators.options[:rails][:orm]
25
+ require "rails/generators/#{orm}"
26
+ "#{orm.to_s.camelize}::Generators::Base".constantize.next_migration_number(dirname)
27
+ rescue
28
+ raise NotImplementedError
29
+ end
30
+ end
31
+
32
+ def self.included(base) #:nodoc:
33
+ puts "MigrationHelper included by #{base}"
34
+ base.extend ClassMethods
35
+ end
36
+
37
+
38
+ def migration(options)
39
+ # migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
40
+ run "rails g migration #{options}"
41
+ end
42
+
43
+ protected
44
+
45
+ include CodeRefactor
46
+
47
+ def model_exists?(name)
48
+ File.exists?(File.join(Rails.root, "app/models/#{name}.rb"))
49
+ end
50
+
51
+ def remove_model(name)
52
+ file = File.join(Rails.root, "app/models/#{name}.rb")
53
+ FileUtils.rm file if file
54
+ end
55
+
56
+ def remove_role_model
57
+ remove_model('role')
58
+ end
59
+
60
+ def remove_role_assignment_model
61
+ remove_model('role_assignment')
62
+ end
63
+
64
+ def generate_role_model
65
+ return if model_exists?('role')
66
+ run 'rails g model Role name:string'
67
+ write_model_file('role', role_file_content)
68
+ end
69
+
70
+ def generate_role_assignment_model
71
+ run 'rails g model RoleAssignment user_id:integer role_id:integer'
72
+ return if model_exists?('role_assignment')
73
+ write_model_file('role_assignment', role_assignment_file_content)
74
+ end
75
+
76
+ def model_file(name)
77
+ File.join(Rails.root, "app/models/#{name}.rb")
78
+ end
79
+ end
80
+ end
81
+
@@ -0,0 +1,48 @@
1
+ module AuthAssist
2
+ module MigrationHelper
3
+ def migration_dir
4
+ File.join(Rails.root, 'db/migrate')
5
+ end
6
+
7
+ def reverse_migration(mig_name)
8
+ if migration_exists?(mig_name)
9
+ new_migration = copy_migration(mig_name, "reverse_#{strategy_name}")
10
+ reverse_migration_method_names(new_migration)
11
+ end
12
+ end
13
+
14
+ def find_migration(name, &block)
15
+ FileUtils.cd migration_dir do
16
+ f = FileList['*.rb'].reject{|f| (f =~ /#{Regexp.escape(name)}/) == nil }.first
17
+ return block.call(f) if block
18
+ end
19
+ f
20
+ end
21
+
22
+ def migration_exists?(name)
23
+ find_migration(name)
24
+ end
25
+
26
+ def copy_migration(name, new_name, &block)
27
+ # create empty new migration
28
+ migration new_name
29
+ new_migration = find_migration(name)
30
+ find_migration(name) do |mig|
31
+ FileUtils.cp mig, new_migration
32
+ end
33
+ return block.call(new_migration) if block
34
+ new_migration
35
+ end
36
+
37
+ def reverse_migration_methods(new_migration)
38
+ file_swap_content(new_migration, 'self.up', 'self.down')
39
+ end
40
+
41
+ def file_reverse(file, str1, str2)
42
+ # use thor action
43
+ file.replace str1, str2
44
+ file.replace str2, str1
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,167 @@
1
+ require 'generators/migration_helper'
2
+
3
+ module AuthAssist
4
+ module RoleMigrations
5
+
6
+ def self.clazz(name)
7
+ "AuthAssist::RoleMigrations::#{name.camelize}".constantize
8
+ end
9
+
10
+ class Base
11
+ include ::AuthAssist::MigrationHelper
12
+
13
+ attr_accessor :generator
14
+
15
+ def initialize(generator)
16
+ @generator = generator
17
+ end
18
+
19
+ def generate_reverse_migration
20
+ return reverse_migration if respond_to? :reverse_migration
21
+ generation.reverse_migration(migration_names.first)
22
+ end
23
+
24
+ def setup
25
+ run_migration if respond_to? :run_migration
26
+ configure if respond_to? :configure
27
+ end
28
+
29
+ protected
30
+ def migration(options)
31
+ generator.migration options
32
+ end
33
+
34
+ def insert_user_relation(relation)
35
+ generator.insert_user_relation relation
36
+ end
37
+
38
+ def remove_user_relation(relation)
39
+ generator.remove_user_relation(relation)
40
+ end
41
+
42
+ def generate_role_model
43
+ generator.generate_role_model
44
+ end
45
+
46
+ def remove_role_model
47
+ generator.remove_role_model
48
+ end
49
+
50
+ def remove_role_assignment_model
51
+ generator.remove_role_model
52
+ end
53
+
54
+ def generate_role_assignment_model
55
+ generator.generate_role_assignment_model
56
+ end
57
+
58
+ def migration_template(source, target)
59
+ generator.migration_template source, target
60
+ end
61
+
62
+ end
63
+
64
+ class AdminField < Base
65
+ def migration_names
66
+ ['add_admin_field_to_user']
67
+ end
68
+
69
+ def run_migration
70
+ migration "#{admin_field_migration_name} admin:boolean"
71
+ end
72
+ end
73
+
74
+
75
+ class RolesMask < Base
76
+ def migration_names
77
+ ['add_roles_mask_to_user']
78
+ end
79
+
80
+ def run_migration
81
+ migration "#{roles_mask_migration_name} roles_mask:integer"
82
+ end
83
+ end
84
+
85
+
86
+ class RolesField < Base
87
+ def migration_names
88
+ ['add_roles_field_to_user']
89
+ end
90
+
91
+ def run_migration
92
+ migration "#{roles_field_migration_name} roles:string"
93
+ end
94
+
95
+ end
96
+
97
+
98
+ class RoleField < Base
99
+ def migration_names
100
+ ['add_role_field_to_user']
101
+ end
102
+
103
+ def run_migration
104
+ migration "#{role_field_migration_name} role:string"
105
+ end
106
+ end
107
+
108
+
109
+ class RoleAssignment < Base
110
+ def migration_names
111
+ ['add_role_id_to_user', 'create_roles']
112
+ end
113
+
114
+ def run_migration
115
+ migration 'add_role_id_to_user role_id:integer'
116
+ migration 'create_roles name:string'
117
+ end
118
+
119
+ def configure
120
+ generate_role_model
121
+ insert_user_relation(has_roles)
122
+ end
123
+
124
+ def reverse_configure
125
+ remove_role_model
126
+ remove_user_relation(has_roles)
127
+ end
128
+
129
+ def reverse_migration
130
+ migration_template 'remove_role_assignments_migration.rb', 'remove_role_assignments'
131
+ end
132
+ end
133
+
134
+ class MultiRoleAssignment < Base
135
+ def migration_names
136
+ ['add_role_assignment_id_to_user', 'create_role_assignments', 'create_roles']
137
+ end
138
+
139
+ def run_migration
140
+ migration 'add_role_assignment_id_to_user role_assignment_id:integer'
141
+ migration 'create_role_assignments role_id:integer user_id:integer'
142
+ migration 'create_roles name:string'
143
+ end
144
+
145
+ def configure
146
+ insert_user_relation(has_role_assignments)
147
+ insert_user_relation(has_roles_through_assignments)
148
+
149
+ generate_role_model
150
+ generate_role_assignment_model
151
+ end
152
+
153
+ def reverse_configure
154
+ remove_role_model
155
+ remove_role_assignment_model
156
+
157
+ remove_user_relation(has_role_assignments)
158
+ remove_user_relation(has_roles_through_assignments)
159
+ end
160
+
161
+ def reverse_migration
162
+ migration_template 'remove_multi_role_assignments_migration.rb', 'remove_multi_role_assignments'
163
+ end
164
+
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,92 @@
1
+ module RolePermit
2
+
3
+ class Base
4
+ attr_accessor :ability
5
+
6
+ def initialize(ability)
7
+ @ability = ability
8
+ end
9
+
10
+ def can(action, subject, conditions = nil, &block)
11
+ ability.can(action, subject, conditions, &block)
12
+ end
13
+
14
+ def cannot(action, subject, conditions = nil, &block)
15
+ ability.cannot(action, subject, conditions, &block)
16
+ end
17
+
18
+ def owns(user, clazz)
19
+ can :manage, clazz do |comment|
20
+ comment.try(:user) == user || comment.try(:owner) == user
21
+ end
22
+ end
23
+
24
+ def permit?(user)
25
+ user.has ability
26
+ end
27
+ end
28
+
29
+ class Admin < Base
30
+ def initialize(ability)
31
+ super
32
+ end
33
+
34
+ def permit?(user)
35
+ super
36
+ return if !user.role? :admin
37
+ can :manage, :all
38
+ end
39
+ end
40
+
41
+ class User < Base
42
+ def initialize(ability)
43
+ super
44
+ end
45
+
46
+ def permit?(user)
47
+ super
48
+ return if user.role? :admin
49
+ can :read, :all
50
+
51
+ # user.owns(Comment)
52
+
53
+ # a user can manage comments he/she created
54
+ # can :manage, Comment do |comment|
55
+ # comment.try(:user) == user
56
+ # end
57
+
58
+ # can :create, Comment
59
+ end
60
+ end
61
+
62
+ class Moderator < Base
63
+ def initialize(ability)
64
+ super
65
+ end
66
+
67
+ def permit?(user)
68
+ super
69
+ return if !user.role?(:moderator)
70
+ can :read, :all
71
+ # owns(user, Comment)
72
+ end
73
+ end
74
+
75
+
76
+ class Author < Base
77
+ def initialize(ability)
78
+ super
79
+ end
80
+
81
+ def permit?(user)
82
+ super
83
+ return if !user.role? :author
84
+ # can :create, Post
85
+
86
+ # an author can manage posts he/she created
87
+ # can :update, Post do |post|
88
+ # post.try(:user) == user
89
+ # end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AuthAssistant" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ require 'generators/auth_assistant/ability/ability_generator'
4
+
5
+ describe "DeviseAssistant Ability Generator" do
6
+ it "works" do
7
+ puts "it works"
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class Hello
2
+ def hi
3
+ puts "hi"
4
+ end
5
+
6
+ alias_method :hello, :hi
7
+ end
8
+
9
+ Hello.new.hello
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rspec'
4
+ require 'rspec/autorun'
5
+ require 'rails'
6
+ # require 'auth-assistant'
7
+
8
+ Rspec.configure do |c|
9
+ # c.mock_with :rspec
10
+ end
11
+
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auth-assistant
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
+ platform: ruby
11
+ authors:
12
+ - Kristian Mandrup
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-18 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 0
31
+ version: 2.0.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: devise
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 0
44
+ version: "1.0"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: cancan
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 1
56
+ - 0
57
+ version: "1.0"
58
+ type: :runtime
59
+ version_requirements: *id003
60
+ description: Provides assistance for setting up an auth solution using devise and cancan auth frameworks
61
+ email: kmandrup@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE
68
+ - README.markdown
69
+ files:
70
+ - .DS_Store
71
+ - .document
72
+ - .gitignore
73
+ - Changelog.txt
74
+ - LICENSE
75
+ - README.markdown
76
+ - Rakefile
77
+ - VERSION
78
+ - app/.DS_Store
79
+ - app/views/.DS_Store
80
+ - app/views/auth_assist/menu/_admin_login_items.html.erb
81
+ - app/views/auth_assist/menu/_login_items.html.erb
82
+ - app/views/auth_assist/menu/_registration_items.html.erb
83
+ - auth-assistant.gemspec
84
+ - config/locales/en.yml
85
+ - init.rb
86
+ - lib/.DS_Store
87
+ - lib/auth-assistant.rb
88
+ - lib/auth_assistant/configure.rb
89
+ - lib/auth_assistant/helpers/admin_role.rb
90
+ - lib/auth_assistant/helpers/all.rb
91
+ - lib/auth_assistant/helpers/localhost.rb
92
+ - lib/auth_assistant/helpers/roles.rb
93
+ - lib/auth_assistant/helpers/user_role.rb
94
+ - lib/auth_assistant/model/user_config.rb
95
+ - lib/auth_assistant/role_strategies/admin_field.rb
96
+ - lib/auth_assistant/role_strategies/all.rb
97
+ - lib/auth_assistant/role_strategies/multi_role_assignment.rb
98
+ - lib/auth_assistant/role_strategies/role_assignment.rb
99
+ - lib/auth_assistant/role_strategies/role_field.rb
100
+ - lib/auth_assistant/role_strategies/roles_field.rb
101
+ - lib/auth_assistant/role_strategies/roles_mask.rb
102
+ - lib/auth_assistant/role_strategies/shared.rb
103
+ - lib/auth_assistant/translate/authlabels.rb
104
+ - lib/auth_assistant/view/all.rb
105
+ - lib/auth_assistant/view/auth_menu_item.rb
106
+ - lib/auth_assistant/view/registration_link.rb
107
+ - lib/auth_assistant/view/rest_link.rb
108
+ - lib/auth_assistant/view/session_link.rb
109
+ - lib/generators/.DS_Store
110
+ - lib/generators/auth_assist/.DS_Store
111
+ - lib/generators/auth_assist/clear/clear_generator.rb
112
+ - lib/generators/auth_assist/config/.DS_Store
113
+ - lib/generators/auth_assist/config/config_generator.rb
114
+ - lib/generators/auth_assist/templates/ability.rb
115
+ - lib/generators/auth_assist/templates/auth_assistant.rb
116
+ - lib/generators/auth_assist/templates/permits.rb
117
+ - lib/generators/auth_assist/templates/remove_multi_role_assignments_migration.rb
118
+ - lib/generators/auth_assist/templates/remove_role_assignments_migration.rb
119
+ - lib/generators/auth_assist/templates/role_assignments_migration.rb
120
+ - lib/generators/auth_assist/templates/roles_migration.rb
121
+ - lib/generators/auth_assist/test.rb
122
+ - lib/generators/auth_assist/views/views_generator.rb
123
+ - lib/generators/auth_code_refactor.rb
124
+ - lib/generators/migration_helper.rb
125
+ - lib/generators/reverse_migrations.rb
126
+ - lib/generators/role_migrations.rb
127
+ - lib/permits.rb
128
+ - spec/auth-assistant_spec.rb
129
+ - spec/generators/ability_gen_spec.rb
130
+ - spec/sandbox.rb
131
+ - spec/spec.opts
132
+ - spec/spec_helper.rb
133
+ has_rdoc: true
134
+ homepage: http://github.com/kristianmandrup/devise-assistant
135
+ licenses: []
136
+
137
+ post_install_message:
138
+ rdoc_options:
139
+ - --charset=UTF-8
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ segments:
154
+ - 0
155
+ version: "0"
156
+ requirements: []
157
+
158
+ rubyforge_project:
159
+ rubygems_version: 1.3.6
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: Provides assistance for setting up an auth solution
163
+ test_files:
164
+ - spec/auth-assistant_spec.rb
165
+ - spec/generators/ability_gen_spec.rb
166
+ - spec/sandbox.rb
167
+ - spec/spec_helper.rb