Refactoring of App.Session class.

This commit is contained in:
Martin Edenhofer 2014-09-21 14:50:39 +02:00
parent 9ee8e5c0c1
commit 2028a564b1
17 changed files with 43 additions and 46 deletions

View file

@ -185,8 +185,8 @@ class App.Controller extends Spine.Controller
authenticate: -> authenticate: ->
# return rtue if session exists # return true if session exists
return true if @Session.get( 'id' ) return true if @Session.get()
# remember requested url # remember requested url
@Config.set( 'requested_url', window.location.hash ) @Config.set( 'requested_url', window.location.hash )
@ -417,7 +417,7 @@ class App.Controller extends Spine.Controller
item.link = object.uiUrl() item.link = object.uiUrl()
item.title = object.displayName() item.title = object.displayName()
item.object_name = object.objectDisplayName() 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 ) item.created_by = App.User.retrieve( item.created_by_id )
items items

View file

@ -5,7 +5,8 @@ App.Config.set( 'User', {
item = {} item = {}
item['name'] = App.Session.get( 'login' ) item['name'] = App.Session.get( 'login' )
item['image'] = App.Session.get( 'imageUrl' ) 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 return item
target: '#current_user', target: '#current_user',
class: 'user' class: 'user'

View file

@ -40,7 +40,7 @@ class Index extends App.Controller
auth_providers.push provider auth_providers.push provider
@html App.view('profile/linked_accounts')( @html App.view('profile/linked_accounts')(
user: App.Session.all() user: App.Session.get()
auth_providers: auth_providers auth_providers: auth_providers
) )

View file

@ -400,8 +400,8 @@ class App.TicketCreate extends App.Controller
ui.scrollTo() ui.scrollTo()
# access to group # access to group
session = App.Session.all() group_ids = App.Session.get('group_ids')
if session && session['group_ids'] && _.contains(session['group_ids'], @group_id) if group_ids && _.contains( group_ids, @group_id )
ui.navigate "#ticket/zoom/#{@id}" ui.navigate "#ticket/zoom/#{@id}"
return return

View file

@ -203,7 +203,7 @@ class Index extends App.ControllerContent
# create article # create article
params['article'] = { params['article'] = {
from: "#{ @Session.get('firstname') } #{ @Session.get('lastname') }" from: "#{ @Session.get().displayName() }"
to: (group && group.name) || '' to: (group && group.name) || ''
subject: params.subject subject: params.subject
body: params.body body: params.body

View file

@ -6,7 +6,7 @@ class Index extends App.ControllerContent
super super
# navigate to # if session if exists # navigate to # if session if exists
if @Session.get( 'id' ) if @Session.get()
@navigate '#' @navigate '#'
return return

View file

@ -126,7 +126,7 @@ class App.Navigation extends App.Controller
# remember old search query # remember old search query
search = @el.find('#global-search').val() search = @el.find('#global-search').val()
user = App.Session.all() user = App.Session.get()
@html App.view('navigation')( @html App.view('navigation')(
user: user user: user
search: search search: search
@ -366,6 +366,10 @@ class App.Navigation extends App.Controller
if part[0] is 'RecendViewed' if part[0] is 'RecendViewed'
delete NavBarRight[key] delete NavBarRight[key]
if !@Session.get()
@Config.set( 'NavBarRight', NavBarRight )
return
# add new views # add new views
items = App.RecentView.search(sortBy: 'created_at', order: 'DESC' ) items = App.RecentView.search(sortBy: 'created_at', order: 'DESC' )
items = @prepareForObjectList(items) items = @prepareForObjectList(items)

View file

@ -19,7 +19,7 @@ class App.TaskbarWidget extends App.Controller
@bind 'auth:logout', => @render() @bind 'auth:logout', => @render()
render: -> render: ->
return if _.isEmpty( @Session.all() ) return if !@Session.get()
tasks = App.TaskManager.all() tasks = App.TaskManager.all()
item_list = [] item_list = []

View file

@ -393,7 +393,7 @@ class Table extends App.ControllerContent
# validate article # validate article
if params['body'] if params['body']
article = new App.TicketArticle article = new App.TicketArticle
params.from = @Session.get( 'firstname' ) + ' ' + @Session.get( 'lastname' ) params.from = @Session.get().displayName()
params.ticket_id = ticket.id params.ticket_id = ticket.id
params.form_id = @form_id params.form_id = @form_id

View file

@ -62,7 +62,7 @@ class App.TicketZoom extends App.Controller
fetch: (ticket_id, force) -> fetch: (ticket_id, force) ->
return if !@Session.all() return if !@Session.get()
# get data # get data
@ajax( @ajax(
@ -80,7 +80,7 @@ class App.TicketZoom extends App.Controller
return if @ticketUpdatedAtLastCall is newTicketRaw.updated_at return if @ticketUpdatedAtLastCall is newTicketRaw.updated_at
# notify if ticket changed not by my self # 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 ) App.TaskManager.notify( @task_key )
# rerender edit box # rerender edit box
@ -835,7 +835,7 @@ class Edit extends App.Controller
articleAttributes = App.TicketArticle.attributesGet( 'edit' ) articleAttributes = App.TicketArticle.attributesGet( 'edit' )
if params['body'] || ( articleAttributes['body'] && articleAttributes['body']['null'] is false ) if params['body'] || ( articleAttributes['body'] && articleAttributes['body']['null'] is false )
article = new App.TicketArticle article = new App.TicketArticle
params.from = @Session.get( 'firstname' ) + ' ' + @Session.get( 'lastname' ) params.from = @Session.get().displayName()
params.ticket_id = ticket.id params.ticket_id = ticket.id
params.form_id = @form_id params.form_id = @form_id

View file

@ -26,7 +26,7 @@ class Widget extends App.Controller
@start() @start()
access: -> access: ->
return false if _.isEmpty( @Session.all() ) return false if !@Session.get()
return true if @isRole('Agent') return true if @isRole('Agent')
return true if @isRole('Admin') return true if @isRole('Admin')
return false return false

View file

@ -21,8 +21,6 @@ class App.OnlineNotificationWidget extends App.Controller
if @access() if @access()
@start() @start()
@start()
@subscribeId = App.OnlineNotification.subscribe( @start ) @subscribeId = App.OnlineNotification.subscribe( @start )
release: => release: =>
@ -30,7 +28,7 @@ class App.OnlineNotificationWidget extends App.Controller
App.OnlineNotification.unsubscribe( @subscribeId ) App.OnlineNotification.unsubscribe( @subscribeId )
access: -> access: ->
return false if _.isEmpty( @Session.all() ) return false if !@Session.get()
return true if @isRole('Agent') return true if @isRole('Agent')
return true if @isRole('Admin') return true if @isRole('Admin')
return false return false

View file

@ -17,7 +17,7 @@ class Widget extends App.Controller
render: (user) -> render: (user) ->
# if no switch to user is active # 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('') @el.html('')
$('#app').removeClass('switchBackToUserSpace') $('#app').removeClass('switchBackToUserSpace')
return return

View file

@ -10,15 +10,10 @@ class App.Session
_instance ?= new _sessionSingleton _instance ?= new _sessionSingleton
_instance.get( key ) _instance.get( key )
@set: ( key, value ) -> @set: ( user ) ->
if _instance == undefined if _instance == undefined
_instance ?= new _sessionSingleton _instance ?= new _sessionSingleton
_instance.set( key, value ) _instance.set( user )
@all: ->
if _instance == undefined
_instance ?= new _sessionSingleton
_instance.all()
class _sessionSingleton extends Spine.Module class _sessionSingleton extends Spine.Module
@include App.LogInclude @include App.LogInclude
@ -27,15 +22,13 @@ class _sessionSingleton extends Spine.Module
@clear() @clear()
clear: -> clear: ->
@data = {} @user = undefined
get: ( key ) -> get: ( key ) ->
@log 'debug', key, @data[key] return if !@user
@data[key] if key
return @user[key]
@user
set: ( key, value ) -> set: ( user ) ->
@log 'debug', 'set', key, value @user = user
@data[key] = value
all: ->
@data

View file

@ -102,9 +102,8 @@ class App.Auth
App.Collection.loadAssets( data.assets ) App.Collection.loadAssets( data.assets )
# store user data # store user data
session = App.User.fullLocal(data.session.id) sessionUser = App.User.fullLocal(data.session.id)
for key, value of session App.Session.set( sessionUser )
App.Session.set( key, value )
# trigger auth ok with new session data # trigger auth ok with new session data
App.Event.trigger( 'auth', data.session ) App.Event.trigger( 'auth', data.session )

View file

@ -114,7 +114,7 @@ class _webSocketSingleton extends App.Controller
# logon websocket # logon websocket
data = data =
action: 'login' action: 'login'
session: App.Session.all() session: App.Session.get()
@send(data) @send(data)
spool: => spool: =>

View file

@ -29,21 +29,23 @@ returns
=end =end
def self.create( client_id, session, meta ) def self.create( client_id, session, meta )
data = {}
if session
data[:id] = session[:id]
end
path = @path + '/' + client_id.to_s path = @path + '/' + client_id.to_s
FileUtils.mkpath path FileUtils.mkpath path
meta[:last_ping] = Time.new.to_i.to_s meta[:last_ping] = Time.new.to_i.to_s
File.open( path + '/session', 'wb' ) { |file| File.open( path + '/session', 'wb' ) { |file|
data = { data = {
:user => { :user => data,
:id => session['id'],
},
:meta => meta, :meta => meta,
} }
file.write Marshal.dump(data) file.write Marshal.dump(data)
} }
# send update to browser # send update to browser
if session['id'] if session && session['id']
self.send( client_id, { self.send( client_id, {
:event => 'ws:login', :event => 'ws:login',
:data => { :success => true }, :data => { :success => true },