[Xfce4-commits] <cafe:master> Start working on member management.
Jannis Pohlmann
noreply at xfce.org
Sun Mar 13 01:48:01 CET 2011
Updating branch refs/heads/master
to 02f3c9e8130180058284883c4ac75a58b84c06f9 (commit)
from 2a3473aaad641542a5b075b2bff83a5a0b977067 (commit)
commit 02f3c9e8130180058284883c4ac75a58b84c06f9
Author: Jannis Pohlmann <jannis at xfce.org>
Date: Sun Mar 13 01:47:28 2011 +0100
Start working on member management.
examples/xfce-foundation/config.ru | 30 ++++++++++++++----
lib/cafe.rb | 1 +
lib/controllers/members.rb | 17 ++++++++--
lib/helpers/authentication.rb | 11 ++----
lib/helpers/general.rb | 13 ++++++++
lib/models/member.rb | 30 ++++++++++++------
lib/models/role.rb | 9 +++++
lib/views/authentication_login.erb | 2 +-
lib/views/head.erb | 2 +-
lib/views/member.erb | 37 ++++++++++++++++++++++
lib/views/members.erb | 29 +++++++++++++++++
lib/views/stylesheet.sass | 59 +++++++++++++++++++++++-------------
12 files changed, 191 insertions(+), 49 deletions(-)
diff --git a/examples/xfce-foundation/config.ru b/examples/xfce-foundation/config.ru
index 6aa5c73..54d165f 100644
--- a/examples/xfce-foundation/config.ru
+++ b/examples/xfce-foundation/config.ru
@@ -19,14 +19,32 @@ DataMapper.setup(:default, "sqlite:///home/jannis/xfce/git/foundation/cafe/examp
require '../../lib/cafe'
# create dummy users
-admin = Cafe::Models::Member.first_or_create(
- :username => 'jannis',
+user = Cafe::Models::Member.first_or_create(
+ :fullname => 'Christoph Wickert',
+ :email => 'christoph.wickert at googlemail.com',
:password => 'test',
- :firstname => 'Jannis',
- :lastname => 'Pohlmann',
- :address => 'Foo 123, 23123 Lübeck',
+ :address => 'Foo 123
+D12393 Wherever
+Germany',
:member_since => '2011-02-06'
)
-admin.save
+user.save
+user = Cafe::Models::Member.first_or_create(
+ :fullname => 'Jannis Pohlmann',
+ :email => 'jannis at xfce.org',
+ :password => 'test',
+ :address => 'Foo 123
+23123 Lübeck',
+ :member_since => '2011-02-06'
+)
+user.save
+
+# create dummy roles
+board = Cafe::Models::Role.first_or_create(:name => 'board')
+board.save
+
+# add roles to dummy users
+user.roles << board
+user.save
run Cafe::Application
diff --git a/lib/cafe.rb b/lib/cafe.rb
index 05eb913..fe187bd 100644
--- a/lib/cafe.rb
+++ b/lib/cafe.rb
@@ -7,6 +7,7 @@ require 'sass'
directory = File.expand_path(File.dirname(__FILE__))
# load models
+require File.join(directory, 'models', 'role')
require File.join(directory, 'models', 'member')
# finalize models and set up the database
diff --git a/lib/controllers/members.rb b/lib/controllers/members.rb
index 869158d..2f1f8d3 100644
--- a/lib/controllers/members.rb
+++ b/lib/controllers/members.rb
@@ -1,15 +1,26 @@
module Cafe
module Controllers
module Members
+ include Cafe::Models
def self.registered(app)
- app.get '/members/:name' do
- 'Hello World'
+ app.get '/members' do
+ view :members
end
- end
+ app.get '/member/:id' do
+ authorization_required
+ @member = Member.first(params[:id])
+ view :member
+ end
+ app.post '/member/:id/information' do
+ authorization_required
+ @member = Member.first(params[:id])
+ end
+
+ end
end
end
end
diff --git a/lib/helpers/authentication.rb b/lib/helpers/authentication.rb
index 3281d7d..814bc3d 100644
--- a/lib/helpers/authentication.rb
+++ b/lib/helpers/authentication.rb
@@ -35,13 +35,11 @@ module Cafe
Warden::Strategies.add(:password) do
def valid?
- puts 'password strategy valid?'
- params['username'] and params['username'] != ''
+ params['email'] and params['email'] != ''
end
def authenticate!
- puts 'password strategy authenticate!'
- member = Cafe::Models::Member.authenticate(params['username'], params['password'])
+ member = Cafe::Models::Member.authenticate(params['email'], params['password'])
if member.nil?
fail!('Nope')
else
@@ -82,12 +80,11 @@ module Cafe
warden.set_user(new_user, {})
end
- def authorization_requred(context = nil, roles = ['board'])
+ def authorization_required(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
+ unless session_user.authorized?(roles)
halt(view(:permission_denied, binding))
end
end
diff --git a/lib/helpers/general.rb b/lib/helpers/general.rb
index da741d1..e25b5de 100644
--- a/lib/helpers/general.rb
+++ b/lib/helpers/general.rb
@@ -35,6 +35,19 @@ module Cafe
import(*args)
end
+ def cycle(*values)
+ @cycle = values unless defined?(@cycle)
+ @cycle_index = -1 unless defined?(@cycle_index)
+
+ if @cycle_index >= @cycle.size-1
+ @cycle_index = 0
+ else
+ @cycle_index += 1
+ end
+
+ return @cycle[@cycle_index]
+ end
+
def error_set(key, value)
env[:error][key] = value
end
diff --git a/lib/models/member.rb b/lib/models/member.rb
index b1faf1f..fe59397 100644
--- a/lib/models/member.rb
+++ b/lib/models/member.rb
@@ -4,23 +4,33 @@ module Cafe
include DataMapper::Resource
property :id, Serial
- property :username, String
+ property :fullname, String
+ property :email, String, :unique => true
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
+ has n, :roles, :through => Resource
+
+ def self.authenticate(email, password)
+ member = first(:email => email)
if member and member.password == password
- return member
+ member
else
- return nil
+ nil
+ end
+ end
+
+ def authorized?(required_roles)
+ if required_roles.is_a?(String)
+ required_roles = [required_roles]
+ end
+
+ matched_roles = []
+ for role in roles
+ matched_roles << role if required_roles.include?(role.name)
end
+ not matched_roles.empty?
end
end
end
diff --git a/lib/models/role.rb b/lib/models/role.rb
new file mode 100644
index 0000000..b927f19
--- /dev/null
+++ b/lib/models/role.rb
@@ -0,0 +1,9 @@
+module Cafe
+ module Models
+ class Role
+ include DataMapper::Resource
+
+ property :name, String, :key => true
+ end
+ end
+end
diff --git a/lib/views/authentication_login.erb b/lib/views/authentication_login.erb
index ef992b8..11c37fa 100644
--- a/lib/views/authentication_login.erb
+++ b/lib/views/authentication_login.erb
@@ -1,7 +1,7 @@
<%= 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="email">Email address:</label> <input type="text" id="email" name="email" /></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>
diff --git a/lib/views/head.erb b/lib/views/head.erb
index 07f4c1a..2013134 100755
--- a/lib/views/head.erb
+++ b/lib/views/head.erb
@@ -9,7 +9,7 @@
<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>
+ <a href="/members">Members</a>
<% end %>
</p>
<div id="body">
diff --git a/lib/views/member.erb b/lib/views/member.erb
new file mode 100644
index 0000000..5f54f8f
--- /dev/null
+++ b/lib/views/member.erb
@@ -0,0 +1,37 @@
+<%= header :title => 'Edit Member %s' % [@member.fullname] %>
+<h2>Member Information</h2>
+<form method="post" action="/member/<%= @member.id %>/information">
+ <fieldset>
+ <p>
+ <label for="fullname">Full name:</label>
+ <input type="text" id="fullname" name="fullname" value="<%= @member.fullname %>"/>
+ </p>
+ <p>
+ <label for="email">Email address:</label>
+ <input type="text" id="email" name="email" value="<%= @member.email %>"/>
+ </p>
+ <p>
+ <label for="email">Email address:</label>
+ <input type="text" id="email" name="email" value="<%= @member.email %>"/>
+ </p>
+ <p>
+ <label for="new_password">Password:</label>
+ <input type="password" id="new_password" name="new_password" value=""/>
+ </p>
+ <p>
+ <label for="new_password">Password (confirmation):</label>
+ <input type="password" id="new_password_repeat" name="new_password_repeat" value=""/>
+ </p>
+ <p>
+ <label for="address">Address:</label>
+ <textarea id="address" name="address" style="height:10em;"><%= @member.address %></textarea>
+ </p>
+ <p>
+ <label for="member_since">Member since:</label>
+ <input type="text" id="member_since" name="member_since" value="<%= @member.member_since %>"/>
+ Format: YYYY-MM-DD
+ </p>
+ <p><span class="placeholder"> </span> <input type="submit" value="Save"/></p>
+</form>
+<h2>Membership Status</h2>
+<%= footer %>
diff --git a/lib/views/members.erb b/lib/views/members.erb
new file mode 100644
index 0000000..44fba06
--- /dev/null
+++ b/lib/views/members.erb
@@ -0,0 +1,29 @@
+<%= header :title => 'Members' %>
+<h2>Member List</h2>
+<% if Member.count == 0 %>
+ <p>The foundation currently has no members.</p>
+<% else %>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <th>Member ID</th>
+ <th>Full Name</th>
+ <th>Email Address</th>
+ <th>Address</th>
+ <th>Member Since</th>
+ <th>Actions</th>
+ </tr>
+ <% for member in Member.all(:order => [:fullname.asc]) %>
+ <tr class="<%= cycle('odd', 'even') %>">
+ <td>#<%= member.id %></td>
+ <td><%= member.fullname %></td>
+ <td><%= member.email %></td>
+ <td><%= member.address.gsub(/\n/, '<br/>') %></td>
+ <td><%= member.member_since %></td>
+ <td>
+ <a href="/member/<%= member.id %>">Edit</a>
+ </td>
+ </tr>
+ <% end %>
+ </table>
+<% end %>
+<%= footer %>
diff --git a/lib/views/stylesheet.sass b/lib/views/stylesheet.sass
index 83cb120..68f870b 100755
--- a/lib/views/stylesheet.sass
+++ b/lib/views/stylesheet.sass
@@ -55,26 +55,32 @@ h2
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)
-//
+table
+ border: 0em
+ width: 100%
+
+ th
+ text-align: left
+ padding:
+ top: 0.25em
+ bottom: 0.25em
+ left: 0.25em
+ right: 2em
+
+ tr.even
+ background:
+ color: rgb(240,240,240)
+
+ td
+ padding: 0.25em
+ vertical-align: top
+
+ a
+ margin-right: 1em
+
+ a.inactive
+ color: rgb(100, 150, 250)
+
form
p
padding: 0.25em
@@ -93,7 +99,7 @@ fieldset
min-width: 25%
text-align: right
padding-right: 0.5em
- line-height: 1.65em
+ line-height: 2em
label.inline
display: inline-block
@@ -103,7 +109,18 @@ fieldset
span.placeholder
height: 2em
+ input[type=text]
+ border: thin solid rgb(150,150,150)
+ line-height: 2em
+ padding:
+ left: 0.5em
+ right: 0.5em
+
+ input[type=password]
+ @extend input[type=text]
+
textarea
+ @extend input[type=text]
width: 60%
span.error, span.info
More information about the Xfce4-commits
mailing list