[Xfce4-commits] <cafe:master> Work on authentication, sqlite database integration.
Jannis Pohlmann
noreply at xfce.org
Sat Mar 12 21:14:01 CET 2011
Updating branch refs/heads/master
to 2a3473aaad641542a5b075b2bff83a5a0b977067 (commit)
from d513424ef05113e7cbaabc40057030bab5e3a2ba (commit)
commit 2a3473aaad641542a5b075b2bff83a5a0b977067
Author: Jannis Pohlmann <jannis at xfce.org>
Date: Sat Mar 12 21:12:15 2011 +0100
Work on authentication, sqlite database integration.
config.ru | 16 -----
examples/xfce-foundation/config.ru | 32 +++++++++
lib/cafe.rb | 40 +++++++++++-
lib/controllers/authentication.rb | 23 +++++++
lib/helpers/authentication.rb | 98 ++++++++++++++++++++++++++++
lib/helpers/general.rb | 56 ++++++++++++++++
lib/models/member.rb | 27 ++++++++
lib/views/authentication_login.erb | 9 +++
lib/views/foot.erb | 3 +
lib/views/head.erb | 15 +++++
lib/views/index.erb | 2 +
lib/views/stylesheet.sass | 123 ++++++++++++++++++++++++++++++++++++
12 files changed, 427 insertions(+), 17 deletions(-)
diff --git a/config.ru b/config.ru
deleted file mode 100644
index 5994d96..0000000
--- a/config.ru
+++ /dev/null
@@ -1,16 +0,0 @@
-#!/usr/bin/env ruby
-
-require 'rubygems'
-require 'sinatra'
-require 'cafe'
-
-disable :run
-enable :static
-enable :session
-enable :methodoverride
-enable :reload
-set :environment, :development
-
-use Rack::ShowExceptions
-
-run Cafe::Application
diff --git a/examples/xfce-foundation/config.ru b/examples/xfce-foundation/config.ru
new file mode 100644
index 0000000..6aa5c73
--- /dev/null
+++ b/examples/xfce-foundation/config.ru
@@ -0,0 +1,32 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'sinatra'
+require 'dm-core'
+
+disable :run
+enable :static
+enable :session
+enable :methodoverride
+enable :reload
+set :environment, :development
+
+use Rack::ShowExceptions
+
+DataMapper::Logger.new($stdout, :debug)
+DataMapper.setup(:default, "sqlite:///home/jannis/xfce/git/foundation/cafe/examples/xfce-foundation/xfce-foundation.db")
+
+require '../../lib/cafe'
+
+# create dummy users
+admin = Cafe::Models::Member.first_or_create(
+ :username => 'jannis',
+ :password => 'test',
+ :firstname => 'Jannis',
+ :lastname => 'Pohlmann',
+ :address => 'Foo 123, 23123 Lübeck',
+ :member_since => '2011-02-06'
+)
+admin.save
+
+run Cafe::Application
diff --git a/lib/cafe.rb b/lib/cafe.rb
index aaae3aa..05eb913 100644
--- a/lib/cafe.rb
+++ b/lib/cafe.rb
@@ -1,4 +1,7 @@
-require 'rubygems'
+require 'dm-core'
+require 'dm-migrations'
+require 'sinatra'
+require 'sass'
# determine the location of this very ruby script
directory = File.expand_path(File.dirname(__FILE__))
@@ -6,14 +9,49 @@ directory = File.expand_path(File.dirname(__FILE__))
# load models
require File.join(directory, 'models', 'member')
+# finalize models and set up the database
+DataMapper.finalize
+DataMapper.auto_upgrade!
+
# load helpers
+require File.join(directory, 'helpers', 'general')
+require File.join(directory, 'helpers', 'authentication')
# load controllers
require File.join(directory, 'controllers', 'members')
+require File.join(directory, 'controllers', 'authentication')
# load middleware
module Cafe
class Application < Sinatra::Base
+
+ include Cafe::Models
+
+ register Cafe::Helpers::General
+ register Cafe::Helpers::Authentication
+
+ register Cafe::Controllers::Members
+ register Cafe::Controllers::Authentication
+
+ authentication_initialize
+
+ get '/stylesheet.css' do
+ content_type 'text/css', :charset => 'utf-8'
+
+ directory = File.join(File.expand_path(File.dirname(__FILE__)), 'views')
+
+ template = File.read(File.join(directory, 'stylesheet.sass'))
+ engine = Sass::Engine.new(template)
+ engine.render
+ end
+
+ get '/' do
+ if authenticated?
+ view :index
+ else
+ redirect '/login'
+ end
+ end
end
end
diff --git a/lib/controllers/authentication.rb b/lib/controllers/authentication.rb
new file mode 100644
index 0000000..16f3db2
--- /dev/null
+++ b/lib/controllers/authentication.rb
@@ -0,0 +1,23 @@
+require 'warden'
+
+module Cafe
+ module Controllers
+ module Authentication
+ def self.registered(app)
+ app.get '/login/?' do
+ view :authentication_login
+ end
+
+ app.post '/login/?' do
+ login
+ redirect '/'
+ end
+
+ app.get '/logout/?' do
+ logout
+ redirect '/'
+ end
+ end
+ end
+ end
+end
diff --git a/lib/helpers/authentication.rb b/lib/helpers/authentication.rb
new file mode 100644
index 0000000..3281d7d
--- /dev/null
+++ b/lib/helpers/authentication.rb
@@ -0,0 +1,98 @@
+require 'warden'
+
+module Cafe
+ module Helpers
+ module Authentication
+ def self.registered(app)
+ app.helpers Helpers
+
+ # enable session
+ app.set :session, true
+
+ # set auth paths
+ app.set :auth_failure_path, '/'
+ app.set :auth_success_path, '/'
+
+ # remember last request URI so that we can return to it
+ # after the authorization
+ app.set :auth_use_referrer, true
+ end
+
+ def authentication_initialize
+ use Rack::Session::Cookie
+
+ Warden::Manager.before_failure do |env, opts|
+ env['REQUEST_METHOD'] = 'POST'
+ end
+
+ Warden::Manager.serialize_into_session do |member|
+ member.id
+ end
+
+ Warden::Manager.serialize_from_session do |id|
+ Cafe::Models::Member.get(id)
+ end
+
+ Warden::Strategies.add(:password) do
+ def valid?
+ puts 'password strategy valid?'
+ params['username'] and params['username'] != ''
+ end
+
+ def authenticate!
+ puts 'password strategy authenticate!'
+ member = Cafe::Models::Member.authenticate(params['username'], params['password'])
+ if member.nil?
+ fail!('Nope')
+ else
+ success!(member)
+ end
+ end
+ end
+
+ use Warden::Manager do |manager|
+ manager.default_strategies :password
+ manager.failure_app = Cafe::Application
+ end
+ end
+
+ module Helpers
+ def warden
+ env['warden']
+ end
+
+ def authenticated?
+ warden.authenticated?
+ end
+
+ def authenticate(*args)
+ warden.authenticate!(*args)
+ end
+ alias_method :login, :authenticate
+
+ def logout
+ warden.logout
+ end
+
+ def session_user
+ warden.user
+ end
+
+ def session_user=(new_user)
+ warden.set_user(new_user, {})
+ end
+
+ def authorization_requred(context = nil, roles = ['board'])
+ if context and not context.authorized?(session_user)
+ halt(view(:permission_denied, binding))
+ else
+ # TODO check session user roles
+ if false
+ halt(view(:permission_denied, binding))
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/helpers/general.rb b/lib/helpers/general.rb
new file mode 100644
index 0000000..da741d1
--- /dev/null
+++ b/lib/helpers/general.rb
@@ -0,0 +1,56 @@
+module Cafe
+ module Helpers
+ module General
+ def self.registered(app)
+ app.before do
+ env[:error] = {}
+ end
+
+ app.helpers Helpers
+ end
+
+ module Helpers
+ include Cafe::Models
+
+ def header(args={})
+ custom_binding = binding.clone
+ eval 'params = ' + args.inspect, custom_binding
+ import('head', custom_binding)
+ end
+
+ def footer
+ import('foot')
+ end
+
+ def import(template, custom_binding=nil)
+ directory = File.expand_path(File.dirname(__FILE__))
+ filename = File.join(directory, '..', 'views', "#{template}.erb")
+ erb = open(filename) do |file|
+ ERB.new(file.read)
+ end
+ erb.result(if custom_binding.nil? then binding else custom_binding end)
+ end
+
+ def view(*args)
+ import(*args)
+ end
+
+ def error_set(key, value)
+ env[:error][key] = value
+ end
+
+ def error(key)
+ env[:error][key]
+ end
+
+ def error_set?(key = nil)
+ if key.nil?
+ not env[:error].empty?
+ else
+ env[:error].has_key?(key)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/models/member.rb b/lib/models/member.rb
index e69de29..b1faf1f 100644
--- a/lib/models/member.rb
+++ b/lib/models/member.rb
@@ -0,0 +1,27 @@
+module Cafe
+ module Models
+ class Member
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :username, String
+ property :password, String
+ property :firstname, String
+ property :lastname, String
+ property :address, Text
+ property :member_since, Date
+
+ def self.authenticate(username, password)
+ p username
+ p password
+ member = first(:username => username)
+ p member
+ if member and member.password == password
+ return member
+ else
+ return nil
+ end
+ end
+ end
+ end
+end
diff --git a/lib/views/authentication_login.erb b/lib/views/authentication_login.erb
new file mode 100644
index 0000000..ef992b8
--- /dev/null
+++ b/lib/views/authentication_login.erb
@@ -0,0 +1,9 @@
+<%= header :title => 'Xfce Foundation Manager | Login' %>
+<form method="post" action="/login">
+ <fieldset>
+ <p><label for="username">Username:</label> <input type="text" id="username" name="username" /></p>
+ <p><label for="password">Password:</label> <input type="password" id="password" name="password"/></p>
+ <p><span class="placeholder"> </span> <input type="submit" value="Log in"/></p>
+ </fieldset>
+</form>
+<%= footer %>
diff --git a/lib/views/foot.erb b/lib/views/foot.erb
new file mode 100644
index 0000000..5b6e2d6
--- /dev/null
+++ b/lib/views/foot.erb
@@ -0,0 +1,3 @@
+ </div>
+ </body>
+</html>
diff --git a/lib/views/head.erb b/lib/views/head.erb
new file mode 100755
index 0000000..07f4c1a
--- /dev/null
+++ b/lib/views/head.erb
@@ -0,0 +1,15 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>Xfce Foundation Manager<%= if params.has_key?(:title) then " | #{params[:title]}" end %></title>
+ <link rel="stylesheet" href="/stylesheet.css" media="all" type="text/css"/>
+ </head>
+ <body>
+ <h1><%= if params.has_key?(:title) then params[:title] else 'Xfce Foundation Manager' end %></h1>
+ <p id="main-toolbar">
+ <% if authenticated? %>
+ <a href="/members/membership">Manage Membership</a>
+ <% end %>
+ </p>
+ <div id="body">
diff --git a/lib/views/index.erb b/lib/views/index.erb
new file mode 100644
index 0000000..ffce996
--- /dev/null
+++ b/lib/views/index.erb
@@ -0,0 +1,2 @@
+<%= header :title => 'Overview' %>
+<%= footer %>
diff --git a/lib/views/stylesheet.sass b/lib/views/stylesheet.sass
new file mode 100755
index 0000000..83cb120
--- /dev/null
+++ b/lib/views/stylesheet.sass
@@ -0,0 +1,123 @@
+html
+ background:
+ color: rgb(255,255,255)
+ margin: 0em
+ padding: 0em
+ font-family: sans-serif
+
+body
+ background:
+ color: rgb(255,255,255)
+ line-height: 1.5em
+ margin: 0em
+ padding: 0em
+
+h1
+ color: rgb(102,153,255)
+ background:
+ color: rgb(51,51,51)
+ padding: 0.5em
+ margin: 0em
+
+//a
+// color: rgb(0,50,150)
+
+#main-toolbar
+ background:
+ color: rgb(70,70,70)
+ margin: 0em
+ padding:
+ left: 1.2em
+ right: 1.2em
+ line-height: 2em
+ a
+ color: rgb(255,255,255)
+ padding: 0em
+ &:hover
+ color: rgb(50,100,200)
+
+#body
+ padding:
+ top: 0em
+ left: 1.1em
+ right: 1.1em
+ bottom: 1em
+
+h2
+ color: rgb(102,102,102)
+ margin:
+ top: 1.5em
+ border-bottom: thin dotted rgb(204,204,204)
+ a
+ font-size: 0.8em
+ margin-left: 2em
+ display: block
+ float: right
+ margin-top: -1.2em
+
+//table
+// border: 0em
+//
+// th
+// text-align: left
+// padding:
+// top: 0.25em
+// bottom: 0.25em
+// left: 0.25em
+// right: 2em
+//
+// td
+// padding: 0.25em
+//
+// a
+// margin-right: 1em
+//
+// a.inactive
+// color: rgb(100, 150, 250)
+//
+form
+ p
+ padding: 0.25em
+
+fieldset
+ border: 0em
+ padding: 0em
+ margin: 0em
+
+ p
+ padding: 0em
+
+ label, span.placeholder
+ display: block
+ float: left
+ min-width: 25%
+ text-align: right
+ padding-right: 0.5em
+ line-height: 1.65em
+
+ label.inline
+ display: inline-block
+ float: none
+ min-width: 0%
+
+ span.placeholder
+ height: 2em
+
+ textarea
+ width: 60%
+
+ span.error, span.info
+ padding-left: 1em
+
+.error
+ color: red
+
+//.preview
+// background-color: rgb(245, 245, 245)
+// overflow-x: auto
+// padding: 0.5em
+// white-space: pre
+// font-family: monospace
+
+.center
+ text-align: center
More information about the Xfce4-commits
mailing list