active_model-session 1.0.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +5 -0
- data/LICENSE +22 -0
- data/README.md +66 -0
- data/Rakefile +10 -0
- data/active_model-session.gemspec +23 -0
- data/lib/active_model/session.rb +26 -0
- data/lib/active_model/session/version.rb +5 -0
- data/test/session_with_user_model_test.rb +65 -0
- data/test/session_without_user_model_test.rb +30 -0
- data/test/test_helper.rb +2 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 58616671194c332ccf649d8a53b7a50b3fed26a2
|
4
|
+
data.tar.gz: e891618fed80a5f384b1dfbe6f1065a2d6afc249
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b8b06d26142b4793f9bbf021d7ceb3882037d8c18d5e129f293373effa3475e9cdc3b5f91ccce8ee0517d36fba9146b841cdcd8c359a0c3e2cc8950369dc500
|
7
|
+
data.tar.gz: f9b384dc4ad955779a72d0e1af1a722bb37d973f5283056ceb3e4f6ec81e45be258968d450f4f6faa41e200f9918c2dbb8450efc5b3d7633b5c9ee5abfa0b47b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kuba Kuźma
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# ActiveModel::Session
|
2
|
+
|
3
|
+
A simple session model implemented using `ActiveModel::Model`.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem "active_model-session"
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install activemodel-session
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The most popular workflow is:
|
22
|
+
|
23
|
+
class SessionController < ApplicationController
|
24
|
+
def new
|
25
|
+
@session = ActiveModel::Session.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def create
|
29
|
+
@session = ActiveModel::Session.new(session_params)
|
30
|
+
if @session.valid?
|
31
|
+
session[:user_id] = @session.user_id
|
32
|
+
redirect_to root_url, notice: "You have been successfully logged in."
|
33
|
+
else
|
34
|
+
render :new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def session_params
|
41
|
+
params.require(:session).permit(:email, :password)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
It uses `User` model by default, and executes `authenticate` method
|
46
|
+
provided normally by `has_secure_password` in `User` model. If you
|
47
|
+
need to authenticate a different type of user, you can do:
|
48
|
+
|
49
|
+
@session = ActiveModel::Session.new(session_params)
|
50
|
+
@session.user = Admin.find_by(email: session.email)
|
51
|
+
if @session.valid?
|
52
|
+
# ...
|
53
|
+
|
54
|
+
If you don't like the default behavior, you can always inherit the
|
55
|
+
session model and override some defaults:
|
56
|
+
|
57
|
+
class Session < ActiveModel::Session
|
58
|
+
def email=(email)
|
59
|
+
@email = email.downcase if email.present?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
## Copyright
|
65
|
+
|
66
|
+
Copyright © 2013 Kuba Kuźma. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "active_model/session/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_model-session"
|
8
|
+
spec.version = ActiveModel::Session::VERSION
|
9
|
+
spec.authors = ["Kuba Kuźma"]
|
10
|
+
spec.email = ["kuba@jah.pl"]
|
11
|
+
spec.description = %q{Simple session model implemented on top of ActiveModel::Model}
|
12
|
+
spec.summary = %q{Simple session model implemented on top of ActiveModel::Model}
|
13
|
+
spec.homepage = "https://github.com/cowbell/active_model-session"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "active_model/session/version"
|
2
|
+
require "active_model"
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
class Session
|
6
|
+
include Model
|
7
|
+
|
8
|
+
attr_accessor :email, :password
|
9
|
+
attr_writer :user
|
10
|
+
|
11
|
+
validates :email, presence: true
|
12
|
+
validate :authenticity, if: -> { email.present? }
|
13
|
+
delegate :id, :authenticate, to: :user, prefix: true, allow_nil: true
|
14
|
+
|
15
|
+
def user
|
16
|
+
return @user if defined?(@user)
|
17
|
+
@user = User.find_by(email: email)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def authenticity
|
23
|
+
errors.add(:password, :invalid) unless user_authenticate(password)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class User
|
4
|
+
attr_accessor :id, :password, :email
|
5
|
+
|
6
|
+
RECORDS = {
|
7
|
+
{email: "alice@example.com"} => {id: 1, email: "alice@example.com", password: "alicesecret"},
|
8
|
+
{email: "bob@example.com"} => {id: 2, email: "bob@example.com", password: "bobsecret"}
|
9
|
+
}
|
10
|
+
|
11
|
+
def self.find_by(options)
|
12
|
+
attributes = RECORDS[options]
|
13
|
+
new(attributes) if attributes.present?
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(options)
|
17
|
+
self.id = options[:id]
|
18
|
+
self.email = options[:email]
|
19
|
+
self.password = options[:password]
|
20
|
+
end
|
21
|
+
|
22
|
+
def authenticate(password)
|
23
|
+
self.password == password
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class SessionWithUserModelTest < Test::Unit::TestCase
|
28
|
+
include ActiveModel::Lint::Tests
|
29
|
+
|
30
|
+
def setup
|
31
|
+
@model = @session = ActiveModel::Session.new
|
32
|
+
end
|
33
|
+
|
34
|
+
test "is valid with valid credentials" do
|
35
|
+
@session.email = "alice@example.com"
|
36
|
+
@session.password = "alicesecret"
|
37
|
+
assert @session.valid?
|
38
|
+
end
|
39
|
+
|
40
|
+
test "returns user_id when valid" do
|
41
|
+
@session.email = "bob@example.com"
|
42
|
+
@session.password = "bobsecret"
|
43
|
+
assert @session.valid?
|
44
|
+
assert_equal 2, @session.user_id
|
45
|
+
end
|
46
|
+
|
47
|
+
test "is invalid when user does not exist" do
|
48
|
+
@session.email = "non-existing@example.com"
|
49
|
+
assert @session.invalid?
|
50
|
+
assert @session.errors[:password].present?
|
51
|
+
end
|
52
|
+
|
53
|
+
test "is invalid with incorrect password" do
|
54
|
+
@session.email = "alice@example.com"
|
55
|
+
@session.password = "wrong-password"
|
56
|
+
assert @session.invalid?
|
57
|
+
assert @session.errors[:password].present?
|
58
|
+
end
|
59
|
+
|
60
|
+
test "is invalid without email" do
|
61
|
+
@session.email = nil
|
62
|
+
assert @session.invalid?
|
63
|
+
assert @session.errors[:email].present?
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class SessionWithoutUserModelTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Object.send(:remove_const, :User) if defined?(User)
|
6
|
+
end
|
7
|
+
|
8
|
+
test "is invalid when user model is nil" do
|
9
|
+
session = ActiveModel::Session.new(user: nil, email: "alice@example.com", password: "secret")
|
10
|
+
assert session.invalid?
|
11
|
+
end
|
12
|
+
|
13
|
+
test "is invalid when user does not authenticate" do
|
14
|
+
user = Object.new
|
15
|
+
def user.authenticate(password)
|
16
|
+
password == "secret"
|
17
|
+
end
|
18
|
+
session = ActiveModel::Session.new(user: user, email: "alice@example.com", password: "wrongsecret")
|
19
|
+
assert session.invalid?
|
20
|
+
end
|
21
|
+
|
22
|
+
test "is valid when user authenticates" do
|
23
|
+
user = Object.new
|
24
|
+
def user.authenticate(password)
|
25
|
+
password == "secret"
|
26
|
+
end
|
27
|
+
session = ActiveModel::Session.new(user: user, email: "alice@example.com", password: "secret")
|
28
|
+
assert session.valid?
|
29
|
+
end
|
30
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model-session
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kuba Kuźma
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Simple session model implemented on top of ActiveModel::Model
|
42
|
+
email:
|
43
|
+
- kuba@jah.pl
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- active_model-session.gemspec
|
54
|
+
- lib/active_model/session.rb
|
55
|
+
- lib/active_model/session/version.rb
|
56
|
+
- test/session_with_user_model_test.rb
|
57
|
+
- test/session_without_user_model_test.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
homepage: https://github.com/cowbell/active_model-session
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Simple session model implemented on top of ActiveModel::Model
|
83
|
+
test_files:
|
84
|
+
- test/session_with_user_model_test.rb
|
85
|
+
- test/session_without_user_model_test.rb
|
86
|
+
- test/test_helper.rb
|