From 3c552ce458db68b096009479f4613317dea4fb57 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 14 Aug 2013 01:55:45 +0200 Subject: [PATCH 01/29] Change config option top be not used in fronted. --- db/seeds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index ed4f5e25b..c78b669d0 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -153,7 +153,7 @@ Setting.create_if_not_exists( ], }, :state => 'Gmaps', - :frontend => true + :frontend => false ) Setting.create_if_not_exists( From 66b4f2d2dbdf3e5440763a67f5fbfa07c402f6b1 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 14 Aug 2013 10:05:23 +0200 Subject: [PATCH 02/29] Moved to defer attribute for js include script. --- app/assets/javascripts/application.js | 5 +++++ app/views/init/index.html.erb | 7 +------ app/views/layouts/application.html.erb | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index aa9197f22..8988c1bb1 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -71,3 +71,8 @@ jQuery.event.special.remove = { if (e.handler) e.handler(); } }; + +// start application +jQuery(function(){ + new App.Run(); +}); \ No newline at end of file diff --git a/app/views/init/index.html.erb b/app/views/init/index.html.erb index 8d1cfe9cc..748969958 100644 --- a/app/views/init/index.html.erb +++ b/app/views/init/index.html.erb @@ -5,9 +5,4 @@
-
- + \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 7ef0850f3..6f8dbd98e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -3,7 +3,11 @@ <%= Setting.get('product_name') %> <%= stylesheet_link_tag "application" %> + <% if Rails.configuration.assets.debug %> <%= javascript_include_tag "application" %> + <% else %> + <%= javascript_include_tag "application", :defer => 'defer' %> + <% end %> <%= csrf_meta_tags %> From 41c26bacd71207d64ed60bfec6dc72742e273309 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 14 Aug 2013 10:15:24 +0200 Subject: [PATCH 03/29] Fixed timing issue. --- test/browser/signup_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/browser/signup_test.rb b/test/browser/signup_test.rb index 4d541539c..2b0023da6 100644 --- a/test/browser/signup_test.rb +++ b/test/browser/signup_test.rb @@ -1,6 +1,6 @@ # encoding: utf-8 require 'browser_test_helper' - + class SignupTest < TestCase def test_signup signup_user_email = 'signup-test-' + rand(999999).to_s + '@example.com' @@ -55,7 +55,7 @@ class SignupTest < TestCase }, { :execute => 'wait', - :value => 2, + :value => 5, }, # check action From 940f7aa63b8d21b62700f8c1bfa0a666b0aec3d9 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 14 Aug 2013 17:11:29 +0200 Subject: [PATCH 04/29] Use kind_of object to check if http call was successfully. --- lib/rss.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rss.rb b/lib/rss.rb index a6ff21149..2925a8a2f 100644 --- a/lib/rss.rb +++ b/lib/rss.rb @@ -9,11 +9,11 @@ module RSS response = Net::HTTP.get_response( URI.parse(url) ) # check if redirect is needed - if response.code.to_s == '301' || response.code.to_s == '302' + if response.kind_of? Net::HTTPRedirection url = response.header['location'] response = Net::HTTP.get_response( URI.parse( url ) ) end - if response.code.to_s != '200' + if ! response.kind_of? Net::HTTPSuccess raise "Can't fetch '#{url}', http code: #{response.code.to_s}" return end From 1e9a072a6e258b5c4164b5dd01ff3d4141fc83ff Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 14 Aug 2013 17:25:46 +0200 Subject: [PATCH 05/29] Added lang attribute to html tag. --- app/assets/javascripts/app/lib/app_post/i18n.js.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee index 5f3fe4bc4..b19e5e23e 100644 --- a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee @@ -99,6 +99,9 @@ class _i18nSingleton extends Spine.Module locale = 'en' @locale = locale + # set lang attribute of html tag + $('html').prop( 'lang', locale.substr(0, 2) ) + @map = {} App.Ajax.request( id: 'i18n-set-' + locale, From 3a0bddd79a13ff8eaadcaae7f271a13a43ac173b Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 15 Aug 2013 22:40:22 +0200 Subject: [PATCH 06/29] Moved to adapter backends for geo location and geo ip lookups. --- app/controllers/application_controller.rb | 4 +- app/models/observer/user/geo.rb | 9 +--- db/migrate/20130815000001_update_geo2.rb | 52 +++++++++++++++++++++++ db/seeds.rb | 30 +++++++++++-- lib/application_lib.rb | 17 ++++++++ lib/geo_ip.rb | 36 ++++++++++++++++ lib/{geoip.rb => geo_ip/freegeoip.rb} | 4 +- lib/geo_location.rb | 46 ++++++++++++++++++++ lib/{ => geo_location}/gmaps.rb | 9 ++-- 9 files changed, 187 insertions(+), 20 deletions(-) create mode 100644 db/migrate/20130815000001_update_geo2.rb create mode 100644 lib/application_lib.rb create mode 100644 lib/geo_ip.rb rename lib/{geoip.rb => geo_ip/freegeoip.rb} (89%) create mode 100644 lib/geo_location.rb rename lib/{ => geo_location}/gmaps.rb (96%) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bd2d0293d..9215386e3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,5 @@ # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ -require 'geoip' - class ApplicationController < ActionController::Base # http_basic_authenticate_with :name => "test", :password => "ttt" @@ -82,7 +80,7 @@ class ApplicationController < ActionController::Base # check if remote ip need to be updated if !session[:remote_id] || session[:remote_id] != request.remote_ip session[:remote_id] = request.remote_ip - session[:geo] = Geoip.location( request.remote_ip ) + session[:geo] = GeoIp.location( request.remote_ip ) end # fill user agent diff --git a/app/models/observer/user/geo.rb b/app/models/observer/user/geo.rb index 0d6053204..bf4d6ca65 100644 --- a/app/models/observer/user/geo.rb +++ b/app/models/observer/user/geo.rb @@ -53,13 +53,8 @@ class Observer::User::Geo < ActiveRecord::Observer # return if no address is given return if address == '' - # load adapter - adapter = Setting.get('geo_backend') - return if !adapter - adapter_module = Object.const_get(adapter) - - # db lookup - latlng = adapter_module.geocode(address) + # lookup + latlng = GeoLocation.geocode( address ) return if !latlng # store data diff --git a/db/migrate/20130815000001_update_geo2.rb b/db/migrate/20130815000001_update_geo2.rb new file mode 100644 index 000000000..a3e57394b --- /dev/null +++ b/db/migrate/20130815000001_update_geo2.rb @@ -0,0 +1,52 @@ +class UpdateGeo2 < ActiveRecord::Migration + def up + Setting.where( :name => 'geo_backend' ).destroy_all + Setting.create_if_not_exists( + :title => 'Geo Location Backend', + :name => 'geo_location_backend', + :area => 'System::Geo', + :description => 'Defines the backend for geo location lookups.', + :options => { + :form => [ + { + :display => '', + :null => true, + :name => 'geo_location_backend', + :tag => 'select', + :options => { + '' => '-', + 'GeoLocation::Gmaps' => 'Google Maps', + }, + }, + ], + }, + :state => 'GeoLocation::Gmaps', + :frontend => false + ) + Setting.create_if_not_exists( + :title => 'Geo IP Backend', + :name => 'geo_ip_backend', + :area => 'System::Geo', + :description => 'Defines the backend for geo ip lookups.', + :options => { + :form => [ + { + :display => '', + :null => true, + :name => 'geo_ip_backend', + :tag => 'select', + :options => { + '' => '-', + 'GeoIp::Freegeoip' => 'freegeoip.net', + }, + }, + ], + }, + :state => 'GeoIp::Freegeoip', + :frontend => false + ) + end + def down + end +end + diff --git a/db/seeds.rb b/db/seeds.rb index c78b669d0..b245f8cb3 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -135,7 +135,7 @@ Setting.create_if_not_exists( ) Setting.create_if_not_exists( :title => 'Geo Location Backend', - :name => 'geo_backend', + :name => 'geo_location_backend', :area => 'System::Geo', :description => 'Defines the backend for geo location lookups.', :options => { @@ -143,16 +143,38 @@ Setting.create_if_not_exists( { :display => '', :null => true, - :name => 'geo_backend', + :name => 'geo_location_backend', :tag => 'select', :options => { '' => '-', - 'Gmaps' => 'Google Maps', + 'GeoLocation::Gmaps' => 'Google Maps', }, }, ], }, - :state => 'Gmaps', + :state => 'GeoLocation::Gmaps', + :frontend => false +) +Setting.create_if_not_exists( + :title => 'Geo IP Backend', + :name => 'geo_ip_backend', + :area => 'System::Geo', + :description => 'Defines the backend for geo ip lookups.', + :options => { + :form => [ + { + :display => '', + :null => true, + :name => 'geo_ip_backend', + :tag => 'select', + :options => { + '' => '-', + 'GeoIp::Freegeoip' => 'freegeoip.net', + }, + }, + ], + }, + :state => 'GeoIp::Freegeoip', :frontend => false ) diff --git a/lib/application_lib.rb b/lib/application_lib.rb new file mode 100644 index 000000000..9f0dae9e3 --- /dev/null +++ b/lib/application_lib.rb @@ -0,0 +1,17 @@ +class ApplicationLib + def self.load_adapter_by_setting(setting) + adapter = Setting.get( setting ) + return if !adapter + + # load backend + self.load_adapter(adapter) + end + def self.load_adapter(adapter) + + # load adapter + backend = Object.const_get(adapter) + + # return backend + return backend + end +end diff --git a/lib/geo_ip.rb b/lib/geo_ip.rb new file mode 100644 index 000000000..cea0858af --- /dev/null +++ b/lib/geo_ip.rb @@ -0,0 +1,36 @@ +class GeoIp < ApplicationLib + +=begin + +lookup location based on ip or hostname + + result = GeoIp.location( '172.0.0.1' ) + +returns + + result = { + "ip" => "172.0.0.1" + "country_code" => "DE", + "country_name" => "Germany", + "region_code" => "05", + "region_name" => "Hessen", + "city" => "Frankfurt Am Main" + "zipcode" => "12345", + "latitude" => 50.1167, + "longitude" => 8.6833, + "metro_code" => "", + "areacode" => "" + } + +=end + + def self.location(address) + + # load backend + backend = self.load_adapter_by_setting( 'geo_ip_backend' ) + return if !backend + + # db lookup + backend.location(address) + end +end diff --git a/lib/geoip.rb b/lib/geo_ip/freegeoip.rb similarity index 89% rename from lib/geoip.rb rename to lib/geo_ip/freegeoip.rb index 319072439..1d111e792 100644 --- a/lib/geoip.rb +++ b/lib/geo_ip/freegeoip.rb @@ -1,11 +1,11 @@ require 'faraday' require 'cache' -module Geoip +module GeoIp::Freegeoip def self.location(address) # check cache - cache_key = "geoip::#{address}" + cache_key = "freegeoip::#{address}" cache = Cache.get( cache_key ) return cache if cache diff --git a/lib/geo_location.rb b/lib/geo_location.rb new file mode 100644 index 000000000..4a574154f --- /dev/null +++ b/lib/geo_location.rb @@ -0,0 +1,46 @@ +class GeoLocation < ApplicationLib + +=begin + +lookup lat and lng for address + + result = GeoLocation.geocode( 'Marienstrasse 13, 10117 Berlin' ) + +returns + + result = [ 4.21312, 1.3123 ] + +=end + + def self.geocode(address) + + # load backend + backend = self.load_adapter_by_setting( 'geo_location_backend' ) + return if !backend + + # db lookup + backend.geocode(address) + end + +=begin + +lookup address for lat and lng + + result = GeoLocation.reverse_geocode( 4.21312, 1.3123 ) + +returns + + result = 'some address' + +=end + + def self.reverse_geocode(lat,lng) + + # load backend + backend = self.load_adapter_by_setting( 'geo_location_backend' ) + return if !backend + + # db lookup + backend.reverse_geocode(lat,lng) + end +end diff --git a/lib/gmaps.rb b/lib/geo_location/gmaps.rb similarity index 96% rename from lib/gmaps.rb rename to lib/geo_location/gmaps.rb index 133d0b77c..6b619c634 100644 --- a/lib/gmaps.rb +++ b/lib/geo_location/gmaps.rb @@ -1,4 +1,5 @@ -module Gmaps +class GeoLocation::Gmaps + def self.geocode(address) url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI::escape address}&sensor=true" response = Net::HTTP.get_response( URI.parse(url) ) @@ -10,15 +11,15 @@ module Gmaps lng = result['results'].first['geometry']['location']['lng'] latlng = [lat,lng] end - + def self.reverse_geocode(lat,lng) url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=#{lat},#{lng}&sensor=true" response = Net::HTTP.get_response( URI.parse(url) ) return if response.code.to_s != '200' result = JSON.parse( response.body ) - + address = result['results'].first['address_components'].first['long_name'] return address end -end \ No newline at end of file +end From 06a4bd6ce352661fdd8da2dd08836cbae821d4cf Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 15 Aug 2013 22:55:06 +0200 Subject: [PATCH 07/29] Moved to response.kind_of? Net::HTTPSuccess for response. --- lib/geo_location/gmaps.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/geo_location/gmaps.rb b/lib/geo_location/gmaps.rb index 6b619c634..5a703003d 100644 --- a/lib/geo_location/gmaps.rb +++ b/lib/geo_location/gmaps.rb @@ -3,7 +3,7 @@ class GeoLocation::Gmaps def self.geocode(address) url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI::escape address}&sensor=true" response = Net::HTTP.get_response( URI.parse(url) ) - return if response.code.to_s != '200' + return if ! response.kind_of? Net::HTTPSuccess result = JSON.parse( response.body ) @@ -15,7 +15,7 @@ class GeoLocation::Gmaps def self.reverse_geocode(lat,lng) url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=#{lat},#{lng}&sensor=true" response = Net::HTTP.get_response( URI.parse(url) ) - return if response.code.to_s != '200' + return if ! response.kind_of? Net::HTTPSuccess result = JSON.parse( response.body ) From 6a38df013d39596a6d8c7b6199c26e82c37149a4 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 15 Aug 2013 23:01:35 +0200 Subject: [PATCH 08/29] Fixed syntax error. --- lib/geo_ip/freegeoip.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/geo_ip/freegeoip.rb b/lib/geo_ip/freegeoip.rb index 1d111e792..2a6abbd76 100644 --- a/lib/geo_ip/freegeoip.rb +++ b/lib/geo_ip/freegeoip.rb @@ -1,7 +1,7 @@ require 'faraday' require 'cache' -module GeoIp::Freegeoip +class GeoIp::Freegeoip def self.location(address) # check cache From 3feab3354d757eb0e8052b5734241d14d7473af5 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 15 Aug 2013 23:15:05 +0200 Subject: [PATCH 09/29] Added doc. --- lib/application_lib.rb | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/application_lib.rb b/lib/application_lib.rb index 9f0dae9e3..f4f208865 100644 --- a/lib/application_lib.rb +++ b/lib/application_lib.rb @@ -1,4 +1,17 @@ class ApplicationLib + +=begin + +load adapter based on setting option + + adapter = self.load_adapter_by_setting( 'some_setting_with_class_name' ) + +returns + + result = adapter_class + +=end + def self.load_adapter_by_setting(setting) adapter = Setting.get( setting ) return if !adapter @@ -6,12 +19,22 @@ class ApplicationLib # load backend self.load_adapter(adapter) end + +=begin + +load adapter + + adapter = self.load_adapter( 'some_class_name' ) + +returns + + result = adapter_class + +=end + def self.load_adapter(adapter) # load adapter - backend = Object.const_get(adapter) - - # return backend - return backend + Object.const_get(adapter) end end From 39cb5c5fa6ead914f2af0ce065fa47add8a60127 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Aug 2013 00:11:10 +0200 Subject: [PATCH 10/29] Fixed doc. --- lib/application_lib.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/application_lib.rb b/lib/application_lib.rb index f4f208865..9b1b40f3f 100644 --- a/lib/application_lib.rb +++ b/lib/application_lib.rb @@ -4,7 +4,7 @@ class ApplicationLib load adapter based on setting option - adapter = self.load_adapter_by_setting( 'some_setting_with_class_name' ) + result = self.load_adapter_by_setting( 'some_setting_with_class_name' ) returns @@ -24,7 +24,7 @@ returns load adapter - adapter = self.load_adapter( 'some_class_name' ) + result = self.load_adapter( 'some_class_name' ) returns From e4381827ab7532f48416a07cae3232644653e72a Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Aug 2013 00:16:38 +0200 Subject: [PATCH 11/29] Moved to separate Ticket::Number class/adapter. --- app/models/channel/email_parser.rb | 2 +- app/models/ticket.rb | 40 ++----------------- app/models/ticket/number/date.rb | 4 +- app/models/ticket/number/increment.rb | 4 +- .../20130815000002_update_ticket_number.rb | 34 ++++++++++++++++ db/seeds.rb | 6 +-- 6 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 db/migrate/20130815000002_update_ticket_number.rb diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index b64818d65..12208abb6 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -333,7 +333,7 @@ class Channel::EmailParser UserInfo.current_user_id = user.id # get ticket# from subject - ticket = Ticket.number_check( mail[:subject] ) + ticket = Ticket::Number.check( mail[:subject] ) # set ticket state to open if not new if ticket diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 2b29a0611..bd02c8859 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -4,7 +4,7 @@ require 'time_calculation' require 'sla' class Ticket < ApplicationModel - before_create :number_generate, :check_defaults + before_create :check_generate, :check_defaults before_update :check_defaults before_destroy :destroy_dependencies after_create :notify_clients_after_create @@ -24,10 +24,6 @@ class Ticket < ApplicationModel attr_accessor :callback_loop - def self.number_check (string) - self.number_adapter.number_check_item(string) - end - def agent_of_group Group.find( self.group_id ).users.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq() end @@ -504,21 +500,6 @@ class Ticket < ApplicationModel return bind end - def self.number_adapter - - # load backend based on config - adapter_name = Setting.get('ticket_number') - adapter = nil - case adapter_name - when Symbol, String - require "ticket/number/#{adapter_name.to_s.downcase}" - adapter = Ticket::Number.const_get("#{adapter_name.to_s.capitalize}") - else - raise "Missing number_adapter '#{adapter_name}'" - end - return adapter - end - def self.escalation_calculation_rebuild ticket_state_list_open = Ticket::State.by_category( 'open' ) @@ -731,21 +712,11 @@ returns private - def number_generate + def check_generate return if self.number - - # generate number - (1..25_000).each do |i| - number = Ticket.number_adapter.number_generate_item() - ticket = Ticket.where( :number => number ).first - if ticket != nil - number = Ticket.number_adapter.number_generate_item() - else - self.number = number - return number - end - end + self.number = Ticket::Number.generate end + def check_defaults if !self.owner_id self.owner_id = 1 @@ -874,7 +845,4 @@ returns diff end - class Number - end - end diff --git a/app/models/ticket/number/date.rb b/app/models/ticket/number/date.rb index 57869822e..f0e2be455 100644 --- a/app/models/ticket/number/date.rb +++ b/app/models/ticket/number/date.rb @@ -3,7 +3,7 @@ module Ticket::Number::Date extend self - def number_generate_item + def generate # get config config = Setting.get('ticket_number_date') @@ -64,7 +64,7 @@ module Ticket::Number::Date end return number end - def number_check_item (string) + def check(string) # get config system_id = Setting.get('system_id') || '' diff --git a/app/models/ticket/number/increment.rb b/app/models/ticket/number/increment.rb index 8a9706c5f..d530785b8 100644 --- a/app/models/ticket/number/increment.rb +++ b/app/models/ticket/number/increment.rb @@ -3,7 +3,7 @@ module Ticket::Number::Increment extend self - def number_generate_item + def generate # get config config = Setting.get('ticket_number_increment') @@ -68,7 +68,7 @@ module Ticket::Number::Increment return number end - def number_check_item (string) + def check(string) # get config system_id = Setting.get('system_id') || '' diff --git a/db/migrate/20130815000002_update_ticket_number.rb b/db/migrate/20130815000002_update_ticket_number.rb new file mode 100644 index 000000000..05910c7cb --- /dev/null +++ b/db/migrate/20130815000002_update_ticket_number.rb @@ -0,0 +1,34 @@ +class UpdateTicketNumber < ActiveRecord::Migration + def up + Setting.create_or_update( + :title => 'Ticket Number Format', + :name => 'ticket_number', + :area => 'Ticket::Number', + :description => 'Selects the ticket number generator module. "Increment" increments the ticket + number, the SystemID and the counter are used with SystemID.Counter format (e.g. 1010138, 1010139). + With "Date" the ticket numbers will be generated by the current date, the SystemID and the counter. + The format looks like Year.Month.Day.SystemID.counter (e.g. 201206231010138, 201206231010139). + With param "Checksum => true" the counter will be appended as checksum to the string. The format + looks like SystemID.Counter.CheckSum (e. g. 10101384, 10101392) or Year.Month.Day.SystemID.Counter.CheckSum (e.g. 2012070110101520, 2012070110101535).', + :options => { + :form => [ + { + :display => '', + :null => true, + :name => 'ticket_number', + :tag => 'select', + :options => { + 'Ticket::Number::Increment' => 'Increment (SystemID.Counter)', + 'Ticket::Number::Date' => 'Date (Year.Month.Day.SystemID.Counter)', + }, + }, + ], + }, + :state => 'Ticket::Number::Increment', + :frontend => false + ) + end + def down + end +end + diff --git a/db/seeds.rb b/db/seeds.rb index b245f8cb3..0e373b66f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -712,13 +712,13 @@ Setting.create_if_not_exists( :name => 'ticket_number', :tag => 'select', :options => { - 'increment' => 'Increment (SystemID.Counter)', - 'date' => 'Date (Year.Month.Day.SystemID.Counter)', + 'Ticket::Number::Increment' => 'Increment (SystemID.Counter)', + 'Ticket::Number::Date' => 'Date (Year.Month.Day.SystemID.Counter)', }, }, ], }, - :state => 'increment', + :state => 'Ticket::Number::Increment', :frontend => false ) Setting.create_if_not_exists( From 521346a65034e1624445fbbe129680d7f2b0f941 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Aug 2013 00:21:47 +0200 Subject: [PATCH 12/29] Moved to separate Ticket::Number class/adapter. --- app/models/ticket/number.rb | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 app/models/ticket/number.rb diff --git a/app/models/ticket/number.rb b/app/models/ticket/number.rb new file mode 100644 index 000000000..1bcfd6403 --- /dev/null +++ b/app/models/ticket/number.rb @@ -0,0 +1,58 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +class Ticket::Number < ApplicationLib + +=begin + +generate new ticket number + + result = Ticket::Number.generate + +returns + + result = "1234556" # new ticket number + +=end + + def self.generate + + # generate number + (1..50_000).each { |i| + number = adapter.generate + ticket = Ticket.where( :number => number ).first + return number if !ticket + } + raise "Can't generate new ticket number!" + end + +=begin + +check if string contrains a valid ticket number + + result = Ticket::Number.check('some string [Ticket#123456]') + +returns + + result = ticket # Ticket model of ticket with matching ticket number + +=end + + def self.check(string) + adapter.check(string) + end + + def self.adapter + + # load backend based on config + adapter_name = Setting.get('ticket_number') + if !adapter_name + raise "Missing ticket_number setting option" + end + adapter = self.load_adapter(adapter_name) + if !adapter + raise "Can't load ticket_number adapter '#{adapter_name}'" + end + adapter + end +end + From 59876576c943e059f2b98a1cff76c566c88bdccc Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Aug 2013 16:30:51 +0200 Subject: [PATCH 13/29] Added more docu, moved escalation to extra file. --- app/models/ticket.rb | 409 +++++++++----------------------- app/models/ticket/escalation.rb | 280 ++++++++++++++++++++++ 2 files changed, 390 insertions(+), 299 deletions(-) create mode 100644 app/models/ticket/escalation.rb diff --git a/app/models/ticket.rb b/app/models/ticket.rb index bd02c8859..b8ad95db8 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -22,6 +22,8 @@ class Ticket < ApplicationModel belongs_to :create_article_type, :class_name => 'Ticket::Article::Type' belongs_to :create_article_sender, :class_name => 'Ticket::Article::Sender' + include Ticket::Escalation + attr_accessor :callback_loop def agent_of_group @@ -116,6 +118,20 @@ class Ticket < ApplicationModel } end +=begin + +merge tickets + + result = Ticket.find(123).merge_to( + :ticket_id => 123, + ) + +returns + + result = true|false + +=end + def merge_to(data) # update articles @@ -153,9 +169,18 @@ class Ticket < ApplicationModel self.save end - # def self.agent - # Role.where( :name => ['Agent'], :active => true ).first.users.where( :active => true ).uniq() - # end +=begin + +build new subject with ticket number in there + + ticket = Ticket.find(123) + result = ticket.subject_build('some subject') + +returns + + result = "[Ticket#1234567] some subject" + +=end def subject_build (subject) @@ -179,6 +204,19 @@ class Ticket < ApplicationModel return "[#{ticket_hook}#{ticket_hook_divider}#{self.number}] " + subject end +=begin + +clean subject remove ticket number and other not needed chars + + ticket = Ticket.find(123) + result = ticket.subject_clean('[Ticket#1234567] some subject') + +returns + + result = "some subject" + +=end + def subject_clean (subject) ticket_hook = Setting.get('ticket_hook') ticket_hook_divider = Setting.get('ticket_hook_divider') @@ -205,9 +243,19 @@ class Ticket < ApplicationModel return subject end - # ticket.permission( - # :current_user => 123 - # ) +=begin + +check if user has access to ticket + + ticket = Ticket.find(123) + result = ticket.permission( :current_user => User.find(123) ) + +returns + + result = true|false + +=end + def permission (data) # check customer @@ -237,11 +285,22 @@ class Ticket < ApplicationModel return false end - # Ticket.search( - # :current_user => 123, - # :query => 'search something', - # :limit => 15, - # ) +=begin + +search tickets + + result = Ticket.search( + :current_user => User.find(123), + :query => 'search something', + :limit => 15, + ) + +returns + + result = [ticket_model1, ticket_model2] + +=end + def self.search (params) # get params @@ -282,9 +341,21 @@ class Ticket < ApplicationModel return tickets end - # Ticket.overview_list( - # :current_user => 123, - # ) + +=begin + +overview list + + result = Ticket.overview_list( + :current_user => User.find(123), + ) + +returns + + result = [overview1, overview2] + +=end + def self.overview_list (data) # get customer overviews @@ -304,10 +375,25 @@ class Ticket < ApplicationModel return overviews end - # Ticket.overview( - # :view => 'some_view_url', - # :current_user => OBJECT, - # ) +=begin + +search tickets + + result = Ticket.overview_list( + :current_user => User.find(123), + :view => 'some_view_url', + ) + +returns + + result = { + :tickets => tickets, # [ticket1, ticket2, ticket3] + :tickets_count => tickets_count, # count of tickets + :overview => overview_selected_raw, # overview attributes + } + +=end + def self.overview (data) overviews = self.overview_list(data) @@ -500,175 +586,6 @@ class Ticket < ApplicationModel return bind end - def self.escalation_calculation_rebuild - ticket_state_list_open = Ticket::State.by_category( 'open' ) - - tickets = Ticket.where( :ticket_state_id => ticket_state_list_open ) - tickets.each {|ticket| - ticket.escalation_calculation - } - end - - def _escalation_calculation_get_sla - - sla_selected = nil - sla_list = Cache.get( 'SLA::List::Active' ) - if sla_list == nil - sla_list = Sla.where( :active => true ).all - Cache.write( 'SLA::List::Active', sla_list, { :expires_in => 1.hour } ) - end - sla_list.each {|sla| - if !sla.condition || sla.condition.empty? - sla_selected = sla - elsif sla.condition - hit = false - map = [ - [ 'tickets.ticket_priority_id', 'ticket_priority_id' ], - [ 'tickets.group_id', 'group_id' ] - ] - map.each {|item| - if sla.condition[ item[0] ] - if sla.condition[ item[0] ].class == String - sla.condition[ item[0] ] = [ sla.condition[ item[0] ] ] - end - if sla.condition[ item[0] ].include?( self[ item[1] ].to_s ) - hit = true - else - hit = false - end - end - } - if hit - sla_selected = sla - end - end - } - - return sla_selected - end - - def _escalation_calculation_higher_time(escalation_time, check_time, done_time) - return escalation_time if done_time - return check_time if !escalation_time - return escalation_time if !check_time - return check_time if escalation_time > check_time - return escalation_time - end - - def escalation_calculation - - # set escalation off if ticket is already closed - ticket_state = Ticket::State.lookup( :id => self.ticket_state_id ) - if ticket_state.ignore_escalation? - self.escalation_time = nil - # self.first_response_escal_date = nil - # self.close_time_escal_date = nil - self.callback_loop = true - self.save - return true - end - - # get sla for ticket - sla_selected = self._escalation_calculation_get_sla - - # reset escalation if no sla is set - if !sla_selected - self.escalation_time = nil - # self.first_response_escal_date = nil - # self.close_time_escal_date = nil - self.callback_loop = true - self.save - return true - end - - # puts sla_selected.inspect - # puts days.inspect - self.escalation_time = nil - self.first_response_escal_date = nil - self.update_time_escal_date = nil - self.close_time_escal_date = nil - - # first response - if sla_selected.first_response_time - - # get escalation date without pending time - self.first_response_escal_date = TimeCalculation.dest_time( self.created_at, sla_selected.first_response_time, sla_selected.data, sla_selected.timezone ) - - # get pending time between created and first response escal. time - time_in_pending = escalation_suspend( self.created_at, self.first_response_escal_date, 'relative', sla_selected, sla_selected.first_response_time ) - - # get new escalation time (original escal_date + time_in_pending) - self.first_response_escal_date = TimeCalculation.dest_time( self.first_response_escal_date, time_in_pending.to_i, sla_selected.data, sla_selected.timezone ) - - # set ticket escalation - self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.first_response_escal_date, self.first_response ) - end - if self.first_response# && !self.first_response_in_min - - # get response time in min between created and first response - self.first_response_in_min = escalation_suspend( self.created_at, self.first_response, 'real', sla_selected ) - - end - - # set time to show if sla is raised ot in - if sla_selected.first_response_time && self.first_response_in_min - self.first_response_diff_in_min = sla_selected.first_response_time - self.first_response_in_min - end - - - # update time - last_update = self.last_contact_agent - if !last_update - last_update = self.created_at - end - if sla_selected.update_time - self.update_time_escal_date = TimeCalculation.dest_time( last_update, sla_selected.update_time, sla_selected.data, sla_selected.timezone ) - - # get pending time between created and update escal. time - time_in_pending = escalation_suspend( last_update, self.update_time_escal_date, 'relative', sla_selected, sla_selected.update_time ) - - # get new escalation time (original escal_date + time_in_pending) - self.update_time_escal_date = TimeCalculation.dest_time( self.update_time_escal_date, time_in_pending.to_i, sla_selected.data, sla_selected.timezone ) - - # set ticket escalation - self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.update_time_escal_date, false ) - end - if self.last_contact_agent - self.update_time_in_min = TimeCalculation.business_time_diff( self.created_at, self.last_contact_agent, sla_selected.data, sla_selected.timezone ) - end - - # set sla time - if sla_selected.update_time && self.update_time_in_min - self.update_time_diff_in_min = sla_selected.update_time - self.update_time_in_min - end - - - # close time - if sla_selected.close_time - - # get escalation date without pending time - self.close_time_escal_date = TimeCalculation.dest_time( self.created_at, sla_selected.close_time, sla_selected.data, sla_selected.timezone ) - - # get pending time between created and close escal. time - extended_escalation = escalation_suspend( self.created_at, self.close_time_escal_date, 'relative', sla_selected, sla_selected.close_time ) - - # get new escalation time (original escal_date + time_in_pending) - self.close_time_escal_date = TimeCalculation.dest_time( self.close_time_escal_date, extended_escalation.to_i, sla_selected.data, sla_selected.timezone ) - - # set ticket escalation - self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.close_time_escal_date, self.close_time ) - end - if self.close_time # && !self.close_time_in_min - self.close_time_in_min = escalation_suspend( self.created_at, self.close_time, 'real', sla_selected ) - end - # set sla time - if sla_selected.close_time && self.close_time_in_min - self.close_time_diff_in_min = sla_selected.close_time - self.close_time_in_min - end - self.callback_loop = true - self.save - end - =begin list tickets by customer groupd in state categroie open and closed @@ -728,121 +645,15 @@ returns self.organization_id = customer.organization_id end end - - end + def destroy_dependencies - # delete history - History.remove( 'Ticket', self.id ) + # delete history + History.remove( 'Ticket', self.id ) - # delete articles - self.articles.destroy_all - end - - #type could be: - # real - time without supsend state - # relative - only suspend time - - def escalation_suspend (start_time, end_time, type, sla_selected, sla_time = 0) - if type == 'relative' - end_time += sla_time * 60 - end - total_time_without_pending = 0 - total_time = 0 - #get history for ticket - history_list = History.list( 'Ticket', self.id ) - - #loop through hist. changes and get time - last_state = nil - last_state_change = nil - last_state_is_pending = false - history_list.each { |history_item| - - # ignore if it isn't a state change - next if !history_item.history_attribute_id - history_attribute = History::Attribute.lookup( :id => history_item.history_attribute_id ); - next if history_attribute.name != 'ticket_state' - - # ignore all newer state before start_time - next if history_item.created_at < start_time - - # ignore all older state changes after end_time - next if last_state_change && last_state_change > end_time - - # if created_at is later then end_time, use end_time as last time - if history_item.created_at > end_time - history_item.created_at = end_time - end - - # get initial state and time - if !last_state - last_state = history_item.value_from - last_state_change = start_time - end - - # check if time need to be counted - counted = true - if history_item.value_from == 'pending' - counted = false - elsif history_item.value_from == 'close' - counted = false - end - - diff = escalation_time_diff( last_state_change, history_item.created_at, sla_selected ) - if counted - puts "Diff count #{history_item.value_from} -> #{history_item.value_to} / #{last_state_change} -> #{history_item.created_at}" - total_time_without_pending = total_time_without_pending + diff - else - puts "Diff not count #{history_item.value_from} -> #{history_item.value_to} / #{last_state_change} -> #{history_item.created_at}" - end - total_time = total_time + diff - - if history_item.value_to == 'pending' - last_state_is_pending = true - else - last_state_is_pending = false - end - - # remember for next loop last state - last_state = history_item.value_to - last_state_change = history_item.created_at - } - - # if last state isnt pending, count rest - if !last_state_is_pending && last_state_change && last_state_change < end_time - diff = escalation_time_diff( last_state_change, end_time, sla_selected ) - puts "Diff count last state was not pending #{diff.to_s} - #{last_state_change} - #{end_time}" - total_time_without_pending = total_time_without_pending + diff - total_time = total_time + diff - end - - # if we have not had any state change - if !last_state_change - diff = escalation_time_diff( start_time, end_time, sla_selected ) - puts 'Diff state has not changed ' + diff.to_s - total_time_without_pending = total_time_without_pending + diff - total_time = total_time + diff - end - - #return sum - if type == 'real' - return total_time_without_pending - elsif type == 'relative' - relative = total_time - total_time_without_pending - return relative - else - raise "ERROR: Unknown type #{type}" - end - end - - def escalation_time_diff( start_time, end_time, sla_selected ) - if sla_selected - diff = TimeCalculation.business_time_diff( start_time, end_time, sla_selected.data, sla_selected.timezone) - else - diff = TimeCalculation.business_time_diff( start_time, end_time ) - end - diff - end + # delete articles + self.articles.destroy_all + end end diff --git a/app/models/ticket/escalation.rb b/app/models/ticket/escalation.rb new file mode 100644 index 000000000..2a8fdbbbc --- /dev/null +++ b/app/models/ticket/escalation.rb @@ -0,0 +1,280 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Ticket::Escalation + + def self.escalation_calculation_rebuild + ticket_state_list_open = Ticket::State.by_category( 'open' ) + + tickets = Ticket.where( :ticket_state_id => ticket_state_list_open ) + tickets.each {|ticket| + ticket.escalation_calculation + } + end + + def escalation_calculation + + # set escalation off if ticket is already closed + ticket_state = Ticket::State.lookup( :id => self.ticket_state_id ) + if ticket_state.ignore_escalation? + self.escalation_time = nil + # self.first_response_escal_date = nil + # self.close_time_escal_date = nil + self.callback_loop = true + self.save + return true + end + + # get sla for ticket + sla_selected = self._escalation_calculation_get_sla + + # reset escalation if no sla is set + if !sla_selected + self.escalation_time = nil + # self.first_response_escal_date = nil + # self.close_time_escal_date = nil + self.callback_loop = true + self.save + return true + end + + # puts sla_selected.inspect + # puts days.inspect + self.escalation_time = nil + self.first_response_escal_date = nil + self.update_time_escal_date = nil + self.close_time_escal_date = nil + + # first response + if sla_selected.first_response_time + + # get escalation date without pending time + self.first_response_escal_date = TimeCalculation.dest_time( self.created_at, sla_selected.first_response_time, sla_selected.data, sla_selected.timezone ) + + # get pending time between created and first response escal. time + time_in_pending = escalation_suspend( self.created_at, self.first_response_escal_date, 'relative', sla_selected, sla_selected.first_response_time ) + + # get new escalation time (original escal_date + time_in_pending) + self.first_response_escal_date = TimeCalculation.dest_time( self.first_response_escal_date, time_in_pending.to_i, sla_selected.data, sla_selected.timezone ) + + # set ticket escalation + self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.first_response_escal_date, self.first_response ) + end + if self.first_response# && !self.first_response_in_min + + # get response time in min between created and first response + self.first_response_in_min = escalation_suspend( self.created_at, self.first_response, 'real', sla_selected ) + + end + + # set time to show if sla is raised ot in + if sla_selected.first_response_time && self.first_response_in_min + self.first_response_diff_in_min = sla_selected.first_response_time - self.first_response_in_min + end + + + # update time + last_update = self.last_contact_agent + if !last_update + last_update = self.created_at + end + if sla_selected.update_time + self.update_time_escal_date = TimeCalculation.dest_time( last_update, sla_selected.update_time, sla_selected.data, sla_selected.timezone ) + + # get pending time between created and update escal. time + time_in_pending = escalation_suspend( last_update, self.update_time_escal_date, 'relative', sla_selected, sla_selected.update_time ) + + # get new escalation time (original escal_date + time_in_pending) + self.update_time_escal_date = TimeCalculation.dest_time( self.update_time_escal_date, time_in_pending.to_i, sla_selected.data, sla_selected.timezone ) + + # set ticket escalation + self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.update_time_escal_date, false ) + end + if self.last_contact_agent + self.update_time_in_min = TimeCalculation.business_time_diff( self.created_at, self.last_contact_agent, sla_selected.data, sla_selected.timezone ) + end + + # set sla time + if sla_selected.update_time && self.update_time_in_min + self.update_time_diff_in_min = sla_selected.update_time - self.update_time_in_min + end + + + # close time + if sla_selected.close_time + + # get escalation date without pending time + self.close_time_escal_date = TimeCalculation.dest_time( self.created_at, sla_selected.close_time, sla_selected.data, sla_selected.timezone ) + + # get pending time between created and close escal. time + extended_escalation = escalation_suspend( self.created_at, self.close_time_escal_date, 'relative', sla_selected, sla_selected.close_time ) + + # get new escalation time (original escal_date + time_in_pending) + self.close_time_escal_date = TimeCalculation.dest_time( self.close_time_escal_date, extended_escalation.to_i, sla_selected.data, sla_selected.timezone ) + + # set ticket escalation + self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.close_time_escal_date, self.close_time ) + end + if self.close_time # && !self.close_time_in_min + self.close_time_in_min = escalation_suspend( self.created_at, self.close_time, 'real', sla_selected ) + end + # set sla time + if sla_selected.close_time && self.close_time_in_min + self.close_time_diff_in_min = sla_selected.close_time - self.close_time_in_min + end + self.callback_loop = true + self.save + end + + private + + #type could be: + # real - time without supsend state + # relative - only suspend time + + def escalation_suspend (start_time, end_time, type, sla_selected, sla_time = 0) + if type == 'relative' + end_time += sla_time * 60 + end + total_time_without_pending = 0 + total_time = 0 + #get history for ticket + history_list = History.list( 'Ticket', self.id ) + + #loop through hist. changes and get time + last_state = nil + last_state_change = nil + last_state_is_pending = false + history_list.each { |history_item| + + # ignore if it isn't a state change + next if !history_item.history_attribute_id + history_attribute = History::Attribute.lookup( :id => history_item.history_attribute_id ); + next if history_attribute.name != 'ticket_state' + + # ignore all newer state before start_time + next if history_item.created_at < start_time + + # ignore all older state changes after end_time + next if last_state_change && last_state_change > end_time + + # if created_at is later then end_time, use end_time as last time + if history_item.created_at > end_time + history_item.created_at = end_time + end + + # get initial state and time + if !last_state + last_state = history_item.value_from + last_state_change = start_time + end + + # check if time need to be counted + counted = true + if history_item.value_from == 'pending' + counted = false + elsif history_item.value_from == 'close' + counted = false + end + + diff = escalation_time_diff( last_state_change, history_item.created_at, sla_selected ) + if counted + puts "Diff count #{history_item.value_from} -> #{history_item.value_to} / #{last_state_change} -> #{history_item.created_at}" + total_time_without_pending = total_time_without_pending + diff + else + puts "Diff not count #{history_item.value_from} -> #{history_item.value_to} / #{last_state_change} -> #{history_item.created_at}" + end + total_time = total_time + diff + + if history_item.value_to == 'pending' + last_state_is_pending = true + else + last_state_is_pending = false + end + + # remember for next loop last state + last_state = history_item.value_to + last_state_change = history_item.created_at + } + + # if last state isnt pending, count rest + if !last_state_is_pending && last_state_change && last_state_change < end_time + diff = escalation_time_diff( last_state_change, end_time, sla_selected ) + puts "Diff count last state was not pending #{diff.to_s} - #{last_state_change} - #{end_time}" + total_time_without_pending = total_time_without_pending + diff + total_time = total_time + diff + end + + # if we have not had any state change + if !last_state_change + diff = escalation_time_diff( start_time, end_time, sla_selected ) + puts 'Diff state has not changed ' + diff.to_s + total_time_without_pending = total_time_without_pending + diff + total_time = total_time + diff + end + + #return sum + if type == 'real' + return total_time_without_pending + elsif type == 'relative' + relative = total_time - total_time_without_pending + return relative + else + raise "ERROR: Unknown type #{type}" + end + end + + def escalation_time_diff( start_time, end_time, sla_selected ) + if sla_selected + diff = TimeCalculation.business_time_diff( start_time, end_time, sla_selected.data, sla_selected.timezone) + else + diff = TimeCalculation.business_time_diff( start_time, end_time ) + end + diff + end + + def _escalation_calculation_get_sla + + sla_selected = nil + sla_list = Cache.get( 'SLA::List::Active' ) + if sla_list == nil + sla_list = Sla.where( :active => true ).all + Cache.write( 'SLA::List::Active', sla_list, { :expires_in => 1.hour } ) + end + sla_list.each {|sla| + if !sla.condition || sla.condition.empty? + sla_selected = sla + elsif sla.condition + hit = false + map = [ + [ 'tickets.ticket_priority_id', 'ticket_priority_id' ], + [ 'tickets.group_id', 'group_id' ] + ] + map.each {|item| + if sla.condition[ item[0] ] + if sla.condition[ item[0] ].class == String + sla.condition[ item[0] ] = [ sla.condition[ item[0] ] ] + end + if sla.condition[ item[0] ].include?( self[ item[1] ].to_s ) + hit = true + else + hit = false + end + end + } + if hit + sla_selected = sla + end + end + } + + return sla_selected + end + + def _escalation_calculation_higher_time(escalation_time, check_time, done_time) + return escalation_time if done_time + return check_time if !escalation_time + return escalation_time if !check_time + return check_time if escalation_time > check_time + return escalation_time + end +end \ No newline at end of file From 37154837d3a436222c4ba01f92f61852286e28dd Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Aug 2013 16:38:16 +0200 Subject: [PATCH 14/29] Fixed syntax error. --- app/models/ticket/escalation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/ticket/escalation.rb b/app/models/ticket/escalation.rb index 2a8fdbbbc..86007685c 100644 --- a/app/models/ticket/escalation.rb +++ b/app/models/ticket/escalation.rb @@ -25,7 +25,7 @@ module Ticket::Escalation end # get sla for ticket - sla_selected = self._escalation_calculation_get_sla + sla_selected = _escalation_calculation_get_sla # reset escalation if no sla is set if !sla_selected From d49478d5821cf84c50c8726ea852a182165e0cfc Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Aug 2013 20:35:49 +0200 Subject: [PATCH 15/29] Improved naming. --- app/models/sla.rb | 2 +- app/models/ticket/escalation.rb | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/sla.rb b/app/models/sla.rb index a73ed4ff6..af26476e0 100644 --- a/app/models/sla.rb +++ b/app/models/sla.rb @@ -14,6 +14,6 @@ class Sla < ApplicationModel private def escalation_calculation_rebuild Cache.delete( 'SLA::List::Active' ) - Ticket.escalation_calculation_rebuild + Ticket::Escalation.rebuild_all end end diff --git a/app/models/ticket/escalation.rb b/app/models/ticket/escalation.rb index 86007685c..e5cc7ef8f 100644 --- a/app/models/ticket/escalation.rb +++ b/app/models/ticket/escalation.rb @@ -2,7 +2,7 @@ module Ticket::Escalation - def self.escalation_calculation_rebuild + def self.rebuild_all ticket_state_list_open = Ticket::State.by_category( 'open' ) tickets = Ticket.where( :ticket_state_id => ticket_state_list_open ) @@ -25,7 +25,7 @@ module Ticket::Escalation end # get sla for ticket - sla_selected = _escalation_calculation_get_sla + sla_selected = escalation_calculation_get_sla # reset escalation if no sla is set if !sla_selected @@ -57,7 +57,7 @@ module Ticket::Escalation self.first_response_escal_date = TimeCalculation.dest_time( self.first_response_escal_date, time_in_pending.to_i, sla_selected.data, sla_selected.timezone ) # set ticket escalation - self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.first_response_escal_date, self.first_response ) + self.escalation_time = calculation_higher_time( self.escalation_time, self.first_response_escal_date, self.first_response ) end if self.first_response# && !self.first_response_in_min @@ -87,7 +87,7 @@ module Ticket::Escalation self.update_time_escal_date = TimeCalculation.dest_time( self.update_time_escal_date, time_in_pending.to_i, sla_selected.data, sla_selected.timezone ) # set ticket escalation - self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.update_time_escal_date, false ) + self.escalation_time = calculation_higher_time( self.escalation_time, self.update_time_escal_date, false ) end if self.last_contact_agent self.update_time_in_min = TimeCalculation.business_time_diff( self.created_at, self.last_contact_agent, sla_selected.data, sla_selected.timezone ) @@ -112,7 +112,7 @@ module Ticket::Escalation self.close_time_escal_date = TimeCalculation.dest_time( self.close_time_escal_date, extended_escalation.to_i, sla_selected.data, sla_selected.timezone ) # set ticket escalation - self.escalation_time = self._escalation_calculation_higher_time( self.escalation_time, self.close_time_escal_date, self.close_time ) + self.escalation_time = calculation_higher_time( self.escalation_time, self.close_time_escal_date, self.close_time ) end if self.close_time # && !self.close_time_in_min self.close_time_in_min = escalation_suspend( self.created_at, self.close_time, 'real', sla_selected ) @@ -232,7 +232,7 @@ module Ticket::Escalation diff end - def _escalation_calculation_get_sla + def escalation_calculation_get_sla sla_selected = nil sla_list = Cache.get( 'SLA::List::Active' ) @@ -270,7 +270,7 @@ module Ticket::Escalation return sla_selected end - def _escalation_calculation_higher_time(escalation_time, check_time, done_time) + def calculation_higher_time(escalation_time, check_time, done_time) return escalation_time if done_time return check_time if !escalation_time return escalation_time if !check_time From 3436db7a3b46b0c13da9278f82aa3b0077f7d464 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Aug 2013 20:44:07 +0200 Subject: [PATCH 16/29] Need require for ruby 1.9. --- db/migrate/20130815000002_update_ticket_number.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/migrate/20130815000002_update_ticket_number.rb b/db/migrate/20130815000002_update_ticket_number.rb index 05910c7cb..a2f0775af 100644 --- a/db/migrate/20130815000002_update_ticket_number.rb +++ b/db/migrate/20130815000002_update_ticket_number.rb @@ -1,3 +1,4 @@ +require 'setting' class UpdateTicketNumber < ActiveRecord::Migration def up Setting.create_or_update( @@ -30,5 +31,4 @@ class UpdateTicketNumber < ActiveRecord::Migration end def down end -end - +end \ No newline at end of file From bdb581cfd804db1e07adbb6820eb9dfb180a8110 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 16 Aug 2013 20:45:38 +0200 Subject: [PATCH 17/29] Added autoload for mysql test env. --- config/environments/test_mysql.rb | 45 +++++++++++++++++++ .../20130815000002_update_ticket_number.rb | 1 - 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/config/environments/test_mysql.rb b/config/environments/test_mysql.rb index e69de29bb..f3e3b8f66 100644 --- a/config/environments/test_mysql.rb +++ b/config/environments/test_mysql.rb @@ -0,0 +1,45 @@ +Zammad::Application.configure do + # Settings specified here will take precedence over those in config/application.rb + + # The test environment is used exclusively to run your application's + # test suite. You never need to work with it otherwise. Remember that + # your test database is "scratch space" for the test suite and is wiped + # and recreated between test runs. Don't rely on the data there! + config.cache_classes = true + + # Configure static asset server for tests with Cache-Control for performance + config.serve_static_assets = true + config.static_cache_control = "public, max-age=3600" + + config.assets.compress = false + config.assets.compile = true + config.assets.digest = true + + # Log error messages when you accidentally call methods on nil + config.whiny_nils = true + + # Show full error reports and disable caching + config.consider_all_requests_local = true + config.action_controller.perform_caching = true + + # Raise exceptions instead of rendering exception templates + config.action_dispatch.show_exceptions = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Raise exception on mass assignment protection for Active Record models + config.active_record.mass_assignment_sanitizer = :strict + + # Print deprecation notices to the stderr + config.active_support.deprecation = :stderr + + # Disable request forgery protection in test environment + config.action_controller.allow_forgery_protection = false + + # autoload on + config.dependency_loading = true + +end diff --git a/db/migrate/20130815000002_update_ticket_number.rb b/db/migrate/20130815000002_update_ticket_number.rb index a2f0775af..6e79cb300 100644 --- a/db/migrate/20130815000002_update_ticket_number.rb +++ b/db/migrate/20130815000002_update_ticket_number.rb @@ -1,4 +1,3 @@ -require 'setting' class UpdateTicketNumber < ActiveRecord::Migration def up Setting.create_or_update( From f26059d0f8e107a01f8ba3cf93b25a5b2e8dde17 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 18:09:06 +0200 Subject: [PATCH 18/29] Added support for ruby 1.9. --- lib/application_lib.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/application_lib.rb b/lib/application_lib.rb index 9b1b40f3f..142743394 100644 --- a/lib/application_lib.rb +++ b/lib/application_lib.rb @@ -8,7 +8,7 @@ load adapter based on setting option returns - result = adapter_class + result = Some::Classname =end @@ -24,17 +24,27 @@ returns load adapter - result = self.load_adapter( 'some_class_name' ) + result = self.load_adapter( 'Some::Classname' ) returns - result = adapter_class + result = Some::Classname =end def self.load_adapter(adapter) # load adapter - Object.const_get(adapter) + + # will only work on ruby 2.0 +# Object.const_get(adapter) + + # will work on ruby 1.9 and 2.0 +# adapter.split('::').inject(Object) do |mod, class_name| +# mod.const_get(class_name) +# end + + # will work with active_support + adapter.constantize end end From 687f7716b4a35ee8d9a90e2b8f5de8b89dd7093d Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 18:09:19 +0200 Subject: [PATCH 19/29] Added doc. --- app/models/ticket/escalation.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app/models/ticket/escalation.rb b/app/models/ticket/escalation.rb index e5cc7ef8f..085071369 100644 --- a/app/models/ticket/escalation.rb +++ b/app/models/ticket/escalation.rb @@ -2,6 +2,18 @@ module Ticket::Escalation +=begin + +rebuild escalations for all open tickets + + result = Ticket::Escalation.rebuild_all + +returns + + result = true + +=end + def self.rebuild_all ticket_state_list_open = Ticket::State.by_category( 'open' ) @@ -11,6 +23,19 @@ module Ticket::Escalation } end +=begin + +rebuild escalation for ticket + + ticket = Ticket.find(123) + result = ticket.escalation_calculation + +returns + + result = true + +=end + def escalation_calculation # set escalation off if ticket is already closed From 4c4c1e599fb48d52b87e6247d8d687e80fa82598 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 22:04:57 +0200 Subject: [PATCH 20/29] Split of app/model/ticket.rb to app/model/ticket/subject.rb and app/model/ticketoverview.rb. --- .../ticket_overviews_controller.rb | 6 +- app/models/ticket.rb | 321 +----------------- app/models/ticket/overview.rb | 253 ++++++++++++++ app/models/ticket/subject.rb | 78 +++++ lib/session.rb | 11 +- 5 files changed, 342 insertions(+), 327 deletions(-) create mode 100644 app/models/ticket/overview.rb create mode 100644 app/models/ticket/subject.rb diff --git a/app/controllers/ticket_overviews_controller.rb b/app/controllers/ticket_overviews_controller.rb index ac3cdeb09..97ff4b23f 100644 --- a/app/controllers/ticket_overviews_controller.rb +++ b/app/controllers/ticket_overviews_controller.rb @@ -8,7 +8,7 @@ class TicketOverviewsController < ApplicationController # get navbar overview data if !params[:view] - result = Ticket.overview( + result = Ticket::Overview.list( :current_user => current_user, ) render :json => result @@ -17,7 +17,7 @@ class TicketOverviewsController < ApplicationController # get real overview data if params[:array] - overview = Ticket.overview( + overview = Ticket::Overview.list( :view => params[:view], :current_user => current_user, :array => true, @@ -36,7 +36,7 @@ class TicketOverviewsController < ApplicationController } return end - overview = Ticket.overview( + overview = Ticket::Overview.list( :view => params[:view], # :view_mode => params[:view_mode], :current_user => User.find( current_user.id ), diff --git a/app/models/ticket.rb b/app/models/ticket.rb index b8ad95db8..a4c0ab51c 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -23,6 +23,7 @@ class Ticket < ApplicationModel belongs_to :create_article_sender, :class_name => 'Ticket::Article::Sender' include Ticket::Escalation + include Ticket::Subject attr_accessor :callback_loop @@ -122,7 +123,8 @@ class Ticket < ApplicationModel merge tickets - result = Ticket.find(123).merge_to( + ticket = Ticket.find(123) + result = ticket.merge_to( :ticket_id => 123, ) @@ -171,80 +173,6 @@ returns =begin -build new subject with ticket number in there - - ticket = Ticket.find(123) - result = ticket.subject_build('some subject') - -returns - - result = "[Ticket#1234567] some subject" - -=end - - def subject_build (subject) - - # clena subject - subject = self.subject_clean(subject) - - ticket_hook = Setting.get('ticket_hook') - ticket_hook_divider = Setting.get('ticket_hook_divider') - - # none position - if Setting.get('ticket_hook_position') == 'none' - return subject - end - - # right position - if Setting.get('ticket_hook_position') == 'right' - return subject + " [#{ticket_hook}#{ticket_hook_divider}#{self.number}] " - end - - # left position - return "[#{ticket_hook}#{ticket_hook_divider}#{self.number}] " + subject - end - -=begin - -clean subject remove ticket number and other not needed chars - - ticket = Ticket.find(123) - result = ticket.subject_clean('[Ticket#1234567] some subject') - -returns - - result = "some subject" - -=end - - def subject_clean (subject) - ticket_hook = Setting.get('ticket_hook') - ticket_hook_divider = Setting.get('ticket_hook_divider') - ticket_subject_size = Setting.get('ticket_subject_size') - - # remove all possible ticket hook formats with [] - subject = subject.gsub /\[#{ticket_hook}: #{self.number}\](\s+?|)/, '' - subject = subject.gsub /\[#{ticket_hook}:#{self.number}\](\s+?|)/, '' - subject = subject.gsub /\[#{ticket_hook}#{ticket_hook_divider}#{self.number}\](\s+?|)/, '' - - # remove all possible ticket hook formats without [] - subject = subject.gsub /#{ticket_hook}: #{self.number}(\s+?|)/, '' - subject = subject.gsub /#{ticket_hook}:#{self.number}(\s+?|)/, '' - subject = subject.gsub /#{ticket_hook}#{ticket_hook_divider}#{self.number}(\s+?|)/, '' - - # remove leading "..:\s" and "..[\d+]:\s" e. g. "Re: " or "Re[5]: " - subject = subject.gsub /^(..(\[\d+\])?:\s)+/, '' - - # resize subject based on config - if subject.length > ticket_subject_size.to_i - subject = subject[ 0, ticket_subject_size.to_i ] + '[...]' - end - - return subject - end - -=begin - check if user has access to ticket ticket = Ticket.find(123) @@ -342,249 +270,6 @@ returns return tickets end -=begin - -overview list - - result = Ticket.overview_list( - :current_user => User.find(123), - ) - -returns - - result = [overview1, overview2] - -=end - - def self.overview_list (data) - - # get customer overviews - if data[:current_user].is_role('Customer') - role = data[:current_user].is_role( 'Customer' ) - if data[:current_user].organization_id && data[:current_user].organization.shared - overviews = Overview.where( :role_id => role.id, :active => true ) - else - overviews = Overview.where( :role_id => role.id, :organization_shared => false, :active => true ) - end - return overviews - end - - # get agent overviews - role = data[:current_user].is_role( 'Agent' ) - overviews = Overview.where( :role_id => role.id, :active => true ) - return overviews - end - -=begin - -search tickets - - result = Ticket.overview_list( - :current_user => User.find(123), - :view => 'some_view_url', - ) - -returns - - result = { - :tickets => tickets, # [ticket1, ticket2, ticket3] - :tickets_count => tickets_count, # count of tickets - :overview => overview_selected_raw, # overview attributes - } - -=end - - def self.overview (data) - - overviews = self.overview_list(data) - - # build up attributes hash - overview_selected = nil - overview_selected_raw = nil - - overviews.each { |overview| - - # remember selected view - if data[:view] && data[:view] == overview.link - overview_selected = overview - overview_selected_raw = Marshal.load( Marshal.dump(overview.attributes) ) - end - - # replace e.g. 'current_user.id' with current_user.id - overview.condition.each { |item, value | - if value && value.class.to_s == 'String' - parts = value.split( '.', 2 ) - if parts[0] && parts[1] && parts[0] == 'current_user' - overview.condition[item] = data[:current_user][parts[1].to_sym] - end - end - } - } - - if data[:view] && !overview_selected - return - end - - # sortby - # prio - # state - # group - # customer - - # order - # asc - # desc - - # groupby - # prio - # state - # group - # customer - - # all = attributes[:myopenassigned] - # all.merge( { :group_id => groups } ) - - # @tickets = Ticket.where(:group_id => groups, attributes[:myopenassigned] ).limit(params[:limit]) - # get only tickets with permissions - if data[:current_user].is_role('Customer') - group_ids = Group.select( 'groups.id' ). - where( 'groups.active = ?', true ). - map( &:id ) - else - group_ids = Group.select( 'groups.id' ).joins(:users). - where( 'groups_users.user_id = ?', [ data[:current_user].id ] ). - where( 'groups.active = ?', true ). - map( &:id ) - end - - # overview meta for navbar - if !overview_selected - - # loop each overview - result = [] - overviews.each { |overview| - - # get count - count = Ticket.where( :group_id => group_ids ).where( self._condition( overview.condition ) ).count() - - # get meta info - all = { - :name => overview.name, - :prio => overview.prio, - :link => overview.link, - } - - # push to result data - result.push all.merge( { :count => count } ) - } - return result - end - - # get result list - if data[:array] - order_by = overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s - if overview_selected.group_by && !overview_selected.group_by.empty? - order_by = overview_selected.group_by + '_id, ' + order_by - end - tickets = Ticket.select( 'id' ). - where( :group_id => group_ids ). - where( self._condition( overview_selected.condition ) ). - order( order_by ). - limit( 500 ) - - ticket_ids = [] - tickets.each { |ticket| - ticket_ids.push ticket.id - } - - tickets_count = Ticket.where( :group_id => group_ids ). - where( self._condition( overview_selected.condition ) ). - count() - - return { - :ticket_list => ticket_ids, - :tickets_count => tickets_count, - :overview => overview_selected_raw, - } - end - - # get tickets for overview - data[:start_page] ||= 1 - tickets = Ticket.where( :group_id => group_ids ). - where( self._condition( overview_selected.condition ) ). - order( overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s )#. - # limit( overview_selected.view[ data[:view_mode].to_sym ][:per_page] ). - # offset( overview_selected.view[ data[:view_mode].to_sym ][:per_page].to_i * ( data[:start_page].to_i - 1 ) ) - - tickets_count = Ticket.where( :group_id => group_ids ). - where( self._condition( overview_selected.condition ) ). - count() - - return { - :tickets => tickets, - :tickets_count => tickets_count, - :overview => overview_selected_raw, - } - - end - def self._condition(condition) - sql = '' - bind = [nil] - condition.each {|key, value| - if sql != '' - sql += ' AND ' - end - if value.class == Array - sql += " #{key} IN (?)" - bind.push value - elsif value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess - time = Time.now - if value['area'] == 'minute' - if value['direction'] == 'last' - time -= value['count'].to_i * 60 - else - time += value['count'].to_i * 60 - end - elsif value['area'] == 'hour' - if value['direction'] == 'last' - time -= value['count'].to_i * 60 * 60 - else - time += value['count'].to_i * 60 * 60 - end - elsif value['area'] == 'day' - if value['direction'] == 'last' - time -= value['count'].to_i * 60 * 60 * 24 - else - time += value['count'].to_i * 60 * 60 * 24 - end - elsif value['area'] == 'month' - if value['direction'] == 'last' - time -= value['count'].to_i * 60 * 60 * 24 * 31 - else - time += value['count'].to_i * 60 * 60 * 24 * 31 - end - elsif value['area'] == 'year' - if value['direction'] == 'last' - time -= value['count'].to_i * 60 * 60 * 24 * 365 - else - time += value['count'].to_i * 60 * 60 * 24 * 365 - end - end - if value['direction'] == 'last' - sql += " #{key} > ?" - bind.push time - else - sql += " #{key} < ?" - bind.push time - end - else - sql += " #{key} = ?" - bind.push value - end - } - bind[0] = sql - return bind - end =begin diff --git a/app/models/ticket/overview.rb b/app/models/ticket/overview.rb new file mode 100644 index 000000000..3bf731452 --- /dev/null +++ b/app/models/ticket/overview.rb @@ -0,0 +1,253 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +require 'Overview' + +class Ticket::Overview + +=begin + +all overview by user + + result = Ticket::Overview.all( + :current_user => User.find(123), + ) + +returns + + result = [overview1, overview2] + +=end + + def self.all (data) + + # get customer overviews + if data[:current_user].is_role('Customer') + role = data[:current_user].is_role( 'Customer' ) + if data[:current_user].organization_id && data[:current_user].organization.shared + overviews = Overview.where( :role_id => role.id, :active => true ) + else + overviews = Overview.where( :role_id => role.id, :organization_shared => false, :active => true ) + end + return overviews + end + + # get agent overviews + role = data[:current_user].is_role( 'Agent' ) + overviews = Overview.where( :role_id => role.id, :active => true ) + return overviews + end + +=begin + +selected overview by user + + result = Ticket::Overview.list( + :current_user => User.find(123), + :view => 'some_view_url', + ) + +returns + + result = { + :tickets => tickets, # [ticket1, ticket2, ticket3] + :tickets_count => tickets_count, # count of tickets + :overview => overview_selected_raw, # overview attributes + } + +=end + + def self.list (data) + + overviews = self.all(data) + + # build up attributes hash + overview_selected = nil + overview_selected_raw = nil + + overviews.each { |overview| + + # remember selected view + if data[:view] && data[:view] == overview.link + overview_selected = overview + overview_selected_raw = Marshal.load( Marshal.dump(overview.attributes) ) + end + + # replace e.g. 'current_user.id' with current_user.id + overview.condition.each { |item, value | + if value && value.class.to_s == 'String' + parts = value.split( '.', 2 ) + if parts[0] && parts[1] && parts[0] == 'current_user' + overview.condition[item] = data[:current_user][parts[1].to_sym] + end + end + } + } + + if data[:view] && !overview_selected + raise "No such view '#{ data[:view] }'" + end + + # sortby + # prio + # state + # group + # customer + + # order + # asc + # desc + + # groupby + # prio + # state + # group + # customer + + # all = attributes[:myopenassigned] + # all.merge( { :group_id => groups } ) + + # @tickets = Ticket.where(:group_id => groups, attributes[:myopenassigned] ).limit(params[:limit]) + # get only tickets with permissions + if data[:current_user].is_role('Customer') + group_ids = Group.select( 'groups.id' ). + where( 'groups.active = ?', true ). + map( &:id ) + else + group_ids = Group.select( 'groups.id' ).joins(:users). + where( 'groups_users.user_id = ?', [ data[:current_user].id ] ). + where( 'groups.active = ?', true ). + map( &:id ) + end + + # overview meta for navbar + if !overview_selected + + # loop each overview + result = [] + overviews.each { |overview| + + # get count + count = Ticket.where( :group_id => group_ids ).where( _condition( overview.condition ) ).count() + + # get meta info + all = { + :name => overview.name, + :prio => overview.prio, + :link => overview.link, + } + + # push to result data + result.push all.merge( { :count => count } ) + } + return result + end + + # get result list + if data[:array] + order_by = overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s + if overview_selected.group_by && !overview_selected.group_by.empty? + order_by = overview_selected.group_by + '_id, ' + order_by + end + tickets = Ticket.select( 'id' ). + where( :group_id => group_ids ). + where( _condition( overview_selected.condition ) ). + order( order_by ). + limit( 500 ) + + ticket_ids = [] + tickets.each { |ticket| + ticket_ids.push ticket.id + } + + tickets_count = Ticket.where( :group_id => group_ids ). + where( _condition( overview_selected.condition ) ). + count() + + return { + :ticket_list => ticket_ids, + :tickets_count => tickets_count, + :overview => overview_selected_raw, + } + end + + # get tickets for overview + data[:start_page] ||= 1 + tickets = Ticket.where( :group_id => group_ids ). + where( _condition( overview_selected.condition ) ). + order( overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s )#. + # limit( overview_selected.view[ data[:view_mode].to_sym ][:per_page] ). + # offset( overview_selected.view[ data[:view_mode].to_sym ][:per_page].to_i * ( data[:start_page].to_i - 1 ) ) + + tickets_count = Ticket.where( :group_id => group_ids ). + where( _condition( overview_selected.condition ) ). + count() + + return { + :tickets => tickets, + :tickets_count => tickets_count, + :overview => overview_selected_raw, + } + + end + + private + def self._condition(condition) + sql = '' + bind = [nil] + condition.each {|key, value| + if sql != '' + sql += ' AND ' + end + if value.class == Array + sql += " #{key} IN (?)" + bind.push value + elsif value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess + time = Time.now + if value['area'] == 'minute' + if value['direction'] == 'last' + time -= value['count'].to_i * 60 + else + time += value['count'].to_i * 60 + end + elsif value['area'] == 'hour' + if value['direction'] == 'last' + time -= value['count'].to_i * 60 * 60 + else + time += value['count'].to_i * 60 * 60 + end + elsif value['area'] == 'day' + if value['direction'] == 'last' + time -= value['count'].to_i * 60 * 60 * 24 + else + time += value['count'].to_i * 60 * 60 * 24 + end + elsif value['area'] == 'month' + if value['direction'] == 'last' + time -= value['count'].to_i * 60 * 60 * 24 * 31 + else + time += value['count'].to_i * 60 * 60 * 24 * 31 + end + elsif value['area'] == 'year' + if value['direction'] == 'last' + time -= value['count'].to_i * 60 * 60 * 24 * 365 + else + time += value['count'].to_i * 60 * 60 * 24 * 365 + end + end + if value['direction'] == 'last' + sql += " #{key} > ?" + bind.push time + else + sql += " #{key} < ?" + bind.push time + end + else + sql += " #{key} = ?" + bind.push value + end + } + bind[0] = sql + return bind + end + +end \ No newline at end of file diff --git a/app/models/ticket/subject.rb b/app/models/ticket/subject.rb new file mode 100644 index 000000000..ff3c491b1 --- /dev/null +++ b/app/models/ticket/subject.rb @@ -0,0 +1,78 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Ticket::Subject + +=begin + +build new subject with ticket number in there + + ticket = Ticket.find(123) + result = ticket.subject_build('some subject') + +returns + + result = "[Ticket#1234567] some subject" + +=end + + def subject_build (subject) + + # clena subject + subject = self.subject_clean(subject) + + ticket_hook = Setting.get('ticket_hook') + ticket_hook_divider = Setting.get('ticket_hook_divider') + + # none position + if Setting.get('ticket_hook_position') == 'none' + return subject + end + + # right position + if Setting.get('ticket_hook_position') == 'right' + return subject + " [#{ticket_hook}#{ticket_hook_divider}#{self.number}] " + end + + # left position + return "[#{ticket_hook}#{ticket_hook_divider}#{self.number}] " + subject + end + +=begin + +clean subject remove ticket number and other not needed chars + + ticket = Ticket.find(123) + result = ticket.subject_clean('[Ticket#1234567] some subject') + +returns + + result = "some subject" + +=end + + def subject_clean (subject) + ticket_hook = Setting.get('ticket_hook') + ticket_hook_divider = Setting.get('ticket_hook_divider') + ticket_subject_size = Setting.get('ticket_subject_size') + + # remove all possible ticket hook formats with [] + subject = subject.gsub /\[#{ticket_hook}: #{self.number}\](\s+?|)/, '' + subject = subject.gsub /\[#{ticket_hook}:#{self.number}\](\s+?|)/, '' + subject = subject.gsub /\[#{ticket_hook}#{ticket_hook_divider}#{self.number}\](\s+?|)/, '' + + # remove all possible ticket hook formats without [] + subject = subject.gsub /#{ticket_hook}: #{self.number}(\s+?|)/, '' + subject = subject.gsub /#{ticket_hook}:#{self.number}(\s+?|)/, '' + subject = subject.gsub /#{ticket_hook}#{ticket_hook_divider}#{self.number}(\s+?|)/, '' + + # remove leading "..:\s" and "..[\d+]:\s" e. g. "Re: " or "Re[5]: " + subject = subject.gsub /^(..(\[\d+\])?:\s)+/, '' + + # resize subject based on config + if subject.length > ticket_subject_size.to_i + subject = subject[ 0, ticket_subject_size.to_i ] + '[...]' + end + + return subject + end +end \ No newline at end of file diff --git a/lib/session.rb b/lib/session.rb index 01a9ec569..333239b42 100644 --- a/lib/session.rb +++ b/lib/session.rb @@ -395,7 +395,7 @@ class UserState # overview cache_key = @cache_key + '_overview' if CacheIn.expired(cache_key) - overview = Ticket.overview( + overview = Ticket::Overview.list( :current_user => user, ) overview_cache = CacheIn.get( cache_key, { :re_expire => true } ) @@ -405,20 +405,19 @@ class UserState # puts overview.inspect # puts '------' # puts overview_cache.inspect - CacheIn.set( cache_key, overview, { :expires_in => 3.seconds } ) + CacheIn.set( cache_key, overview, { :expires_in => 4.seconds } ) end end # overview lists - overviews = Ticket.overview_list( + overviews = Ticket::Overview.all( :current_user => user, ) overviews.each { |overview| cache_key = @cache_key + '_overview_data_' + overview.link if CacheIn.expired(cache_key) - overview_data = Ticket.overview( + overview_data = Ticket::Overview.list( :view => overview.link, -# :view_mode => params[:view_mode], :current_user => user, :array => true, ) @@ -638,7 +637,7 @@ class ClientState end # overview_data - overviews = Ticket.overview_list( + overviews = Ticket::Overview.all( :current_user => user, ) overviews.each { |overview| From e78890133c3dc3a7cf75eaafc42a452f01ed95fe Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 22:12:27 +0200 Subject: [PATCH 21/29] Added doc. --- app/models/application_model.rb | 149 +++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 3 deletions(-) diff --git a/app/models/application_model.rb b/app/models/application_model.rb index 79fc64f05..5ddeb65e5 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -25,6 +25,18 @@ class ApplicationModel < ActiveRecord::Base end end +=begin + +remove all not used model attributes of params + + result = Model.param_cleanup(params) + +returns + + result = params # params with valid attributes of model + +=end + def self.param_cleanup(params) # only use object attributes @@ -40,6 +52,18 @@ class ApplicationModel < ActiveRecord::Base self.param_validation(data) end +=begin + +remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id) + + result = Model.param_validation(params) + +returns + + result = params # params without listed attributes + +=end + def self.param_validation(data) # we do want to set this via database @@ -51,7 +75,20 @@ class ApplicationModel < ActiveRecord::Base data end - # set created_by_id & updated_by_id if not given based on UserInfo +=begin + +set created_by_id & updated_by_id if not given based on UserInfo (current session) + +Used as before_create callback, no own use needed + + result = Model.fill_up_user_create(params) + +returns + + result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session) + +=end + def fill_up_user_create if self.class.column_names.include? 'updated_by_id' if UserInfo.current_user_id @@ -71,7 +108,20 @@ class ApplicationModel < ActiveRecord::Base end end - # set updated_by_id if not given based on UserInfo +=begin + +set updated_by_id if not given based on UserInfo (current session) + +Used as before_update callback, no own use needed + + result = Model.fill_up_user_update(params) + +returns + + result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session) + +=end + def fill_up_user_update return if !self.class.column_names.include? 'updated_by_id' if UserInfo.current_user_id @@ -129,6 +179,20 @@ class ApplicationModel < ActiveRecord::Base Cache.get( key.to_s ) end +=begin + +lookup model from cache (if exists) or retrieve it from db, id, name or login possible + + result = Model.lookup( :id => 123 ) + result = Model.lookup( :name => 'some name' ) + result = Model.lookup( :login => 'some login' ) + +returns + + result = model # with all attributes + +=end + def self.lookup(data) if data[:id] # puts "GET- + #{self.to_s}.#{data[:id].to_s}" @@ -168,6 +232,18 @@ class ApplicationModel < ActiveRecord::Base end end +=begin + +create model if not exists (check exists based on id, name, login or locale) + + result = Model.create_if_not_exists( attributes ) + +returns + + result = model # with all attributes + +=end + def self.create_if_not_exists(data) if data[:id] record = self.where( :id => data[:id] ).first @@ -191,6 +267,18 @@ class ApplicationModel < ActiveRecord::Base self.create(data) end +=begin + +create or update model (check exists based on name, login or locale) + + result = Model.create_or_update( attributes ) + +returns + + result = model # with all attributes + +=end + def self.create_or_update(data) if data[:name] records = self.where( :name => data[:name] ) @@ -214,11 +302,37 @@ class ApplicationModel < ActiveRecord::Base record = self.new( data ) record.save return record + elsif data[:locale] + records = self.where( :locale => data[:locale] ) + records.each {|record| + if record.locale.downcase == data[:locale].downcase + record.update_attributes( data ) + return record + end + } + record = self.new( data ) + record.save + return record else - raise "Need name or login for create_or_update()" + raise "Need name, login or locale for create_or_update()" end end +=begin + +notify_clients_after_create after model got created + +used as callback in model file + +class OwnModel < ApplicationModel + after_create :notify_clients_after_create + after_update :notify_clients_after_update + after_destroy :notify_clients_after_destroy + + [...] + +=end + def notify_clients_after_create # return if we run import mode @@ -232,6 +346,21 @@ class ApplicationModel < ActiveRecord::Base ) end +=begin + +notify_clients_after_update after model got updated + +used as callback in model file + +class OwnModel < ApplicationModel + after_create :notify_clients_after_create + after_update :notify_clients_after_update + after_destroy :notify_clients_after_destroy + + [...] + +=end + def notify_clients_after_update # return if we run import mode @@ -245,6 +374,20 @@ class ApplicationModel < ActiveRecord::Base ) end +=begin + +notify_clients_after_destroy after model got destroyed + +used as callback in model file + +class OwnModel < ApplicationModel + after_create :notify_clients_after_create + after_update :notify_clients_after_update + after_destroy :notify_clients_after_destroy + + [...] + +=end def notify_clients_after_destroy # return if we run import mode From d2b1b26646bd57d00e691e23ef132bbcfd0a67d6 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 22:19:26 +0200 Subject: [PATCH 22/29] Fixed syntax error. --- app/models/ticket/overview.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/ticket/overview.rb b/app/models/ticket/overview.rb index 3bf731452..6a900a464 100644 --- a/app/models/ticket/overview.rb +++ b/app/models/ticket/overview.rb @@ -1,6 +1,6 @@ # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ -require 'Overview' +require 'overview' class Ticket::Overview From 4b623f9e8e78cd129b7abe9f35743f5af3236c1d Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 23:10:11 +0200 Subject: [PATCH 23/29] Split of app/model/ticket.rb to app/model/ticket/search.rb and app/model/permission.rb. --- .../ticket_overviews_controller.rb | 2 +- app/controllers/tickets_controller.rb | 9 +- app/models/ticket.rb | 234 +----------------- app/models/ticket/permission.rb | 47 ++++ app/models/ticket/screen_options.rb | 170 +++++++++++++ app/models/ticket/search.rb | 62 +++++ lib/session.rb | 4 +- 7 files changed, 291 insertions(+), 237 deletions(-) create mode 100644 app/models/ticket/permission.rb create mode 100644 app/models/ticket/screen_options.rb create mode 100644 app/models/ticket/search.rb diff --git a/app/controllers/ticket_overviews_controller.rb b/app/controllers/ticket_overviews_controller.rb index 97ff4b23f..63b2e1c77 100644 --- a/app/controllers/ticket_overviews_controller.rb +++ b/app/controllers/ticket_overviews_controller.rb @@ -70,7 +70,7 @@ class TicketOverviewsController < ApplicationController group_ids.push group.id } agents = {} - Ticket.agents.each { |user| + Ticket::ScreenOptions.agents.each { |user| agents[ user.id ] = 1 } groups_users = {} diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index a9bf0893b..860579b1a 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -111,7 +111,7 @@ class TicketsController < ApplicationController def ticket_customer # return result - result = Ticket.list_by_customer( + result = Ticket::ScreenOptions.list_by_customer( :customer_id => params[:customer_id], :limit => 15, ) @@ -217,6 +217,9 @@ class TicketsController < ApplicationController if !users[ data['created_by_id'] ] users[ data['created_by_id'] ] = User.user_data_full( data['created_by_id'] ) end + if !users[ data['updated_by_id'] ] + users[ data['updated_by_id'] ] = User.user_data_full( data['updated_by_id'] ) + end } recent_viewed = RecentView.list_fulldata( current_user, 8 ) @@ -326,7 +329,7 @@ class TicketsController < ApplicationController end # get attributes to update - attributes_to_change = Ticket.attributes_to_change( :user => current_user, :ticket => ticket ) + attributes_to_change = Ticket::ScreenOptions.attributes_to_change( :user => current_user, :ticket => ticket ) attributes_to_change[:owner_id].each { |user_id| if !users[user_id] @@ -384,7 +387,7 @@ class TicketsController < ApplicationController def ticket_create # get attributes to update - attributes_to_change = Ticket.attributes_to_change( + attributes_to_change = Ticket::ScreenOptions.attributes_to_change( :user => current_user, # :ticket_id => params[:ticket_id], # :article_id => params[:article_id] diff --git a/app/models/ticket.rb b/app/models/ticket.rb index a4c0ab51c..ecb724fa3 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -24,6 +24,8 @@ class Ticket < ApplicationModel include Ticket::Escalation include Ticket::Subject + extend Ticket::Search + extend Ticket::Permission attr_accessor :callback_loop @@ -31,94 +33,6 @@ class Ticket < ApplicationModel Group.find( self.group_id ).users.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq() end - def self.agents - User.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq() - end - - def self.attributes_to_change(params) - if params[:ticket_id] - params[:ticket] = self.find( params[:ticket_id] ) - end - if params[:article_id] - params[:article] = self.find( params[:article_id] ) - end - - # get ticket states - ticket_state_ids = [] - if params[:ticket] - ticket_state_type = params[:ticket].ticket_state.state_type - end - ticket_state_types = ['open', 'closed', 'pending action', 'pending reminder'] - if ticket_state_type && !ticket_state_types.include?(ticket_state_type.name) - ticket_state_ids.push params[:ticket].ticket_state.id - end - ticket_state_types.each {|type| - ticket_state_type = Ticket::StateType.where( :name => type ).first - if ticket_state_type - ticket_state_type.states.each {|ticket_state| - ticket_state_ids.push ticket_state.id - } - end - } - - # get owner - owner_ids = [] - if params[:ticket] - params[:ticket].agent_of_group.each { |user| - owner_ids.push user.id - } - end - - # get group - group_ids = [] - Group.where( :active => true ).each { |group| - group_ids.push group.id - } - - # get group / user relations - agents = {} - Ticket.agents.each { |user| - agents[ user.id ] = 1 - } - groups_users = {} - group_ids.each {|group_id| - groups_users[ group_id ] = [] - Group.find( group_id ).users.each {|user| - next if !agents[ user.id ] - groups_users[ group_id ].push user.id - } - } - - # get priorities - ticket_priority_ids = [] - Ticket::Priority.where( :active => true ).each { |priority| - ticket_priority_ids.push priority.id - } - - ticket_article_type_ids = [] - if params[:ticket] - ticket_article_types = ['note', 'phone'] - if params[:ticket].group.email_address_id - ticket_article_types.push 'email' - end - ticket_article_types.each {|ticket_article_type_name| - ticket_article_type = Ticket::Article::Type.lookup( :name => ticket_article_type_name ) - if ticket_article_type - ticket_article_type_ids.push ticket_article_type.id - end - } - end - - return { - :ticket_article_type_id => ticket_article_type_ids, - :ticket_state_id => ticket_state_ids, - :ticket_priority_id => ticket_priority_ids, - :owner_id => owner_ids, - :group_id => group_ids, - :group_id__owner_id => groups_users, - } - end - =begin merge tickets @@ -171,147 +85,6 @@ returns self.save end -=begin - -check if user has access to ticket - - ticket = Ticket.find(123) - result = ticket.permission( :current_user => User.find(123) ) - -returns - - result = true|false - -=end - - def permission (data) - - # check customer - if data[:current_user].is_role('Customer') - - # access ok if its own ticket - return true if self.customer_id == data[:current_user].id - - # access ok if its organization ticket - if data[:current_user].organization_id && self.organization_id - return true if self.organization_id == data[:current_user].organization_id - end - - # no access - return false - end - - # check agent - - # access if requestor is owner - return true if self.owner_id == data[:current_user].id - - # access if requestor is in group - data[:current_user].groups.each {|group| - return true if self.group.id == group.id - } - return false - end - -=begin - -search tickets - - result = Ticket.search( - :current_user => User.find(123), - :query => 'search something', - :limit => 15, - ) - -returns - - result = [ticket_model1, ticket_model2] - -=end - - def self.search (params) - - # get params - query = params[:query] - limit = params[:limit] || 12 - current_user = params[:current_user] - - conditions = [] - if current_user.is_role('Agent') - group_ids = Group.select( 'groups.id' ).joins(:users). - where( 'groups_users.user_id = ?', current_user.id ). - where( 'groups.active = ?', true ). - map( &:id ) - conditions = [ 'group_id IN (?)', group_ids ] - else - if !current_user.organization || ( !current_user.organization.shared || current_user.organization.shared == false ) - conditions = [ 'customer_id = ?', current_user.id ] - else - conditions = [ '( customer_id = ? OR organization_id = ? )', current_user.id, current_user.organization.id ] - end - end - - # do query - tickets_all = Ticket.select('DISTINCT(tickets.id)'). - where(conditions). - where( '( `tickets`.`title` LIKE ? OR `tickets`.`number` LIKE ? OR `ticket_articles`.`body` LIKE ? OR `ticket_articles`.`from` LIKE ? OR `ticket_articles`.`to` LIKE ? OR `ticket_articles`.`subject` LIKE ?)', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" ). - joins(:articles). - limit(limit). - order('`tickets`.`created_at` DESC') - - # build result list - tickets = [] - users = {} - tickets_all.each do |ticket| - ticket_tmp = Ticket.lookup( :id => ticket.id ) - tickets.push ticket_tmp - end - - return tickets - end - - -=begin - -list tickets by customer groupd in state categroie open and closed - - result = Ticket.list_by_customer( - :customer_id => 123, - :limit => 15, # optional, default 15 - ) - -returns - - result = { - :open => tickets_open, - :closed => tickets_closed, - } - -=end - - def self.list_by_customer(data) - - # get closed/open states - ticket_state_list_open = Ticket::State.by_category( 'open' ) - ticket_state_list_closed = Ticket::State.by_category( 'closed' ) - - # get tickets - tickets_open = Ticket.where( - :customer_id => data[:customer_id], - :ticket_state_id => ticket_state_list_open - ).limit( data[:limit] || 15 ).order('created_at DESC') - - tickets_closed = Ticket.where( - :customer_id => data[:customer_id], - :ticket_state_id => ticket_state_list_closed - ).limit( data[:limit] || 15 ).order('created_at DESC') - - return { - :open => tickets_open, - :closed => tickets_closed, - } - end - private def check_generate @@ -323,10 +96,9 @@ returns if !self.owner_id self.owner_id = 1 end - # if self.customer_id && ( !self.organization_id || self.organization_id.empty? ) if self.customer_id customer = User.find( self.customer_id ) - if self.organization_id != customer.organization_id + if self.organization_id != customer.organization_id self.organization_id = customer.organization_id end end diff --git a/app/models/ticket/permission.rb b/app/models/ticket/permission.rb new file mode 100644 index 000000000..5f7b3caf6 --- /dev/null +++ b/app/models/ticket/permission.rb @@ -0,0 +1,47 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Ticket::Permission + +=begin + +check if user has access to ticket + + ticket = Ticket.find(123) + result = ticket.permission( :current_user => User.find(123) ) + +returns + + result = true|false + +=end + + def permission (data) + + # check customer + if data[:current_user].is_role('Customer') + + # access ok if its own ticket + return true if self.customer_id == data[:current_user].id + + # access ok if its organization ticket + if data[:current_user].organization_id && self.organization_id + return true if self.organization_id == data[:current_user].organization_id + end + + # no access + return false + end + + # check agent + + # access if requestor is owner + return true if self.owner_id == data[:current_user].id + + # access if requestor is in group + data[:current_user].groups.each {|group| + return true if self.group.id == group.id + } + return false + end + +end \ No newline at end of file diff --git a/app/models/ticket/screen_options.rb b/app/models/ticket/screen_options.rb new file mode 100644 index 000000000..d91bf2620 --- /dev/null +++ b/app/models/ticket/screen_options.rb @@ -0,0 +1,170 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Ticket::ScreenOptions + +=begin + +list of active agents + + result = Ticket::ScreenOptions.agents() + +returns + + result = [user1, user2] + +=end + + def self.agents + User.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq() + end + +=begin + +list attributes + + result = Ticket::ScreenOptions.attributes_to_change( + :ticket_id => 123, + :article_id => 123, + + :ticket => ticket_model, + ) + +returns + + result = { + :ticket_article_type_id => ticket_article_type_ids, + :ticket_state_id => ticket_state_ids, + :ticket_priority_id => ticket_priority_ids, + :owner_id => owner_ids, + :group_id => group_ids, + :group_id__owner_id => groups_users, + } + +=end + + def self.attributes_to_change(params) + if params[:ticket_id] + params[:ticket] = self.find( params[:ticket_id] ) + end + if params[:article_id] + params[:article] = self.find( params[:article_id] ) + end + + # get ticket states + ticket_state_ids = [] + if params[:ticket] + ticket_state_type = params[:ticket].ticket_state.state_type + end + ticket_state_types = ['open', 'closed', 'pending action', 'pending reminder'] + if ticket_state_type && !ticket_state_types.include?(ticket_state_type.name) + ticket_state_ids.push params[:ticket].ticket_state.id + end + ticket_state_types.each {|type| + ticket_state_type = Ticket::StateType.where( :name => type ).first + if ticket_state_type + ticket_state_type.states.each {|ticket_state| + ticket_state_ids.push ticket_state.id + } + end + } + + # get owner + owner_ids = [] + if params[:ticket] + params[:ticket].agent_of_group.each { |user| + owner_ids.push user.id + } + end + + # get group + group_ids = [] + Group.where( :active => true ).each { |group| + group_ids.push group.id + } + + # get group / user relations + agents = {} + Ticket::ScreenOptions.agents.each { |user| + agents[ user.id ] = 1 + } + groups_users = {} + group_ids.each {|group_id| + groups_users[ group_id ] = [] + Group.find( group_id ).users.each {|user| + next if !agents[ user.id ] + groups_users[ group_id ].push user.id + } + } + + # get priorities + ticket_priority_ids = [] + Ticket::Priority.where( :active => true ).each { |priority| + ticket_priority_ids.push priority.id + } + + ticket_article_type_ids = [] + if params[:ticket] + ticket_article_types = ['note', 'phone'] + if params[:ticket].group.email_address_id + ticket_article_types.push 'email' + end + ticket_article_types.each {|ticket_article_type_name| + ticket_article_type = Ticket::Article::Type.lookup( :name => ticket_article_type_name ) + if ticket_article_type + ticket_article_type_ids.push ticket_article_type.id + end + } + end + + return { + :ticket_article_type_id => ticket_article_type_ids, + :ticket_state_id => ticket_state_ids, + :ticket_priority_id => ticket_priority_ids, + :owner_id => owner_ids, + :group_id => group_ids, + :group_id__owner_id => groups_users, + } + end + +=begin + +list tickets by customer groupd in state categroie open and closed + + result = Ticket::ScreenOptions.list_by_customer( + :customer_id => 123, + :limit => 15, # optional, default 15 + ) + +returns + + result = { + :open => tickets_open, + :closed => tickets_closed, + } + +=end + + def self.list_by_customer(data) + + # get closed/open states + ticket_state_list_open = Ticket::State.by_category( 'open' ) + ticket_state_list_closed = Ticket::State.by_category( 'closed' ) + + # get tickets + tickets_open = Ticket.where( + :customer_id => data[:customer_id], + :ticket_state_id => ticket_state_list_open + ).limit( data[:limit] || 15 ).order('created_at DESC') + + tickets_closed = Ticket.where( + :customer_id => data[:customer_id], + :ticket_state_id => ticket_state_list_closed + ).limit( data[:limit] || 15 ).order('created_at DESC') + + return { + :open => tickets_open, + :closed => tickets_closed, + } + end + +end \ No newline at end of file diff --git a/app/models/ticket/search.rb b/app/models/ticket/search.rb new file mode 100644 index 000000000..42437fc53 --- /dev/null +++ b/app/models/ticket/search.rb @@ -0,0 +1,62 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Ticket::Search + +=begin + +search tickets + + result = Ticket.search( + :current_user => User.find(123), + :query => 'search something', + :limit => 15, + ) + +returns + + result = [ticket_model1, ticket_model2] + +=end + + def self.search (params) + + # get params + query = params[:query] + limit = params[:limit] || 12 + current_user = params[:current_user] + + conditions = [] + if current_user.is_role('Agent') + group_ids = Group.select( 'groups.id' ).joins(:users). + where( 'groups_users.user_id = ?', current_user.id ). + where( 'groups.active = ?', true ). + map( &:id ) + conditions = [ 'group_id IN (?)', group_ids ] + else + if !current_user.organization || ( !current_user.organization.shared || current_user.organization.shared == false ) + conditions = [ 'customer_id = ?', current_user.id ] + else + conditions = [ '( customer_id = ? OR organization_id = ? )', current_user.id, current_user.organization.id ] + end + end + + # do query + tickets_all = Ticket.select('DISTINCT(tickets.id)'). + where(conditions). + where( '( `tickets`.`title` LIKE ? OR `tickets`.`number` LIKE ? OR `ticket_articles`.`body` LIKE ? OR `ticket_articles`.`from` LIKE ? OR `ticket_articles`.`to` LIKE ? OR `ticket_articles`.`subject` LIKE ?)', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" ). + joins(:articles). + limit(limit). + order('`tickets`.`created_at` DESC') + + # build result list + tickets = [] + users = {} + tickets_all.each do |ticket| + ticket_tmp = Ticket.lookup( :id => ticket.id ) + tickets.push ticket_tmp + end + + return tickets + end + +end \ No newline at end of file diff --git a/lib/session.rb b/lib/session.rb index 333239b42..aefc416c8 100644 --- a/lib/session.rb +++ b/lib/session.rb @@ -433,7 +433,7 @@ class UserState # create_attributes cache_key = @cache_key + '_ticket_create_attributes' if CacheIn.expired(cache_key) - ticket_create_attributes = Ticket.attributes_to_change( + ticket_create_attributes = Ticket::ScreenOptions.attributes_to_change( :current_user_id => user.id, ) ticket_create_attributes_cache = CacheIn.get( cache_key, { :re_expire => true } ) @@ -660,7 +660,7 @@ class ClientState group_ids.push group.id } agents = {} - Ticket.agents.each { |user| + Ticket::ScreenOptions.agents.each { |user| agents[ user.id ] = 1 } groups_users = {} From 9fa835014d87724c16c3db5f17f3f7823ca56835 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 23:10:36 +0200 Subject: [PATCH 24/29] Split of app/model/ticket.rb to app/model/ticket/search.rb and app/model/permission.rb. --- app/models/ticket.rb | 17 ++++++++++++++++- app/models/ticket/search.rb | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index ecb724fa3..e8631a3ac 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -24,11 +24,26 @@ class Ticket < ApplicationModel include Ticket::Escalation include Ticket::Subject + include Ticket::Permission extend Ticket::Search - extend Ticket::Permission attr_accessor :callback_loop +=begin + +merge tickets + + ticket = Ticket.find(123) + result = ticket.merge_to( + :ticket_id => 123, + ) + +returns + + result = true|false + +=end + def agent_of_group Group.find( self.group_id ).users.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq() end diff --git a/app/models/ticket/search.rb b/app/models/ticket/search.rb index 42437fc53..c51158d70 100644 --- a/app/models/ticket/search.rb +++ b/app/models/ticket/search.rb @@ -18,7 +18,7 @@ returns =end - def self.search (params) + def search (params) # get params query = params[:query] From 622419ac751c1eb4c7904afc9c840977c980b206 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 23:13:34 +0200 Subject: [PATCH 25/29] Fixed pod. --- app/models/ticket.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index e8631a3ac..84c01b645 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -31,16 +31,14 @@ class Ticket < ApplicationModel =begin -merge tickets +list of agents in group of ticket ticket = Ticket.find(123) - result = ticket.merge_to( - :ticket_id => 123, - ) + result = ticket.agent_of_group returns - result = true|false + result = [user1, user2, ...] =end From cb7809cef938d20d255095fff83465ebafe902f0 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 23:48:01 +0200 Subject: [PATCH 26/29] Rewrite of Auth and SSO module layer. --- app/models/user.rb | 82 ++++---------------------------------------- db/seeds.rb | 4 +-- lib/auth.rb | 63 ++++++++++++++++++++++++++++++++++ lib/auth/internal.rb | 6 ++-- lib/auth/ldap.rb | 6 ++-- lib/auth/otrs.rb | 7 ++-- lib/auth/test.rb | 6 ++-- lib/sso.rb | 63 ++++++++++++++++++++++++++++++++++ lib/sso/env.rb | 6 ++-- lib/sso/otrs.rb | 6 ++-- 10 files changed, 153 insertions(+), 96 deletions(-) create mode 100644 lib/auth.rb create mode 100644 lib/sso.rb diff --git a/app/models/user.rb b/app/models/user.rb index 393e47d7a..3fb778fce 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -60,96 +60,26 @@ class User < ApplicationModel return false end - # use auth backends - config = [ - { - :adapter => 'internal', - }, - { - :adapter => 'test', - }, - ] - Setting.where( :area => 'Security::Authentication' ).each {|setting| - if setting.state[:value] - config.push setting.state[:value] - end - } - - # try to login against configure auth backends - user_auth = nil - config.each {|config_item| - next if !config_item[:adapter] - next if config_item.class == TrueClass - file = "auth/#{config_item[:adapter]}" - require file - user_auth = Auth.const_get("#{config_item[:adapter].to_s.upcase}").check( username, password, config_item, user ) - - # auth ok - if user_auth - - # remember last login date - user_auth.update_last_login - - # reset login failed - user_auth.login_failed = 0 - user_auth.save - - return user_auth - end - } + user_auth = Auth.check( username, password, user ) # set login failed +1 if !user_auth && user + sleep 1 user.login_failed = user.login_failed + 1 user.save end - # auth failed - sleep 1 + # auth ok return user_auth end def self.sso(params) - # use auth backends - config = [ - { - :adapter => 'env', - }, - { - :adapter => 'otrs', - }, - ] - # Setting.where( :area => 'Security::Authentication' ).each {|setting| - # if setting.state[:value] - # config.push setting.state[:value] - # end - # } - # try to login against configure auth backends - user_auth = nil - config.each {|config_item| - next if !config_item[:adapter] - next if config_item.class == TrueClass - file = "sso/#{config_item[:adapter]}" - require file - user_auth = SSO.const_get("#{config_item[:adapter].to_s.upcase}").check( params, config_item ) + user_auth = Sso.check( params, user ) + return if !user_auth - # auth ok - if user_auth - - # remember last login date - user_auth.update_last_login - - # reset login failed - user_auth.login_failed = 0 - user_auth.save - - return user_auth - end - } - - return false + return user_auth end def self.create_from_hash!(hash) diff --git a/db/seeds.rb b/db/seeds.rb index 0e373b66f..597b082bf 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -250,7 +250,7 @@ Setting.create_if_not_exists( :area => 'Security::Authentication', :description => 'Enables user authentication via OTRS.', :state => { - :adapter => 'otrs', + :adapter => 'Auth::Otrs', :required_group_ro => 'stats', :group_rw_role_map => { 'admin' => 'Admin', @@ -271,7 +271,7 @@ Setting.create_if_not_exists( :area => 'Security::Authentication', :description => 'Enables user authentication via LDAP.', :state => { - :adapter => 'ldap', + :adapter => 'Auth::Ldap', :host => 'localhost', :port => 389, :bind_dn => 'cn=Manager,dc=example,dc=org', diff --git a/lib/auth.rb b/lib/auth.rb new file mode 100644 index 000000000..85b1e0867 --- /dev/null +++ b/lib/auth.rb @@ -0,0 +1,63 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +class Auth < ApplicationLib + +=begin + +authenticate user via username and password + + result = Auth.check( username, password, user ) + +returns + + result = user_model # if authentication was successfully + +=end + + def self.check(username, password, user) + + # use std. auth backends + config = [ + { + :adapter => 'Auth::Internal', + }, + { + :adapter => 'Auth::Test', + }, + ] + + # added configured backends + Setting.where( :area => 'Security::Authentication' ).each {|setting| + if setting.state[:value] + config.push setting.state[:value] + end + } + + # try to login against configure auth backends + user_auth = nil + config.each {|config_item| + next if !config_item[:adapter] + + # load backend + backend = self.load_adapter( config_item[:adapter] ) + return if !backend + + user_auth = backend.check( username, password, config_item, user ) + + # auth ok + if user_auth + + # remember last login date + user_auth.update_last_login + + # reset login failed + user_auth.login_failed = 0 + user_auth.save + + return user_auth + end + } + return + + end +end diff --git a/lib/auth/internal.rb b/lib/auth/internal.rb index 923eb3136..48d69bc8b 100644 --- a/lib/auth/internal.rb +++ b/lib/auth/internal.rb @@ -1,6 +1,6 @@ -module Auth -end -module Auth::INTERNAL +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Auth::Internal def self.check( username, password, config, user ) # return if no user exists diff --git a/lib/auth/ldap.rb b/lib/auth/ldap.rb index 99dcea5fe..f72426973 100644 --- a/lib/auth/ldap.rb +++ b/lib/auth/ldap.rb @@ -1,8 +1,8 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + require 'net/ldap' -module Auth -end -module Auth::LDAP +module Auth::Ldap def self.check( username, password, config, user ) scope = Net::LDAP::SearchScope_WholeSubtree diff --git a/lib/auth/otrs.rb b/lib/auth/otrs.rb index a0ad88336..3219d86cb 100644 --- a/lib/auth/otrs.rb +++ b/lib/auth/otrs.rb @@ -1,7 +1,8 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + require 'import/otrs' -module Auth -end -module Auth::OTRS + +module Auth::Otrs def self.check( username, password, config, user ) endpoint = Setting.get('import_otrs_endpoint') diff --git a/lib/auth/test.rb b/lib/auth/test.rb index cf68a85be..a06a2f330 100644 --- a/lib/auth/test.rb +++ b/lib/auth/test.rb @@ -1,6 +1,6 @@ -module Auth -end -module Auth::TEST +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Auth::Test def self.check( username, password, config, user ) # development systems diff --git a/lib/sso.rb b/lib/sso.rb new file mode 100644 index 000000000..4715723e9 --- /dev/null +++ b/lib/sso.rb @@ -0,0 +1,63 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +class Sso < ApplicationLib + +=begin + +authenticate user via username and password + + result = Sso.check( params, config_item ) + +returns + + result = user_model # if authentication was successfully + +=end + + def self.check(params) + + # use std. auth backends + config = [ + { + :adapter => 'Sso::Env', + }, + { + :adapter => 'Sso::Otrs', + }, + ] + + # added configured backends + Setting.where( :area => 'Security::SSO' ).each {|setting| + if setting.state[:value] + config.push setting.state[:value] + end + } + + # try to login against configure auth backends + user_auth = nil + config.each {|config_item| + next if !config_item[:adapter] + + # load backend + backend = self.load_adapter( config_item[:adapter] ) + return if !backend + + user_auth = backend.check( params, config_item ) + + # auth ok + if user_auth + + # remember last login date + user_auth.update_last_login + + # reset login failed + user_auth.login_failed = 0 + user_auth.save + + return user_auth + end + } + return + + end +end diff --git a/lib/sso/env.rb b/lib/sso/env.rb index 6904719e1..633658ced 100644 --- a/lib/sso/env.rb +++ b/lib/sso/env.rb @@ -1,6 +1,6 @@ -module SSO -end -module SSO::ENV +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Sso::Env def self.check( params, config_item ) # try to find user based on login diff --git a/lib/sso/otrs.rb b/lib/sso/otrs.rb index 349ab47cf..660fa3e38 100644 --- a/lib/sso/otrs.rb +++ b/lib/sso/otrs.rb @@ -1,6 +1,6 @@ -module SSO -end -module SSO::OTRS +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Sso::Otrs def self.check( params, config_item ) endpoint = Setting.get('import_otrs_endpoint') From 300453e2c13352ea0498b7c472a2d6193df51645 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 23:48:48 +0200 Subject: [PATCH 27/29] Added file header. --- lib/geo_ip.rb | 4 +++- lib/geo_ip/freegeoip.rb | 2 ++ lib/geo_location.rb | 2 ++ lib/geo_location/gmaps.rb | 2 ++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/geo_ip.rb b/lib/geo_ip.rb index cea0858af..098b00e03 100644 --- a/lib/geo_ip.rb +++ b/lib/geo_ip.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + class GeoIp < ApplicationLib =begin @@ -33,4 +35,4 @@ returns # db lookup backend.location(address) end -end +end \ No newline at end of file diff --git a/lib/geo_ip/freegeoip.rb b/lib/geo_ip/freegeoip.rb index 2a6abbd76..cdf25a643 100644 --- a/lib/geo_ip/freegeoip.rb +++ b/lib/geo_ip/freegeoip.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + require 'faraday' require 'cache' diff --git a/lib/geo_location.rb b/lib/geo_location.rb index 4a574154f..c79608f47 100644 --- a/lib/geo_location.rb +++ b/lib/geo_location.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + class GeoLocation < ApplicationLib =begin diff --git a/lib/geo_location/gmaps.rb b/lib/geo_location/gmaps.rb index 5a703003d..707a40fb2 100644 --- a/lib/geo_location/gmaps.rb +++ b/lib/geo_location/gmaps.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + class GeoLocation::Gmaps def self.geocode(address) From 098f1f845f0de465f3b5ab84d345a236222c67b7 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 17 Aug 2013 23:58:22 +0200 Subject: [PATCH 28/29] Rewrite of Auth and SSO module layer. --- test/unit/auth_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/auth_test.rb b/test/unit/auth_test.rb index 32e86767f..443f5c4d4 100644 --- a/test/unit/auth_test.rb +++ b/test/unit/auth_test.rb @@ -7,7 +7,7 @@ Setting.create_or_update( :area => 'Security::Authentication', :description => 'Enables user authentication via LDAP.', :state => { - :adapter => 'ldap', + :adapter => 'Auth::Ldap', :host => 'localhost', :port => 389, :bind_dn => 'cn=Manager,dc=example,dc=org', @@ -45,6 +45,7 @@ else :created_by_id => 1 ) end + class AuthTest < ActiveSupport::TestCase test 'auth' do tests = [ From e4bfb82e063b0ce0c2f0c84a9a6f1db551b530e4 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 18 Aug 2013 00:10:02 +0200 Subject: [PATCH 29/29] Added doc. --- app/models/user.rb | 128 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 3fb778fce..4f4fcf8a8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,6 +19,19 @@ class User < ApplicationModel store :preferences +=begin + +fullname of user + + user = User.find(123) + result = user.fulename + +returns + + result = "Bob Smith" + +=end + def fullname fullname = '' if self.firstname @@ -33,6 +46,19 @@ class User < ApplicationModel return fullname end +=begin + +check if user is in role + + user = User.find(123) + result = user.is_role('Customer') + +returns + + result = true|false + +=end + def is_role( role_name ) self.roles.each { |role| return role if role.name == role_name @@ -40,6 +66,18 @@ class User < ApplicationModel return false end +=begin + +authenticate user + + result = User.authenticate(username, password) + +returns + + result = user_model # user model if authentication was successfully + +=end + def self.authenticate( username, password ) # do not authenticate with nothing @@ -73,6 +111,18 @@ class User < ApplicationModel return user_auth end +=begin + +authenticate user agains sso + + result = User.sso(sso_params) + +returns + + result = user_model # user model if authentication was successfully + +=end + def self.sso(params) # try to login against configure auth backends @@ -82,6 +132,18 @@ class User < ApplicationModel return user_auth end +=begin + +create user from from omni auth hash + + result = User.create_from_hash!(hash) + +returns + + result = user_model # user model if create was successfully + +=end + def self.create_from_hash!(hash) url = '' if hash['info']['urls'] then @@ -103,6 +165,18 @@ class User < ApplicationModel end +=begin + +send reset password email with token to user + + result = User.password_reset_send(username) + +returns + + result = true|false + +=end + def self.password_reset_send(username) return if !username || username == '' @@ -160,7 +234,18 @@ class User < ApplicationModel return true end - # check token +=begin + +check reset password token + + result = User.password_reset_check(token) + +returns + + result = user_model # user_model if token was verified + +=end + def self.password_reset_check(token) user = Token.check( :action => 'PasswordReset', :name => token ) @@ -172,6 +257,18 @@ class User < ApplicationModel return user end +=begin + +reset reset password with token and set new password + + result = User.password_reset_via_token(token,password) + +returns + + result = user_model # user_model if token was verified + +=end + def self.password_reset_via_token(token,password) # check token @@ -186,6 +283,22 @@ class User < ApplicationModel return user end +=begin + +search user + + result = User.search( + :query => 'some search term' + :limit => 15, + :current_user => user_model, + ) + +returns + + result = [user_model1, user_model2, ...] + +=end + def self.search(params) # get params @@ -308,6 +421,19 @@ class User < ApplicationModel return user end +=begin + +update last login date (is automatically done by auth and sso backend) + + user = User.find(123) + result = user.update_last_login + +returns + + result = new_user_model + +=end + def update_last_login self.last_login = Time.now self.save