application_controller.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 current_user_set(user)
  45. @_current_user = user
  46. set_user
  47. end
  48. def authentication_check
  49. puts 'authentication_check'
  50. # puts params.inspect
  51. # check http basic auth
  52. authenticate_with_http_basic do |user, password|
  53. puts 'http basic auth check'
  54. userdata = User.where( :login => user ).first
  55. message = ''
  56. if !userdata
  57. message = 'authentication failed, user'
  58. else
  59. if password != userdata.password
  60. message = 'authentication failed, pw'
  61. end
  62. end
  63. # return auth ok
  64. if message == ''
  65. # set basic auth user to current user
  66. current_user_set(userdata)
  67. return true
  68. end
  69. # return auth not ok
  70. render(
  71. :json => {
  72. :error => message,
  73. },
  74. :status => :unauthorized
  75. )
  76. return false
  77. end
  78. # check logon session
  79. if params['logon_session']
  80. logon_session = ActiveRecord::SessionStore::Session.where( :session_id => params['logon_session'] ).first
  81. if logon_session
  82. userdata = User.find( user_id = logon_session.data[:user_id] )
  83. end
  84. # set logon session user to current user
  85. current_user_set(userdata)
  86. return true
  87. end
  88. # return auth not ok (no session exists)
  89. if !session[:user_id]
  90. message = 'no valid session, user_id'
  91. puts message
  92. render(
  93. :json => {
  94. :error => message,
  95. },
  96. :status => :unauthorized
  97. )
  98. return false
  99. end
  100. # return auth ok
  101. return true
  102. end
  103. # Sets the current user into a named Thread location so that it can be accessed
  104. # by models and observers
  105. def set_user
  106. return if !current_user
  107. UserInfo.current_user_id = current_user.id
  108. end
  109. def log_view (object)
  110. history_type = History::Type.where( :name => 'viewed' ).first
  111. if !history_type || !history_type.id
  112. history_type = History::Type.create(
  113. :name => 'viewed'
  114. )
  115. end
  116. history_object = History::Object.where( :name => object.class.name ).first
  117. if !history_object || !history_object.id
  118. history_object = History::Object.create(
  119. :name => object.class.name
  120. )
  121. end
  122. History.create(
  123. :o_id => object.id,
  124. :history_type_id => history_type.id,
  125. :history_object_id => history_object.id,
  126. :created_by_id => current_user.id
  127. )
  128. end
  129. def config_frontend
  130. # config
  131. config = {}
  132. Setting.select('name').where( :frontend => true ).each { |setting|
  133. config[setting.name] = Setting.get(setting.name)
  134. }
  135. return config
  136. end
  137. end