auth-assistant 0.4.0

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. 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,7 @@
1
+ require 'auth_assistant/role_strategies/shared'
2
+ require 'auth_assistant/role_strategies/admin_field'
3
+ require 'auth_assistant/role_strategies/role_field'
4
+ require 'auth_assistant/role_strategies/roles_field'
5
+ require 'auth_assistant/role_strategies/roles_mask'
6
+ require 'auth_assistant/role_strategies/role_assignment'
7
+ require 'auth_assistant/role_strategies/multi_role_assignment'
@@ -0,0 +1,34 @@
1
+ module AuthAssistant
2
+ module RoleStrategy
3
+ module MultiRoleAssignment
4
+
5
+ def role?(role)
6
+ return true if roles && roles.include?(role.to_s)
7
+ false
8
+ end
9
+
10
+ module ClassMethods
11
+ def set_scope
12
+ scope :with_role, lambda { |role|
13
+ joins(:roles, :role_assignments).
14
+ where("role_assigments.user_id == id AND role_assigments.role_id == role.id AND roles.id == ?", role)
15
+ }
16
+ end
17
+
18
+ def set_relationships
19
+ has_many :role_assignments
20
+ has_many :roles, :through => :role_assignments
21
+ end
22
+ end
23
+
24
+ def self.included(base)
25
+ base.extend(ClassMethods)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+
33
+
34
+
@@ -0,0 +1,41 @@
1
+ module AuthAssistant
2
+ module RoleStrategy
3
+ module RoleAssignment
4
+
5
+ def roles=(*roles)
6
+ new_role = roles.first.to_s
7
+ self.role = new_role if CanCan.available_roles.include? new_role
8
+ end
9
+
10
+ def roles
11
+ [role]
12
+ end
13
+
14
+ def role?(_role)
15
+ roles.include? _role
16
+ end
17
+
18
+ module ClassMethods
19
+ def set_scope
20
+ scope :with_role, lambda { |role|
21
+ joins(:roles).
22
+ where("user.roles_id = roles.id AND roles.id == ?", role)
23
+ }
24
+ end
25
+
26
+ def set_relationships
27
+ has_many :roles
28
+ end
29
+ end
30
+
31
+ def self.included(base)
32
+ base.extend(ClassMethods)
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+
41
+
@@ -0,0 +1,32 @@
1
+ module AuthAssistant
2
+ module RoleStrategy
3
+ module RoleField
4
+
5
+ def roles=(*roles)
6
+ new_role = roles.first.to_s
7
+ self.role = new_role if CanCan.available_roles.include? new_role
8
+ end
9
+
10
+ def roles
11
+ [role]
12
+ end
13
+
14
+ def role?(_role)
15
+ roles.include? _role
16
+ end
17
+
18
+ module ClassMethods
19
+ def set_scope
20
+ scope :with_role, lambda { |role|
21
+ where("role?(#{role})")
22
+ }
23
+ end
24
+ end
25
+
26
+ def self.included(base)
27
+ base.extend(ClassMethods)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ module AuthAssistant
2
+ module RoleStrategy
3
+ module RolesField
4
+
5
+ def roles=(*roles)
6
+ self.roles = roles.split(',').reject{|e| (e =~ /^\w+$/) == nil}
7
+ end
8
+
9
+ def roles
10
+ roles.split(',')
11
+ end
12
+
13
+ def role?(role)
14
+ roles.include? role.to_s
15
+ end
16
+
17
+ module ClassMethods
18
+ def set_scope
19
+ scope :with_role, lambda { |role|
20
+ where("role?(#{role})")
21
+ }
22
+ end
23
+ end
24
+
25
+ def self.included(base)
26
+ base.extend(ClassMethods)
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,35 @@
1
+ module AuthAssistant
2
+ module RoleStrategy
3
+ module RolesMask
4
+
5
+ def roles=(*roles)
6
+ self.roles_mask = (roles & available_roles).map { |r| calc_index(r) }.sum
7
+ end
8
+
9
+ def roles
10
+ ROLES.reject { |r| ((roles_mask || 0) & calc_index(r)).zero? }
11
+ end
12
+
13
+ def role?(role)
14
+ roles.include? role.to_s
15
+ end
16
+
17
+ module ClassMethods
18
+ def set_scope
19
+ scope :with_role, lambda { |role|
20
+ where("roles_mask & #{calc_index(role.to_s)} > 0")
21
+ }
22
+ end
23
+ end
24
+
25
+ def self.included(base)
26
+ base.extend(ClassMethods)
27
+ end
28
+
29
+ protected
30
+ def calc_index(r)
31
+ 2**available_roles.index(r)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ module AuthAssistant
2
+ module RoleStrategy
3
+ module Shared
4
+ attr_accessor :ability
5
+
6
+ def admin?
7
+ role? 'admin'
8
+ end
9
+
10
+ def has(ability)
11
+ @ability ||= ability
12
+ end
13
+
14
+ def owns(clazz)
15
+ return if !ability
16
+ base ||= RolePermit::Base.new(ability)
17
+ base.owns(self, clazz)
18
+ end
19
+
20
+ def available_roles
21
+ AuthAssistant::Model.available_roles
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module AuthAssistant
2
+ module Helpers
3
+ module AuthLabel
4
+ def auth_labels
5
+ @auth_labels ||= translate_labels
6
+ end
7
+
8
+ def translate_labels
9
+ ns_actions = 'auth_assistant.actions'
10
+ labels = {}
11
+ %w{new edit delete show sign_in sign_out sign_up edit_registration}.each do |action|
12
+ labels[action.to_sym] = t "#{ns_actions}.#{action}"
13
+ end
14
+ labels[:confirm] = t 'auth_assistant.confirm'
15
+ labels
16
+ end
17
+
18
+ def self.included(base)
19
+ base.helper_method :auth_labels
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ require 'auth_assistant/view/auth_menu_item'
2
+ require 'auth_assistant/view/rest_link'
3
+ require 'auth_assistant/view/registration_link'
4
+ require 'auth_assistant/view/session_link'
@@ -0,0 +1,27 @@
1
+ module AuthAssistant
2
+ module ViewHelpers
3
+ module AuthMenuItem
4
+ def sign_out_menu_item
5
+ "<li>#{sign_out_link}</li>".html_safe if current_user
6
+ end
7
+
8
+ def sign_in_menu_item
9
+ "<li>#{sign_in_link}</li>".html_safe if !current_user
10
+ end
11
+
12
+ def sign_up_menu_item
13
+ "<li>#{sign_up_link}</li>".html_safe if !current_user
14
+ end
15
+
16
+ def edit_registration_menu_item
17
+ "<li>#{edit_registration_link}</li>".html_safe if current_user
18
+ end
19
+
20
+ alias_method :logout_menu_item, :sign_out_menu_item
21
+ alias_method :login_menu_item, :sign_in_menu_item
22
+ alias_method :register_menu_item, :sign_up_menu_item
23
+ alias_method :edit_user_menu_item, :edit_registration_menu_item
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ module AuthAssistant
2
+ module ViewHelpers
3
+ module RegistrationLink
4
+
5
+ def sign_up_link(options = {})
6
+ label = options[:label] || auth_labels[:sign_up]
7
+ path = registration_path options[:role]
8
+ link_to(label, path)
9
+ end
10
+
11
+ def edit_registration_link(options = {})
12
+ label = options[:label] || auth_labels[:edit_registration]
13
+ path = edit_registration_path options[:role]
14
+ link_to(label, path)
15
+ end
16
+
17
+ alias_method :register_link, :sign_up_link
18
+ alias_method :edit_profile_link, :edit_registration_link
19
+
20
+ protected
21
+ def registration_path(role)
22
+ role == 'admin' ? new_admin_registration_path : new_user_registration_path
23
+ end
24
+
25
+ def edit_registration_path(role)
26
+ role == 'admin' ? edit_admin_registration_path : edit_user_registration_path
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,70 @@
1
+ module AuthAssistant
2
+ module ViewHelpers
3
+ module RestLink
4
+ def index_link(object, label = nil)
5
+ label ||= auth_labels[:index]
6
+ puts object.inspect
7
+ obj = index_obj(object)
8
+ puts "index obj: #{obj.inspect}"
9
+ path = send :"#{obj}_path"
10
+ link = link_to(label, path) if can?(:read, object)
11
+ end
12
+
13
+ def index_obj(obj)
14
+ o = case obj
15
+ when Array
16
+ obj.first.class
17
+ when Class
18
+ obj
19
+ else
20
+ obj.class
21
+ end
22
+ o.name.pluralize.downcase
23
+ end
24
+
25
+
26
+ def create_link(object, label = nil)
27
+ label ||= auth_labels[:new]
28
+ path = send :"new_#{object.class.to_s.downcase}_path"
29
+ link = link_to(label, path) if can?(:create, object)
30
+ end
31
+
32
+ def edit_link(object, label = nil)
33
+ label ||= auth_labels[:edit]
34
+ link_to(label, [:edit, object]) if can?(:edit, object)
35
+ end
36
+
37
+ def delete_link(object, options = nil)
38
+ options ||= {:label => auth_labels[:delete], :confirm => auth_labels[:confirm]}
39
+ case options
40
+ when String
41
+ label = options
42
+ when Hash
43
+ label = options[:label]
44
+ confirm_msg = options[:confirm]
45
+ when Array
46
+ label = options[0]
47
+ confirm_msg = options.size > 1 ? options[1] : auth_labels[:confirm]
48
+ end
49
+ link_to(label, object, :method => :delete, :confirm => confirm_msg) if can?(:destroy, object)
50
+ end
51
+
52
+ def show_link(object, label = nil)
53
+ label ||= auth_labels[:show]
54
+ if can?(:read, object)
55
+ puts "can read: #{object}"
56
+ link_to(label, object)
57
+ else
58
+ puts "no link"
59
+ end
60
+ end
61
+
62
+ alias_method :list_link, :index_link
63
+ alias_method :new_link, :create_link
64
+ alias_method :destroy_link, :delete_link
65
+ alias_method :update_link, :edit_link
66
+ alias_method :read_link, :show_link
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,31 @@
1
+ module AuthAssistant
2
+ module ViewHelpers
3
+ module SessionLink
4
+ def sign_out_link(options = {})
5
+ label = options[:label] || auth_labels[:sign_out]
6
+ path = destroy_session_path options[:role]
7
+ link_to(label, path)
8
+ end
9
+
10
+ def sign_in_link(label, options = {})
11
+ label = options[:label] || auth_labels[:sign_in]
12
+ path = create_session_path options[:role]
13
+ link_to(label, path)
14
+ end
15
+
16
+ alias_method :log_out_link, :sign_out_link
17
+ alias_method :log_in_link, :sign_in_link
18
+
19
+ protected
20
+ def destroy_session_path(role)
21
+ return send :"destroy_#{role}_session_path" if role && role != 'user'
22
+ destroy_user_session_path
23
+ end
24
+
25
+ def create_session_path(role)
26
+ return send :"new_#{role}_session_path" if role && role != 'user'
27
+ new_user_session_path # default
28
+ end
29
+ end
30
+ end
31
+ end
Binary file
@@ -0,0 +1,30 @@
1
+ require 'generators/migration_helper'
2
+ require 'generators/reverse_migrations'
3
+ require 'generators/role_migrations'
4
+ require 'rake'
5
+
6
+ module AuthAssist
7
+ module Generators
8
+ class ClearGenerator < Rails::Generators::NamedBase
9
+
10
+ desc "Clears the rails project from any artifacts generated by auth_assist"
11
+
12
+ class_option :migration, :type => :boolean, :aliases => "-m", :default => true,
13
+ :desc => "To generate a migration to clear the user role."
14
+
15
+ def self.source_root
16
+ @source_root ||= File.expand_path("../../templates", __FILE__)
17
+ end
18
+
19
+ def generate_clear_migration
20
+ return nil if !options[:migration]
21
+ clazz = AuthAssist::RoleMigrations.clazz(name)
22
+ obj = clazz.new(self)
23
+ obj.generate_reverse_migration
24
+ obj.reverse_configure if obj.respond_to? :reverse_configure
25
+ end
26
+
27
+ include ::AuthAssist::MigrationHelper
28
+ end
29
+ end
30
+ end