From 2028a564b10ec2bd6fc765a8e23e628c548235d9 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 21 Sep 2014 14:50:39 +0200 Subject: [PATCH] Refactoring of App.Session class. --- .../_application_controller.js.coffee | 6 ++--- .../app/controllers/_default_navbar.js.coffee | 3 ++- .../_profile/linked_accounts.js.coffee | 2 +- .../controllers/agent_ticket_create.js.coffee | 4 +-- .../customer_ticket_create.js.coffee | 2 +- .../app/controllers/login.js.coffee | 2 +- .../app/controllers/navigation.js.coffee | 6 ++++- .../app/controllers/taskbar_widget.js.coffee | 2 +- .../app/controllers/ticket_overview.js.coffee | 2 +- .../app/controllers/ticket_zoom.js.coffee | 6 ++--- .../app/controllers/widget/chat.js.coffee | 2 +- .../widget/online_notification.js.coffee | 6 ++--- .../widget/switch_back_to_user.js.coffee | 2 +- .../app/lib/app_init/session.js.coffee | 25 +++++++------------ .../app/lib/app_post/auth.js.coffee | 5 ++-- .../app/lib/app_post/websocket.js.coffee | 2 +- lib/sessions.rb | 12 +++++---- 17 files changed, 43 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index d80a9ec9c..0b3762346 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -185,8 +185,8 @@ class App.Controller extends Spine.Controller authenticate: -> - # return rtue if session exists - return true if @Session.get( 'id' ) + # return true if session exists + return true if @Session.get() # remember requested url @Config.set( 'requested_url', window.location.hash ) @@ -417,7 +417,7 @@ class App.Controller extends Spine.Controller item.link = object.uiUrl() item.title = object.displayName() item.object_name = object.objectDisplayName() - item.cssIcon = object.iconActivity( @Session.all() ) + item.cssIcon = object.iconActivity( @Session.get() ) item.created_by = App.User.retrieve( item.created_by_id ) items diff --git a/app/assets/javascripts/app/controllers/_default_navbar.js.coffee b/app/assets/javascripts/app/controllers/_default_navbar.js.coffee index 0a7ed0f11..592d07938 100644 --- a/app/assets/javascripts/app/controllers/_default_navbar.js.coffee +++ b/app/assets/javascripts/app/controllers/_default_navbar.js.coffee @@ -5,7 +5,8 @@ App.Config.set( 'User', { item = {} item['name'] = App.Session.get( 'login' ) item['image'] = App.Session.get( 'imageUrl' ) - item['avatar'] = App.User.fullLocal( App.Session.get('id') ).avatar() + if App.Session.get() + item['avatar'] = App.Session.get().avatar() return item target: '#current_user', class: 'user' diff --git a/app/assets/javascripts/app/controllers/_profile/linked_accounts.js.coffee b/app/assets/javascripts/app/controllers/_profile/linked_accounts.js.coffee index 3dbc7e609..b3475fcb3 100644 --- a/app/assets/javascripts/app/controllers/_profile/linked_accounts.js.coffee +++ b/app/assets/javascripts/app/controllers/_profile/linked_accounts.js.coffee @@ -40,7 +40,7 @@ class Index extends App.Controller auth_providers.push provider @html App.view('profile/linked_accounts')( - user: App.Session.all() + user: App.Session.get() auth_providers: auth_providers ) diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index a499093a7..e59537424 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -400,8 +400,8 @@ class App.TicketCreate extends App.Controller ui.scrollTo() # access to group - session = App.Session.all() - if session && session['group_ids'] && _.contains(session['group_ids'], @group_id) + group_ids = App.Session.get('group_ids') + if group_ids && _.contains( group_ids, @group_id ) ui.navigate "#ticket/zoom/#{@id}" return diff --git a/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee index 17fb074d6..dfdaeb906 100644 --- a/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/customer_ticket_create.js.coffee @@ -203,7 +203,7 @@ class Index extends App.ControllerContent # create article params['article'] = { - from: "#{ @Session.get('firstname') } #{ @Session.get('lastname') }" + from: "#{ @Session.get().displayName() }" to: (group && group.name) || '' subject: params.subject body: params.body diff --git a/app/assets/javascripts/app/controllers/login.js.coffee b/app/assets/javascripts/app/controllers/login.js.coffee index 0b9daf8d5..8a2a0efa7 100644 --- a/app/assets/javascripts/app/controllers/login.js.coffee +++ b/app/assets/javascripts/app/controllers/login.js.coffee @@ -6,7 +6,7 @@ class Index extends App.ControllerContent super # navigate to # if session if exists - if @Session.get( 'id' ) + if @Session.get() @navigate '#' return diff --git a/app/assets/javascripts/app/controllers/navigation.js.coffee b/app/assets/javascripts/app/controllers/navigation.js.coffee index dae7113fd..dddf1d5f0 100644 --- a/app/assets/javascripts/app/controllers/navigation.js.coffee +++ b/app/assets/javascripts/app/controllers/navigation.js.coffee @@ -126,7 +126,7 @@ class App.Navigation extends App.Controller # remember old search query search = @el.find('#global-search').val() - user = App.Session.all() + user = App.Session.get() @html App.view('navigation')( user: user search: search @@ -366,6 +366,10 @@ class App.Navigation extends App.Controller if part[0] is 'RecendViewed' delete NavBarRight[key] + if !@Session.get() + @Config.set( 'NavBarRight', NavBarRight ) + return + # add new views items = App.RecentView.search(sortBy: 'created_at', order: 'DESC' ) items = @prepareForObjectList(items) diff --git a/app/assets/javascripts/app/controllers/taskbar_widget.js.coffee b/app/assets/javascripts/app/controllers/taskbar_widget.js.coffee index aee945e6d..bf7f638e1 100644 --- a/app/assets/javascripts/app/controllers/taskbar_widget.js.coffee +++ b/app/assets/javascripts/app/controllers/taskbar_widget.js.coffee @@ -19,7 +19,7 @@ class App.TaskbarWidget extends App.Controller @bind 'auth:logout', => @render() render: -> - return if _.isEmpty( @Session.all() ) + return if !@Session.get() tasks = App.TaskManager.all() item_list = [] diff --git a/app/assets/javascripts/app/controllers/ticket_overview.js.coffee b/app/assets/javascripts/app/controllers/ticket_overview.js.coffee index 151791c70..fbd6be3a3 100644 --- a/app/assets/javascripts/app/controllers/ticket_overview.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_overview.js.coffee @@ -393,7 +393,7 @@ class Table extends App.ControllerContent # validate article if params['body'] article = new App.TicketArticle - params.from = @Session.get( 'firstname' ) + ' ' + @Session.get( 'lastname' ) + params.from = @Session.get().displayName() params.ticket_id = ticket.id params.form_id = @form_id diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee index 733c01bd9..3c7f76773 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee @@ -62,7 +62,7 @@ class App.TicketZoom extends App.Controller fetch: (ticket_id, force) -> - return if !@Session.all() + return if !@Session.get() # get data @ajax( @@ -80,7 +80,7 @@ class App.TicketZoom extends App.Controller return if @ticketUpdatedAtLastCall is newTicketRaw.updated_at # notify if ticket changed not by my self - if newTicketRaw.updated_by_id isnt @Session.all().id + if newTicketRaw.updated_by_id isnt @Session.get('id') App.TaskManager.notify( @task_key ) # rerender edit box @@ -835,7 +835,7 @@ class Edit extends App.Controller articleAttributes = App.TicketArticle.attributesGet( 'edit' ) if params['body'] || ( articleAttributes['body'] && articleAttributes['body']['null'] is false ) article = new App.TicketArticle - params.from = @Session.get( 'firstname' ) + ' ' + @Session.get( 'lastname' ) + params.from = @Session.get().displayName() params.ticket_id = ticket.id params.form_id = @form_id diff --git a/app/assets/javascripts/app/controllers/widget/chat.js.coffee b/app/assets/javascripts/app/controllers/widget/chat.js.coffee index 25bcd6908..1e0e55013 100644 --- a/app/assets/javascripts/app/controllers/widget/chat.js.coffee +++ b/app/assets/javascripts/app/controllers/widget/chat.js.coffee @@ -26,7 +26,7 @@ class Widget extends App.Controller @start() access: -> - return false if _.isEmpty( @Session.all() ) + return false if !@Session.get() return true if @isRole('Agent') return true if @isRole('Admin') return false diff --git a/app/assets/javascripts/app/controllers/widget/online_notification.js.coffee b/app/assets/javascripts/app/controllers/widget/online_notification.js.coffee index 2a94d8cfd..aebff64b3 100644 --- a/app/assets/javascripts/app/controllers/widget/online_notification.js.coffee +++ b/app/assets/javascripts/app/controllers/widget/online_notification.js.coffee @@ -21,16 +21,14 @@ class App.OnlineNotificationWidget extends App.Controller if @access() @start() - - @start() - @subscribeId = App.OnlineNotification.subscribe( @start ) + @subscribeId = App.OnlineNotification.subscribe( @start ) release: => @stop() App.OnlineNotification.unsubscribe( @subscribeId ) access: -> - return false if _.isEmpty( @Session.all() ) + return false if !@Session.get() return true if @isRole('Agent') return true if @isRole('Admin') return false diff --git a/app/assets/javascripts/app/controllers/widget/switch_back_to_user.js.coffee b/app/assets/javascripts/app/controllers/widget/switch_back_to_user.js.coffee index 6913c1ea6..59914361f 100644 --- a/app/assets/javascripts/app/controllers/widget/switch_back_to_user.js.coffee +++ b/app/assets/javascripts/app/controllers/widget/switch_back_to_user.js.coffee @@ -17,7 +17,7 @@ class Widget extends App.Controller render: (user) -> # if no switch to user is active - if !App.Config.get('switch_back_to_possible') || _.isEmpty( App.Session.all() ) + if !App.Config.get('switch_back_to_possible') || !App.Session.get() @el.html('') $('#app').removeClass('switchBackToUserSpace') return diff --git a/app/assets/javascripts/app/lib/app_init/session.js.coffee b/app/assets/javascripts/app/lib/app_init/session.js.coffee index 27bd4686d..719b9150a 100644 --- a/app/assets/javascripts/app/lib/app_init/session.js.coffee +++ b/app/assets/javascripts/app/lib/app_init/session.js.coffee @@ -10,15 +10,10 @@ class App.Session _instance ?= new _sessionSingleton _instance.get( key ) - @set: ( key, value ) -> + @set: ( user ) -> if _instance == undefined _instance ?= new _sessionSingleton - _instance.set( key, value ) - - @all: -> - if _instance == undefined - _instance ?= new _sessionSingleton - _instance.all() + _instance.set( user ) class _sessionSingleton extends Spine.Module @include App.LogInclude @@ -27,15 +22,13 @@ class _sessionSingleton extends Spine.Module @clear() clear: -> - @data = {} + @user = undefined get: ( key ) -> - @log 'debug', key, @data[key] - @data[key] + return if !@user + if key + return @user[key] + @user - set: ( key, value ) -> - @log 'debug', 'set', key, value - @data[key] = value - - all: -> - @data + set: ( user ) -> + @user = user \ No newline at end of file diff --git a/app/assets/javascripts/app/lib/app_post/auth.js.coffee b/app/assets/javascripts/app/lib/app_post/auth.js.coffee index a9bb71617..d7cb4e850 100644 --- a/app/assets/javascripts/app/lib/app_post/auth.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/auth.js.coffee @@ -102,9 +102,8 @@ class App.Auth App.Collection.loadAssets( data.assets ) # store user data - session = App.User.fullLocal(data.session.id) - for key, value of session - App.Session.set( key, value ) + sessionUser = App.User.fullLocal(data.session.id) + App.Session.set( sessionUser ) # trigger auth ok with new session data App.Event.trigger( 'auth', data.session ) diff --git a/app/assets/javascripts/app/lib/app_post/websocket.js.coffee b/app/assets/javascripts/app/lib/app_post/websocket.js.coffee index 07e2d80da..6b944da80 100644 --- a/app/assets/javascripts/app/lib/app_post/websocket.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/websocket.js.coffee @@ -114,7 +114,7 @@ class _webSocketSingleton extends App.Controller # logon websocket data = action: 'login' - session: App.Session.all() + session: App.Session.get() @send(data) spool: => diff --git a/lib/sessions.rb b/lib/sessions.rb index b0da69bcc..6731c8869 100644 --- a/lib/sessions.rb +++ b/lib/sessions.rb @@ -29,21 +29,23 @@ returns =end def self.create( client_id, session, meta ) + data = {} + if session + data[:id] = session[:id] + end path = @path + '/' + client_id.to_s FileUtils.mkpath path meta[:last_ping] = Time.new.to_i.to_s File.open( path + '/session', 'wb' ) { |file| data = { - :user => { - :id => session['id'], - }, + :user => data, :meta => meta, } file.write Marshal.dump(data) } # send update to browser - if session['id'] + if session && session['id'] self.send( client_id, { :event => 'ws:login', :data => { :success => true }, @@ -564,4 +566,4 @@ returns puts "/LOOP #{client_id} - #{try_count}" end -end \ No newline at end of file +end