merbful_authentication 0.1.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.
- data/LICENSE +25 -0
- data/README +69 -0
- data/Rakefile +35 -0
- data/TODO +5 -0
- data/activerecord_generators/merbful_authentication_model/merbful_authentication_model_generator.rb +65 -0
- data/activerecord_generators/merbful_authentication_model/templates/authenticated_system_orm_map.rb +34 -0
- data/activerecord_generators/merbful_authentication_model/templates/migration.rb +20 -0
- data/activerecord_generators/merbful_authentication_model/templates/model.rb +63 -0
- data/datamapper_generators/merbful_authentication_model/merbful_authentication_model_generator.rb +60 -0
- data/datamapper_generators/merbful_authentication_model/templates/authenticated_system_orm_map.rb +34 -0
- data/datamapper_generators/merbful_authentication_model/templates/model.rb +78 -0
- data/lib/merbful_authentication.rb +10 -0
- data/lib/merbful_authentication/merbtasks.rb +6 -0
- data/merb_generators/authentication/USAGE +5 -0
- data/merb_generators/authentication/authentication_generator.rb +256 -0
- data/merb_generators/authentication/templates/activation.html.erb +1 -0
- data/merb_generators/authentication/templates/activation.text.erb +1 -0
- data/merb_generators/authentication/templates/authenticated_system_controller.rb +132 -0
- data/merb_generators/authentication/templates/authenticated_system_model.rb +97 -0
- data/merb_generators/authentication/templates/login.html.erb +14 -0
- data/merb_generators/authentication/templates/mail_controller.rb +13 -0
- data/merb_generators/authentication/templates/model_controller.rb +33 -0
- data/merb_generators/authentication/templates/new_model.html.erb +18 -0
- data/merb_generators/authentication/templates/session_controller.rb +33 -0
- data/merb_generators/authentication/templates/signup.html.erb +8 -0
- data/merb_generators/authentication/templates/signup.text.erb +8 -0
- data/rspec_generators/merbful_authentication_tests/merbful_authentication_tests_generator.rb +83 -0
- data/rspec_generators/merbful_authentication_tests/templates/authenticated_system_spec_helper.rb +22 -0
- data/rspec_generators/merbful_authentication_tests/templates/model_controller_spec.rb +78 -0
- data/rspec_generators/merbful_authentication_tests/templates/model_spec.rb +357 -0
- data/rspec_generators/merbful_authentication_tests/templates/model_spec_helper.rb +8 -0
- data/rspec_generators/merbful_authentication_tests/templates/session_controller_spec.rb +101 -0
- data/rspec_generators/merbful_authentication_tests/templates/user_mailer_spec.rb +70 -0
- data/test_unit_generators/merbful_authentication_tests/USAGE +5 -0
- data/test_unit_generators/merbful_authentication_tests/merbful_authentication_tests_generator.rb +84 -0
- data/test_unit_generators/merbful_authentication_tests/templates/authenticated_system_test_helper.rb +50 -0
- data/test_unit_generators/merbful_authentication_tests/templates/functional_test.rb +92 -0
- data/test_unit_generators/merbful_authentication_tests/templates/mailer_test.rb +66 -0
- data/test_unit_generators/merbful_authentication_tests/templates/model_functional_test.rb +92 -0
- data/test_unit_generators/merbful_authentication_tests/templates/model_test_helper.rb +8 -0
- data/test_unit_generators/merbful_authentication_tests/templates/unit_test.rb +142 -0
- metadata +114 -0
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2007 Daniel Neighman
|
2
|
+
|
3
|
+
All permissions are as per the original restful_authentication project.
|
4
|
+
|
5
|
+
Licence of restful_authentication
|
6
|
+
|
7
|
+
Copyright (c) 2005 Rick Olson
|
8
|
+
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
10
|
+
this software and associated documentation files (the "Software"), to deal in
|
11
|
+
the Software without restriction, including without limitation the rights to
|
12
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
13
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
14
|
+
subject to the following conditions:
|
15
|
+
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
17
|
+
copies or substantial portions of the Software.
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
21
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
22
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
23
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
24
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
+
=======
|
data/README
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
merbful_authentication
|
2
|
+
=================
|
3
|
+
|
4
|
+
This is a port of the rails plugin "restful_authentication" by Rick Olson to the Merb Web Framework.
|
5
|
+
|
6
|
+
It is currently very freshly ported. Please report any bugs / patches to
|
7
|
+
has.sox /at/ gmail /dot/ com
|
8
|
+
|
9
|
+
You can find the original at http://svn.techno-weenie.net/projects/plugins/restful_authentication/
|
10
|
+
|
11
|
+
To use:
|
12
|
+
|
13
|
+
./script/generate authenticated user sessions \
|
14
|
+
--include-activation
|
15
|
+
|
16
|
+
The first parameter specifies the model that gets created in signup
|
17
|
+
(typically a user or account model). A model with migration is
|
18
|
+
created (if migrations are available), as well as a basic controller with the create method.
|
19
|
+
|
20
|
+
The second parameter specifies the sessions controller name. This is
|
21
|
+
the controller that handles the actual login/logout function on the
|
22
|
+
site.
|
23
|
+
|
24
|
+
The third parameter (--include-activation) generates the code for a
|
25
|
+
ActionMailer and its respective Activation Code through email.
|
26
|
+
|
27
|
+
=== Currently supported ORMs
|
28
|
+
Active Record
|
29
|
+
Datamapper
|
30
|
+
|
31
|
+
=== Required Routes
|
32
|
+
|
33
|
+
At the moment this version of the plugin requires some named routes
|
34
|
+
|
35
|
+
In config/router.rb
|
36
|
+
|
37
|
+
r.resources :users
|
38
|
+
r.match("/login").to(:controller => "Sessions", :action => "create").name(:login)
|
39
|
+
r.match("/logout").to(:controller => "Sessions", :action => "destroy").name(:logout)
|
40
|
+
r.match("/users/activate/:activation_code").to(:controller => "Users", :action => "activate").name(:user_activation)
|
41
|
+
|
42
|
+
|
43
|
+
Note:
|
44
|
+
When using activation don't forget to setup your mailer.
|
45
|
+
|
46
|
+
|
47
|
+
Copyright of original project
|
48
|
+
|
49
|
+
Copyright (c) 2005 Rick Olson
|
50
|
+
|
51
|
+
Unless noted specifically, all plugins in this repository are MIT licensed:
|
52
|
+
|
53
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
54
|
+
this software and associated documentation files (the "Software"), to deal in
|
55
|
+
the Software without restriction, including without limitation the rights to
|
56
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
57
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
58
|
+
subject to the following conditions:
|
59
|
+
|
60
|
+
The above copyright notice and this permission notice shall be included in all
|
61
|
+
copies or substantial portions of the Software.
|
62
|
+
|
63
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
64
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
65
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
66
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
67
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
68
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
69
|
+
=======
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
|
4
|
+
PLUGIN = "merbful_authentication"
|
5
|
+
NAME = "merbful_authentication"
|
6
|
+
VERSION = "0.1.0"
|
7
|
+
AUTHOR = "Daniel Neighman"
|
8
|
+
EMAIL = "has.sox@gmail.com"
|
9
|
+
HOMEPAGE = "http://rubyforge.org/projects/merbful-auth/"
|
10
|
+
SUMMARY = "A Merb plugin that is essentially a port of Rick Olsons restful_authentication plugin for rails"
|
11
|
+
|
12
|
+
spec = Gem::Specification.new do |s|
|
13
|
+
s.name = NAME
|
14
|
+
s.version = VERSION
|
15
|
+
s.platform = Gem::Platform::RUBY
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
|
18
|
+
s.summary = SUMMARY
|
19
|
+
s.description = s.summary
|
20
|
+
s.author = AUTHOR
|
21
|
+
s.email = EMAIL
|
22
|
+
s.homepage = HOMEPAGE
|
23
|
+
s.add_dependency('merb', '>= 0.4.0')
|
24
|
+
s.require_path = 'lib'
|
25
|
+
s.autorequire = PLUGIN
|
26
|
+
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs,merb_generators,datamapper_generators,activerecord_generators,rspec_generators,test_unit_generators}/**/*")
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
30
|
+
pkg.gem_spec = spec
|
31
|
+
end
|
32
|
+
|
33
|
+
task :install => [:package] do
|
34
|
+
sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
|
35
|
+
end
|
data/TODO
ADDED
data/activerecord_generators/merbful_authentication_model/merbful_authentication_model_generator.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
class MerbfulAuthenticationModelGenerator < RubiGen::Base
|
2
|
+
|
3
|
+
attr_reader :name,
|
4
|
+
:class_name,
|
5
|
+
:class_path,
|
6
|
+
:file_name,
|
7
|
+
:class_nesting,
|
8
|
+
:class_nesting_depth,
|
9
|
+
:plural_name,
|
10
|
+
:singular_name,
|
11
|
+
:include_activation
|
12
|
+
|
13
|
+
def initialize(runtime_args, runtime_options = {})
|
14
|
+
super
|
15
|
+
usage if args.empty?
|
16
|
+
@name = args.shift
|
17
|
+
extract_options
|
18
|
+
runtime_options.each{ |k,v| self.instance_variable_set("@#{k}", v) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def manifest
|
22
|
+
record do |m|
|
23
|
+
# Ensure appropriate folder(s) exists
|
24
|
+
m.class_collisions [], 'AuthenticatedSystem::OrmMap'
|
25
|
+
|
26
|
+
m.directory File.join('app/models', class_path)
|
27
|
+
m.directory File.join('lib')
|
28
|
+
|
29
|
+
m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
30
|
+
m.template 'authenticated_system_orm_map.rb', "lib/authenticated_system_orm_map.rb"
|
31
|
+
|
32
|
+
m.migration_template('migration.rb', 'schema/migrations', :migration_file_name => "create_#{plural_name}",
|
33
|
+
:assigns => {
|
34
|
+
:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
|
35
|
+
})
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
protected
|
40
|
+
def banner
|
41
|
+
<<-EOS
|
42
|
+
Creates a ...
|
43
|
+
|
44
|
+
USAGE: #{$0} #{spec.name} name"
|
45
|
+
EOS
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_options!(opts)
|
49
|
+
# opts.separator ''
|
50
|
+
# opts.separator 'Options:'
|
51
|
+
# For each option below, place the default
|
52
|
+
# at the top of the file next to "default_options"
|
53
|
+
# opts.on("-a", "--author=\"Your Name\"", String,
|
54
|
+
# "Some comment about this option",
|
55
|
+
# "Default: none") { |options[:author]| }
|
56
|
+
# opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
|
57
|
+
end
|
58
|
+
|
59
|
+
def extract_options
|
60
|
+
# for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
|
61
|
+
# Templates can access these value via the attr_reader-generated methods, but not the
|
62
|
+
# raw instance variable value.
|
63
|
+
# @author = options[:author]
|
64
|
+
end
|
65
|
+
end
|
data/activerecord_generators/merbful_authentication_model/templates/authenticated_system_orm_map.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module AuthenticatedSystem
|
2
|
+
module OrmMap
|
3
|
+
|
4
|
+
def find_authenticated_model_with_id(id)
|
5
|
+
<%= class_name %>.find_by_id(id)
|
6
|
+
end
|
7
|
+
|
8
|
+
def find_authenticated_model_with_remember_token(rt)
|
9
|
+
<%= class_name %>.find_by_remember_token(rt)
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_activated_authenticated_model_with_login(login)
|
13
|
+
if <%= class_name %>.instance_methods.include?("activated_at")
|
14
|
+
<%= class_name %>.find(:first, :conditions => ["login=? AND activated_at IS NOT NULL", login])
|
15
|
+
else
|
16
|
+
<%= class_name %>.find_by_login(login)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_activated_authenticated_model(activation_code)
|
21
|
+
<%= class_name %>.find_by_activation_code(activation_code)
|
22
|
+
end
|
23
|
+
|
24
|
+
def find_with_conditions(conditions)
|
25
|
+
<%= class_name %>.find(:first, :conditions => conditions)
|
26
|
+
end
|
27
|
+
|
28
|
+
# A method to assist with specs
|
29
|
+
def clear_database_table
|
30
|
+
<%= class_name %>.delete_all
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class <%= migration_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table "<%= plural_name %>", :force => true do |t|
|
4
|
+
t.column :login, :string
|
5
|
+
t.column :email, :string
|
6
|
+
t.column :crypted_password, :string, :limit => 40
|
7
|
+
t.column :salt, :string, :limit => 40
|
8
|
+
t.column :created_at, :datetime
|
9
|
+
t.column :updated_at, :datetime
|
10
|
+
t.column :remember_token, :string
|
11
|
+
t.column :remember_token_expires_at, :datetime
|
12
|
+
<% if options[:include_activation] %>t.column :activation_code, :string, :limit => 40
|
13
|
+
t.column :activated_at, :datetime<% end %>
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table "<%= singular_name %>"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require 'authenticated_system_model'
|
3
|
+
class <%= class_name %> < ActiveRecord::Base
|
4
|
+
include AuthenticatedSystem::Model
|
5
|
+
|
6
|
+
# Virtual attribute for the unencrypted password
|
7
|
+
attr_accessor :password
|
8
|
+
|
9
|
+
validates_presence_of :login, :email
|
10
|
+
validates_presence_of :password, :if => :password_required?
|
11
|
+
validates_presence_of :password_confirmation, :if => :password_required?
|
12
|
+
validates_length_of :password, :within => 4..40, :if => :password_required?
|
13
|
+
validates_confirmation_of :password, :if => :password_required?
|
14
|
+
validates_length_of :login, :within => 3..40
|
15
|
+
validates_length_of :email, :within => 3..100
|
16
|
+
validates_uniqueness_of :login, :email, :case_sensitive => false
|
17
|
+
before_save :encrypt_password
|
18
|
+
<% if include_activation -%>
|
19
|
+
before_create :make_activation_code
|
20
|
+
after_create :send_signup_notification
|
21
|
+
<% end -%>
|
22
|
+
# prevents a user from submitting a crafted form that bypasses activation
|
23
|
+
# anything else you want your user to change should be added here.
|
24
|
+
attr_accessible :login, :email, :password, :password_confirmation
|
25
|
+
|
26
|
+
def login=(login_name)
|
27
|
+
self[:login] = login_name.downcase unless login_name.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
<% if options[:include_activation] -%>
|
31
|
+
EMAIL_FROM = "info@mysite.com"
|
32
|
+
SIGNUP_MAIL_SUBJECT = "Welcome to MYSITE. Please activate your account."
|
33
|
+
ACTIVATE_MAIL_SUBJECT = "Welcome to MYSITE"
|
34
|
+
|
35
|
+
# Activates the <%= singular_name %> in the database
|
36
|
+
def activate
|
37
|
+
@activated = true
|
38
|
+
self.activated_at = Time.now.utc
|
39
|
+
self.activation_code = nil
|
40
|
+
save
|
41
|
+
|
42
|
+
# send mail for activation
|
43
|
+
<%= class_name %>Mailer.dispatch_and_deliver( :activation_notification,
|
44
|
+
{ :from => <%= class_name %>::EMAIL_FROM,
|
45
|
+
:to => self.email,
|
46
|
+
:subject => <%= class_name %>::ACTIVATE_MAIL_SUBJECT },
|
47
|
+
|
48
|
+
:<%= singular_name %> => self )
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def send_signup_notification
|
53
|
+
<%= class_name %>Mailer.dispatch_and_deliver(
|
54
|
+
:signup_notification,
|
55
|
+
{ :from => <%= class_name %>::EMAIL_FROM,
|
56
|
+
:to => self.email,
|
57
|
+
:subject => <%= class_name %>::SIGNUP_MAIL_SUBJECT },
|
58
|
+
:<%= singular_name %> => self
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
<% end -%>
|
63
|
+
end
|
data/datamapper_generators/merbful_authentication_model/merbful_authentication_model_generator.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
class MerbfulAuthenticationModelGenerator < RubiGen::Base
|
2
|
+
|
3
|
+
attr_reader :name,
|
4
|
+
:class_name,
|
5
|
+
:class_path,
|
6
|
+
:file_name,
|
7
|
+
:class_nesting,
|
8
|
+
:class_nesting_depth,
|
9
|
+
:plural_name,
|
10
|
+
:singular_name,
|
11
|
+
:include_activation
|
12
|
+
|
13
|
+
def initialize(runtime_args, runtime_options = {})
|
14
|
+
super
|
15
|
+
usage if args.empty?
|
16
|
+
@name = args.shift
|
17
|
+
extract_options
|
18
|
+
runtime_options.each{ |k,v| self.instance_variable_set("@#{k}", v) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def manifest
|
22
|
+
record do |m|
|
23
|
+
# Ensure appropriate folder(s) exists
|
24
|
+
m.class_collisions [], 'AuthenticatedSystem::OrmMap'
|
25
|
+
|
26
|
+
m.directory File.join('app/models', class_path)
|
27
|
+
m.directory File.join('lib')
|
28
|
+
|
29
|
+
m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
30
|
+
m.template 'authenticated_system_orm_map.rb', "lib/authenticated_system_orm_map.rb"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
def banner
|
36
|
+
<<-EOS
|
37
|
+
Creates a ...
|
38
|
+
|
39
|
+
USAGE: #{$0} #{spec.name} name"
|
40
|
+
EOS
|
41
|
+
end
|
42
|
+
|
43
|
+
def add_options!(opts)
|
44
|
+
# opts.separator ''
|
45
|
+
# opts.separator 'Options:'
|
46
|
+
# For each option below, place the default
|
47
|
+
# at the top of the file next to "default_options"
|
48
|
+
# opts.on("-a", "--author=\"Your Name\"", String,
|
49
|
+
# "Some comment about this option",
|
50
|
+
# "Default: none") { |options[:author]| }
|
51
|
+
# opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
|
52
|
+
end
|
53
|
+
|
54
|
+
def extract_options
|
55
|
+
# for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
|
56
|
+
# Templates can access these value via the attr_reader-generated methods, but not the
|
57
|
+
# raw instance variable value.
|
58
|
+
# @author = options[:author]
|
59
|
+
end
|
60
|
+
end
|
data/datamapper_generators/merbful_authentication_model/templates/authenticated_system_orm_map.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module AuthenticatedSystem
|
2
|
+
module OrmMap
|
3
|
+
|
4
|
+
def find_authenticated_model_with_id(id)
|
5
|
+
<%= class_name %>.first(:id => id)
|
6
|
+
end
|
7
|
+
|
8
|
+
def find_authenticated_model_with_remember_token(rt)
|
9
|
+
<%= class_name %>.first(:remember_token => rt)
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_activated_authenticated_model_with_login(login)
|
13
|
+
if <%= class_name %>.instance_methods.include?("activated_at")
|
14
|
+
<%= class_name %>.first(:login => login, :activated_at.not => nil)
|
15
|
+
else
|
16
|
+
<%= class_name %>.first(:login => login)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_activated_authenticated_model(activation_code)
|
21
|
+
<%= class_name %>.first(:activation_code => activation_code)
|
22
|
+
end
|
23
|
+
|
24
|
+
def find_with_conditions(conditions)
|
25
|
+
<%= class_name %>.first(conditions)
|
26
|
+
end
|
27
|
+
|
28
|
+
# A method to assist with specs
|
29
|
+
def clear_database_table
|
30
|
+
<%= class_name %>.auto_migrate!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require 'authenticated_system_model'
|
3
|
+
class <%= class_name %> < DataMapper::Base
|
4
|
+
include AuthenticatedSystem::Model
|
5
|
+
|
6
|
+
attr_accessor :password, :password_confirmation
|
7
|
+
|
8
|
+
property :login, :string
|
9
|
+
property :email, :string
|
10
|
+
property :crypted_password, :string
|
11
|
+
property :salt, :string
|
12
|
+
<% if include_activation -%>
|
13
|
+
property :activation_code, :string
|
14
|
+
property :activated_at, :datetime
|
15
|
+
<% end -%>
|
16
|
+
property :remember_token_expires_at, :datetime
|
17
|
+
property :remember_token, :string
|
18
|
+
property :created_at, :datetime
|
19
|
+
property :updated_at, :datetime
|
20
|
+
|
21
|
+
validates_length_of :login, :within => 3..40
|
22
|
+
validates_uniqueness_of :login
|
23
|
+
validates_presence_of :email
|
24
|
+
# validates_format_of :email, :as => :email_address
|
25
|
+
validates_length_of :email, :within => 3..100
|
26
|
+
validates_uniqueness_of :email
|
27
|
+
validates_presence_of :password, :if => proc {password_required?}
|
28
|
+
validates_presence_of :password_confirmation, :if => proc {password_required?}
|
29
|
+
validates_length_of :password, :within => 4..40, :if => proc {password_required?}
|
30
|
+
validates_confirmation_of :password, :groups => :create
|
31
|
+
|
32
|
+
before_save :encrypt_password
|
33
|
+
<% if include_activation -%>
|
34
|
+
before_create :make_activation_code
|
35
|
+
after_create :send_signup_notification
|
36
|
+
<% end -%>
|
37
|
+
|
38
|
+
def login=(value)
|
39
|
+
@login = value.downcase unless value.nil?
|
40
|
+
end
|
41
|
+
|
42
|
+
<% if include_activation -%>
|
43
|
+
EMAIL_FROM = "info@mysite.com"
|
44
|
+
SIGNUP_MAIL_SUBJECT = "Welcome to MYSITE. Please activate your account."
|
45
|
+
ACTIVATE_MAIL_SUBJECT = "Welcome to MYSITE"
|
46
|
+
|
47
|
+
# Activates the <%= singular_name %> in the database
|
48
|
+
def activate
|
49
|
+
@activated = true
|
50
|
+
self.activated_at = Time.now.utc
|
51
|
+
self.activation_code = nil
|
52
|
+
save
|
53
|
+
|
54
|
+
# send mail for activation
|
55
|
+
<%= class_name %>Mailer.dispatch_and_deliver( :activation_notification,
|
56
|
+
{ :from => <%= class_name %>::EMAIL_FROM,
|
57
|
+
:to => self.email,
|
58
|
+
:subject => <%= class_name %>::ACTIVATE_MAIL_SUBJECT },
|
59
|
+
|
60
|
+
:<%= singular_name %> => self )
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
def send_signup_notification
|
65
|
+
<%= class_name %>Mailer.dispatch_and_deliver(
|
66
|
+
:signup_notification,
|
67
|
+
{ :from => <%= class_name %>::EMAIL_FROM,
|
68
|
+
:to => self.email,
|
69
|
+
:subject => <%= class_name %>::SIGNUP_MAIL_SUBJECT },
|
70
|
+
:<%= singular_name %> => self
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
<% end -%>
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
end
|