application_controller.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. class ApplicationController < ActionController::Base
  2. include UserInfo
  3. protect_from_forgery
  4. # http_basic_authenticate_with :name => "test", :password => "ttt"
  5. helper_method :current_user, :authentication_check, :config_frontend, :user_data_full
  6. before_filter :set_user, :cors_preflight_check
  7. after_filter :set_access_control_headers
  8. # For all responses in this controller, return the CORS access control headers.
  9. def set_access_control_headers
  10. # headers['Access-Control-Allow-Origin'] = 'http://localhost/'
  11. # headers['Access-Control-Request-Method'] = '*'
  12. # headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
  13. headers['Access-Control-Allow-Origin'] = 'http://localhost/'
  14. headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, OPTIONS'
  15. headers['Access-Control-Max-Age'] = "1728000"
  16. headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
  17. headers['Access-Control-Allow-Credentials'] = 'true'
  18. end
  19. # If this is a preflight OPTIONS request, then short-circuit the
  20. # request, return only the necessary headers and return an empty
  21. # text/plain.
  22. def cors_preflight_check
  23. if request.method == :options
  24. headers['Access-Control-Allow-Origin'] = '*'
  25. headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
  26. headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
  27. headers['Access-Control-Max-Age'] = '1728000'
  28. # headers['Access-Control-Allow-Credentials'] = 'true'
  29. render :text => '', :content_type => 'text/plain'
  30. end
  31. end
  32. private
  33. # Finds the User with the ID stored in the session with the key
  34. # :current_user_id This is a common way to handle user login in
  35. # a Rails application; logging in sets the session value and
  36. # logging out removes it.
  37. def current_user
  38. @_current_user ||= session[:user_id] &&
  39. User.find_by_id( session[:user_id] )
  40. end
  41. def authentication_check
  42. logger.debug 'authentication_check'
  43. # logger.debug session.inspect
  44. # check http basic auth
  45. authenticate_with_http_basic do |user, password|
  46. logger.debug 'http basic auth check'
  47. # logger.debug user
  48. # logger.debug password
  49. userdata = User.where( :login => user ).first
  50. message = ''
  51. if !userdata
  52. message = 'authentication failed, user'
  53. else
  54. if password != userdata.password
  55. message = 'authentication failed, pw'
  56. end
  57. end
  58. if message != ''
  59. render(
  60. :json => {
  61. :error => message,
  62. },
  63. :status => :unauthorized
  64. )
  65. end
  66. return false
  67. end
  68. # logger.debug 'session check'
  69. # logger.debug session.inspect
  70. # session[:user_id] = 2
  71. if !session[:user_id]
  72. logger.debug '!session user_id'
  73. message = 'no valid session, user_id'
  74. render(
  75. :json => {
  76. :error => message,
  77. },
  78. :status => :unauthorized
  79. )
  80. return false
  81. end
  82. # return 1231
  83. # request_http_basic_authentication
  84. return false
  85. end
  86. # Sets the current user into a named Thread location so that it can be accessed
  87. # by models and observers
  88. def 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]
  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