as_user 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/CHANGELOG.rdoc +6 -0
  2. data/app/controllers/as_user/abouts_controller.rb +11 -0
  3. data/app/controllers/as_user/application_controller.rb +1 -0
  4. data/app/controllers/as_user/sessions_controller.rb +30 -0
  5. data/app/controllers/as_user/users_controller.rb +29 -10
  6. data/app/helpers/as_user/sessions_helper.rb +41 -0
  7. data/app/models/user.rb +15 -2
  8. data/app/views/as_user/abouts/index.html.erb +10 -0
  9. data/app/views/as_user/sessions/new.html.erb +18 -0
  10. data/app/views/as_user/users/_form.html.erb +6 -2
  11. data/app/views/as_user/users/edit.html.erb +14 -1
  12. data/app/views/as_user/users/edit_password.html.erb +20 -0
  13. data/app/views/as_user/users/index.html.erb +5 -10
  14. data/app/views/as_user/users/show.html.erb +3 -9
  15. data/config/routes.rb +11 -1
  16. data/lib/as_user/version.rb +1 -1
  17. data/test/as_user_test.rb +1 -1
  18. data/test/dummy/config/routes.rb +0 -1
  19. data/test/dummy/log/development.log +6806 -0
  20. data/test/dummy/log/test.log +20335 -1611
  21. data/test/dummy/tmp/cache/assets/C9F/E70/sprockets%2F8b67757c740fd825548e755943fcc159 +0 -0
  22. data/test/dummy/tmp/cache/assets/CD0/0D0/sprockets%2F8d0de3340227ac0a16d94e24230fc686 +0 -0
  23. data/test/dummy/tmp/cache/assets/D0E/5F0/sprockets%2Fba437c1d4e76801fd3bc104599c2c578 +0 -0
  24. data/test/dummy/tmp/cache/assets/D22/DD0/sprockets%2Fdac6c00d18f185d1858ff2166ee69877 +0 -0
  25. data/test/dummy/tmp/cache/assets/D43/1C0/sprockets%2Fce38efff2c50751ae16b548458151df4 +0 -0
  26. data/test/dummy/tmp/cache/assets/D71/8B0/sprockets%2Fd4cf3307f9d2c74049e38b32bfc7c1a9 +0 -0
  27. data/test/dummy/tmp/cache/assets/D79/0E0/sprockets%2Fafbf9d2823b6c5d256e6db15985e80c4 +0 -0
  28. data/test/dummy/tmp/cache/assets/D9F/CA0/sprockets%2F32d6752ac6b8fe73364a60b90cfceac9 +0 -0
  29. data/test/dummy/tmp/cache/assets/DE4/400/sprockets%2F5ba79f02c56ff6a6e8f5830e4e5cee8f +0 -0
  30. data/test/dummy/tmp/cache/assets/E44/0D0/sprockets%2Ffefa905655ba62adaadcc1b225dcd1c1 +0 -0
  31. data/test/dummy/tmp/pids/server.pid +1 -0
  32. data/test/factories/users.rb +14 -0
  33. data/test/fixtures/as_user/users.yml +6 -6
  34. data/test/functional/as_user/abouts_controller_test.rb +16 -0
  35. data/test/functional/as_user/sessions_controller_test.rb +41 -0
  36. data/test/functional/as_user/users_controller_test.rb +91 -16
  37. data/test/integration/as_user/user_flows_test.rb +34 -0
  38. data/test/test_helper.rb +9 -0
  39. data/test/unit/as_user/user_test.rb +45 -10
  40. metadata +39 -5
  41. data/test/integration/navigation_test.rb +0 -10
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.0.3
2
+ * basic user signup,signin,signout,update feature
3
+ * no remember me
4
+ * no mail confirmation
5
+ * no token authentication
6
+ * nothing else
1
7
 
2
8
  == 0.0.2
3
9
 
