cream 0.8.9.1 → 0.8.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -76,7 +76,13 @@ Example idea:
76
76
 
77
77
  This way, any editor user created will always be given the :editor role on creation and he can later add the :publisher and/or :blog_editor roles, depending on whether Cream is set up as a single or multi role system. In the future, the framework might even allow some user types to have multiple roles and others to have only a single role (here indicated by the _roles_config :multi_ statement) ? What do you think?
78
78
 
79
- *Ideas and suggestions are most welcome!*
79
+ *Ideas and suggestions are most welcome!*
80
+
81
+ h3. The path towards 1.0
82
+
83
+ I will continue the 0.8.9.x line only with bug fixes and other minor fixes or additions.
84
+
85
+ The 0.9 line will likely be a total redesign/refactoring using what I am learning from reading _Jose Valim's_ book *Crafting Rails Applications*. I will create a new project using _enginex_, complete with one or more dummy apllications and take it from there.
80
86
 
81
87
  h2. Cream edge, debugging and assisting developing the framework
82
88
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.9.1
1
+ 0.8.9.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cream}
8
- s.version = "0.8.9.1"
8
+ s.version = "0.8.9.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2011-01-15}
12
+ s.date = %q{2011-01-18}
13
13
  s.description = %q{An integrated Authentication, Authorization and Roles solution for your Rails 3 app with support for multiple ORMs}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -40,9 +40,10 @@ Gem::Specification.new do |s|
40
40
  "init.rb",
41
41
  "lib/cream.rb",
42
42
  "lib/cream/configure/after_init/role_config.rb",
43
+ "lib/cream/configure/engine.rb",
43
44
  "lib/cream/configure/rails.rb",
44
45
  "lib/cream/controller/ability.rb",
45
- "lib/cream/controller/application_controller.rb",
46
+ "lib/cream/controller/user_control.rb",
46
47
  "lib/cream/helper/host.rb",
47
48
  "lib/cream/helper/role.rb",
48
49
  "lib/cream/namespaces.rb",
@@ -0,0 +1,22 @@
1
+ # Not yet used! Should use it?
2
+
3
+ module Cream
4
+ # Include helpers in the given scope to AC and AV.
5
+ # "Borrowed" from devise
6
+ def self.include_helpers(scope)
7
+ ActiveSupport.on_load(:action_controller) do
8
+ include scope::Helpers
9
+ include scope::UrlHelpers
10
+ end
11
+
12
+ ActiveSupport.on_load(:action_view) do
13
+ include scope::UrlHelpers
14
+ end
15
+ end
16
+
17
+ class Engine < ::Rails::Engine
18
+ initializer "cream.helpers" do
19
+ Cream.include_helpers(Cream::Controllers)
20
+ end
21
+ end
22
+ end
@@ -9,7 +9,7 @@ Rails3::Plugin::Extender.new do
9
9
  extend_rails :controller do
10
10
  extend_from_module Cream, :role
11
11
  extend_from_module Cream::Controller, :ability
12
- extend_from_module Cream::Helper, :role, :host
12
+ extend_from_module Cream::Helper, :role, :host
13
13
  end
14
14
 
15
15
  # extend action_view with methods from some modules
@@ -20,10 +20,13 @@ Rails3::Plugin::Extender.new do
20
20
 
21
21
  after :initialize do
22
22
  load File.dirname(__FILE__) + '/after_init/role_config.rb'
23
- #
24
- # extend_rails :view do
25
- # extend_from_module Cream::View, :role
26
- # extend_from_module Cream::Helper, :role
27
- # end
23
+
24
+ if defined? Cream::UserControl
25
+ # puts "Extending ApplicationController with UserControl"
26
+ ApplicationController.class_eval "include Cream::UserControl"
27
+ else
28
+ puts "Cream::UserControl not defined!"
29
+ end
28
30
  end
29
31
  end
