Skip to content

Commit

Permalink
Added user functionallty
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhalldor committed Mar 12, 2009
1 parent 27ef70a commit a6c326f
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,7 +1,6 @@
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
encodings.xml
misc.xml
Expand Down
85 changes: 85 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,85 @@
class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
@users = User.find(:all, :order => :name)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end

# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/new
# GET /users/new.xml
def new
@user = User.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/1/edit
def edit
@user = User.find(params[:id])
end

# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
flash[:notice] = "User #{@user.name} was successfully created."
format.html { redirect_to(:action=>'index') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])

respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = "User #{@user.name} was successfully updated."
format.html { redirect_to(:action =>'index') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy

respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
module UsersHelper
end
50 changes: 50 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,50 @@
require 'digest/sha2'

class User < ActiveRecord::Base

validates_presence_of :name
validates_uniqueness_of :name
attr_accessor :password_confirmation
validates_confirmation_of :password
validate :password_non_blank
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

def self.authenticate(name, password)
user = self.find_by_name(name)
if user
expected_password = encrypted_password(password, user.salt)
if user.hashed_password != expected_password
user = nil
end
end
user
end

def password
@password
end

def password=(pwd)
@password = pwd
return if pwd.blank?
create_new_salt
self.hashed_password = User.encrypted_password(self.password, self.salt)
end


def password_non_blank
errors.add(:password, "Missing password") if hashed_password.blank?
end

private

def self.encrypted_password(password, salt)
string_to_hash = password + "kisi" + salt
Digest::SHA256.hexdigest(string_to_hash)
end

def create_new_salt
self.salt = self.object_id.to_s + rand.to_s
end

end
17 changes: 17 additions & 0 deletions app/views/layouts/users.html.erb
@@ -0,0 +1,17 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Users: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<%= yield %>

</body>
</html>
28 changes: 28 additions & 0 deletions app/views/users/edit.html.erb
@@ -0,0 +1,28 @@
<h1>Editing user</h1>

<% form_for(@user) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :hashed_password %><br />
<%= f.text_field :hashed_password %>
</p>
<p>
<%= f.label :salt %><br />
<%= f.text_field :salt %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>
22 changes: 22 additions & 0 deletions app/views/users/index.html.erb
@@ -0,0 +1,22 @@
<h1>Listing users</h1>

<table>
<tr>
<th>Name</th>
<th>Email</th>
</tr>

<% for user in @users %>
<tr>
<td><%=h user.name %></td>
<td><%=h user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New user', new_user_path %>
27 changes: 27 additions & 0 deletions app/views/users/new.html.erb
@@ -0,0 +1,27 @@
<h1>New user</h1>

<% form_for(@user) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.text_field :password %>
</p>
<p>
<%= f.label :user_password_confirmation %><br />
<%= f.text_field :password_confirmation %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', users_path %>
23 changes: 23 additions & 0 deletions app/views/users/show.html.erb
@@ -0,0 +1,23 @@
<p>
<b>Name:</b>
<%=h @user.name %>
</p>

<p>
<b>Email:</b>
<%=h @user.email %>
</p>

<p>
<b>Hashed password:</b>
<%=h @user.hashed_password %>
</p>

<p>
<b>Salt:</b>
<%=h @user.salt %>
</p>


<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
20 changes: 20 additions & 0 deletions config/database.yml
@@ -0,0 +1,20 @@
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
timeout: 5000

# Need to change pefore deploying to prod
production:
adapter: sqlite3
database: db/production.sqlite3
timeout: 5000
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :users

# The priority is based upon order of creation: first created -> highest priority.

# Sample of regular route:
Expand Down
23 changes: 23 additions & 0 deletions db/schema.rb
@@ -0,0 +1,23 @@
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20090312174058) do

create_table "users", :force => true do |t|
t.string "name", :limit => 128, :null => false
t.string "email", :limit => 128, :null => false
t.string "hashed_password", :limit => 64
t.string "salt"
t.datetime "created_at"
t.datetime "updated_at"
end

end
54 changes: 54 additions & 0 deletions public/stylesheets/scaffold.css
@@ -0,0 +1,54 @@
body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

.fieldWithErrors {
padding: 2px;
background-color: red;
display: table;
}

#errorExplanation {
width: 400px;
border: 2px solid red;
padding: 7px;
padding-bottom: 12px;
margin-bottom: 20px;
background-color: #f0f0f0;
}

#errorExplanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
background-color: #c00;
color: #fff;
}

#errorExplanation p {
color: #333;
margin-bottom: 0;
padding: 5px;
}

#errorExplanation ul li {
font-size: 12px;
list-style: square;
}

13 changes: 13 additions & 0 deletions test/fixtures/users.yml
@@ -0,0 +1,13 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
name: MyString
email: MyString
hashed_password: MyString
salt: MyString

two:
name: MyString
email: MyString
hashed_password: MyString
salt: MyString

0 comments on commit a6c326f

Please sign in to comment.