@@ -0,0 +1,11 @@
1
+ require_dependency "as_user/application_controller"
2
+
3
+ module AsUser
4
+ class AboutsController < ApplicationController
5
+ # GET /users
6
+ # GET /users.json
7
+ def index
8
+
9
+ end
10
+ end
11
+ end
@@ -1,4 +1,5 @@
1
1
  module AsUser
2
2
  class ApplicationController < ActionController::Base
3
+ include SessionsHelper
3
4
  end
4
5
  end
@@ -0,0 +1,30 @@
1
+ require_dependency "as_user/application_controller"
2
+
3
+ module AsUser
4
+ class SessionsController < ApplicationController
5
+ def new
6
+ if signed_in? then
7
+ redirect_to current_user
8
+ end
9
+ end
10
+
11
+ def create
12
+ user = User.find_by_email params[:session][:email].downcase
13
+ if user && user.authenticate(params[:session][:password])
14
+ sign_in user
15
+ redirect_back_or user
16
+ else
17
+ flash.now[:error] = "Invalid email/password combination"
18
+ render 'new'
19
+ end
20
+ end
21
+
22
+ def destroy
23
+ sign_out
24
+ redirect_to root_path, notice: "signed out."
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+
@@ -2,6 +2,8 @@ require_dependency "as_user/application_controller"
2
2
 
3
3
  module AsUser
4
4
  class UsersController < ApplicationController
5
+ before_filter :signed_in_as_self, except: [:index, :show, :new, :create]
6
+
5
7
  # GET /users
6
8
  # GET /users.json
7
9
  def index
@@ -35,11 +37,6 @@ module AsUser
35
37
  end
36
38
  end
37
39
 
38
- # GET /users/1/edit
39
- def edit
40
- @user = User.find(params[:id])
41
- end
42
-
43
40
  # POST /users
44
41
  # POST /users.json
45
42
  def create
@@ -47,35 +44,43 @@ module AsUser
47
44
 
48
45
  respond_to do |format|
49
46
  if @user.save
47
+ sign_in @user
50
48
  format.html { redirect_to @user, notice: 'User was successfully created.' }
51
49
  format.json { render json: @user, status: :created, location: @user }
52
50
  else
51
+ flash[:error]="create user failed."
53
52
  format.html { render action: "new" }
54
53
  format.json { render json: @user.errors, status: :unprocessable_entity }
55
54
  end
56
55
  end
57
56
  end
58
57
 
58
+ # GET /users/1/edit
59
+ def edit
60
+ end
61
+
62
+ def edit_password
63
+ end
64
+
59
65
  # PUT /users/1
60
66
  # PUT /users/1.json
61
67
  def update
62
- @user = User.find(params[:id])
63
-
64
68
  respond_to do |format|
65
69
  if @user.update_attributes(params[:user])
66
70
  format.html { redirect_to @user, notice: 'User was successfully updated.' }
67
71
  format.json { head :no_content }
68
72
  else
69
- format.html { render action: "edit" }
73
+ flash[:error] = "update password failed."
74
+ format.html { redirect_to @user }
70
75
  format.json { render json: @user.errors, status: :unprocessable_entity }
71
76
  end
72
77
  end
73
78
  end
74
-
79
+
75
80
  # DELETE /users/1
76
81
  # DELETE /users/1.json
77
82
  def destroy
78
- @user = User.find(params[:id])
83
+ #@user = User.find(params[:id])
79
84
  @user.destroy
80
85
 
81
86
  respond_to do |format|
@@ -83,5 +88,19 @@ module AsUser
83
88
  format.json { head :no_content }
84
89
  end
85
90
  end
91
+
92
+ private
93
+ def signed_in_as_self
94
+ @user = User.find(params[:id])
95
+ if current_user
96
+ unless current_user?(@user)
97
+ flash[:error] = "can only modify your own account."
98
+ redirect_to root_path
99
+ end
100
+ else
101
+ store_location
102
+ redirect_to signin_path
103
+ end
104
+ end
86
105
  end
87
106
  end
