Permalink
Browse files

new style of manage user dependencies

  • Loading branch information...
1 parent 3cf4f82 commit eb36de69e4bd9b7ea727e72a200f53e7ddc13a70 @shingara committed Jul 13, 2009
Showing with 72 additions and 43 deletions.
  1. +45 −0 lib/include/authenticated_system.rb
  2. +3 −30 lib/models/user.rb
  3. +24 −13 lib/sinatra-authentication.rb
@@ -0,0 +1,45 @@
+p 'include'
+module Sinatra
+ module LilAuthentication
+ module AuthenticatedSystem
+
+ attr_accessor :password, :password_confirmation
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ def password=(pass)
+ @password = pass
+ self.salt = self.class.random_string(10) if !self.salt
+ self.hashed_password = self.class.encrypt(@password, self.salt)
+ end
+
+ def admin?
+ self.permission_level == -1 || self.id == 1
+ end
+
+ module ClassMethods
+ def authenticate(email, pass)
+ current_user = self.get_by_email(email)
+ return nil if current_user.nil?
+ return current_user if self.encrypt(pass, current_user.salt) == current_user.hashed_password
+ nil
+ end
+
+ def random_string(len)
+ #generate a random password consisting of strings and digits
+ chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
+ newpass = ""
+ 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
+ return newpass
+ end
+
+ def encrypt(pass, salt)
+ Digest::SHA1.hexdigest(pass+salt)
+ end
+ end
+
+ end
+ end
+end
View
@@ -4,6 +4,7 @@
class User
include DataMapper::Resource
+ include Sinatra::LilAuthentication::AuthenticatedSystem
attr_accessor :password, :password_confirmation
@@ -18,37 +19,9 @@ class User
validates_present :password, :unless => Proc.new { |t| t.hashed_password }
validates_is_confirmed :password
- def self.authenticate(email, pass)
- current_user = first(:email => email)
- return nil if current_user.nil?
- return current_user if User.encrypt(pass, current_user.salt) == current_user.hashed_password
- nil
- end
- def password=(pass)
- @password = pass
- self.salt = User.random_string(10) if !self.salt
- self.hashed_password = User.encrypt(@password, self.salt)
+ def get_by_email(email)
+ first(:email => email)
end
- def admin?
- self.permission_level == -1 || self.id == 1
- end
- protected
-
- def self.encrypt(pass, salt)
- Digest::SHA1.hexdigest(pass+salt)
- end
-
- def self.random_string(len)
- #generate a random password consisting of strings and digits
- chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
- newpass = ""
- 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
- return newpass
- end
-
- def method_missing(m, *args)
- return false
- end
end
@@ -1,4 +1,5 @@
require 'sinatra/base'
+require 'include/authenticated_system'
module SinatraAuthentication
VERSION = "0.0.2"
@@ -24,6 +25,21 @@ def self.registered(app)
# Test if User is define. isn't use User embeded
begin
User
+ raise NotImplementedError.new('You need include Sinatra::LilAuthentication::AuthenticatedSystem in your User model') unless User.include?(Sinatra::LilAuthentication::AuthenticatedSystem)
+ raise NotImplementedError.new('No self#all method in User') unless User.respond_to?(:all)
+ raise NotImplementedError.new('No self#get(id) method in User') unless User.respond_to?(:get)
+ raise NotImplementedError.new('No self#get_by_email(email) method in User') unless User.respond_to?(:get_by_email)
+ [:id, :created_at].each do |reader_property|
+ eval %{
+ raise NotImplementedError.new('No reader accessor to id property in User') unless User.new.respond_to?(:#{reader_property})
+ }
+ end
+ [:salt, :email, :hashed_password, :permission_level, :password, :password_confirmation].each do |property|
+ eval %{
+ raise NotImplementedError.new('No reader accessor to #{property} property in User') unless User.new.respond_to?(:#{property})
+ raise NotImplementedError.new('No writer accessor to #{property} property in User') unless User.new.respond_to?(:#{property}=)
+ }
+ end
rescue NameError => e
require 'models/user'
end
@@ -39,12 +55,7 @@ def self.registered(app)
get '/users/:id' do
login_required
-
- #INVESTIGATE
- #
- #WHY THE HECK WON'T GET RETURN ANYTHING?
- #if I user User.get(params[:id]) it returns nil for some inexplicable reason
- @user = User.first(:id => params[:id])
+ @user = User.get(params[:id])
haml get_view_as_string("show.haml"), :layout => use_layout?
end
@@ -62,12 +73,12 @@ def self.registered(app)
end
post '/login' do
- if user = User.authenticate(params[:email], params[:password])
- session[:user] = user.id
- redirect '/'
- else
- redirect '/login'
- end
+ if user = User.authenticate(params[:email], params[:password])
+ session[:user] = user.id
+ redirect '/'
+ else
+ redirect '/login'
+ end
end
get '/logout' do
@@ -142,7 +153,7 @@ def login_required
def current_user
if session[:user]
- User.first(:id => session[:user])
+ User.get(session[:user])
else
GuestUser.new
end

0 comments on commit eb36de6

Please sign in to comment.