Skip to content

Instantly share code, notes, and snippets.

View iHiD's full-sized avatar
💙

Jeremy Walker iHiD

💙
View GitHub Profile
@iHiD
iHiD / sessions_controller.rb
Created June 6, 2012 15:30
How to Build A Secure Website With Ruby On Rails 6
# Authenticate user
@user = #...
# Destroy the existing session in case anyone is sharing it and
# create a new session that you know to be unique to the user.
reset_session
# Store the user's id as normal
session[:user_id] = @user.id
@iHiD
iHiD / settings_controller.rb
Created June 6, 2012 15:30
How to Build A Secure Website With Ruby On Rails 7
class SettingsController < ApplicationController
def show
@user = User.find(session[:user_id])
end
def update
@user = User.find(session[:user_id])
@user.update_attributes(params[:settings])
end
@iHiD
iHiD / settings_routes.rb
Created June 6, 2012 15:31
How to Build A Secure Website With Ruby On Rails 8
Security::Application.routes.draw do
match 'show_settings' => "settings#show"
match 'update_settings' => "settings#update"
end
@iHiD
iHiD / settings_routes_2.rb
Created June 6, 2012 15:32
How to Build A Secure Website With Ruby On Rails 9
Security::Application.routes.draw do
get 'show_settings' => "settings#show"
put 'update_settings' => "settings#update"
end
@iHiD
iHiD / settings_routes_3.rb
Created June 6, 2012 15:32
How to Build A Secure Website With Ruby On Rails 10
Security::Application.routes.draw do
resource :settings
end
@iHiD
iHiD / user.rb
Created June 10, 2012 13:07
Security Article Part 2 - 1
class User < ActiveRecord::Base
# Has attributes: [:username, :hashed_password, :is_admin]
end
@iHiD
iHiD / users_controller.rb
Created June 10, 2012 13:10
Security Article Part 2 - 2
class UsersController < ApplicationController
#...
def update
@user = User.find(params[:id])
@user.update_attributes(params[:user])
#...
end
#...
@iHiD
iHiD / edit.html.erb
Created June 10, 2012 13:11
Security Article Part 2 - 3
<%= form_for @user do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= submit_tag %>
<% end %>
@iHiD
iHiD / user.rb
Created June 10, 2012 13:30
Security Article Part 2 - 4
class User < ActiveRecord::Base
# Has attributes: [:username, :hashed_password, :is_admin]
attr_protected :is_admin
end
@iHiD
iHiD / user.rb
Created June 10, 2012 13:49
Security Article Part 2 - 5
# Migration
create_table :users do |t|
t.boolean :can_do_dangerous_things, null: false
#...
t.timestamps
end
class User < ActiveRecord::Base
# Blacklisting attribute