@@ -0,0 +1,41 @@
1
+ module AsUser
2
+ module SessionsHelper
3
+ def sign_in(user)
4
+ session[:user_id] = user.id
5
+ self.current_user = user
6
+ end
7
+
8
+ def signed_in?
9
+ #session[:original_url] = request.url
10
+ !current_user.nil?
11
+ end
12
+
13
+ def sign_out
14
+ #cookies.delete(:remember_token)
15
+ session[:user_id] = nil
16
+ self.current_user = nil
17
+ end
18
+
19
+ def current_user=(user)
20
+ @current_user = user
21
+ end
22
+
23
+ def current_user
24
+ #@current_user ||= User.find_by_remember_token(cookies[:remember_token])
25
+ @current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
26
+ end
27
+
28
+ def current_user?(user)
29
+ user == current_user
30
+ end
31
+
32
+ def redirect_back_or(default)
33
+ redirect_to(session[:return_to] || default)
34
+ session.delete(:return_to)
35
+ end
36
+
37
+ def store_location
38
+ session[:return_to] = request.url
39
+ end
40
+ end
41
+ end
data/app/models/user.rb CHANGED
@@ -1,3 +1,15 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: users
4
+ #
5
+ # id :integer not null, primary key
6
+ # email :string(255) not null
7
+ # name :string(100)
8
+ # password_digest :string(60)
9
+ # created_at :datetime not null
10
+ # updated_at :datetime not null
11
+ #
12
+
1
13
  class User < ActiveRecord::Base
2
14
  self.table_name = "users"
3
15
  attr_accessible :email, :name, :password, :password_confirmation
@@ -10,6 +22,7 @@ class User < ActiveRecord::Base
10
22
  validates :email, presence: true,
11
23
  format: {with: VALID_EMAIL_REGEX},
12
24
  uniqueness: {case_sensitive: false}
13
- validates :password, presence: true, length: {minimum: 3}
14
- validates :password_confirmation, presence: true
25
+ validates :password, :password_confirmation, presence: true, length: {minimum: 3}, if: Proc.new { |user|
26
+ user.new_record? or user.password
27
+ }
15
28
  end
@@ -0,0 +1,10 @@
1
+
2
+ <br/>
3
+ <br/>
4
+ welcome to use as_user gem for user handling
5
+ <br/>
6
+ <br/>
7
+ manual to be done...
8
+
9
+ <%= link_to 'Signin', signin_path %>
10
+ <%= link_to 'Signup', signup_path %>
@@ -0,0 +1,18 @@
1
+ <h1>Sign in</h1>
2
+
3
+ <div class="message_box">
4
+ <%= flash[:error] if flash[:error] %>
5
+ </div>
6
+
7
+ <div class="login_form">
8
+ <%= form_for(:session, url: sessions_path) do |f| %>
9
+ <%= f.label :email %>
10
+ <%= f.text_field :email %>
11
+
12
+ <%= f.label :password %>
13
+ <%= f.password_field :password %>
14
+
15
+ <%= f.submit "Sign in" %>
16
+ <%end%>
17
+ </div>
18
+
@@ -20,8 +20,12 @@
20
20
  <%= f.text_field :name %>
21
21
  </div>
22
22
  <div class="field">
23
- <%= f.label :password_digest %><br />
24
- <%= f.text_field :password_digest %>
23
+ <%= f.label :password%><br />
24
+ <%= f.password_field :password%>
25
+ </div>
26
+ <div class="field">
27
+ <%= f.label :password_confirmation %><br />
28
+ <%= f.password_field :password_confirmation %>
25
29
  </div>
26
30
  <div class="actions">
27
31
  <%= f.submit %>
@@ -1,6 +1,19 @@
1
1
  <h1>Editing user</h1>
2
2
 
3
- <%= render 'form' %>
3
+ <%#= render 'form' %>
4
+ <%= form_for(@user) do |f| %>
5
+ <div class="field">
6
+ <%= f.label :email %><br />
7
+ <%= f.text_field :email %>
8
+ </div>
9
+ <div class="field">
10
+ <%= f.label :name %><br />
11
+ <%= f.text_field :name %>
12
+ </div>
13
+ <div class="actions">
14
+ <%= f.submit %>
15
+ </div>
16
+ <% end %>
4
17
 
