From e7af7e82b58df7dbc88e81adab90b07346695576 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sat, 20 Mar 2010 02:26:30 +0100 Subject: [PATCH 1/9] Implemented routing filter for foodcoop select. * Allows now a multi coop installation for one domain! * SSL, we are coming... * TODO: Remove all the hardcoded urls, check email-links etc. --- app/controllers/application_controller.rb | 215 +++++++++++----------- config/environment.rb | 1 + config/routes.rb | 3 + lib/foodcoop_filter.rb | 37 ++++ 4 files changed, 151 insertions(+), 105 deletions(-) create mode 100644 lib/foodcoop_filter.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 64160b21..4266de88 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -23,128 +23,133 @@ class ApplicationController < ActionController::Base protected - def current_user - begin - # check if there is a valid session and return the logged-in user (its object) - if session['user_and_subdomain'] - id, subdomain = session['user_and_subdomain'].split - # for shared-host installations. check if the cookie-subdomain fits to request. - return User.current_user = User.find(id) if request.subdomains.first == subdomain - end - rescue - reset_session - flash[:error]= _("An error has occurred. Please login again.") - redirect_to :controller => 'login' + def current_user + begin + # check if there is a valid session and return the logged-in user (its object) + if session['user_and_subdomain'] + id, subdomain = session['user_and_subdomain'].split + # for shared-host installations. check if the cookie-subdomain fits to request. + return User.current_user = User.find(id) if request.subdomains.first == subdomain end + rescue + reset_session + flash[:error]= _("An error has occurred. Please login again.") + redirect_to :controller => 'login' end + end - def current_user=(user) - session['user_and_subdomain'] = [user.id, request.subdomains.first].join(" ") - end + def current_user=(user) + session['user_and_subdomain'] = [user.id, request.subdomains.first].join(" ") + end - def return_to - session['return_to'] - end + def return_to + session['return_to'] + end - def return_to=(uri) - session['return_to'] = uri - end + def return_to=(uri) + session['return_to'] = uri + end - def deny_access - self.return_to = request.request_uri - redirect_to :controller => '/login', :action => 'denied' - return false - end + def deny_access + self.return_to = request.request_uri + redirect_to :controller => '/login', :action => 'denied' + return false + end private - def authenticate(role = 'any') - # Attempt to retrieve authenticated user from controller instance or session... - if !(user = current_user) - # No user at all: redirect to login page. - self.return_to = request.request_uri - redirect_to :controller => '/login' - return false + def authenticate(role = 'any') + # Attempt to retrieve authenticated user from controller instance or session... + if !(user = current_user) + # No user at all: redirect to login page. + self.return_to = request.request_uri + redirect_to :controller => '/login' + return false + else + # We have an authenticated user, now check role... + # Roles gets the user through his memberships. + hasRole = case role + when "admin" then user.role_admin? + when "finance" then user.role_finance? + when "article_meta" then user.role_article_meta? + when "suppliers" then user.role_suppliers? + when "orders" then user.role_orders? + when "any" then true # no role required + else false # any unknown role will always fail + end + if hasRole + @current_user = user else - # We have an authenticated user, now check role... - # Roles gets the user through his memberships. - hasRole = case role - when "admin" then user.role_admin? - when "finance" then user.role_finance? - when "article_meta" then user.role_article_meta? - when "suppliers" then user.role_suppliers? - when "orders" then user.role_orders? - when "any" then true # no role required - else false # any unknown role will always fail - end - if hasRole - @current_user = user - else - deny_access - end - end - end - - def authenticate_admin - authenticate('admin') - end - - def authenticate_finance - authenticate('finance') - end - - def authenticate_article_meta - authenticate('article_meta') - end - - def authenticate_suppliers - authenticate('suppliers') - end - - def authenticate_orders - authenticate('orders') - end - - # checks if the current_user is member of given group. - # if fails the user will redirected to startpage - def authenticate_membership_or_admin - @group = Group.find(params[:id]) - unless @group.member?(@current_user) or @current_user.role_admin? - flash[:error] = "Diese Aktion ist nur für Mitglieder der Gruppe erlaubt!" - if request.xml_http_request? - render(:update) {|page| page.redirect_to root_path } - else - redirect_to root_path - end + deny_access end end - - # Stores this controller instance as a thread local varibale to be accessible from outside ActionController/ActionView. - def store_controller - Thread.current[:application_controller] = self - end + end - # Sets the thread local variable that holds a reference to the current controller to nil. - def remove_controller - Thread.current[:application_controller] = nil - end + def authenticate_admin + authenticate('admin') + end + + def authenticate_finance + authenticate('finance') + end + + def authenticate_article_meta + authenticate('article_meta') + end - # Get supplier in nested resources - def find_supplier - @supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id] - end + def authenticate_suppliers + authenticate('suppliers') + end - # Set config and database connection for each request - # It uses the subdomain to select the appropriate section in the config files - # Use this method as a before filter (first filter!) in ApplicationController - def select_foodcoop - if Foodsoft.config[:multi_coop_install] - # Get subdomain - subdomain = request.subdomains.first + def authenticate_orders + authenticate('orders') + end + + # checks if the current_user is member of given group. + # if fails the user will redirected to startpage + def authenticate_membership_or_admin + @group = Group.find(params[:id]) + unless @group.member?(@current_user) or @current_user.role_admin? + flash[:error] = "Diese Aktion ist nur für Mitglieder der Gruppe erlaubt!" + if request.xml_http_request? + render(:update) {|page| page.redirect_to root_path } + else + redirect_to root_path + end + end + end + + # Stores this controller instance as a thread local varibale to be accessible from outside ActionController/ActionView. + def store_controller + Thread.current[:application_controller] = self + end + + # Sets the thread local variable that holds a reference to the current controller to nil. + def remove_controller + Thread.current[:application_controller] = nil + end + + # Get supplier in nested resources + def find_supplier + @supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id] + end + + # Set config and database connection for each request + # It uses the subdomain to select the appropriate section in the config files + # Use this method as a before filter (first filter!) in ApplicationController + def select_foodcoop + if Foodsoft.config[:multi_coop_install] + if !params[:foodcoop].blank? # Set Config - Foodsoft.env = subdomain + Foodsoft.env = params[:foodcoop] # Set database-connection - ActiveRecord::Base.establish_connection(Foodsoft.database(subdomain)) + ActiveRecord::Base.establish_connection(Foodsoft.database) + else + redirect_to root_path end + else + # Deactivate routing filter + RoutingFilter::Foodcoop.active = false end + end end diff --git a/config/environment.rb b/config/environment.rb index 0289b71a..94f56d76 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -68,6 +68,7 @@ Rails::Initializer.run do |config| config.gem "fastercsv" config.gem "prawn" config.gem "haml", :version => '>=2.0.6' + config.gem "routing-filter", :lib => "routing_filter" # The internationalization framework can be changed to have another default locale (standard is :en) or more load paths. # All files from config/locales/*.rb,yml are added automatically. diff --git a/config/routes.rb b/config/routes.rb index a8fcaa91..6506e4cd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ ActionController::Routing::Routes.draw do |map| + + map.filter 'foodcoop', :file => File.join(RAILS_ROOT, "lib", "foodcoop_filter") + map.resources :pages, :collection => { :all => :get }, :member => {:version => :get, :revert => :get} map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show', :permalink => /[^\s]+/ map.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'Home' diff --git a/lib/foodcoop_filter.rb b/lib/foodcoop_filter.rb new file mode 100644 index 00000000..0505e96a --- /dev/null +++ b/lib/foodcoop_filter.rb @@ -0,0 +1,37 @@ +require 'routing_filter/base' + +module RoutingFilter + class Foodcoop < Base + def around_recognize(path, env, &block) + token = extract_token!(path) # remove the token from the beginning of the path + returning yield do |params| # invoke the given block (calls more filters and finally routing) + params[:foodcoop] = token if token # set recognized token to the resulting params hash + end + end + + def around_generate(*args, &block) + token = args.extract_options!.delete(:foodcoop) # extract the passed :token option + token = Foodsoft.env if token.nil? # default to Foodsoft.env + + returning yield do |result| + if token + url = result.is_a?(Array) ? result.first : result + prepend_token!(url, token) + end + end + end + + protected + + def extract_token!(path) + foodcoop = nil + path.sub! %r(^/([a-zA-Z0-9]*)(?=/|$)) do foodcoop = $1; '' end + foodcoop + end + + def prepend_token!(url, token) + url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{token}#{$2}" } + end + + end +end \ No newline at end of file From 2c1952d625cbb56529a49eea2eaf9c797261b7a6 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sat, 20 Mar 2010 13:45:58 +0100 Subject: [PATCH 2/9] Removed hardcoded links from main tabnav. * Reorganized routes and foodsoft config * Avoid saving of wrong foodcoop tokens when following hard coded link --- app/controllers/tasks_controller.rb | 4 +- app/views/home/index.html.haml | 4 +- app/views/layouts/_main_tabnav.html.erb | 46 ++++----- app/views/layouts/application.haml | 2 +- app/views/tasks/_nav.haml | 2 +- .../tasks/{myTasks.haml => user.html.haml} | 0 config/initializers/load_app_config.rb | 38 +++---- config/routes.rb | 98 +++++++------------ 8 files changed, 81 insertions(+), 113 deletions(-) rename app/views/tasks/{myTasks.haml => user.html.haml} (100%) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 96a5b1c3..07a3e505 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -6,7 +6,7 @@ class TasksController < ApplicationController @groups = Workgroup.all end - def myTasks + def user @unaccepted_tasks = @current_user.unaccepted_tasks @accepted_tasks = @current_user.accepted_tasks end @@ -78,7 +78,7 @@ class TasksController < ApplicationController task.assignments.create(:user => current_user, :accepted => true) end flash[:notice] = "Du hast die Aufgabe übernommen" - redirect_to my_tasks_path + redirect_to user_tasks_path end # deletes assignment between current_user and given task diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 1d451125..8f5ee0b5 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -21,11 +21,11 @@ - unless @unaccepted_tasks.empty? %h3 Aufgaben übernehmen Du bis für Aufgaben verantwortlich. - = link_to "Aufgaben übernehmen/ablehnen", my_tasks_path + = link_to "Aufgaben übernehmen/ablehnen", user_tasks_path - unless @unassigned_tasks_number == 0 %h3 Offene Aufgaben = "Es gibt #{@unassigned_tasks_number} #{link_to 'offene Aufgabe(n)', :controller => 'tasks'}" - %p{:style => "clear:both"}= link_to "Meine Aufgaben", my_tasks_path + %p{:style => "clear:both"}= link_to "Meine Aufgaben", user_tasks_path - if @ordergroup // Current orders diff --git a/app/views/layouts/_main_tabnav.html.erb b/app/views/layouts/_main_tabnav.html.erb index 0fcec695..e0c41b55 100644 --- a/app/views/layouts/_main_tabnav.html.erb +++ b/app/views/layouts/_main_tabnav.html.erb @@ -3,55 +3,55 @@ tabs = [ { :name => "Start", :url => root_path, :active => ["index", "home"], :subnav => [ - { :name => "Meine Aufgaben", :url => "/home/tasks" }, - { :name => "Meine Bestellgruppe", :url => "/home/ordergroup", :access_denied? => (!u.ordergroup)}, - { :name => "Mein Profil", :url => "/home/profile"} + { :name => "Meine Aufgaben", :url => user_tasks_path }, + { :name => "Meine Bestellgruppe", :url => my_ordergroup_path, :access_denied? => (!u.ordergroup)}, + { :name => "Mein Profil", :url => my_profile_path} ] }, - { :name => "Foodcoop", :url => "/tasks", + { :name => "Foodcoop", :url => tasks_path, :active => ["foodcoop", "tasks", "messages", "foodcoop/ordergroups", "foodcoop/workgroups", "foodcoop/users"], :subnav => [ - { :name => "Mitglieder", :url => "/foodcoop/users"}, - { :name => "Abeitsgruppen", :url => "/foodcoop/workgroups"}, - { :name => "Bestellgruppen", :url => "/foodcoop/ordergroups"}, - { :name => "Nachrichten", :url => "/messages"}, - { :name => "Aufgaben", :url => "/tasks"} + { :name => "Mitglieder", :url => foodcoop_users_path}, + { :name => "Abeitsgruppen", :url => foodcoop_workgroups_path}, + { :name => "Bestellgruppen", :url => foodcoop_ordergroups_path}, + { :name => "Nachrichten", :url => messages_path}, + { :name => "Aufgaben", :url => tasks_path} ] }, - { :name => "Wiki", :url => "/wiki", :active => ["pages", "wiki"], + { :name => "Wiki", :url => wiki_path, :active => ["pages", "wiki"], :subnav => [ - { :name => "Startseite", :url => "/wiki" }, - { :name => "Alle Seiten", :url => "/pages/all" } + { :name => "Startseite", :url => wiki_path }, + { :name => "Alle Seiten", :url => all_pages_path } ] }, - { :name => "Bestellungen", :url => u.ordergroup ? "/ordering/" : "/orders", + { :name => "Bestellungen", :url => u.ordergroup ? ordering_path : orders_path, :active => ["orders", "ordering"], :subnav => [ - { :name => "Bestellen!", :url => "/ordering" }, - { :name => "Meine Bestellungen", :url => "/ordering/myOrders" }, - { :name => "Bestellverwaltung", :url => "/orders", :access_denied? => (!u.role_orders?) } + { :name => "Bestellen!", :url => ordering_path }, + { :name => "Meine Bestellungen", :url => my_orders_path }, + { :name => "Bestellverwaltung", :url => orders_path, :access_denied? => (!u.role_orders?) } ] }, - { :name => "Artikel", :url => "/suppliers", + { :name => "Artikel", :url => suppliers_path, :active => ["articles", "suppliers", "deliveries", "article_categories", "stockit", "stock_takings"], :access_denied? => (!u.role_article_meta? && !u.role_suppliers?), :subnav => [ { :name => "Artikel", :url => supplier_articles_path(Supplier.first) }, - { :name => "Lager", :url => "/stockit" }, + { :name => "Lager", :url => stock_articles_path }, { :name => "Lieferantinnen", :url => suppliers_path, :access_denied? => (!u.role_suppliers?) }, - { :name => "Kategorien", :url => "/article_categories"} + { :name => "Kategorien", :url => article_categories_path } ] }, - { :name => "Finanzen", :url => "/finance", + { :name => "Finanzen", :url => finance_root_path, :active => ["finance/invoices", "finance/transactions", "finance/balancing"], :access_denied? => (!u.role_finance?), :subnav => [ - { :name => "Konten verwalten", :url => "/finance/transactions" }, - { :name => "Bestellungen abrechnen", :url => "/finance/balancing/list" }, + { :name => "Konten verwalten", :url => finance_transactions_path }, + { :name => "Bestellungen abrechnen", :url => finance_balancing_path }, { :name => "Rechnungen", :url => finance_invoices_path } ] }, - { :name => "Administration", :url => "/admin", + { :name => "Administration", :url => admin_root_path, :active => ["admin", "admin/users", "admin/ordergroups", "admin/workgroups"], :access_denied? => (!u.role_admin?), :subnav => [ diff --git a/app/views/layouts/application.haml b/app/views/layouts/application.haml index 8f531233..fc8b52b0 100644 --- a/app/views/layouts/application.haml +++ b/app/views/layouts/application.haml @@ -15,7 +15,7 @@ #header #logo - %a{:href => "/"} + - link_to root_path do foodsoft %span{:style => "color:white; font-size:45%; letter-spacing: -1px;"}= Foodsoft.config[:name] #nav= render :partial => 'layouts/main_tabnav' diff --git a/app/views/tasks/_nav.haml b/app/views/tasks/_nav.haml index 699cec05..bed59970 100644 --- a/app/views/tasks/_nav.haml +++ b/app/views/tasks/_nav.haml @@ -8,7 +8,7 @@ %li Seiten %ul - %li= link_to "Meine Aufgaben", my_tasks_path + %li= link_to "Meine Aufgaben", user_tasks_path %li= link_to "Alle Aufgaben", :action => "index" %li= link_to "Erledigt Aufgaben (Archiv)", :action => "archive" diff --git a/app/views/tasks/myTasks.haml b/app/views/tasks/user.html.haml similarity index 100% rename from app/views/tasks/myTasks.haml rename to app/views/tasks/user.html.haml diff --git a/config/initializers/load_app_config.rb b/config/initializers/load_app_config.rb index e0ff3ff7..53e9d938 100644 --- a/config/initializers/load_app_config.rb +++ b/config/initializers/load_app_config.rb @@ -1,33 +1,25 @@ # Loads and returns config and databases for selected foodcoop. +# TODO: When to use class or module. It seems this could also be a Foodsoft-class? module Foodsoft - @@configs = YAML.load(File.read(RAILS_ROOT + "/config/app_config.yml")) - @@databases = YAML.load(File.read(RAILS_ROOT + "/config/database.yml")) - @@env = RAILS_ENV + mattr_accessor :env, :config, :database + CONFIGS = YAML.load(File.read(RAILS_ROOT + "/config/app_config.yml")) + DATABASES = YAML.load(File.read(RAILS_ROOT + "/config/database.yml")) - def env=(env) - @@env = env + class << self + def env=(env) + raise "No config or database for this environment (#{env}) available!" if CONFIGS[env].nil? or DATABASES[env].nil? + @@config = CONFIGS[env].symbolize_keys + @@database = DATABASES[env].symbolize_keys + @@env = env + end end - - def env - @@env - end - - def config(rails_env = @@env) - raise "No config for this environment (or subdomain) available!" if @@configs[rails_env].nil? - @@configs[rails_env].symbolize_keys - end - - def database(rails_env = @@env) - raise "No database for this environment (or subdomain) available!" if @@databases[rails_env].nil? - @@databases[rails_env].symbolize_keys - end - - extend self end - +# Initial load the default config and database from rails environment +Foodsoft.env = RAILS_ENV # Configuration of the exception_notification plugin # Mailadresses are set in config/foodsoft.yaml ExceptionNotifier.exception_recipients = Foodsoft.config[:notification]['error_recipients'] ExceptionNotifier.sender_address = Foodsoft.config[:notification]['sender_address'] -ExceptionNotifier.email_prefix = Foodsoft.config[:notification]['email_prefix'] \ No newline at end of file +ExceptionNotifier.email_prefix = Foodsoft.config[:notification]['email_prefix'] + diff --git a/config/routes.rb b/config/routes.rb index 6506e4cd..8eee3473 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,23 +1,35 @@ ActionController::Routing::Routes.draw do |map| + # Use routing filter to select foodcoop config and datbase map.filter 'foodcoop', :file => File.join(RAILS_ROOT, "lib", "foodcoop_filter") - + + # Root path + map.root :controller => 'home', :action => 'index' + + # User specific + map.logout '/logout', :controller => 'login', :action => 'logout' + map.my_profile '/home/profile', :controller => 'home', :action => 'profile' + map.my_ordergroup '/home/ordergroup', :controller => 'home', :action => 'ordergroup' + + # Wiki map.resources :pages, :collection => { :all => :get }, :member => {:version => :get, :revert => :get} map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show', :permalink => /[^\s]+/ map.wiki "/wiki", :controller => 'pages', :action => 'show', :permalink => 'Home' - map.logout '/logout', :controller => 'login', :action => 'logout' - map.my_profile '/home/profile', :controller => 'home', :action => 'profile' - map.my_ordergroup '/home/ordergroup', :controller => 'home', :action => 'ordergroup' - map.my_tasks '/home/tasks', :controller => 'tasks', :action => 'myTasks' - + # Orders, ordering map.resources :orders, :member => { :finish => :post, :add_comment => :post } + map.with_options :controller => "ordering" do |ordering| + ordering.ordering "/ordering", :action => "index" + ordering.my_orders "/ordering/myOrders", :action => "myOrders" + end + + # Foodcoop orga + map.resources :invites, :only => [:new, :create] + map.resources :tasks, + :collection => {:user => :get} map.resources :messages, :only => [:index, :show, :new, :create], :member => { :reply => :get, :user => :get, :group => :get } - - map.resources :invites, :only => [:new, :create] - map.namespace :foodcoop do |foodcoop| foodcoop.root :controller => "users", :action => "index" foodcoop.resources :users, :only => [:index] @@ -26,23 +38,13 @@ ActionController::Routing::Routes.draw do |map| :member => {:memberships => :get} end - map.namespace :admin do |admin| - admin.resources :users - admin.resources :workgroups, :member => { :memberships => :get } - admin.resources :ordergroups, :member => { :memberships => :get } - end - - map.namespace :finance do |finance| - finance.root :controller => 'balancing' - finance.resources :invoices - end - + # Article management map.resources :stock_takings, :collection => {:fill_new_stock_article_form => :get, :add_stock_article => :post} map.resources :stock_articles, :controller => 'stockit', :as => 'stockit', :collection => {:auto_complete_for_article_name => :get, :fill_new_stock_article_form => :get} - + map.resources :suppliers, :collection => { :shared_suppliers => :get } do |suppliers| suppliers.resources :deliveries, @@ -55,47 +57,21 @@ ActionController::Routing::Routes.draw do |map| end map.resources :article_categories - map.root :controller => 'home', :action => 'index' - - # The priority is based upon order of creation: first created -> highest priority. + # Finance + map.namespace :finance do |finance| + finance.root :controller => 'balancing' + finance.balancing "balancing/list", :controller => 'balancing', :action => 'list' + finance.resources :invoices + finance.resources :transactions + end - # Sample of regular route: - # map.connect 'products/:id', :controller => 'catalog', :action => 'view' - # Keep in mind you can assign values other than :controller and :action - - # Sample of named route: - # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase' - # This route can be invoked with purchase_url(:id => product.id) - - # Sample resource route (maps HTTP verbs to controller actions automatically): - # map.resources :products - - # Sample resource route with options: - # map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get } - - # Sample resource route with sub-resources: - # map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller - - # Sample resource route with more complex sub-resources - # map.resources :products do |products| - # products.resources :comments - # products.resources :sales, :collection => { :recent => :get } - # end - - # Sample resource route within a namespace: - # map.namespace :admin do |admin| - # # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb) - # admin.resources :products - # end - - # You can have the root of your site routed with map.root -- just remember to delete public/index.html. - # map.root :controller => "welcome" - - # See how all your routes lay out with "rake routes" - - # Install the default routes as the lowest priority. - # Note: These default routes make all actions in every controller accessible via GET requests. You should - # consider removing the them or commenting them out if you're using named routes and resources. + # Administration + map.namespace :admin do |admin| + admin.root :controller => "admin", :action => "index" + admin.resources :users + admin.resources :workgroups, :member => { :memberships => :get } + admin.resources :ordergroups, :member => { :memberships => :get } + end # Install the default route as the lowest priority. map.connect ':controller/:action/:id' From f49c6493f4209b26cff4228b36f6a9cd9abbd37c Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sat, 20 Mar 2010 14:49:46 +0100 Subject: [PATCH 3/9] Removed hard coded links from mailer and views. * Not tested every mail template. Hopefully everything works ;-) --- app/models/mailer.rb | 12 ++++++------ app/views/login/forgot_password.html.haml | 2 +- app/views/login/password.html.haml | 2 +- app/views/mailer/not_enough_users_assigned.erb | 2 +- app/views/mailer/order_result.html.erb | 2 +- app/views/mailer/upcoming_tasks.html.erb | 2 +- config/app_config.yml.SAMPLE | 6 ++++++ config/initializers/load_app_config.rb | 7 +++++++ config/routes.rb | 1 + 9 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/models/mailer.rb b/app/models/mailer.rb index a86e5aa6..761ac2e9 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -12,9 +12,9 @@ class Mailer < ActionMailer::Base body :body => message.body, :sender => message.sender.nick, :recipients => recipient.nick, - :reply => "#{Foodsoft.config[:base_url]}/messages/reply/#{message.id}", - :link => "#{Foodsoft.config[:base_url]}/messages/show/#{message.id}", - :profile => "#{Foodsoft.config[:base_url]}/home/profile" + :reply => url_for(:controller => "messages", :action => "reply", :id => message.id), + :link => url_for(:controller => "messages", :action => "show", :id => message.id), + :profile => url_for(:controller => "home", :action => "profile") end # Sends an email with instructions on how to reset the password. @@ -23,7 +23,7 @@ class Mailer < ActionMailer::Base prepare_system_message(user) subject "[#{Foodsoft.config[:name]}] Neues Passwort für/ New password for #{user.nick}" body :user => user, - :link => "#{Foodsoft.config[:base_url]}/login/password/#{user.id}?token=#{user.reset_password_token}" + :link => url_for(:controller => "login", :action => "password", :id => user.id, :token => user.reset_password_token) end # Sends an invite email. @@ -31,7 +31,7 @@ class Mailer < ActionMailer::Base prepare_system_message(invite) subject "Einladung in die Foodcoop #{Foodsoft.config[:name]} - Invitation to the Foodcoop" body :invite => invite, - :link => "#{Foodsoft.config[:base_url]}/login/invite/#{invite.token}" + :link => url_for(:controller => "login", :action => "invite", :id => invite.token) end # Notify user of upcoming task. @@ -71,7 +71,7 @@ class Mailer < ActionMailer::Base prepare_system_message(user) subject "[#{Foodsoft.config[:name]}] #{task.name} braucht noch Leute!" body :task => task, :user => user, - :task_url => File.join(Foodsoft.config[:base_url], "tasks/workgroup", task.workgroup_id.to_s) + :task_url => url_for(:controller => "tasks", :action => "workgroup", :id => task.workgroup_id) end protected diff --git a/app/views/login/forgot_password.html.haml b/app/views/login/forgot_password.html.haml index 6d33cab0..0578df73 100644 --- a/app/views/login/forgot_password.html.haml +++ b/app/views/login/forgot_password.html.haml @@ -15,4 +15,4 @@ = text_field 'login', 'email' = submit_tag 'Neues Passwort anfordern' | - = link_to 'Abbrechen', :action => 'login' + = link_to 'Abbrechen', login_path diff --git a/app/views/login/password.html.haml b/app/views/login/password.html.haml index e85b79a0..b83e435a 100644 --- a/app/views/login/password.html.haml +++ b/app/views/login/password.html.haml @@ -13,4 +13,4 @@ = form.password_field :password_confirmation = form.submit 'Speichern' | - = link_to 'Abbrechen', :action => 'login' + = link_to 'Abbrechen', login_path diff --git a/app/views/mailer/not_enough_users_assigned.erb b/app/views/mailer/not_enough_users_assigned.erb index 9032b4f3..c85dd912 100644 --- a/app/views/mailer/not_enough_users_assigned.erb +++ b/app/views/mailer/not_enough_users_assigned.erb @@ -7,5 +7,5 @@ Sofern Du Dich noch nicht für diese Aufgabe eingetragen hast ist das jetzt die <%= @task_url %> -- -Deine Aufgaben: <%= Foodsoft.config[:base_url] %>/home/tasks +Deine Aufgaben: <%= url_for(:controller => "home", :actions => "user") %> diff --git a/app/views/mailer/order_result.html.erb b/app/views/mailer/order_result.html.erb index 1b0bbc6b..6381856f 100644 --- a/app/views/mailer/order_result.html.erb +++ b/app/views/mailer/order_result.html.erb @@ -9,6 +9,6 @@ Für Euch wurden die folgenden Artikel bestellt: <% end -%> Gesamtpreis: <%= @group_order.price %> -Bestellung online einsehen: <%= "#{Foodsoft.config[:base_url]}/ordering/my_order_result/#{@order.id}" %> +Bestellung online einsehen: <%= url_for(:controller => "ordering", :action => "my_order_result", :id => @order.id) %> Viele Grüße von <%= Foodsoft.config[:name] %> \ No newline at end of file diff --git a/app/views/mailer/upcoming_tasks.html.erb b/app/views/mailer/upcoming_tasks.html.erb index b310b710..cadeddf5 100644 --- a/app/views/mailer/upcoming_tasks.html.erb +++ b/app/views/mailer/upcoming_tasks.html.erb @@ -11,6 +11,6 @@ Aufgaben für die nächste Woche: <% end -%> <% end -%> -Meine Aufgaben: <%= Foodsoft.config[:base_url] %>/home/tasks +Meine Aufgaben: <%= url_for(:controller => "home", :actions => "user") %> Viele Grüße von <%= Foodsoft.config[:name] %> \ No newline at end of file diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index 911c43f1..d5043011 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -5,6 +5,12 @@ development: &defaults # Don't forget to setup databases for each foodcoop. See also MULTI_COOP_INSTALL multi_coop_install: false + # http config for this host + # Required for action mailer + protocol: http + host: localhost + port: 3000 + # name of this foodcoop name: FC Test # foodcoop contact information (used for FAX messages) diff --git a/config/initializers/load_app_config.rb b/config/initializers/load_app_config.rb index 53e9d938..ed3ff403 100644 --- a/config/initializers/load_app_config.rb +++ b/config/initializers/load_app_config.rb @@ -17,6 +17,13 @@ end # Initial load the default config and database from rails environment Foodsoft.env = RAILS_ENV +# Set action mailer default host for url generating +ActionMailer::Base.default_url_options = { + :host => Foodsoft.config[:host], + :port => Foodsoft.config[:port], + :protocol => Foodsoft.config[:protocol] +} + # Configuration of the exception_notification plugin # Mailadresses are set in config/foodsoft.yaml ExceptionNotifier.exception_recipients = Foodsoft.config[:notification]['error_recipients'] diff --git a/config/routes.rb b/config/routes.rb index 8eee3473..547f8509 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ ActionController::Routing::Routes.draw do |map| map.root :controller => 'home', :action => 'index' # User specific + map.login "/login", :controller => 'login', :action => 'login' map.logout '/logout', :controller => 'login', :action => 'logout' map.my_profile '/home/profile', :controller => 'home', :action => 'profile' map.my_ordergroup '/home/ordergroup', :controller => 'home', :action => 'ordergroup' From 61a87dacb6209a3ead082618be3575a9cef43b7b Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 21 Mar 2010 00:08:02 +0100 Subject: [PATCH 4/9] Fixed routing bug in last commit. --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 547f8509..1e8dc850 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ ActionController::Routing::Routes.draw do |map| map.root :controller => 'home', :action => 'index' # User specific - map.login "/login", :controller => 'login', :action => 'login' + map.login "/login", :controller => 'login', :action => 'index' map.logout '/logout', :controller => 'login', :action => 'logout' map.my_profile '/home/profile', :controller => 'home', :action => 'profile' map.my_ordergroup '/home/ordergroup', :controller => 'home', :action => 'ordergroup' From 18e8a11b7dafdf42faf375bf72f928e275fd8b76 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Mon, 22 Mar 2010 01:25:24 +0100 Subject: [PATCH 5/9] Removed unnecessary config params. Improved error handling. --- app/controllers/application_controller.rb | 13 +++++++++---- app/views/layouts/email.html.erb | 6 +++--- config/app_config.yml.SAMPLE | 3 --- config/initializers/load_app_config.rb | 9 +++++---- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4266de88..f31c2907 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -140,10 +140,15 @@ class ApplicationController < ActionController::Base def select_foodcoop if Foodsoft.config[:multi_coop_install] if !params[:foodcoop].blank? - # Set Config - Foodsoft.env = params[:foodcoop] - # Set database-connection - ActiveRecord::Base.establish_connection(Foodsoft.database) + begin + # Set Config + Foodsoft.env = params[:foodcoop] + # Set database-connection + ActiveRecord::Base.establish_connection(Foodsoft.database) + rescue => error + flash[:error] = error.to_s + redirect_to root_path + end else redirect_to root_path end diff --git a/app/views/layouts/email.html.erb b/app/views/layouts/email.html.erb index 78af305d..a5aa7eff 100644 --- a/app/views/layouts/email.html.erb +++ b/app/views/layouts/email.html.erb @@ -1,6 +1,6 @@ <%= yield %> -- -FoodSoft: <%= @foodsoftUrl %> -Foodcoop-Homepage: <%= Foodsoft.config[:base_url] %> -Hilfe/Help: <%= Foodsoft.config[:help_url] %> \ No newline at end of file +FoodSoft: <%= url_for(:controller => "home", :action => "index", :only_path => false) %> +Foodcoop-Homepage: <%= Foodsoft.config[:homepage] %> +Hilfe/Help: <%= Foodsoft.config[:help_url] %> diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index d5043011..700d114f 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -22,9 +22,6 @@ development: &defaults email: foodsoft@myfoodcoop.org phone: "030 323 23249" - # base URL for this installation - base_url: http://foodsoft.fctest.de - # Homepage homepage: http://www.fctest.de diff --git a/config/initializers/load_app_config.rb b/config/initializers/load_app_config.rb index ed3ff403..260dd278 100644 --- a/config/initializers/load_app_config.rb +++ b/config/initializers/load_app_config.rb @@ -18,11 +18,12 @@ end Foodsoft.env = RAILS_ENV # Set action mailer default host for url generating -ActionMailer::Base.default_url_options = { - :host => Foodsoft.config[:host], - :port => Foodsoft.config[:port], - :protocol => Foodsoft.config[:protocol] +url_options = { + :host => Foodsoft.config[:host], + :protocol => Foodsoft.config[:protocol] } +url_options.merge!({:port => Foodsoft.config[:port]}) if Foodsoft.config[:port] +ActionMailer::Base.default_url_options = url_options # Configuration of the exception_notification plugin # Mailadresses are set in config/foodsoft.yaml From 9a54d8504c02a637ae38eafe15f656c878369b75 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Mon, 22 Mar 2010 01:38:23 +0100 Subject: [PATCH 6/9] Secured users session, considering foodcoop token. --- app/controllers/application_controller.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f31c2907..ed15383a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -26,10 +26,9 @@ class ApplicationController < ActionController::Base def current_user begin # check if there is a valid session and return the logged-in user (its object) - if session['user_and_subdomain'] - id, subdomain = session['user_and_subdomain'].split + if session[:user] and session[:foodcoop] # for shared-host installations. check if the cookie-subdomain fits to request. - return User.current_user = User.find(id) if request.subdomains.first == subdomain + return User.current_user = User.find(session[:user]) if session[:foodcoop] == Foodsoft.env end rescue reset_session @@ -39,7 +38,7 @@ class ApplicationController < ActionController::Base end def current_user=(user) - session['user_and_subdomain'] = [user.id, request.subdomains.first].join(" ") + session[:user], session[:foodcoop] = user.id, Foodsoft.env end def return_to From 571548dfbc3d1b861aa54e8b032c298e0078cbfc Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Mon, 22 Mar 2010 01:58:37 +0100 Subject: [PATCH 7/9] Fixed routing bug on (ex) admin controller. --- .../{admin_controller.rb => admin/base_controller.rb} | 4 +--- app/controllers/admin/ordergroups_controller.rb | 3 +-- app/controllers/admin/users_controller.rb | 4 +--- app/controllers/admin/workgroups_controller.rb | 3 +-- app/controllers/application_controller.rb | 1 + app/controllers/login_controller.rb | 1 - app/helpers/application_helper.rb | 2 +- app/views/admin/{ => base}/index.html.haml | 0 app/views/layouts/_main_tabnav.html.erb | 4 ++-- config/routes.rb | 2 +- 10 files changed, 9 insertions(+), 15 deletions(-) rename app/controllers/{admin_controller.rb => admin/base_controller.rb} (65%) rename app/views/admin/{ => base}/index.html.haml (100%) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin/base_controller.rb similarity index 65% rename from app/controllers/admin_controller.rb rename to app/controllers/admin/base_controller.rb index e24a1bdd..e614e7ac 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,7 +1,5 @@ -class AdminController < ApplicationController +class Admin::BaseController < ApplicationController before_filter :authenticate_admin - filter_parameter_logging :password, :password_confirmation # do not log passwort parameters - def index @user = self.current_user diff --git a/app/controllers/admin/ordergroups_controller.rb b/app/controllers/admin/ordergroups_controller.rb index a35e32fd..fb1c0356 100644 --- a/app/controllers/admin/ordergroups_controller.rb +++ b/app/controllers/admin/ordergroups_controller.rb @@ -1,5 +1,4 @@ -class Admin::OrdergroupsController < ApplicationController - before_filter :authenticate_admin +class Admin::OrdergroupsController < Admin::BaseController def index if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index c1bcd98e..ce2ada44 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,6 +1,4 @@ -class Admin::UsersController < ApplicationController - before_filter :authenticate_admin - filter_parameter_logging :password, :password_confirmation # do not log passwort parameters +class Admin::UsersController < Admin::BaseController def index if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100) diff --git a/app/controllers/admin/workgroups_controller.rb b/app/controllers/admin/workgroups_controller.rb index 88462edb..e01af47a 100644 --- a/app/controllers/admin/workgroups_controller.rb +++ b/app/controllers/admin/workgroups_controller.rb @@ -1,5 +1,4 @@ -class Admin::WorkgroupsController < ApplicationController - before_filter :authenticate_admin +class Admin::WorkgroupsController < Admin::BaseController def index if (params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ed15383a..06307475 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,6 @@ class ApplicationController < ActionController::Base + filter_parameter_logging :password, :password_confirmation # do not log passwort parameters before_filter :select_foodcoop, :authenticate, :store_controller after_filter :remove_controller diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb index 46a11267..e3388311 100644 --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -1,6 +1,5 @@ class LoginController < ApplicationController skip_before_filter :authenticate # no authentication since this is the login page - filter_parameter_logging "password" # do not log "password" parameter before_filter :validate_token, :only => [:password, :update_password] verify :method => :post, :only => [:login, :reset_password, :new], :redirect_to => { :action => :index } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index baa8b1a3..5ea4120c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -117,7 +117,7 @@ module ApplicationHelper end def tab_is_active?(tab) - tab[:active].detect {|c| c == controller.controller_path } + tab[:active].detect {|c| controller.controller_path.match(c) } end def icon(name, options={}) diff --git a/app/views/admin/index.html.haml b/app/views/admin/base/index.html.haml similarity index 100% rename from app/views/admin/index.html.haml rename to app/views/admin/base/index.html.haml diff --git a/app/views/layouts/_main_tabnav.html.erb b/app/views/layouts/_main_tabnav.html.erb index e0c41b55..1babc0d6 100644 --- a/app/views/layouts/_main_tabnav.html.erb +++ b/app/views/layouts/_main_tabnav.html.erb @@ -52,7 +52,7 @@ ] }, { :name => "Administration", :url => admin_root_path, - :active => ["admin", "admin/users", "admin/ordergroups", "admin/workgroups"], + :active => ["admin/"], :access_denied? => (!u.role_admin?), :subnav => [ { :name => "Benutzerinnen", :url => admin_users_path }, @@ -77,4 +77,4 @@ <% end -%> <% end -%> - \ No newline at end of file + diff --git a/config/routes.rb b/config/routes.rb index 1e8dc850..38c3ef7f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -68,7 +68,7 @@ ActionController::Routing::Routes.draw do |map| # Administration map.namespace :admin do |admin| - admin.root :controller => "admin", :action => "index" + admin.root :controller => "base", :action => "index" admin.resources :users admin.resources :workgroups, :member => { :memberships => :get } admin.resources :ordergroups, :member => { :memberships => :get } From d6ee00db17f2ce6472c08b6c23b6e2ff06964208 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Mon, 22 Mar 2010 03:11:32 +0100 Subject: [PATCH 8/9] Fixed hard coded links in wiki pages. --- app/helpers/pages_helper.rb | 4 ++-- lib/wikilink.rb | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/app/helpers/pages_helper.rb b/app/helpers/pages_helper.rb index 548e6a90..b98e7a8a 100644 --- a/app/helpers/pages_helper.rb +++ b/app/helpers/pages_helper.rb @@ -7,9 +7,9 @@ module PagesHelper def link_to_wikipage(page, text = nil) if text == nil - link_to page.title, "/wiki/#{page.title}" + link_to page.title, wiki_page_path(page.permalink) else - link_to text, "/wiki/#{page.title}" + link_to text, wiki_page_path(page.permalink) end end diff --git a/lib/wikilink.rb b/lib/wikilink.rb index bdbff519..6512c3fb 100644 --- a/lib/wikilink.rb +++ b/lib/wikilink.rb @@ -1,23 +1,21 @@ class Wikilink < WikiCloth::WikiLinkHandler - - def url_for(page, parent = nil) - if parent - "/pages/new?title=#{page}&parent=#{parent}" - else - "/wiki/#{page}" - end - end - + include ActionController::UrlWriter # To use named routes + def link_attributes_for(page) permalink = Page.permalink(page) + url_options = {:host => Foodsoft.config[:host], :protocol => Foodsoft.config[:protocol]} + url_options.merge!({:port => Foodsoft.config[:port]}) if Foodsoft.config[:port] + if Page.exists?(:permalink => permalink) - { :href => url_for(permalink) } + { :href => url_for(url_options.merge({:controller => "pages", :action => "show", + :permalink => permalink, :use_route => :wiki_page})) } else - { :href => url_for(page, params[:referer]), :class => "new_wiki_link"} + { :href => url_for(url_options.merge({:controller => "pages", :action => "new", + :title => page, :parent => params[:referer]})), :class => "new_wiki_link"} end end def section_link(section) "" end -end \ No newline at end of file +end From fc823bb6d6b4b9bd16de1445851ac3f6c1a07559 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 28 Mar 2010 16:56:24 +0200 Subject: [PATCH 9/9] Fixed wrong my-tasks-path on home page. --- app/views/home/_start_nav.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/home/_start_nav.haml b/app/views/home/_start_nav.haml index f6dc65b7..e9e031b7 100644 --- a/app/views/home/_start_nav.haml +++ b/app/views/home/_start_nav.haml @@ -4,7 +4,7 @@ Foodcoop %ul %li= link_to "Mitglieder", foodcoop_users_path - %li= link_to "Meine Aufgaben", :controller => "home", :action => "tasks" + %li= link_to "Meine Aufgaben", user_tasks_path %li= link_to "Nachricht schreiben", :controller => "messages", :action => "new" // Orders