i0n_rails3_generators 0.2.9 → 0.2.10
Sign up to get free protection for your applications and to get access to all the features.
- data/config/version.yml +1 -1
- data/lib/generators/i0n/authentication/USAGE +8 -0
- data/lib/generators/i0n/authentication/authentication_generator.rb +48 -0
- data/lib/generators/i0n/authentication/templates/app/controllers/_sessions_controller.rb +23 -0
- data/lib/generators/i0n/authentication/templates/app/controllers/_users_controller.rb +38 -0
- data/lib/generators/i0n/authentication/templates/app/models/user.rb +46 -0
- data/lib/generators/i0n/authentication/templates/app/views/user_sessions/new.haml +25 -0
- data/lib/generators/i0n/authentication/templates/app/views/users/_form.haml +16 -0
- data/lib/generators/i0n/authentication/templates/app/views/users/edit.haml +8 -0
- data/lib/generators/i0n/authentication/templates/app/views/users/index.haml +9 -0
- data/lib/generators/i0n/authentication/templates/app/views/users/new.haml +8 -0
- data/lib/generators/i0n/authentication/templates/lib/authentication.rb +50 -0
- data/lib/generators/i0n/authentication/templates/lib/tasks/populate.rake +16 -0
- data/lib/generators/i0n/layout/layout_generator.rb +6 -6
- metadata +13 -2
- data/lib/generators/i0n/layout/templates/config/_application.rb +0 -4
data/config/version.yml
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
class AuthenticationGenerator < Rails::Generators::NamedBase
|
2
|
+
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
def gemfile
|
6
|
+
gem("bcrypt-ruby")
|
7
|
+
end
|
8
|
+
|
9
|
+
def setup_application
|
10
|
+
application do
|
11
|
+
'
|
12
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
13
|
+
config.filter_parameters += [:password, :password_confirmation]
|
14
|
+
|
15
|
+
config.autoload_paths += %W(#{Rails.root}/lib)
|
16
|
+
'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_authentication_lib_and_include_in_application_helper
|
21
|
+
copy_file "lib/authentication.rb", "#{Rails.root}/lib/authentication.rb"
|
22
|
+
inject_into_class("app/controllers/application_controller.rb", "include Authentication")
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_routes
|
26
|
+
route("match 'register' => 'users#new', :as => :register")
|
27
|
+
route("resources :users")
|
28
|
+
route("resources :user_sessions, :only => [:new, :create, :destroy]")
|
29
|
+
route("match 'login' => 'user_sessions#new', :as => :login")
|
30
|
+
route("match 'logout' => 'user_sessions#destroy', :as => :logout")
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_models
|
34
|
+
generate("model", "user email:string password_hash:string password_salt:string")
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_controllers
|
38
|
+
generate("controller", "user_sessions")
|
39
|
+
generate("controller", "users")
|
40
|
+
inject_into_class("app/controllers/users_controller.rb", IO.read("#{AuthenticationGenerator.source_root}/app/controllers/_users_controller.rb"))
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_views
|
44
|
+
directory "app/views/users", "#{Rails.root}/app/views/users"
|
45
|
+
directory "app/view/user_sessions", "#{Rails.root}/app/views/user_sessions"
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
before_filter :login_required, :except => [:new, :create]
|
3
|
+
|
4
|
+
def new
|
5
|
+
end
|
6
|
+
|
7
|
+
def create
|
8
|
+
user = User.authenticate(params[:login], params[:password])
|
9
|
+
if user
|
10
|
+
session[:user_id] = user.id
|
11
|
+
flash[:notice] = "Logged in successfully."
|
12
|
+
redirect_to_target_or_default("/")
|
13
|
+
else
|
14
|
+
flash.now[:error] = "Invalid login or password."
|
15
|
+
render :action => 'new'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def destroy
|
20
|
+
session[:user_id] = nil
|
21
|
+
flash[:notice] = "You have been logged out."
|
22
|
+
redirect_to "/"
|
23
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
before_filter :login_required
|
3
|
+
|
4
|
+
def index
|
5
|
+
@users = User.all
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
def new
|
10
|
+
@user = User.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def create
|
14
|
+
@user = User.new(params[:user])
|
15
|
+
if @user.save
|
16
|
+
session[:user_id] = @user.id
|
17
|
+
flash[:notice] = "Thank you for signing up! You are now logged in."
|
18
|
+
redirect_to "/"
|
19
|
+
else
|
20
|
+
render :action => 'new'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def edit
|
25
|
+
@user = current_user
|
26
|
+
end
|
27
|
+
|
28
|
+
def update
|
29
|
+
@user = current_user
|
30
|
+
respond_to do |format|
|
31
|
+
if @user.update_attributes(params[:user])
|
32
|
+
format.html { redirect_to(root_path, :notice => 'User information updated.') }
|
33
|
+
else
|
34
|
+
format.html { render :action => "edit" }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class User
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Timestamps
|
4
|
+
field :username, :type => String
|
5
|
+
field :email, :type => String
|
6
|
+
field :password_salt, :type => String
|
7
|
+
field :password_hash, :type => String
|
8
|
+
|
9
|
+
# new columns need to be added here to be writable through mass assignment
|
10
|
+
attr_accessible :username, :email, :password, :password_confirmation
|
11
|
+
|
12
|
+
attr_accessor :password
|
13
|
+
before_save :prepare_password
|
14
|
+
|
15
|
+
validates_presence_of :username
|
16
|
+
validates_uniqueness_of :username, :email, :allow_blank => true
|
17
|
+
validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
|
18
|
+
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
|
19
|
+
validates_presence_of :password, :on => :create
|
20
|
+
validates_confirmation_of :password
|
21
|
+
validates_length_of :password, :minimum => 4, :allow_blank => true
|
22
|
+
|
23
|
+
# login can be either username or email address
|
24
|
+
def self.authenticate(login, pass)
|
25
|
+
user = find(:first, :conditions => {:username => login}) || find(:first, :conditions => {:email => login})
|
26
|
+
return user if user && user.matching_password?(pass)
|
27
|
+
end
|
28
|
+
|
29
|
+
def matching_password?(pass)
|
30
|
+
self.password_hash == encrypt_password(pass)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def prepare_password
|
36
|
+
unless password.blank?
|
37
|
+
self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
|
38
|
+
self.password_hash = encrypt_password(password)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def encrypt_password(pass)
|
43
|
+
Digest::SHA1.hexdigest([pass, password_salt].join)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
- content_for :title do
|
2
|
+
Log in
|
3
|
+
|
4
|
+
- content_for :description do
|
5
|
+
Ian Web Designs - Website design and web development. Friendly helpful advice and state of the art web design. Contact me now for a free consultation!
|
6
|
+
|
7
|
+
- content_for :keywords do
|
8
|
+
web design,website design,web development,website development,user experience design,usability
|
9
|
+
|
10
|
+
- content_for :body_id do
|
11
|
+
form_layout
|
12
|
+
|
13
|
+
- content_for :h1 do
|
14
|
+
Login
|
15
|
+
|
16
|
+
%article
|
17
|
+
= form_tag user_sessions_path do
|
18
|
+
.field
|
19
|
+
= label_tag :login, "Username or Email Address"
|
20
|
+
= text_field_tag :login, params[:login], :autofocus => true
|
21
|
+
.field
|
22
|
+
= label_tag :password
|
23
|
+
= password_field_tag :password
|
24
|
+
.field
|
25
|
+
= submit_tag "Log in"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
= form_for @user do |f|
|
2
|
+
= render "shared/error_messages", :target => @user
|
3
|
+
%p
|
4
|
+
= f.label :email, "Email Address"
|
5
|
+
%br/
|
6
|
+
= f.text_field :email
|
7
|
+
%p
|
8
|
+
= f.label :password
|
9
|
+
%br/
|
10
|
+
= f.password_field :password
|
11
|
+
%p
|
12
|
+
= f.label :password_confirmation, "Confirm Password"
|
13
|
+
%br/
|
14
|
+
= f.password_field :password_confirmation
|
15
|
+
%p
|
16
|
+
= f.submit "Submit"
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# This module is included in your application controller which makes
|
2
|
+
# several methods available to all controllers and views. Here's a
|
3
|
+
# common example you might add to your application layout file.
|
4
|
+
#
|
5
|
+
# <% if logged_in? %>
|
6
|
+
# Welcome <%= current_user.username %>! Not you?
|
7
|
+
# <%= link_to "Log out", logout_path %>
|
8
|
+
# <% else %>
|
9
|
+
# <%= link_to "Sign up", signup_path %> or
|
10
|
+
# <%= link_to "log in", login_path %>.
|
11
|
+
# <% end %>
|
12
|
+
#
|
13
|
+
# You can also restrict unregistered users from accessing a controller using
|
14
|
+
# a before filter. For example.
|
15
|
+
#
|
16
|
+
# before_filter :login_required, :except => [:index, :show]
|
17
|
+
#
|
18
|
+
|
19
|
+
module Authentication
|
20
|
+
def self.included(controller)
|
21
|
+
controller.send :helper_method, :current_user, :logged_in?, :redirect_to_target_or_default
|
22
|
+
end
|
23
|
+
|
24
|
+
def current_user
|
25
|
+
@current_user ||= User.find(session[:user_id]) if session[:user_id]
|
26
|
+
end
|
27
|
+
|
28
|
+
def logged_in?
|
29
|
+
current_user
|
30
|
+
end
|
31
|
+
|
32
|
+
def login_required
|
33
|
+
unless logged_in?
|
34
|
+
flash[:error] = "You must first log in or sign up before accessing this page."
|
35
|
+
store_target_location
|
36
|
+
redirect_to login_url
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def redirect_to_target_or_default(default)
|
41
|
+
redirect_to(session[:return_to] || default)
|
42
|
+
session[:return_to] = nil
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def store_target_location
|
48
|
+
session[:return_to] = request.fullpath
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
namespace :db do
|
2
|
+
|
3
|
+
desc "Erase and fill database"
|
4
|
+
task :populate => :environment do
|
5
|
+
require 'faker'
|
6
|
+
[Contact].each(&:delete_all)
|
7
|
+
10.times do
|
8
|
+
contact = Contact.create(:name => Faker::Name.name,
|
9
|
+
:email => (rand(10000).to_s + Faker::Internet.email),
|
10
|
+
:message => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -17,12 +17,12 @@ module I0n
|
|
17
17
|
|
18
18
|
def setup_application
|
19
19
|
application do
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
'
|
21
|
+
config.generators do |g|
|
22
|
+
g.stylesheets false
|
23
|
+
g.template_engine :haml
|
24
|
+
end
|
25
|
+
'
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: i0n_rails3_generators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.10
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ian Alexander Wood (i0n)
|
@@ -63,6 +63,18 @@ files:
|
|
63
63
|
- lib/generators/authlogic/complete/templates/users.yml
|
64
64
|
- lib/generators/authlogic/complete/templates/users_controller.rb
|
65
65
|
- lib/generators/authlogic/complete/templates/users_controller_test.rb
|
66
|
+
- lib/generators/i0n/authentication/authentication_generator.rb
|
67
|
+
- lib/generators/i0n/authentication/templates/app/controllers/_sessions_controller.rb
|
68
|
+
- lib/generators/i0n/authentication/templates/app/controllers/_users_controller.rb
|
69
|
+
- lib/generators/i0n/authentication/templates/app/models/user.rb
|
70
|
+
- lib/generators/i0n/authentication/templates/app/views/user_sessions/new.haml
|
71
|
+
- lib/generators/i0n/authentication/templates/app/views/users/_form.haml
|
72
|
+
- lib/generators/i0n/authentication/templates/app/views/users/edit.haml
|
73
|
+
- lib/generators/i0n/authentication/templates/app/views/users/index.haml
|
74
|
+
- lib/generators/i0n/authentication/templates/app/views/users/new.haml
|
75
|
+
- lib/generators/i0n/authentication/templates/lib/authentication.rb
|
76
|
+
- lib/generators/i0n/authentication/templates/lib/tasks/populate.rake
|
77
|
+
- lib/generators/i0n/authentication/USAGE
|
66
78
|
- lib/generators/i0n/i0n.rb
|
67
79
|
- lib/generators/i0n/layout/layout_generator.rb
|
68
80
|
- lib/generators/i0n/layout/templates/app/sass/_base.sass
|
@@ -72,7 +84,6 @@ files:
|
|
72
84
|
- lib/generators/i0n/layout/templates/app/sass/screen.sass
|
73
85
|
- lib/generators/i0n/layout/templates/app/views/layouts/application.haml
|
74
86
|
- lib/generators/i0n/layout/templates/app/views/shared/_error_messages.haml
|
75
|
-
- lib/generators/i0n/layout/templates/config/_application.rb
|
76
87
|
- lib/generators/i0n/layout/templates/config/compass.rb
|
77
88
|
- lib/generators/i0n/layout/templates/config/initializers/compass.rb
|
78
89
|
- lib/generators/i0n/layout/templates/lib/sass_extensions.rb
|