5
18
  <%= link_to 'Show', @user %> |
6
19
  <%= link_to 'Back', users_path %>
@@ -0,0 +1,20 @@
1
+ <h1>Editing user</h1>
2
+
3
+ <%#= render 'form' %>
4
+ <%= form_for(@user) do |f| %>
5
+ <h3><%=@user.name%></h3>
6
+ <h3><%=@user.email%></h3>
7
+ <div class="field">
8
+ <%= f.label :password %><br />
9
+ <%= f.password_field :password %>
10
+ </div>
11
+ <div class="field">
12
+ <%= f.label :password_confirmation %><br />
13
+ <%= f.password_field :password_confirmation %>
14
+ </div>
15
+ <div class="actions">
16
+ <%= f.submit %>
17
+ </div>
18
+ <% end %>
19
+
20
+ <%= link_to 'Show', @user %> |
@@ -1,23 +1,17 @@
1
- <h1>Listing users</h1>
1
+ <h1>users</h1>
2
2
 
3
3
  <table>
4
4
  <tr>
5
+ <th>Id</th>
5
6
  <th>Email</th>
6
7
  <th>Name</th>
7
- <th>Password digest</th>
8
- <th></th>
9
- <th></th>
10
- <th></th>
11
8
  </tr>
12
9
 
13
10
  <% @users.each do |user| %>
14
11
  <tr>
15
- <td><%= user.email %></td>
12
+ <td><%= user.id %></td>
13
+ <td><%= "***" || user.email %></td>
16
14
  <td><%= user.name %></td>
17
- <td><%= user.password_digest %></td>
18
- <td><%= link_to 'Show', user %></td>
19
- <td><%= link_to 'Edit', edit_user_path(user) %></td>
20
- <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
21
15
  </tr>
22
16
  <% end %>
23
17
  </table>
@@ -25,3 +19,4 @@
25
19
  <br />
26
20
 
27
21
  <%= link_to 'New User', new_user_path %>
22
+ <%= link_to 'Signin', signin_path %>
@@ -1,8 +1,8 @@
1
1
  <p id="notice"><%= notice %></p>
2
2
 
3
3
  <p>
4
- <b>Email:</b>
5
- <%= @user.email %>
4
+ <b>Id:</b>
5
+ <%= @user.id %>
6
6
  </p>
7
7
 
8
8
  <p>
@@ -10,11 +10,5 @@
10
10
  <%= @user.name %>
11
11
  </p>
12
12
 
13
- <p>
14
- <b>Password digest:</b>
15
- <%= @user.password_digest %>
16
- </p>
17
-
18
-
19
- <%= link_to 'Edit', edit_user_path(@user) %> |
20
13
  <%= link_to 'Back', users_path %>
14
+ <%= link_to 'Logout', signout_path,method: :delete if current_user%>
data/config/routes.rb CHANGED
@@ -1,5 +1,15 @@
1
1
  AsUser::Engine.routes.draw do
2
- resources :users
2
+ resources :users do
3
+ member do
4
+ get :edit_password
5
+ end
6
+ end
7
+ resources :sessions, only: [:new, :create, :destroy]
3
8
 
9
+ match "/signup", to: 'users#new'
10
+ match "/signin", to: 'sessions#new'
11
+ match "/signout", to: 'sessions#destroy', via: :delete
4
12
 
13
+ get "abouts/index" => "abouts#index"
14
+ root to: "abouts#index"
5
15
  end
@@ -1,3 +1,3 @@
1
1
  module AsUser
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/test/as_user_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class AsUserTest < ActiveSupport::TestCase
4
- test "truth" do
4
+ test "AsUser should be Module" do
5
5
  assert_kind_of Module, AsUser
6
6
  end
7
7
  end
@@ -1,4 +1,3 @@
1
1
  Rails.application.routes.draw do
2
-
3
2
  mount AsUser::Engine => "/as_user"
4
3
  end