barjillo 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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +53 -0
- data/lib/barjillo.rb +2 -0
- data/lib/barjillo/act_as_auth_model.rb +82 -0
- data/lib/barjillo/barji_helpers.rb +96 -0
- data/test/helper.rb +10 -0
- data/test/test_barjillo.rb +7 -0
- metadata +84 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Dieinzige
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= barjillo
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Dieinzige. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "barjillo"
|
8
|
+
gem.summary = %Q{Customizible, rails 3 authentification system}
|
9
|
+
gem.description = %Q{yet another customizible auth system}
|
10
|
+
gem.email = "dieinzige@me.com"
|
11
|
+
gem.homepage = "http://github.com/Dieinzige/barjillo"
|
12
|
+
gem.authors = ["Dieinzige"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "barjillo #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/lib/barjillo.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
module Barjillo
|
3
|
+
module ActAsAuthModel
|
4
|
+
|
5
|
+
def self.included(model)
|
6
|
+
model.extend(ClassMethods)
|
7
|
+
model.send(:include , AttrAccessor)
|
8
|
+
model.send(:include, InstanceMethods)
|
9
|
+
model.send(:include, Callbacks)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
module Callbacks
|
15
|
+
def self.included(model)
|
16
|
+
model.class_eval do
|
17
|
+
before_save :initialize_salt,
|
18
|
+
:encrypt_password
|
19
|
+
before_create :generate_remember_token
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
module AttrAccessor
|
26
|
+
# Hook for attr_accessor virtual attributes.
|
27
|
+
#
|
28
|
+
# :password
|
29
|
+
def self.included(model)
|
30
|
+
model.class_eval do
|
31
|
+
attr_accessor :password
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
module InstanceMethods
|
36
|
+
|
37
|
+
def authenticated?(password)
|
38
|
+
encrypted_password == encrypt(password)
|
39
|
+
end
|
40
|
+
|
41
|
+
def reset_remember_token!
|
42
|
+
generate_remember_token
|
43
|
+
save(:validate => false)
|
44
|
+
end
|
45
|
+
|
46
|
+
def forgot_password!
|
47
|
+
generate_confirmation_token
|
48
|
+
save(:validate => false)
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def initialize_salt
|
54
|
+
if new_record?
|
55
|
+
self.password_salt = Digest::SHA1.hexdigest Time.now.to_s
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def generate_remember_token
|
60
|
+
self.remember_token = encrypt("#{Time.now} -- #{Time.now}")
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
def encrypt_password
|
65
|
+
self.encrypted_password = encrypt(password) unless password.blank?
|
66
|
+
end
|
67
|
+
|
68
|
+
def encrypt(password)
|
69
|
+
Digest::SHA1.hexdigest "#{password}#{password_salt}"
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
module ClassMethods
|
76
|
+
def authenticate(login, password)
|
77
|
+
return nil unless user = find_by_login(login)
|
78
|
+
return user if user.authenticated?(password)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Barjillo::BarjiHelpers
|
2
|
+
|
3
|
+
def self.included(controller) # :nodoc:
|
4
|
+
controller.send(:include, InstanceMethods)
|
5
|
+
controller.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def self.extended(controller)
|
10
|
+
controller.helper_method :current_user, :signed_in?, :signed_out?
|
11
|
+
controller.hide_action :current_user, :current_user=,
|
12
|
+
:signed_in?, :signed_out?,
|
13
|
+
:sign_in, :sign_out,
|
14
|
+
:authenticate, :deny_access
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
|
21
|
+
def current_user
|
22
|
+
@_current_user ||= user_from_cookie
|
23
|
+
end
|
24
|
+
|
25
|
+
def current_user=(user)
|
26
|
+
@_current_user = user
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def signed_in?
|
31
|
+
current_user.present?
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def signed_out?
|
36
|
+
current_user.nil?
|
37
|
+
end
|
38
|
+
|
39
|
+
def authenticate
|
40
|
+
deny_access if signed_out?
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def sign_in(user)
|
45
|
+
if user
|
46
|
+
cookies[:remember_token] = {
|
47
|
+
:value => user.remember_token,
|
48
|
+
:expires => 1.month.from_now.utc
|
49
|
+
}
|
50
|
+
current_user = user
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def sign_out
|
56
|
+
current_user.reset_remember_token! if current_user
|
57
|
+
cookies.delete(:remember_token)
|
58
|
+
current_user = nil
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
def deny_access(flash_message = nil)
|
63
|
+
store_location
|
64
|
+
flash[:failure] = flash_message if flash_message
|
65
|
+
redirect_to(signup_url)
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
def user_from_cookie
|
71
|
+
if token = cookies[:remember_token]
|
72
|
+
::User.find_by_remember_token(token)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def store_location
|
77
|
+
if request.get?
|
78
|
+
session[:return_to] = request.request_uri
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def redirect_back_or(default)
|
83
|
+
redirect_to(return_to || default)
|
84
|
+
clear_return_to
|
85
|
+
end
|
86
|
+
|
87
|
+
def return_to
|
88
|
+
session[:return_to] || params[:return_to]
|
89
|
+
end
|
90
|
+
|
91
|
+
def clear_return_to
|
92
|
+
session[:return_to] = nil
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barjillo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Dieinzige
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-19 00:00:00 +03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: thoughtbot-shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description: yet another customizible auth system
|
33
|
+
email: dieinzige@me.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- LICENSE
|
40
|
+
- README.rdoc
|
41
|
+
files:
|
42
|
+
- .document
|
43
|
+
- .gitignore
|
44
|
+
- LICENSE
|
45
|
+
- README.rdoc
|
46
|
+
- Rakefile
|
47
|
+
- lib/barjillo.rb
|
48
|
+
- lib/barjillo/act_as_auth_model.rb
|
49
|
+
- lib/barjillo/barji_helpers.rb
|
50
|
+
- test/helper.rb
|
51
|
+
- test/test_barjillo.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/Dieinzige/barjillo
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --charset=UTF-8
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.3.6
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Customizible, rails 3 authentification system
|
82
|
+
test_files:
|
83
|
+
- test/helper.rb
|
84
|
+
- test/test_barjillo.rb
|