Permalink
Browse files

adding contact, about, register, and login system

  • Loading branch information...
1 parent c276f58 commit 3a638acebde9db73bfa262da11ecc2a0fdc20de6 @ghostandthemachine committed Feb 9, 2012
View
93 app.rb
@@ -1,24 +1,103 @@
-
+require './model/dm'
require 'sinatra'
+require './helpers/sinatra'
+require './helpers/helpers'
require 'haml'
+configure do
+ enable :sessions
+end
+
get '/' do
- haml :index
+ @user = session[:user]
+ unless @user == nil
+ @user_name = session[:user].user_name
+ end
+ haml :index
+end
+
+get '/user/:user_name/dashboard' do
+ @user = User.first(user_name: params[:user_name])
+ @user_name = session[:user].user_name
+ haml :user_dashboard
+end
+
+get '/user' do
+ redirect '/user/' + session[:user].user_name
end
get '/about' do
- haml :about
+ unless session[:user] == nil
+ @user_name = session[:user].user_name
+ end
+ haml :about
end
get '/contact' do
- haml :about
+ haml :contact
end
-get '/register' do
- haml :about
+
+get '/user/:user_name/profile' do
+ @user = User.first(user_name: params[:user_name])
+ unless session[:user] == nil
+ @user_name = session[:user].user_name
+ end
+ haml :user_profile
end
+
get '/login' do
- haml :about
+ haml :login
+end
+
+post '/login' do
+ if session[:user] = User.authenticate(params["username"], params["password"])
+ flash("Login successful")
+ redirect "/user/" << session[:user].user_name << "/dashboard"
+ else
+ flash("Login failed - Try again")
+ redirect '/login'
+ end
+end
+
+get '/logout' do
+ session[:user] = nil
+ flash("Logout successful")
+ redirect '/'
end
+
+get '/list' do
+ unless session[:user] == nil
+ @user_name = session[:user].user_name
+ end
+ @users = User.all
+ haml :list
+end
+
+get '/register' do
+ haml :register
+end
+
+post '/register' do
+ u = User.new
+ u.user_name = params[:user_name]
+ u.password = params[:password]
+ u.email = params[:email]
+
+ if u.save
+ flash("User created")
+ session[:user] = User.authenticate( params["user_name"],
+ params["password"])
+ redirect '/user/' << session[:user].user_name.to_s << "/dashboard"
+ else
+ tmp = []
+ u.errors.each do |e|
+ tmp << (e.join("<br/>"))
+ end
+ flash(tmp)
+ redirect '/create'
+ end
+end
+
View
Binary file not shown.
View
@@ -0,0 +1,8 @@
+module Helpers
+ def self.random_string(len)
+ chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
+ str = ""
+ 1.upto(len) { |i| str << chars[rand(chars.size-1)] }
+ return str
+ end
+end
View
@@ -0,0 +1,33 @@
+helpers do
+ def logged_in?
+ return true if session[:user]
+ nil
+ end
+
+ def link_to(name, location, alternative = false)
+ if alternative and alternative[:condition]
+ "<a href=#{alternative[:location]}>#{alternative[:name]}</a>"
+ else
+ "<a href=#{location}>#{name}</a>"
+ end
+ end
+
+ def random_string(len)
+ chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
+ str = ""
+ 1.upto(len) { |i| str << chars[rand(chars.size-1)] }
+ return str
+ end
+
+ def flash(msg)
+ session[:flash] = msg
+ end
+
+ def show_flash
+ if session[:flash]
+ tmp = session[:flash]
+ session[:flash] = false
+ "<fieldset><legend>Notice</legend><p>#{tmp}</p></fieldset>"
+ end
+ end
+end
View
@@ -0,0 +1,67 @@
+require 'dm-core'
+require 'dm-validations'
+require 'dm-timestamps'
+require 'dm-migrations'
+require './helpers/helpers'
+
+
+DataMapper.setup(:default, "sqlite://#{Dir.pwd}/db/user.db")
+
+class User
+ include DataMapper::Resource
+
+ property(:id, Serial)
+
+ property(:user_name, String, key: true, length: (3..40), required: true)
+
+ property(:img_url, String)
+
+ property(:email, String)
+
+ property(:password, String)
+ property(:salt, String)
+ property(:hashed_password, String)
+
+ property(:created_at, DateTime, default: DateTime.now)
+ property(:upadted_at, DateTime)
+
+
+ def username= new_username
+ @username = new_username.downcase
+ end
+
+ def password=(pass)
+ @password = pass
+ self.salt = random_string(10) unless self.salt
+ self.hashed_password = User.encrypt(@password, self.salt)
+ end
+
+ def self.encrypt(pass, salt)
+ Digest::SHA1.hexdigest(pass + salt)
+ end
+
+ def self.authenticate(login, pass)
+ u = User.first(user_name: login)
+ return nil if u.nil?
+ return u if User.encrypt(pass, u.salt) == u.hashed_password
+ nil
+ end
+
+ def random_string(len)
+ chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
+ str = ""
+ 1.upto(len) { |i| str << chars[rand(chars.size-1)] }
+ return str
+ end
+
+end
+
+def get_day(record)
+ record.created_at.asctime.slice(0..3)
+end
+
+def get_month(creation_date)
+ record.created_at.asctime.slice(4..6)
+end
+
+DataMapper.auto_upgrade!
View
@@ -4,4 +4,8 @@ body {
}
.sidebar-nav {
padding: 9px 0;
+}
+
+.form-input {
+ height: 28px;
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,4 @@
+.container
+ .content
+ %p
+ about project...
View
@@ -0,0 +1,16 @@
+.content
+ .container
+ .row
+ .span12
+ %form{ :action => "/register", :method => "post"}
+ %h1= 'Contact'
+ %p Name:
+ %input.form-input{ :name => "user_name", :type => "text", :size => "30"}
+ %p Email:
+ %input.form-input{ :name => "email", :type => "text", :size => "30"}
+ %br
+ %textarea{:name => "contact_body", :cols => "20", :rows => "10"}
+ %br
+ %input.btn{ :name => "submit", :type => "submit", :value => "Send"}
+ %input.btn{ :name => "clear", :type => "submit", :value => "Clear"}
+
View
@@ -0,0 +1,26 @@
+.container
+ .content
+ %table{class: "bordered-table zebra-striped", id: "userSortedTable"}
+ %thead
+ %tr
+ %td{colspan: '8'}
+ %h4 Users
+ %tr
+ %th{class: 'header'} ID
+ %th{class: 'header red'} User Name
+ %th{class: 'header blue'} Email
+ %th{class: 'header green'} Creation Date
+
+ ~@users.each do |user|
+ %tr
+ %td= user.id
+ %td= user.user_name
+ %td= user.email
+ %td= user.created_at
+
+:plain
+ <script>
+ $(function() {
+ $("table#userSortedTable").tablesorter({ sortList: [[1,0]] });
+ });
+ </script>
View
@@ -0,0 +1,15 @@
+.content
+ .container
+ .row
+ .span12
+ %form{ :action => "/login", :method => "post"}
+ %h1= 'Login'
+ %br
+ %p Name
+ %input.form-input{ :name => "username", :type => "text", :size => "20", :value => "username"}
+ %p Password:
+ %input.form-input{ :name => "password", :type => "password", :size => "20", :value => ""}
+ %br
+ %br
+ %input.btn{ :name => "submit", :type => "submit", :value => "Submit"}
+
View
@@ -11,9 +11,17 @@
%li
%a{:href => '/'}Home
%li
- %a{:href => '/'}About
+ %a{:href => '/about'}About
%li
- %a{:href => '/'}Contact
+ %a{:href => '/register'}Register
+ %li
+ %a{:href => '/contact'}Contact
%p.navbar-text.pull-right
- Logged in as
- %a{:href => '/'} username
+ -if logged_in?
+ %a{:href => "/user/" << @user.user_name.to_s << "/dashboard"} #{@user.user_name}
+ Dashboard
+ |
+ %a{:href => "/logout" } Logout
+
+ -else
+ %a{:href => '/login'} Login
View
@@ -0,0 +1,17 @@
+.content
+ .container
+ .row
+ .span12
+ %form{ :action => "/register", :method => "post"}
+ %h1= 'Register'
+ %small= 'and start swapping books now'
+ %p Name:
+ %input.form-input{ :name => "user_name", :type => "text", :size => "30"}
+ %p Password:
+ %input.form-input{ :name => "password", :type => "password", :size => "30"}
+ %p Email:
+ %input.form-input{ :name => "email", :type => "text", :size => "30"}
+ %br
+ %br
+ %input.btn{ :name => "submit", :type => "submit", :value => "Submit"}
+
View
@@ -0,0 +1,19 @@
+.container
+ .content
+ .page-header
+ -unless @user == nil?
+ %h2="Dashboard"
+ %br
+ -unless @user.img_url == nil? || @user.img_url.to_s.length < 1
+ %image{width: 30, height: 30, src: "/images/" << @user.user_name.to_s << "/" << @user.img_url.to_s}
+ -else
+ %image{width: 30, height: 30, src: "/images/missing_avatar_small.png"}
+
+
+ %h3= @user.user_name.to_s
+ -else
+ redirect '/'
+ %small
+ %a{href: "/user/" << @user.user_name.to_s << "/profile"} profile
+ .container#main-topic-nav
+ // do your user data tables etc. here
View
@@ -0,0 +1,4 @@
+.container
+ .content
+ %h2= @user.user_name.to_s << ' profile'
+ %p= 'email: ' << @user.email.to_s

0 comments on commit 3a638ac

Please sign in to comment.