-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added authentication and a bit of styling to code pastes
- Loading branch information
universaL
committed
May 2, 2008
1 parent
eb68a63
commit 45139d0
Showing
69 changed files
with
7,959 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Filters added to this controller apply to all controllers in the application. | ||
# Likewise, all the methods added will be available for all controllers. | ||
|
||
class ApplicationController < ActionController::Base | ||
include AuthenticatedSystem | ||
|
||
helper :all # include all helpers, all the time | ||
|
||
# See ActionController::RequestForgeryProtection for details | ||
# Uncomment the :secret if you're not using the cookie session store | ||
protect_from_forgery # :secret => '381c0a61fe231551469ee3a6d6e66a1e' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
class PastesController < ApplicationController | ||
# GET /pastes | ||
# GET /pastes.xml | ||
def index | ||
@pastes = Paste.find(:all) | ||
|
||
respond_to do |format| | ||
format.html # index.html.erb | ||
format.xml { render :xml => @pastes } | ||
end | ||
end | ||
|
||
# GET /pastes/1 | ||
# GET /pastes/1.xml | ||
def show | ||
@paste = Paste.find(params[:id]) | ||
|
||
respond_to do |format| | ||
format.html # show.html.erb | ||
format.xml { render :xml => @paste } | ||
end | ||
end | ||
|
||
# GET /pastes/new | ||
# GET /pastes/new.xml | ||
def new | ||
@paste = Paste.new | ||
|
||
respond_to do |format| | ||
format.html # new.html.erb | ||
format.xml { render :xml => @paste } | ||
end | ||
end | ||
|
||
# GET /pastes/1/edit | ||
def edit | ||
@paste = Paste.find(params[:id]) | ||
end | ||
|
||
# POST /pastes | ||
# POST /pastes.xml | ||
def create | ||
@paste = Paste.new(params[:paste]) | ||
|
||
respond_to do |format| | ||
if @paste.save | ||
flash[:notice] = 'Paste was successfully created.' | ||
format.html { redirect_to(@paste) } | ||
format.xml { render :xml => @paste, :status => :created, :location => @paste } | ||
else | ||
format.html { render :action => "new" } | ||
format.xml { render :xml => @paste.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PUT /pastes/1 | ||
# PUT /pastes/1.xml | ||
def update | ||
@paste = Paste.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @paste.update_attributes(params[:paste]) | ||
flash[:notice] = 'Paste was successfully updated.' | ||
format.html { redirect_to(@paste) } | ||
format.xml { head :ok } | ||
else | ||
format.html { render :action => "edit" } | ||
format.xml { render :xml => @paste.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /pastes/1 | ||
# DELETE /pastes/1.xml | ||
def destroy | ||
@paste = Paste.find(params[:id]) | ||
@paste.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to(pastes_url) } | ||
format.xml { head :ok } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This controller handles the login/logout function of the site. | ||
class SessionsController < ApplicationController | ||
skip_before_filter :login_required | ||
|
||
# render new.rhtml | ||
def new | ||
end | ||
|
||
def create | ||
self.current_user = User.authenticate(params[:login], params[:password]) | ||
if logged_in? | ||
if params[:remember_me] == "1" | ||
current_user.remember_me unless current_user.remember_token? | ||
cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at } | ||
end | ||
redirect_back_or_default('/') | ||
flash[:notice] = "Logged in successfully" | ||
else | ||
render :action => 'new' | ||
end | ||
end | ||
|
||
def destroy | ||
self.current_user.forget_me if logged_in? | ||
cookies.delete :auth_token | ||
reset_session | ||
flash[:notice] = "You have been logged out." | ||
redirect_back_or_default('/') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# This controller handles the login/logout function of the site. | ||
class SessionsController < ApplicationController | ||
skip_before_filter :login_required | ||
# render new.rhtml | ||
def new | ||
end | ||
|
||
def create | ||
self.current_user = User.authenticate(params[:login], params[:password]) | ||
if logged_in? | ||
if params[:remember_me] == "1" | ||
current_user.remember_me unless current_user.remember_token? | ||
cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at } | ||
end | ||
redirect_back_or_default('/') | ||
flash[:notice] = "Logged in successfully" | ||
else | ||
render :action => 'new' | ||
end | ||
end | ||
|
||
def destroy | ||
self.current_user.forget_me if logged_in? | ||
cookies.delete :auth_token | ||
reset_session | ||
flash[:notice] = "You have been logged out." | ||
redirect_back_or_default('/') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module SessionsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module UsersHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
class Paste < ActiveRecord::Base | ||
validates_presence_of :language, :code | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Paste < ActiveRecord::Base | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
require 'digest/sha1' | ||
class User < ActiveRecord::Base | ||
# Virtual attribute for the unencrypted password | ||
attr_accessor :password | ||
|
||
validates_presence_of :login, :email | ||
validates_presence_of :password, :if => :password_required? | ||
validates_presence_of :password_confirmation, :if => :password_required? | ||
validates_length_of :password, :within => 4..40, :if => :password_required? | ||
validates_confirmation_of :password, :if => :password_required? | ||
validates_length_of :login, :within => 3..40 | ||
validates_length_of :email, :within => 3..100 | ||
validates_uniqueness_of :login, :email, :case_sensitive => false | ||
before_save :encrypt_password | ||
|
||
# prevents a user from submitting a crafted form that bypasses activation | ||
# anything else you want your user to change should be added here. | ||
attr_accessible :login, :email, :password, :password_confirmation | ||
|
||
# Authenticates a user by their login name and unencrypted password. Returns the user or nil. | ||
def self.authenticate(login, password) | ||
u = find_by_login(login) # need to get the salt | ||
u && u.authenticated?(password) ? u : nil | ||
end | ||
|
||
# Encrypts some data with the salt. | ||
def self.encrypt(password, salt) | ||
Digest::SHA1.hexdigest("--#{salt}--#{password}--") | ||
end | ||
|
||
# Encrypts the password with the user salt | ||
def encrypt(password) | ||
self.class.encrypt(password, salt) | ||
end | ||
|
||
def authenticated?(password) | ||
crypted_password == encrypt(password) | ||
end | ||
|
||
def remember_token? | ||
remember_token_expires_at && Time.now.utc < remember_token_expires_at | ||
end | ||
|
||
# These create and unset the fields required for remembering users between browser closes | ||
def remember_me | ||
remember_me_for 2.weeks | ||
end | ||
|
||
def remember_me_for(time) | ||
remember_me_until time.from_now.utc | ||
end | ||
|
||
def remember_me_until(time) | ||
self.remember_token_expires_at = time | ||
self.remember_token = encrypt("#{email}--#{remember_token_expires_at}") | ||
save(false) | ||
end | ||
|
||
def forget_me | ||
self.remember_token_expires_at = nil | ||
self.remember_token = nil | ||
save(false) | ||
end | ||
|
||
# Returns true if the user has just been activated. | ||
def recently_activated? | ||
@activated | ||
end | ||
|
||
protected | ||
# before filter | ||
def encrypt_password | ||
return if password.blank? | ||
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record? | ||
self.crypted_password = encrypt(password) | ||
end | ||
|
||
def password_required? | ||
crypted_password.blank? || !password.blank? | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<tbody> | ||
<tr> | ||
<td><%=h paste.code[0, 25] %></td> | ||
<td><%=h paste.language %></td> | ||
<td><%= link_to 'Show', paste %></td> | ||
<td><%= link_to 'Edit', edit_paste_path(paste) %></td> | ||
<td><%= link_to 'Destroy', paste, :confirm => 'Are you sure?', :method => :delete %></td> | ||
</tr> | ||
</tbody> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<%- unless @paste.errors.empty? -%> | ||
<tr> | ||
<td><%= error_messages_for :paste %></td> | ||
</tr> | ||
<%- end -%> | ||
<tr> | ||
<td> | ||
<% form_for(@paste) do |f| %> | ||
<p> | ||
<%= f.label :language %><br/> | ||
<%= f.select :language, SYNTAXES, :include_blank => true %> | ||
</p> | ||
<p> | ||
<%= f.label :code %><br/> | ||
<%= f.text_area :code, :cols => 80, :rows => 25 %> | ||
</p> | ||
<p> | ||
<%= f.submit (@paste.new_record? ? "Create" : "Update") %> | ||
</p> | ||
<% end %> | ||
</td> | ||
</tr> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<%- unless @paste.errors.empty? -%> | ||
<tr> | ||
<td><%= error_messages_for :paste %></td> | ||
</tr> | ||
<%- end -%> | ||
<tr> | ||
<td> | ||
<% form_for(@paste) do |f| %> | ||
<p> | ||
<%= f.label :language %><br/> | ||
<%= f.select :language, SYNTAXES, :include_blank => true %> | ||
</p> | ||
<p> | ||
<%= f.label :code %><br/> | ||
<%= f.text_area :code, :cols => 80, :rows => 25 %> | ||
</p> | ||
<p> | ||
<%= f.submit "Create" %> | ||
</p> | ||
<% end %> | ||
</td> | ||
</tr> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<tbody> | ||
<tr> | ||
<td><%=h paste.code[0, 25] %></td> | ||
<td><%=h paste.language %></td> | ||
<td><%= link_to 'Show', paste %></td> | ||
<td><%= link_to 'Edit', edit_paste_path(paste) %></td> | ||
<td><%= link_to 'Destroy', paste, :confirm => 'Are you sure?', :method => :delete %></td> | ||
</tr> | ||
</tbody> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,14 @@ | ||
<h1>Editing paste</h1> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th><h1><h1>Editing paste</h1></h1></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= render :partial => 'form' %> | ||
<tr> | ||
<td><%= link_to 'Show', @paste %> | <%= link_to 'Back', pastes_path %></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
<%= error_messages_for :paste %> | ||
|
||
<% form_for(@paste) do |f| %> | ||
<p> | ||
<b>Language</b><br /> | ||
<%= f.select :language, SYNTAXES %> | ||
</p> | ||
<p> | ||
<b>Code</b><br /> | ||
<%= f.text_area :code %> | ||
</p> | ||
<p> | ||
<%= f.submit "Update" %> | ||
</p> | ||
<% end %> | ||
|
||
<%= link_to 'Show', @paste %> | | ||
<%= link_to 'Back', pastes_path %> |
Oops, something went wrong.