32
+
@@ -0,0 +1,79 @@
1
+ module Cream
2
+ module UserControl
3
+
4
+ def current_user
5
+ # puts "current_user"
6
+ if !session[:user_id]
7
+ # puts "make guest"
8
+ @guest ||= Guest.create(guest_options)
9
+ # puts "guest: #{@guest}"
10
+ return @guest
11
+ end
12
+ if session[:user_id]
13
+ begin
14
+ clazz = session[:user_class_name].constantize
15
+ @current_user ||= clazz.find session[:user_id]
16
+ # puts "logged in user: #{@current_user}"
17
+ return @current_user
18
+ rescue Exception => e
19
+ puts "Error with current_user: user_class_name = '#{session[:user_class_name]}' error: #{e}"
20
+ end
21
+ end
22
+ end
23
+
24
+ def set_language language_code
25
+ current_user.language_code = language_code if current_user # for non-guest user
26
+ guest_options[:language_code] = language_code # for guest user
27
+ end
28
+
29
+ def guest_options
30
+ session[:guest_options] ||= {}
31
+ end
32
+
33
+ def user_signed_in?
34
+ current_user && !current_user.has_role?(:guest)
35
+ end
36
+
37
+ def user_session
38
+ user_signed_in? ? super : session
39
+ end
40
+
41
+ # Sign in an user that already was authenticated. This helper is useful for logging
42
+ # users in after sign up.
43
+ #
44
+ # Examples:
45
+ #
46
+ # sign_in :user, @user # sign_in(scope, resource)
47
+ # sign_in @user # sign_in(resource)
48
+ # sign_in @user, :event => :authentication # sign_in(resource, options)
49
+ #
50
+ def sign_in(resource_or_scope, *args)
51
+ options = args.extract_options!
52
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
53
+ resource = args.last || resource_or_scope
54
+ expire_session_data_after_sign_in!
55
+ warden.set_user(resource, options.merge!(:scope => scope))
56
+ # set user id
57
+ session[:user_id] = resource.id
58
+ session[:user_class_name] = resource.class.name
59
+ end
60
+
61
+ # Sign out a given user or scope. This helper is useful for signing out an user
62
+ # after deleting accounts.
63
+ #
64
+ # Examples:
65
+ #
66
+ # sign_out :user # sign_out(scope)
67
+ # sign_out @user # sign_out(resource)
68
+ #
69
+ def sign_out(resource_or_scope)
70
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
71
+ warden.user(scope) # Without loading user here, before_logout hook is not called
72
+ warden.raw_session.inspect # Without this inspect here. The session does not clear.
73
+ warden.logout(scope)
74
+ # user id
75
+ session[:user_id] = nil
76
+ @current_user = nil
77
+ end
78
+ end
79
+ end
@@ -1,10 +1,11 @@
1
1
  module Cream
2
2
  module Rails
3
3
  class Config
4
- attr_accessor :roles
4
+ attr_accessor :roles, :user_types
5
5
 
6
6
  def initialize options = {}
7
7
  self.roles ||= options[:roles]
8
+ self.user_types ||= options[:user_types]
8
9
  end
9
10
  end
10
11
  end
@@ -1,6 +1,16 @@
1
1
  module Cream
2
2
  class << self
3
- attr_accessor :available_roles
3
+ attr_accessor :available_roles
4
+ attr_accessor :available_user_types
5
+ end
6
+
7
+ module UserTypes
8
+ def self.available
9
+ return Cream.available_user_types if Cream.available_user_types.present?
10
+ return Cream.user_types if Cream.user_types.present?
11
+ puts "Cream has not been configured with any devise User Types"
12
+ []
13
+ end
4
14
  end
5
15
 
6
16
  module Role
@@ -1,6 +1,7 @@
1
1
  module Cream
2
- mattr_accessor :roles
2
+ mattr_accessor :roles, :user_types
3
3
  @@roles = []
4
+ @@user_types = []
4
5
 
5
6
  def self.setup
6
7
  yield self
@@ -4,9 +4,18 @@ module Cream::View
4
4
  yield block if localhost?
5
5
  end
6
6
 
7
+ def not_for_localhost(&block)
8
+ yield block if !localhost?
9
+ end
10
+
7
11
  def for_public(&block)
8
12
  # with_output_buffer(&block) if publichost?
