Skip to content

Instantly share code, notes, and snippets.

@Jimgerneer
Created June 5, 2012 03:54
Show Gist options
  • Save Jimgerneer/2872531 to your computer and use it in GitHub Desktop.
Save Jimgerneer/2872531 to your computer and use it in GitHub Desktop.
Models/user and routes/user
require_relative '../rook'
class Rook::User
include DataMapper::Resource
attr_accessor :password, :password_confirmation
property :id, Serial, :writer => :protected, :key => true
property :email, String, :required => true, :length => (5..40),
:unique => true, :format => :email_address
property :username, String, :required => true, :length => (2..32),
:unique => true
property :hashed_password, String, :writer => :protected
property :salt, String, :required => true, :writer => :protected
property :created_at, DateTime
property :account_type, String, :required => true, :default => 'standard',
:writer => :protected
property :active, Boolean, :default => true, :writer => :protected
validates_presence_of :password_confirmation
validates_confirmation_of :password
def self.authenticate(username_or_email, pass)
binding.pry
current_user = first(:username => username_or_email)# || first(:email => username_or_email)
return nil if current_user.nil? || Rook::User.class.encrypt(pass, current_user.salt) != current_user.hashed_password
current_user
end
def password=(pass)
@password = pass
self.salt = (1..12).map{(rand(26)+65).chr}.join if !self.salt
self.hashed_password = Rook::User.class.encrypt(@password, self.salt)
end
protected
def self.encrypt(pass, salt)
Digest::SHA1.hexdigest(pass+salt)
end
end
DataMapper.finalize
DataMapper.auto_upgrade!
# has n, :profile
# has n, :courses
# has n, :opportunities
class Rook < Sinatra::Base
get "/login" do
haml :login
end
post "/login" do
login
end
get "/logout" do
logout
end
get "/signup" do
haml :signup
end
post "/signup" do
signup
end
get "/test" do
login_required
haml :opportunity
end
def login
if user = Rook::User.authenticate(params[:username], params[:password])
session[:user] = user.id
redirect_to_stored
else
redirect "/login"
end
end
def logout
session[:user] = nil
redirect "/"
end
def signup
@user = Rook::User.new(params[:user])
if @user.save
session[:user] = @user.id
redirect "/"
else
puts @user.errors.full_messages
redirect "/signup"
end
end
helpers do
def login_required
if session[:user]
return true
elsif request.env['REQUEST_PATH'] =~ /(\.json|\.xml)$/ && request.env['HTTP_USER_AGENT'] !~ /Mozilla/
@auth ||= Rack::Auth::Basic::Request.new(request.env)
if @auth.provided? && @auth.basic? && @auth.credentials && Rook::User.authenticate(@auth.credentials.first, @auth.credentials.last)
session[:user] = Rook::User.first(:username => @auth.credentials.first).id
return true
else
status 401
halt("401 Unauthorized") rescue throw(:halt, "401 Unauthorized")
end
else
session[:return_to] = request.fullpath
redirect "/login"
pass rescue throw :pass
end
end
def admin_required
return true if login_required && current_user.accout_type == 'admin'
redirect '/'
end
def current_user
Rook::User.get(session[:user])
end
def redirect_to_stored
if return_to = session[:return_to]
session[:return_to] = nil
redirect return_to
else
redirect '/'
end
end
#might not need clean?
def clean(str); str.gsub(/^\s{#{str[/\s+/].length}}/, ''); end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment