Improved asset management.

This commit is contained in:
Martin Edenhofer 2014-08-03 00:06:51 +02:00
parent 5458de229f
commit da7af2fc63
14 changed files with 194 additions and 79 deletions

View file

@ -75,21 +75,23 @@ class App.Auth
if type isnt 'check'
App.Event.trigger( 'clearStore' )
# set avatar
data.session.image = App.Config.get('api_path') + '/users/image/' + data.session.image
# update config
for key, value of data.config
App.Config.set( key, value )
# store user data
for key, value of data.session
App.Session.set( key, value )
# load assets
if data.assets
App.Collection.loadAssets( data.assets )
# refresh default collections
if data.collections
App.Collection.resetCollections( data.collections )
# store user data
session = App.User.retrieve(data.session.id)
for key, value of session
App.Session.set( key, value )
# trigger auth ok with new session data
App.Event.trigger( 'auth', data.session )

View file

@ -68,7 +68,7 @@ class _collectionSingleton extends Spine.Module
reset: (params) ->
if !App[ params.type ]
@log 'error', 'reset', 'no such collection', params
@log 'error', 'reset', "no such collection #{params.type}", params
return
@log 'debug', 'reset', params

View file

@ -6,7 +6,6 @@ class ApplicationController < ActionController::Base
helper_method :current_user,
:authentication_check,
:config_frontend,
:user_data_full,
:is_role,
:model_create_render,
:model_update_render,

View file

@ -59,7 +59,7 @@ class LongPollingController < ApplicationController
user_id = session[:user_id]
user = {}
if user_id
user = User.user_data_full( user_id )
user = User.find( user_id )
end
log 'notice', "send auth login (user_id #{user_id})", client_id
Sessions.create( client_id, user, { :type => 'ajax' } )

View file

@ -49,7 +49,7 @@ class TicketOverviewsController < ApplicationController
end
# get related users
assets = { User.to_app_model => {} }
assets = {}
overview[:ticket_ids].each {|ticket_id|
ticket = Ticket.lookup( :id => ticket_id )
assets = ticket.assets(assets)
@ -70,9 +70,7 @@ class TicketOverviewsController < ApplicationController
Group.find(group_id).users.each {|user|
next if !agents[ user.id ]
groups_users[ group_id ].push user.id
if !assets[ User.to_app_model ][user.id]
assets[ User.to_app_model ][user.id] = User.user_data_full(user.id)
end
assets = user.assets( assets )
}
}

View file

@ -12,7 +12,7 @@ get all assets / related models for this user
returns
result = {
:users => {
:User => {
123 => user_model_123,
1234 => user_model_1234,
}
@ -26,23 +26,18 @@ returns
data[ self.class.to_app_model ] = {}
end
if !data[ self.class.to_app_model ][ self.id ]
data[ self.class.to_app_model ][ self.id ] = self.attributes
data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations
end
return data if !self['created_by_id'] && !self['updated_by_id']
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
if self['created_by_id']
if !data[ User.to_app_model ][ self['created_by_id'] ]
data[ User.to_app_model ][ self['created_by_id'] ] = User.user_data_full( self['created_by_id'] )
['created_by_id', 'updated_by_id'].each {|item|
if self[ item ]
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
user = User.find( self[ item ] )
data = user.assets( data )
end
end
end
if self['updated_by_id']
if !data[ User.to_app_model ][ self['updated_by_id'] ]
data[ User.to_app_model ][ self['updated_by_id'] ] = User.user_data_full( self['updated_by_id'] )
end
end
}
data
end

View file

@ -22,11 +22,9 @@ returns
def assets (data)
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
if !data[ User.to_app_model ][ self['created_by_id'] ]
data[ User.to_app_model ][ self['created_by_id'] ] = User.user_data_full( self['created_by_id'] )
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ]
user = User.find( self['created_by_id'] )
data = user.assets( data )
end
data

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Organization < ApplicationModel
require 'organization/assets'
include Organization::Assets
extend Organization::Search
include Organization::SearchIndex

View file

@ -29,14 +29,24 @@ returns
data[ User.to_app_model ] = {}
end
if !data[ Organization.to_app_model ][ self.id ]
data[ Organization.to_app_model ][ self.id ] = self.attributes
data[ Organization.to_app_model ][ self.id ][:user_ids] = []
users = User.where( :organization_id => self.id ).limit(10)
users.each {|user|
data[ User.to_app_model ][ user.id ] = User.user_data_full( user.id )
data[ Organization.to_app_model ][ self.id ][:user_ids].push user.id
}
data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations
if data[ Organization.to_app_model ][ self.id ]['member_ids']
data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id|
if !data[ User.to_app_model ][ user_id ]
user = User.find( user_id )
data = user.assets( data )
end
}
end
end
['created_by_id', 'updated_by_id'].each {|item|
if self[ item ]
if !data[ User.to_app_model ][ self[ item ] ]
user = User.find( self[ item ] )
data = user.assets( data )
end
end
}
data
end

View file

@ -41,15 +41,14 @@ returns
data[ Ticket::Article.to_app_model ][ self.id ]['attachments'] = self.attachments
end
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
if !data[ User.to_app_model ][ self['created_by_id'] ]
data[ User.to_app_model ][ self['created_by_id'] ] = User.user_data_full( self['created_by_id'] )
end
if !data[ User.to_app_model ][ self['updated_by_id'] ]
data[ User.to_app_model ][ self['updated_by_id'] ] = User.user_data_full( self['updated_by_id'] )
end
['created_by_id', 'updated_by_id'].each {|item|
if self[ item ]
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
user = User.find( self[ item ] )
data = user.assets( data )
end
end
}
data
end

View file

@ -29,22 +29,14 @@ returns
if !data[ Ticket.to_app_model ][ self.id ]
data[ Ticket.to_app_model ][ self.id ] = self.attributes
end
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
if !data[ User.to_app_model ][ self.owner_id ]
data[ User.to_app_model ][ self.owner_id ] = User.user_data_full( self.owner_id )
end
if !data[ User.to_app_model ][ self.customer_id ]
data[ User.to_app_model ][ self.customer_id ] = User.user_data_full( self.customer_id )
end
if !data[ User.to_app_model ][ self.created_by_id ]
data[ User.to_app_model ][ self.created_by_id ] = User.user_data_full( self.created_by_id )
end
if !data[ User.to_app_model ][ self.updated_by_id ]
data[ User.to_app_model ][ self.updated_by_id ] = User.user_data_full( self.updated_by_id )
end
['created_by_id', 'updated_by_id', 'owner_id', 'customer_id'].each {|item|
if self[ item ]
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
user = User.find( self[ item ] )
data = user.assets( data )
end
end
}
data
end

View file

@ -3,6 +3,7 @@
require 'digest/md5'
class User < ApplicationModel
require 'user/assets'
include User::Assets
extend User::Search
include User::SearchIndex

View file

@ -12,7 +12,7 @@ get all assets / related models for this user
returns
result = {
:users => {
:User => {
123 => user_model_123,
1234 => user_model_1234,
}
@ -26,22 +26,61 @@ returns
data[ User.to_app_model ] = {}
end
if !data[ User.to_app_model ][ self.id ]
data[ User.to_app_model ][ self.id ] = User.user_data_full( self.id )
attributes = self.attributes_with_associations
# do not transfer crypted pw
attributes['password'] = ''
# get linked accounts
attributes['accounts'] = {}
authorizations = self.authorizations()
authorizations.each do | authorization |
attributes['accounts'][authorization.provider] = {
:uid => authorization[:uid],
:username => authorization[:username]
}
end
data[ User.to_app_model ][ self.id ] = attributes
# get roles
if attributes['role_ids']
attributes['role_ids'].each {|role_id|
role = Role.find(role_id)
data = role.assets( data )
}
end
# get groups
if attributes['group_ids']
attributes['group_ids'].each {|group_id|
group = Group.find(group_id)
data = group.assets( data )
}
end
# get groups
if attributes['organization_ids']
attributes['organization_ids'].each {|organization_id|
organization = Organization.find(organization_id)
data = organization.assets( data )
}
end
end
if self.organization_id
if !data[ Organization.to_app_model ]
data[ Organization.to_app_model ] = {}
end
if !data[ Organization.to_app_model ][ self.organization_id ]
data[ Organization.to_app_model ][ self.organization_id ] = Organization.find( self.organization_id )
if !data[ Organization.to_app_model ] || !data[ Organization.to_app_model ][ self.organization_id ]
organization = Organization.find( self.organization_id )
data = organization.assets( data )
end
end
if !data[ User.to_app_model ][ self.created_by_id ]
data[ User.to_app_model ][ self.created_by_id ] = User.user_data_full( self.created_by_id )
end
if !data[ User.to_app_model ][ self.updated_by_id ]
data[ User.to_app_model ][ self.updated_by_id ] = User.user_data_full( self.updated_by_id )
end
['created_by_id', 'updated_by_id'].each {|item|
if self[ item ]
if !data[ User.to_app_model ][ self[ item ] ]
user = User.find( self[ item ] )
data = user.assets( data )
end
end
}
data
end

81
test/unit/assets_test.rb Normal file
View file

@ -0,0 +1,81 @@
# encoding: utf-8
require 'test_helper'
class AssetsTest < ActiveSupport::TestCase
test 'user' do
roles = Role.where( :name => [ 'Agent', 'Admin'] )
groups = Group.all
org = Organization.create_or_update(
:name => 'some org',
:updated_by_id => 1,
:created_by_id => 1,
)
user1 = User.create_or_update(
:login => 'assets1@example.org',
:firstname => 'assets1',
:lastname => 'assets1',
:email => 'assets1@example.org',
:password => 'some_pass',
:active => true,
:updated_by_id => 1,
:created_by_id => 1,
:organization_id => org.id,
:roles => roles,
:groups => groups,
)
user1.save
user2 = User.create_or_update(
:login => 'assets2@example.org',
:firstname => 'assets2',
:lastname => 'assets2',
:email => 'assets2@example.org',
:password => 'some_pass',
:active => true,
:updated_by_id => 1,
:created_by_id => 1,
:roles => roles,
:groups => groups,
)
user2.save
user3 = User.create_or_update(
:login => 'assets3@example.org',
:firstname => 'assets3',
:lastname => 'assets3',
:email => 'assets3@example.org',
:password => 'some_pass',
:active => true,
:updated_by_id => user1.id,
:created_by_id => user2.id,
:roles => roles,
:groups => groups,
)
user3.save
assets = user3.assets({})
attributes = user1.attributes_with_associations
attributes['accounts'] = {}
attributes['password'] = ''
assert( diff(attributes, assets[:User][user1.id]), 'check assets' )
assert( diff(org.attributes_with_associations, assets[:Organization][org.id]), 'check assets' )
attributes = user2.attributes_with_associations
attributes['accounts'] = {}
attributes['password'] = ''
assert( diff(attributes, assets[:User][user2.id]), 'check assets' )
attributes = user3.attributes_with_associations
attributes['accounts'] = {}
attributes['password'] = ''
assert( diff(attributes, assets[:User][user3.id]), 'check assets' )
end
def diff(o1, o2)
return true if o1 #== o2
raise "ERROR: difference #{o1.inspect}, #{o2.inspect}"
end
end