From 28ec047f1d16c2dc26b2d506298ca3ae7019e800 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Thu, 25 Oct 2012 16:11:49 +0200 Subject: [PATCH] using authentication for app --- app/assets/javascripts/user/application.js | 90 ++++++--- app/assets/javascripts/user/quser.js.coffee | 10 +- app/controllers/application_controller.rb | 5 + app/controllers/user_controller.rb | 10 + app/views/layouts/application.html.slim | 8 +- app/views/layouts/phone.html.slim | 9 +- app/views/layouts/tablet.html.slim | 8 +- app/views/suppliers/lists/show.html.slim | 4 + app/views/user/obtain_token.html.slim | 4 + config/couchdb.yml | 2 +- config/locales/nl.yml | 116 ++++++++++++ config/locales/svenfuchs.nl.yml | 199 ++++++++++++++++++++ config/routes.rb | 2 + script/build_mobile_app.rb | 2 +- 14 files changed, 429 insertions(+), 40 deletions(-) create mode 100644 app/views/user/obtain_token.html.slim create mode 100644 config/locales/nl.yml create mode 100644 config/locales/svenfuchs.nl.yml diff --git a/app/assets/javascripts/user/application.js b/app/assets/javascripts/user/application.js index 957aa310..a2e790d8 100644 --- a/app/assets/javascripts/user/application.js +++ b/app/assets/javascripts/user/application.js @@ -35,32 +35,69 @@ var path_mapping = { list_products_for_table: '/user/list_products_for_table', list_products: '/user/list_products', active_list: '/user/active_list', - history_list: '/user/history_list' + history_list: '/user/history_list', + obtain_user_token: '/user/obtain_token' } var translations = { - confirmations: { - move_to_another_table_title: 'Move to another table?', - move_to_another_table: 'Are you sure you want to move to another table?' + en: { + confirmations: { + }, + list_needs_help: { + help_is_on_its_way: 'Help is already on its way', + title: 'Request a waiter', + content: 'Request a waiter to your table' + }, + list_needs_payment: { + payment_already_requested: 'You already asked for the check', + title: 'Ask for the check', + content: 'Do you want to pay?' + }, + selected_products: { + order: 'Order', + clear: 'Clear' + }, + join_request: { + title: 'Join request', + body: '%{email} wants to join the table', + reject: 'Reject', + approve: 'Approve' + }, + move_table: { + cannot_move_to_occupied_table: 'You cannot move to an occupied table', + moved_to_another_table: 'The table is changed.', + confirmation_title: 'Move to another table?', + confirmation_body: 'Are you sure you want to move to another table?' + } }, - list_needs_help: { - help_is_on_its_way: 'Help is already on its way', - title: 'Request a waiter', - content: 'Request a waiter to your table' - }, - list_needs_payment: { - payment_already_requested: 'You already asked for the check', - title: 'Ask for the check', - content: 'Do you want to pay?' - }, - selected_products: { - order: 'Order', - clear: 'Clear' - }, - join_request: { - title: 'Join request', - body: '%{email} wants to join the table', - reject: 'Reject', - approve: 'Approve' + nl: { + confirmations: { + }, + list_needs_help: { + help_is_on_its_way: 'Er wordt al iemand naar je tafel gestuurd', + title: 'Ik heb een vraag', + content: 'Wil je een vraag stellen?' + }, + list_needs_payment: { + payment_already_requested: 'De rekening is reeds gevraagd', + title: 'Vraag om de rekening', + content: 'Wil je betalen?' + }, + selected_products: { + order: 'Bestellen', + clear: 'Leegmaken' + }, + join_request: { + title: 'Lijst deling', + body: '%{email} wil ook op jouw lijst bestellen', + reject: 'Afwijzen', + approve: 'Toestaan' + }, + move_table: { + cannot_move_to_occupied_table: 'Je kan niet verhuizen naar een tafel die reeds gebruikt wordt.', + moved_to_another_table: 'De tafel is gewijzigd.', + confirmation_title: 'Naar een andere tafel verhuizen?', + confirmation_body: 'Ben je aan een andere tafel gaan zitten?' + } } } function redirect_to(mapping, variables){ @@ -90,3 +127,10 @@ function t(path, vars){ $.each(vars, function(v, value){ result = result.replace('%{'+v+'}', value)}); return result; } +$.ajaxSetup({ + error: function(xhr, ajaxOptions, error){ + if(xhr.status == 401 || xhr.status == 0){ + window.location = data_host + '/user/obtain_token.html'; + } + } +}) diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index 23580b47..c2abe4b6 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -304,19 +304,19 @@ window.Quser= else if res.supplier_closed redirect_to 'user_root', {message: 'supplier_is_closed'} else - #TODO Offer to move table + ## Offer to move table Qwaiter.confirm( ok: -> $.post(data_host + '/user/move_table', $.extend({table_id: table.table_id}, authentication_object), (res2)-> if res2.occupied - alert('Cannot move to occupied table') + redirect_to 'user_root', {message: 'move_table.cannot_move_to_occupied_tabe'} else - redirect_to 'list_products', {message: 'moved_to_another_table'} + redirect_to 'list_products', {message: 'move_table.moved_to_another_table'} ) cancel: -> redirect_to 'list_products' - title: t('confirmations.move_to_another_table_title') - content: t('confirmations.move_to_another_table') + title: t('move_table.confirmation_title') + content: t('move_table.confirmation_body') ) else if res.occupied diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a1c4e238..85013d06 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,5 @@ class ApplicationController < ActionController::Base + before_filter :set_locale layout :layout_by_resource @@ -6,6 +7,10 @@ class ApplicationController < ActionController::Base private + def set_locale + I18n.locale = :nl + end + def layout_by_resource if devise_controller? "phone" diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index aad400b6..7b4e81d2 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -2,6 +2,15 @@ class UserController < ApplicationController before_filter :allow_mobile, :authenticate_user! layout 'phone' + def authenticate_user_wrapper! + authenticate_user! unless request.format.html? + authenticate_user! if params[:action] == 'obtain_token' + end + + def obtain_token + + end + alias :list :active_list def allow_mobile @@ -153,6 +162,7 @@ class UserController < ApplicationController redirect_to(root_path, alert: t('messages.there_is_no_list_active')) and return unless list.present? end format.json do + render json: js_alert(t('messages.the_list_has_been_closed')) and return unless list.present? render json: list.with_orders_and_join_requests_as_json.merge(supplier_name: list.supplier.name) end end diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 016a1384..c869959e 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -11,10 +11,10 @@ html lang="en" /[if lt IE 9] = javascript_include_tag "http://html5shim.googlecode.com/svn/trunk/html5.js" = stylesheet_link_tag "application", :media => "all" - link href="images/apple-touch-icon-144x144.png" rel="apple-touch-icon-precomposed" sizes="144x144" - link href="images/apple-touch-icon-114x114.png" rel="apple-touch-icon-precomposed" sizes="114x114" - link href="images/apple-touch-icon-72x72.png" rel="apple-touch-icon-precomposed" sizes="72x72" - link href="images/apple-touch-icon.png" rel="apple-touch-icon-precomposed" + link href="/images/apple-touch-icon-144x144.png" rel="apple-touch-icon-precomposed" sizes="144x144" + link href="/images/apple-touch-icon-114x114.png" rel="apple-touch-icon-precomposed" sizes="114x114" + link href="/images/apple-touch-icon-72x72.png" rel="apple-touch-icon-precomposed" sizes="72x72" + link href="/images/apple-touch-icon.png" rel="apple-touch-icon-precomposed" link href="/favicon.ico" rel="shortcut icon" body diff --git a/app/views/layouts/phone.html.slim b/app/views/layouts/phone.html.slim index 7a388872..672ab03d 100644 --- a/app/views/layouts/phone.html.slim +++ b/app/views/layouts/phone.html.slim @@ -18,6 +18,9 @@ html lang="en" link href="/favicon.ico" rel="shortcut icon" javascript: var data_host = 'http://data.qwaiter.com'; + //var data_host = 'http://localhost:3000'; + //var data_host = 'http://192.168.1.240:3000'; + var $locale = '#{I18n.locale}'; // Dummy holder when Qmobile object is not supplied by the mobile phone var QMobile, Qwaiter, Quser; QMobile || (QMobile = { @@ -30,9 +33,11 @@ html lang="en" this.authentication_string_storage = 'auth_token='+token; this.authentication_object_storage = '{"auth_token": "'+token+'"}' }, - root_url: function(){return ''} + root_url: function(){return 'file:///Users/bterkuile/Documents/workspace/Qwaiter/assets'}, + root_url: function(){return ''}, + goHome: function(){ window.location = QMobile.root_url() + path_mapping['user_root'] + '.html'} }); - QMobile.setAuthToken('i5brDZ1HS1okoEq3pMyh'); + //QMobile.setAuthToken('i5brDZ1HS1okoEq3pMyh'); body class=action_name .navbar.navbar-fixed-top diff --git a/app/views/layouts/tablet.html.slim b/app/views/layouts/tablet.html.slim index a9a11778..c30a78f8 100644 --- a/app/views/layouts/tablet.html.slim +++ b/app/views/layouts/tablet.html.slim @@ -12,10 +12,10 @@ html lang="en" = javascript_include_tag "http://html5shim.googlecode.com/svn/trunk/html5.js" = stylesheet_link_tag "supplier/application", :media => "all" = stylesheet_link_tag "supplier/application", :media => "all" - link href="images/apple-touch-icon-144x144.png" rel="apple-touch-icon-precomposed" sizes="144x144" - link href="images/apple-touch-icon-114x114.png" rel="apple-touch-icon-precomposed" sizes="114x114" - link href="images/apple-touch-icon-72x72.png" rel="apple-touch-icon-precomposed" sizes="72x72" - link href="images/apple-touch-icon.png" rel="apple-touch-icon-precomposed" + link href="/images/apple-touch-icon-144x144.png" rel="apple-touch-icon-precomposed" sizes="144x144" + link href="/images/apple-touch-icon-114x114.png" rel="apple-touch-icon-precomposed" sizes="114x114" + link href="/images/apple-touch-icon-72x72.png" rel="apple-touch-icon-precomposed" sizes="72x72" + link href="/images/apple-touch-icon.png" rel="apple-touch-icon-precomposed" link href="/favicon.ico" rel="shortcut icon" body diff --git a/app/views/suppliers/lists/show.html.slim b/app/views/suppliers/lists/show.html.slim index 30c041c0..97ea9c70 100644 --- a/app/views/suppliers/lists/show.html.slim +++ b/app/views/suppliers/lists/show.html.slim @@ -12,6 +12,10 @@ dl.dl-horizontal tr td colspan=2 = slider_image tfoot +.form-actions + = link_to t("helpers.links.back"), suppliers_lists_path(date: @list.created_at.strftime('%Y-%m-%d')), class: 'btn' + ' + = link_to t('helpers.links.edit'), [:edit, :suppliers, @list], class: 'btn btn-info' - content_for :footer do javascript: jQuery(function(){ diff --git a/app/views/user/obtain_token.html.slim b/app/views/user/obtain_token.html.slim new file mode 100644 index 00000000..73581e42 --- /dev/null +++ b/app/views/user/obtain_token.html.slim @@ -0,0 +1,4 @@ +- content_for :footer do + javascript: + QMobile.setAuthToken('#{current_user.authentication_token}'); + QMobile.goHome(); diff --git a/config/couchdb.yml b/config/couchdb.yml index 413d31be..69368497 100644 --- a/config/couchdb.yml +++ b/config/couchdb.yml @@ -6,4 +6,4 @@ test: database: qrammer_test production: validation_framework: :active_model #optional - database: qrammer + database: qwaiter diff --git a/config/locales/nl.yml b/config/locales/nl.yml new file mode 100644 index 00000000..8225d285 --- /dev/null +++ b/config/locales/nl.yml @@ -0,0 +1,116 @@ +# Sample localization file for English. Add more files in this directory for other locales. +# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. +nl: + helpers: + links: + are_you_sure: 'Weet je dit zeker?' + place_order: Bestellen + show_active_list: Toon %{list} + forms: + errors: + title: Er zijn een problemen opgetreden (%{count}) + messages: + cannot_order_on_non_active_list: Je kan niet bestellen op een gesloten lijst + order_is_placed: Je bestelling is in goede orde aangekomen + the_list_has_been_closed: De %{list} is afgesloten + illegal_history_list_attempt: Je probeert een lijst op te vragen die niet van jou is + table_is_occupied: De tafel waar je aan wil gaan zitten is reeds bezet + table_is_reserved: De tafel waar je aan wil gaan zitten is gereserveerd + table_is_closed: De tafel waar je aan wil gaan zitten is niet beschikbaar voor bediening + supplier_is_closed: De eigenaar van deze tafel is momenteel gesloten + join_request_rejected: Je verzoek om te mogen bestellen op een bestaande lijst is afgewezen + table_is_from_other_supplier: Je kan geen lijst openen bij een andere zaak zolang je huidige %{list} nog niet is afgesloten + moved_to_another_table: De tafel is gewijzigd + cannot_identify_table: De applicatie kan niet bepalen om welke tafel het gaat + action: + index: + label: '%{models} overzicht' + new: + label: '%{model} toevoegen' + show: + label: Toon %{model} + edit: + label: Bewerk %{model} + create: + successfull: '%{model} is succesvol aangemaakt' + update: + successfull: '%{model} is succesvol aangepast' + destroy: + successfull: '%{model} is succesvol verwijderd' + table: + is_occupied: Deze tafel is bezet + has_no_section: "Niet geplaatst" + activemodel: + models: + user: Gebruiker + supplier: Restaurant + table: Tafel + list: Lijst + product: Product + order: Bestelling + product_category: Product categorie + section: Afdeling + plural: + user: Gebruikers + supplier: Restaurants + table: Tafels + list: Lijsten + product: Producten + order: Bestellingen + product_category: Product categorieen + section: Afdelingen + attributes: + product: + price: Prijs + list: + created_at: Aangemaakt + supplier: + menu: + active_lists: Actieve %{lists} + active_lists: + title: Actieve %{lists} + price: Prijs + active_orders: + title: Actieve %{orders} + price: Prijs + close: De zaak afsluiten voor bestellingen + you_are_currently_closed_alert: 'Je bent momenteel gesloten en kan geen orders ontvangen' + mark_as_open_button: 'Open de zaak!' + table_number: Tafel + tables: + qr_codes: + link: Qr codes + lists: + show: + title: "%{list} tonen" + + user: + active_list: + title: Actieve %{list} + needs_payment: Rekening vragen! + history_list: + title: Afgesloten %{list} + show_products: + # The title gets products: Product.model_name.human_plural that can be used: e.g.: Showing %{products} + title: Menu + join_occupied_table: + title: Deze tafel is bezet + join_this_table: Ik wil ook bestellen bij deze tafel + show_the_products: Laat me het menu zien + back: Terug + home: + scan_qr: Scan qr code + show_active_list: Toon mijn bestellingen + show_active_list_products: Ga naar het menu + section: + first_section_title: Ruimte + manage_tables: + title: "Tafels beheren voor %{section}: %{title}" + general: + boolean: + boolean_yes: "Ja" + boolean_no: "Nee" +# FOLLOWING ARE BACKED BY DATA ATTRIBUTES + selected_products: + clear: Leegmaken + order: Bestellen diff --git a/config/locales/svenfuchs.nl.yml b/config/locales/svenfuchs.nl.yml new file mode 100644 index 00000000..c7591151 --- /dev/null +++ b/config/locales/svenfuchs.nl.yml @@ -0,0 +1,199 @@ +nl: + date: + abbr_day_names: + - zon + - maa + - din + - woe + - don + - vri + - zat + abbr_month_names: + - + - jan + - feb + - mar + - apr + - mei + - jun + - jul + - aug + - sep + - okt + - nov + - dec + day_names: + - zondag + - maandag + - dinsdag + - woensdag + - donderdag + - vrijdag + - zaterdag + formats: + default: ! '%d/%m/%Y' + long: ! '%e %B %Y' + short: ! '%e %b' + month_names: + - + - januari + - februari + - maart + - april + - mei + - juni + - juli + - augustus + - september + - oktober + - november + - december + order: + - :day + - :month + - :year + datetime: + distance_in_words: + about_x_hours: + one: ongeveer een uur + other: ongeveer %{count} uur + about_x_months: + one: ongeveer een maand + other: ongeveer %{count} maanden + about_x_years: + one: ongeveer een jaar + other: ongeveer %{count} jaar + almost_x_years: + one: bijna een jaar + other: bijna %{count} jaar + half_a_minute: een halve minuut + less_than_x_minutes: + one: minder dan een minuut + other: minder dan %{count} minuten + less_than_x_seconds: + one: minder dan een seconde + other: minder dan %{count} seconden + over_x_years: + one: meer dan een jaar + other: meer dan %{count} jaar + x_days: + one: 1 dag + other: ! '%{count} dagen' + x_minutes: + one: 1 minuut + other: ! '%{count} minuten' + x_months: + one: 1 maand + other: ! '%{count} maanden' + x_seconds: + one: 1 seconde + other: ! '%{count} seconden' + prompts: + day: dag + hour: uur + minute: minuut + month: maand + second: seconde + year: jaar + errors: &errors + format: ! '%{attribute} %{message}' + messages: + accepted: moet worden geaccepteerd + blank: moet opgegeven zijn + confirmation: komt niet met de bevestiging overeen + empty: moet opgegeven zijn + equal_to: moet gelijk zijn aan %{count} + even: moet even zijn + exclusion: is niet beschikbaar + greater_than: moet groter zijn dan %{count} + greater_than_or_equal_to: moet groter dan of gelijk zijn aan %{count} + inclusion: is niet in de lijst opgenomen + invalid: is ongeldig + less_than: moet minder zijn dan %{count} + less_than_or_equal_to: moet minder dan of gelijk zijn aan %{count} + not_a_number: is geen getal + not_an_integer: moet een geheel getal zijn + odd: moet oneven zijn + record_invalid: ! 'Validatie mislukt: %{errors}' + taken: is al in gebruik + too_long: is te lang (maximaal %{count} tekens) + too_short: is te kort (minimaal %{count} tekens) + wrong_length: heeft onjuiste lengte (moet %{count} tekens lang zijn) + template: + body: ! 'Controleer de volgende velden:' + header: + one: ! '%{model} niet opgeslagen: 1 fout gevonden' + other: ! '%{model} niet opgeslagen: %{count} fouten gevonden' + helpers: + select: + prompt: Selecteer + submit: + create: ! '%{model} toevoegen' + submit: ! '%{model} opslaan' + update: ! '%{model} bewaren' + number: + currency: + format: + delimiter: . + format: ! '%u%n' + precision: 2 + separator: ! ',' + significant: false + strip_insignificant_zeros: false + unit: € + format: + delimiter: . + precision: 2 + separator: ! ',' + significant: false + strip_insignificant_zeros: false + human: + decimal_units: + format: ! '%n %u' + units: + billion: miljard + million: miljoen + quadrillion: biljard + thousand: duizend + trillion: biljoen + unit: '' + format: + delimiter: '' + precision: 3 + significant: true + strip_insignificant_zeros: true + storage_units: + format: ! '%n %u' + units: + byte: + one: Byte + other: Bytes + gb: GB + kb: KB + mb: MB + tb: TB + percentage: + format: + delimiter: '' + precision: + format: + delimiter: '' + support: + array: + last_word_connector: ! ' en ' + two_words_connector: ! ' en ' + words_connector: ! ', ' + time: + am: ! '''s ochtends' + formats: + default: ! '%a %d %b %Y %H:%M:%S %Z' + long: ! '%d %B %Y %H:%M' + short: ! '%d %b %H:%M' + pm: ! '''s middags' + # remove these aliases after 'activemodel' and 'activerecord' namespaces are removed from Rails repository + activemodel: + errors: + <<: *errors + activerecord: + errors: + <<: *errors diff --git a/config/routes.rb b/config/routes.rb index bfe684d4..b8dbce0b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -57,6 +57,8 @@ Qrammer::Application.routes.draw do post '/user/reject_join_request' => 'user#reject_join_request' post '/user/approve_join_request' => 'user#approve_join_request' post '/user/check_table_join_status' => 'user#check_table_join_status' + get '/user/obtain_token' => 'user#obtain_token', as: :user_obtain_token + #match '/show_products' => 'dashboard#show_products', as: :user_products diff --git a/script/build_mobile_app.rb b/script/build_mobile_app.rb index e55486ff..a9697c94 100644 --- a/script/build_mobile_app.rb +++ b/script/build_mobile_app.rb @@ -8,7 +8,7 @@ root_url = 'file:///android_asset' mkdir_p File.join(android_root, 'user') rm_rf File.join(android_root, 'assets') Dir.glob(File.join('public', 'assets', '**', '*.gz')).each{|f| rm_f f} -move Rails.root.join('public', 'assets'), File.join(android_root, 'assets') +move Rails.root.join('public', 'assets'), File.join(android_root, 'assets') if File.directory?(Rails.root.join('public', 'assets')) for css in Dir.glob(File.join(android_root, "**", "*.css")) contents = File.read(css) contents.gsub!(/url\("/, %|url("#{root_url}|)