9
13
  yield block if publichost?
10
14
  end
15
+
16
+ def not_for_public(&block)
17
+ # with_output_buffer(&block) if publichost?
18
+ yield block if !publichost?
19
+ end
11
20
  end
12
21
  end
@@ -75,14 +75,21 @@ require "rails/test_unit/railtie"
75
75
  remove_model :guest if has_model?(:guest)
76
76
  create_model :guest do
77
77
  %Q{
78
- # modify this as needed for now!
79
- # attr_accessor :login, :username, :email
80
- # attr_accessor :password
81
- #
82
- # attr_accessible :login
78
+ # modify this as needed (for any forms using current_user)
79
+ # attr_accessor :login, :password
80
+ # attr_accessor :username, :email
81
+ # attr_accessor :country, :country_code, :language, :language_code, :city
83
82
 
84
- def self.create
85
- Guest.new
83
+ def self.create options = {}
84
+ Guest.new options.merge(session[:guest_options] || {})
85
+ end
86
+
87
+ def save
88
+ false
89
+ end
90
+
91
+ def save!
92
+ false
86
93
  end
87
94
 
88
95
  def is? role
@@ -104,12 +111,6 @@ require "rails/test_unit/railtie"
104
111
  end
105
112
  say "A Guest user model has been created. Users that have not signed in will get an instance of this class as the current_user."
106
113
  end
107
-
108
- protected
109
-
110
- def guest_user?
111
- options[:gust_user]
112
- end
113
114
  end
114
115
  end
115
116
  end
@@ -15,30 +15,33 @@ module Cream
15
15
  class_option :strategy, :type => :string, :default => 'role_string', :desc => "Role strategy to use"
16
16
 
17
17
  # Create Admin user
18
- class_option :user_class, :type => :string, :default => 'User', :desc => "User class"
19
- class_option :admin_class, :type => :string, :default => nil, :desc => "Admin user class"
20
- class_option :role_class, :type => :string, :default => 'Role', :desc => "Role class"
21
- class_option :user_role_class, :type => :string, :default => 'UserRole', :desc => "UserRole class"
18
+ class_option :user_class, :type => :string, :default => 'User', :desc => "User class", :aliases => '-u'
19
+ class_option :role_class, :type => :string, :default => 'Role', :desc => "Role class", :aliases => '-rc'
20
+ class_option :user_role_class, :type => :string, :default => 'UserRole', :desc => "UserRole class", :aliases => '-urc'
21
+
22
+ class_option :guest_user, :type => :boolean, :default => true, :desc => "Create guest user", :aliases => '-guest'
22
23
 
23
24
  # Roles
24
- class_option :default_roles, :type => :boolean, :default => true, :desc => "Create default roles :admin and :guest"
25
- class_option :roles, :type => :array, :default => [], :desc => "Roles to create"
25
+ class_option :default_roles, :type => :boolean, :default => true, :desc => "Create default roles :admin and :guest", :aliases => '-dr'
26
+ class_option :roles, :type => :array, :default => [], :desc => "Roles to create", :aliases => '-r'
27
+
28
+ class_option :user_types, :type => :array, :default => ['Admin'], :desc => "Devise Users to create that override the generic base User", :aliases => '-ut'
26
29
 
27
30
  # ORM to use
28
- class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
31
+ class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use", :aliases => '-o'
29
32
 
30
- class_option :locales, :type => :array, :default => ['all'], :desc => "List of locales - 'all' means ALL locales"
31
- class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
33
+ class_option :locales, :type => :array, :default => ['all'], :desc => "List of locales - 'all' means ALL locales", :aliases => '-l'
34
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location", :aliases => '-lf'
32
35
 
33
- class_option :configure, :type => :array, :default => [], :desc => "Finetune which generators to run: app, permits, roles, devise, cancan"
34
- class_option :gems, :type => :boolean, :default => true, :desc => "Add gems to gemfile?"
35
- class_option :migrations, :type => :boolean, :default => false, :desc => "Autorun database migrations?", :aliases => '-m'
36
+ class_option :configure, :type => :array, :default => [], :desc => "Finetune which generators to run: app, permits, roles, devise, cancan", :aliases => '-c'
37
+ class_option :gems, :type => :boolean, :default => true, :desc => "Add gems to gemfile?", :aliases => '-g'
38
+ class_option :migrations, :type => :boolean, :default => false, :desc => "Auto-run database migrations?", :aliases => '-m'
36
39
 
37
40
  # Devise customize
38
- class_option :customize, :type => :array, :default => [], :desc => "Which elements of devise to customize (customize generator)"
41
+ class_option :customize, :type => :array, :default => [], :desc => "Which elements of devise to customize (customize generator)", :aliases => '-cust'
39
42
 
40
- class_option :user_name, :type => :boolean, :default => true, :desc => "Add username as login option"
41
- class_option :login_type, :type => :string, :default => 'generic', :desc => "How to login: 'email', 'username', 'generic' (i.e 'username' or 'email')"
43
+ class_option :user_name, :type => :boolean, :default => true, :desc => "Add username as login option", :aliases => '-un'
44
+ class_option :login_type, :type => :string, :default => 'generic', :desc => "How to login: 'email', 'username', 'generic' (i.e 'username' or 'email')", :aliases => '-lt'
42
45
 
43
46
  def main
44
47
  execute_generator if validate_orm && validate_strategy
@@ -88,12 +91,12 @@ module Cream
88
91
  end
89
92
 
90
93
  def run_app
91
- rgen "cream:app --orm #{orm}"
94
+ rgen "cream:app --orm #{orm} #{guest_user_option}"
92
95
  end
93
96
 
94
97
  def run_devise
95
- rgen "devise:config #{user_class} --orm #{orm} #{admin_user_option}"
96
- rgen "devise:users --orm #{orm} --roles #{roles_list} #{admin_user_option} --no-gems"
98
+ rgen "devise:config #{user_class} --orm #{orm}" # --user-types #{user_types}
99
+ rgen "devise:users --orm #{orm} --roles #{roles_list} --user-types #{user_types_list} --no-gems"
97
100
 
98
101
  say("Devise credentials not customized since --customize option was not used to say so!", :green) if !customize_credentials?
99
102
 
@@ -113,9 +116,10 @@ module Cream
113
116
  end
114
117
 
115
118
  def cream_initializer
116
- create_initializer :cream do
119
+ create_initializer :cream do
117
120
  %Q{Cream.setup do |config|
118
121
  config.roles = #{sym_roles.inspect}
122
+ config.user_types = #{user_types_code}
119
123
  end
120
124
 
121
125
  require 'cream/configure/rails'
@@ -123,6 +127,10 @@ require 'cream/configure/rails'
123
127
  end
124
128
  end
125
129
 
130
+ def guest_user_option
131
+ "--guest-user" if guest_user?
132
+ end
133
+
126
134
  def customize_credentials?
127
135
  customize.include? 'credentials'
128
136
  end
@@ -50,6 +50,10 @@ module Cream
50
50
  options[:strategy]
51
51
  end
52
52
 
53
+ def user_types
54
+ options[:user_types]
55
+ end
56
+
53
57
  def admin_user?
54
58
  options[:admin_user]
55
59
  end
@@ -58,6 +62,18 @@ module Cream
58
62
  roles.map(&:to_sym)
59
63
  end
60
64
 
65
+ def guest_user?
66
+ options[:guest_user]
67
+ end
68
+
69
+ def user_types_code
70
+ user_types.map {|ut| "'#{ut}'"}.join(', ')
71
+ end
72
+
73
+ def user_types_list
74
+ user_types.map {|ut| "#{ut}"}.join(' ')
75
+ end
76
+
61
77
  def user_class
62
78
  options[:user_class] || 'User'
63
79
  end
@@ -19,7 +19,6 @@ module Devise
19
19
  class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
20
20
  class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
21
21
  class_option :gems, :type => :boolean, :default => true, :desc => "Add gems to gemfile?"
22
- class_option :admin_user, :type => :boolean, :default => false, :desc => "Setup for admin user"
23
22
 
24
23
  def configure_devise
25
24
  logger.add_logfile :logfile => logfile if logfile
@@ -21,10 +21,12 @@ module Devise
21
21
  # ORM to use
22
22
  class_option :orm, :type => :string, :default => 'active_record', :desc => "ORM to use"
23
23
  class_option :roles, :type => :array, :default => [], :desc => "Roles"
24
- class_option :default_roles, :type => :boolean, :default => true, :desc => "Create default roles :admin and :guest"
24
+ class_option :default_roles, :type => :boolean, :default => true, :desc => "Create default roles :admin and :guest"
25
+
26
+ class_option :user_types, :type => :array, :default => ['admin'], :desc => "Devise Users to create that override the generic base User", :aliases => '-ut'
25
27
 
26
28
  class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
27
- class_option :gems, :type => :boolean, :default => false, :desc => "Add gems to gemfile?"
29
+ class_option :gems, :type => :boolean, :default => false, :desc => "Add gems to gemfile?"
28
30
 
29
31
  def configure_devise_users
30
32
  logger.add_logfile :logfile => logfile if logfile
@@ -40,12 +42,9 @@ module Devise
40
42
  # # if User model is NOT configured with devise strategy
41
43
  insert_devise_strategy user_class, :defaults if !has_devise_user? user_class
42
44
 
43
- # create Admin User model for with devise strategy if admin user should be present
44
- devise_user admin_user if admin_user?
45
-
46
45
  # create role specific user inheriting from base User for each role
47
- roles.each do |role|
48
- create_user(role) if !has_model? role
46
+ user_types.flatten.each do |user_type|
47
+ create_user(user_type) if !has_model?(user_type.as_filename)
49
48
  end
50
49
 
51
50
  routes_configure!
@@ -68,7 +67,7 @@ module Devise
68
67
  def create_user name
69
68
  return if name.to_sym == :guest
70
69
 
71
- logger.debug "create_user: #{name}"
70
+ logger.debug "create devise user: #{name}"
72
71
  create_user_model name
73
72
  # remove any current inheritance
74
73
  remove_inheritance name
@@ -104,16 +103,11 @@ module Devise
104
103
  create_devise_model user_class
105
104
  end
106
105
 
107
- def devise_admin_user
108
- # if app does NOT have a Admin model
109
- create_user(admin_class) if !has_admin_model?
110
- end
111
-
112
106
  private
113
107
 
114
108
  def customize_note
115
109
  %q{
116
- Customize controller action if needed to render individual registration form for each role
110
+ Customize controller action if needed to render an individual registration form for each devise User type
117
111
 
118
112
  Example:
119
113
 
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 8
8
8
  - 9
9
- - 1
10
- version: 0.8.9.1
9
+ - 2
10
+ version: 0.8.9.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kristian Mandrup
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-15 00:00:00 +01:00
18
+ date: 2011-01-18 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -501,9 +501,10 @@ files:
501
501
  - init.rb
502
502
  - lib/cream.rb
503
503
  - lib/cream/configure/after_init/role_config.rb
504
+ - lib/cream/configure/engine.rb
504
505
  - lib/cream/configure/rails.rb
505
506
  - lib/cream/controller/ability.rb
506
- - lib/cream/controller/application_controller.rb
507
+ - lib/cream/controller/user_control.rb
507
508
  - lib/cream/helper/host.rb
508
509
  - lib/cream/helper/role.rb
509
510
  - lib/cream/namespaces.rb
@@ -1,22 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
-
3
- def current_user
4
- if defined? Cream::Role
5
- # Try to get instance of any of the available roles as the current user
6
- Cream::Role.available.each do |role|
7
- method = :"current_#{role}"
8
- @cu ||= send method if respond_to?(method)
9
- end
10
- end
11
- return @cu if @cu
12
- session[:guest] ||= Guest.create if defined?(Guest)
13
- end
14
-
15
- def user_signed_in?
16
- current_user && !current_user.has_role?(:guest)
17
- end
18
-
19
- def user_session
20
- user_signed_in? ? super : session
21
- end
22
- end