application_controller.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. class ApplicationController < ActionController::Base
  2. include UserInfo
  3. # http_basic_authenticate_with :name => "test", :password => "ttt"
  4. helper_method :current_user, :authentication_check, :config_frontend, :user_data_full
  5. before_filter :set_user
  6. before_filter :cors_preflight_check
  7. after_filter :set_access_control_headers
  8. after_filter :trigger_events
  9. # For all responses in this controller, return the CORS access control headers.
  10. def set_access_control_headers
  11. headers['Access-Control-Allow-Origin'] = '*'
  12. headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
  13. headers['Access-Control-Max-Age'] = '1728000'
  14. headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
  15. headers['Access-Control-Allow-Credentials'] = 'true'
  16. end
  17. # If this is a preflight OPTIONS request, then short-circuit the
  18. # request, return only the necessary headers and return an empty
  19. # text/plain.
  20. def cors_preflight_check
  21. if request.method == 'OPTIONS'
  22. headers['Access-Control-Allow-Origin'] = '*'
  23. headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
  24. headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
  25. headers['Access-Control-Max-Age'] = '1728000'
  26. headers['Access-Control-Allow-Credentials'] = 'true'
  27. render :text => '', :content_type => 'text/plain'
  28. return false
  29. end
  30. end
  31. private
  32. # execute events
  33. def trigger_events
  34. Ticket::Observer::Notification.transaction
  35. end
  36. # Finds the User with the ID stored in the session with the key
  37. # :current_user_id This is a common way to handle user login in
  38. # a Rails application; logging in sets the session value and
  39. # logging out removes it.
  40. def current_user
  41. @_current_user ||= session[:user_id] &&
  42. User.find_by_id( session[:user_id] )
  43. end
  44. def authentication_check
  45. puts 'authentication_check'
  46. # puts params.inspect
  47. # check http basic auth
  48. authenticate_with_http_basic do |user, password|
  49. puts 'http basic auth check'
  50. userdata = User.where( :login => user ).first
  51. message = ''
  52. if !userdata
  53. message = 'authentication failed, user'
  54. else
  55. if password != userdata.password
  56. message = 'authentication failed, pw'
  57. end
  58. end
  59. # return auth ok
  60. return true if message == ''
  61. # return auth not ok
  62. render(
  63. :json => {
  64. :error => message,
  65. },
  66. :status => :unauthorized
  67. )
  68. return false
  69. end
  70. # return auth not ok (no session exists)
  71. if !session[:user_id]
  72. message = 'no valid session, user_id'
  73. puts message
  74. render(
  75. :json => {
  76. :error => message,
  77. },
  78. :status => :unauthorized
  79. )
  80. return false
  81. end
  82. # return auth ok
  83. return true
  84. end
  85. # Sets the current user into a named Thread location so that it can be accessed
  86. # by models and observers
  87. def set_user
  88. puts 'set_user'
  89. UserInfo.current_user_id = session[:user_id]
  90. end
  91. def log_view (object)
  92. history_type = History::Type.where( :name => 'viewed' ).first
  93. if !history_type || !history_type.id
  94. history_type = History::Type.create(
  95. :name => 'viewed'
  96. )
  97. end
  98. history_object = History::Object.where( :name => object.class.name ).first
  99. if !history_object || !history_object.id
  100. history_object = History::Object.create(
  101. :name => object.class.name
  102. )
  103. end
  104. History.create(
  105. :o_id => object.id,
  106. :history_type_id => history_type.id,
  107. :history_object_id => history_object.id,
  108. :created_by_id => session[:user_id]
  109. )
  110. end
  111. def config_frontend
  112. # config
  113. config = {}
  114. Setting.select('name').where( :frontend => true ).each { |setting|
  115. config[setting.name] = Setting.get(setting.name)
  116. }
  117. return config
  118. end
  119. def user_data_full (user_id)
  120. # get user
  121. user = User.find_fulldata(user_id)
  122. # do not show password
  123. user['password'] = ''
  124. # show linked topics and items
  125. user['links'] = []
  126. # TEMP: compat. reasons
  127. user['preferences'] = {} if user['preferences'] == nil
  128. topic = {
  129. :title => 'Tickets',
  130. :items => [
  131. {
  132. :url => '',
  133. :name => 'open (' + user['preferences'][:tickets_open].to_s + ')',
  134. :title => 'Open Tickets',
  135. :class => 'user-tickets',
  136. :data => 'open'
  137. },
  138. {
  139. :url => '',
  140. :name => 'closed (' + user['preferences'][:tickets_closed].to_s + ')',
  141. :title => 'Closed Tickets',
  142. :class => 'user-tickets',
  143. :data => 'closed'
  144. }
  145. ]
  146. }
  147. user['links'].push topic
  148. return user
  149. end
  150. end