From 286c16c3e1db5ea7c0d773be91fb864784a607d7 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 11 Apr 2012 08:37:54 +0200 Subject: [PATCH] Code cleanup. --- .../app/controllers/navigation.js.coffee | 9 +- .../app/controllers/signup.js.coffee | 9 +- app/controllers/application_controller.rb | 152 ++++++++++-------- app/controllers/sessions_controller.rb | 106 ++++++------ db/migrate/20120101000020_create_network.rb | 23 ++- 5 files changed, 139 insertions(+), 160 deletions(-) diff --git a/app/assets/javascripts/app/controllers/navigation.js.coffee b/app/assets/javascripts/app/controllers/navigation.js.coffee index c877725be..cb05e8e26 100644 --- a/app/assets/javascripts/app/controllers/navigation.js.coffee +++ b/app/assets/javascripts/app/controllers/navigation.js.coffee @@ -1,10 +1,4 @@ $ = jQuery.sub() -Note = App.Note - -$.fn.item = -> - elementID = $(@).data('id') - elementID or= $(@).parents('[data-id]').data('id') - Note.find(elementID) class App.Navigation extends Spine.Controller events: @@ -21,13 +15,14 @@ class App.Navigation extends Spine.Controller Spine.bind 'navrebuild', (user) => @log 'navbarrebuild', user @render(user) + @delay( @sync, 1600 ) Spine.bind 'navupdate_remote', (user) => @log 'navupdate_remote' @sync # rerender if new overview data is there - @delay( @sync, 1800 ) + @delay( @sync, 1600 ) render: (user) -> # @log 'nav render', Config.NavBar diff --git a/app/assets/javascripts/app/controllers/signup.js.coffee b/app/assets/javascripts/app/controllers/signup.js.coffee index 805ba14cd..f66b7d0dc 100644 --- a/app/assets/javascripts/app/controllers/signup.js.coffee +++ b/app/assets/javascripts/app/controllers/signup.js.coffee @@ -14,11 +14,9 @@ class Index extends App.Controller # set title @title 'Sign up' - - @render() - @navupdate '#signup' + @render() render: -> @@ -108,8 +106,3 @@ class Index extends App.Controller ) Config.Routes['signup'] = Index - -#class App.SignUp extends App.Router -# routes: -# 'signup': Index -#Config.Controller.push App.SignUp \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2a3e06c6c..01fec8bdd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,65 +3,11 @@ class ApplicationController < ActionController::Base protect_from_forgery # http_basic_authenticate_with :name => "test", :password => "ttt" - helper_method :current_user, :authentication_check + helper_method :current_user, :authentication_check, :config_frontend, :user_data_full before_filter :set_user, :cors_preflight_check after_filter :set_access_control_headers - def user_data_full (user_id) - - # get user - user = User.find(user_id) - - # get linked accounts - user['accounts'] = {} - authorizations = user.authorizations() || [] - authorizations.each do | authorization | - user['accounts'][authorization.provider] = { - :uid => authorization[:uid], - :username => authorization[:username] - } - end - - # do not show password - user['password'] = '' - - # show linked topics and items - user['links'] = [] - ticket_state_list_open = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['new','open', 'pending remidner', 'pending action']) ) - ticket_state_list_closed = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['closed'] ) ) - - tickets_open = Ticket.where(:customer_id => user_id, :ticket_state_id => ticket_state_list_open).count() - tickets_closed = Ticket.where(:customer_id => user_id, :ticket_state_id => ticket_state_list_closed).count() - - topic = { - :title => 'Tickets', - :items => [ - { - :url => '', - :name => 'open (' + tickets_open.to_s + ')', - :title => 'Open Tickets', - :class => 'user-tickets', - :data => 'open' - }, - { - :url => '', - :name => 'closed (' + tickets_closed.to_s + ')', - :title => 'Closed Tickets', - :class => 'user-tickets', - :data => 'closed' - } - ] - } - user['links'].push topic - - # set roles - user['roles'] = user.roles.select('id, name').where(:active => true) - user['groups'] = user.groups.select('id, name').where(:active => true) - user['organization'] = user.organization - user['organizations'] = user.organizations.select('id, name').where(:active => true) - return user - end # For all responses in this controller, return the CORS access control headers. def set_access_control_headers @@ -121,15 +67,15 @@ class ApplicationController < ActionController::Base end end - # message = 'login required' if message != '' - respond_to do |format| - format.json { - render :json => { :error => message }, :status => :unauthorized - } - end + render( + :json => { + :error => message, + }, + :status => :unauthorized + ) end - return true + return false end # logger.debug 'session check' @@ -137,17 +83,16 @@ class ApplicationController < ActionController::Base # session[:user_id] = 2 if !session[:user_id] logger.debug '!session user_id' -# logger.debug session.inspect message = 'no valid session, user_id' - respond_to do |format| - format.json { - render :json => { :error => message }, :status => :unauthorized - } - end - return true + render( + :json => { + :error => message, + }, + :status => :unauthorized + ) + return false end - # check session auth # return 1231 # request_http_basic_authentication return false @@ -178,7 +123,72 @@ class ApplicationController < ActionController::Base :history_type_id => history_type.id, :history_object_id => history_object.id, :created_by_id => session[:user_id] - ) + ) + end + + def config_frontend + + # config + config = {} + Setting.where( :frontend => true ).each { |setting| + config[setting.name] = setting.state[:value] + } + return config + end + + def user_data_full (user_id) + + # get user + user = User.find(user_id) + + # get linked accounts + user['accounts'] = {} + authorizations = user.authorizations() || [] + authorizations.each do | authorization | + user['accounts'][authorization.provider] = { + :uid => authorization[:uid], + :username => authorization[:username] + } + end + + # do not show password + user['password'] = '' + + # show linked topics and items + user['links'] = [] + ticket_state_list_open = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['new','open', 'pending remidner', 'pending action']) ) + ticket_state_list_closed = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['closed'] ) ) + + tickets_open = Ticket.where(:customer_id => user_id, :ticket_state_id => ticket_state_list_open).count() + tickets_closed = Ticket.where(:customer_id => user_id, :ticket_state_id => ticket_state_list_closed).count() + + topic = { + :title => 'Tickets', + :items => [ + { + :url => '', + :name => 'open (' + tickets_open.to_s + ')', + :title => 'Open Tickets', + :class => 'user-tickets', + :data => 'open' + }, + { + :url => '', + :name => 'closed (' + tickets_closed.to_s + ')', + :title => 'Closed Tickets', + :class => 'user-tickets', + :data => 'closed' + } + ] + } + user['links'].push topic + + # set roles + user['roles'] = user.roles.select('id, name').where(:active => true) + user['groups'] = user.groups.select('id, name').where(:active => true) + user['organization'] = user.organization + user['organizations'] = user.organizations.select('id, name').where(:active => true) + return user end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 825a43a91..ec3fe5bfa 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -6,81 +6,65 @@ class SessionsController < ApplicationController # "Create" a login, aka "log the user in" def create logger.debug 'session create' - logger.debug params.inspect - logger.debug 'b auth' - user = User.authenticate(params[:username], params[:password]) - logger.debug 'a auth' -# user = User.authenticate('hansi', 'test') +# logger.debug params.inspect + user = User.authenticate( params[:username], params[:password] ) - respond_to do |format| - if user - - # do not show password - user['password'] = '' - - user['roles'] = user.roles.select('id, name').where(:active => true) - user['groups'] = user.groups.select('id, name').where(:active => true) - user['organization'] = user.organization - user['organizations'] = user.organizations.select('id, name').where(:active => true) - - # auto population of default collections - default_collection = default_collections() - - session[:user_id] = user.id - format.json { render :json => { :session => user, :default_collections => default_collection }, :status => :created } - else - format.json { render :json => { :error => 'login failed' }, :status => :unprocessable_entity } - end + # auth failed + if !user + render :json => { :error => 'login failed' }, :status => :unprocessable_entity end + + # do not show password + user['password'] = '' + + user['roles'] = user.roles.select('id, name').where(:active => true) + user['groups'] = user.groups.select('id, name').where(:active => true) + user['organization'] = user.organization + user['organizations'] = user.organizations.select('id, name').where(:active => true) + + # auto population of default collections + default_collection = default_collections() + + # set session user_id + session[:user_id] = user.id + + # return new session data + render :json => { :session => user, :default_collections => default_collection }, :status => :created end def show - logger.debug 'session show' - logger.debug params.inspect -# user = User.authenticate('hansi', 'test') - respond_to do |format| - - # config - config = {} - Setting.where( :frontend => true ).each { |setting| - config[setting.name] = setting.state[:value] - } - - # no valid sessions - if !session[:user_id] - render :json => { - :error => 'no valid session', - :config => config, - } - return - end - - # Save the user ID in the session so it can be used in - # subsequent requests - user = user_data_full( session[:user_id] ) - - # auto population of default collections - default_collection = default_collections() - - #, :status => :created - format.json { - render :json => { - :session => user, - :default_collections => default_collection, - :config => config, - } + # no valid sessions + if !session[:user_id] + render :json => { + :error => 'no valid session', + :config => config_frontend, } + return end + + # Save the user ID in the session so it can be used in + # subsequent requests + user = user_data_full( session[:user_id] ) + + # auto population of default collections + default_collection = default_collections() + + # return current session + render :json => { + :session => user, + :default_collections => default_collection, + :config => config_frontend, + } end # "Delete" a login, aka "log the user out" def destroy + # Remove the user id from the session @_current_user = session[:user_id] = nil - respond_to do |format| - format.json { render :json => { } } - end + + render :json => { } end def create_omniauth diff --git a/db/migrate/20120101000020_create_network.rb b/db/migrate/20120101000020_create_network.rb index 62ca22ece..f05c7943e 100644 --- a/db/migrate/20120101000020_create_network.rb +++ b/db/migrate/20120101000020_create_network.rb @@ -24,9 +24,9 @@ class CreateNetwork < ActiveRecord::Migration add_index :network_privacies, [:name], :unique => true create_table :network_categories do |t| - t.references :network_category_type, :null => false - t.references :network_privacy, :null => false - t.references :network, :null => false + t.references :network_category_type, :null => false + t.references :network_privacy, :null => false + t.references :network, :null => false t.column :name, :string, :limit => 200, :null => false t.column :note, :string, :limit => 250, :null => true t.column :allow_comments, :boolean, :null => false, :default => true @@ -41,7 +41,7 @@ class CreateNetwork < ActiveRecord::Migration end create_table :network_items do |t| - t.references :network_category, :null => false + t.references :network_category, :null => false t.column :title, :string, :limit => 200, :null => false t.column :body, :string, :limit => 25000, :null => false t.column :created_by_id, :integer, :null => false @@ -50,36 +50,33 @@ class CreateNetwork < ActiveRecord::Migration add_index :network_items, [:network_category_id] create_table :network_item_comments do |t| - t.references :network_item, :null => false + t.references :network_item, :null => false t.column :body, :string, :limit => 25000, :null => false t.column :created_by_id, :integer, :null => false t.timestamps end add_index :network_item_comments, [:network_item_id] - create_table :network_item_plus do |t| - t.references :network_item, :null => false + t.references :network_item, :null => false t.column :created_by_id, :integer, :null => false t.timestamps end add_index :network_item_plus, [:network_item_id, :created_by_id], :unique => true create_table :network_category_subscriptions do |t| - t.references :network_categories, :null => false - t.column :created_by_id, :integer, :null => false + t.references :network_categories, :null => false + t.column :created_by_id, :integer, :null => false t.timestamps end add_index :network_category_subscriptions, [:network_categories_id, :created_by_id], :unique => true, :name => 'index_network_category_subscriptions_on_network_c_i_and_c' - create_table :network_item_subscriptions do |t| - t.references :network_item, :null => false - t.column :created_by_id, :integer, :null => false + t.references :network_item, :null => false + t.column :created_by_id, :integer, :null => false t.timestamps end add_index :network_item_subscriptions, [:network_item_id, :created_by_id], :unique => true, :name => 'index_network_item_subscriptions_on_item_id_and_created_by_id' - end