sessions_controller.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class SessionsController < ApplicationController
  3. # "Create" a login, aka "log the user in"
  4. def create
  5. # in case, remove switched_from_user_id
  6. session[:switched_from_user_id] = nil
  7. # authenticate user
  8. user = User.authenticate( params[:username], params[:password] )
  9. # auth failed
  10. if !user
  11. render :json => { :error => 'login failed' }, :status => :unauthorized
  12. return
  13. end
  14. # remember me - set session cookie to expire later
  15. if params[:remember_me]
  16. request.env['rack.session.options'][:expire_after] = 1.year
  17. else
  18. request.env['rack.session.options'][:expire_after] = nil
  19. end
  20. # both not needed to set :expire_after works fine
  21. # request.env['rack.session.options'][:renew] = true
  22. # reset_session
  23. # set session user
  24. current_user_set(user)
  25. # log new session
  26. user.activity_stream_log( 'session started', user.id, true )
  27. # auto population of default collections
  28. collections, assets = SessionHelper::default_collections(user)
  29. # add session user assets
  30. assets = user.assets(assets)
  31. # get models
  32. models = SessionHelper::models(user)
  33. # check logon session
  34. logon_session_key = nil
  35. if params['logon_session']
  36. logon_session_key = Digest::MD5.hexdigest( rand(999999).to_s + Time.new.to_s )
  37. # session = ActiveRecord::SessionStore::Session.create(
  38. # :session_id => logon_session_key,
  39. # :data => {
  40. # :user_id => user['id']
  41. # }
  42. # )
  43. end
  44. # return new session data
  45. render :json => {
  46. :session => user,
  47. :models => models,
  48. :collections => collections,
  49. :assets => assets,
  50. :logon_session => logon_session_key,
  51. },
  52. :status => :created
  53. end
  54. def show
  55. user_id = nil
  56. # no valid sessions
  57. if session[:user_id]
  58. user_id = session[:user_id]
  59. end
  60. # check logon session
  61. if params['logon_session']
  62. session = SessionHelper::get( params['logon_session'] )
  63. if session
  64. user_id = session.data[:user_id]
  65. end
  66. end
  67. if !user_id
  68. # get models
  69. models = SessionHelper::models()
  70. render :json => {
  71. :error => 'no valid session',
  72. :config => config_frontend,
  73. :models => models,
  74. }
  75. return
  76. end
  77. # Save the user ID in the session so it can be used in
  78. # subsequent requests
  79. user = User.find( user_id )
  80. # auto population of default collections
  81. collections, assets = SessionHelper::default_collections(user)
  82. # add session user assets
  83. assets = user.assets(assets)
  84. # get models
  85. models = SessionHelper::models(user)
  86. # return current session
  87. render :json => {
  88. :session => user,
  89. :models => models,
  90. :collections => collections,
  91. :assets => assets,
  92. :config => config_frontend,
  93. }
  94. end
  95. # "Delete" a login, aka "log the user out"
  96. def destroy
  97. # Remove the user id from the session
  98. @_current_user = session[:user_id] = nil
  99. # reset session cookie (reset :expire_after in case remember_me is active)
  100. request.env['rack.session.options'][:expire_after] = -1.year
  101. request.env['rack.session.options'][:renew] = true
  102. render :json => { }
  103. end
  104. def create_omniauth
  105. # in case, remove switched_from_user_id
  106. session[:switched_from_user_id] = nil
  107. auth = request.env['omniauth.auth']
  108. if !auth
  109. logger.info("AUTH IS NULL, SERVICE NOT LINKED TO ACCOUNT")
  110. # redirect to app
  111. redirect_to '/'
  112. end
  113. # Create a new user or add an auth to existing user, depending on
  114. # whether there is already a user signed in.
  115. authorization = Authorization.find_from_hash(auth)
  116. if !authorization
  117. authorization = Authorization.create_from_hash(auth, current_user)
  118. end
  119. # set current session user
  120. current_user_set(authorization.user)
  121. # log new session
  122. user.activity_stream_log( 'session started', authorization.user.id, true )
  123. # remember last login date
  124. authorization.user.update_last_login
  125. # redirect to app
  126. redirect_to '/'
  127. end
  128. def create_sso
  129. # in case, remove switched_from_user_id
  130. session[:switched_from_user_id] = nil
  131. user = User.sso(params)
  132. # Log the authorizing user in.
  133. if user
  134. # set current session user
  135. current_user_set(user)
  136. # log new session
  137. user.activity_stream_log( 'session started', user.id, true )
  138. # remember last login date
  139. user.update_last_login
  140. end
  141. # redirect to app
  142. redirect_to '/#'
  143. end
  144. # "switch" to user
  145. def switch_to_user
  146. return if deny_if_not_role('Admin')
  147. # check user
  148. if !params[:id]
  149. render(
  150. :json => { :message => 'no user given' },
  151. :status => :not_found
  152. )
  153. return false
  154. end
  155. user = User.lookup( :id => params[:id] )
  156. if !user
  157. render(
  158. :json => {},
  159. :status => :not_found
  160. )
  161. return false
  162. end
  163. # remember old user
  164. session[:switched_from_user_id] = current_user.id
  165. # log new session
  166. user.activity_stream_log( 'switch to', current_user.id, true )
  167. # set session user
  168. current_user_set(user)
  169. redirect_to '/#'
  170. end
  171. # "switch" back to user
  172. def switch_back_to_user
  173. # check if it's a swich back
  174. if !session[:switched_from_user_id]
  175. response_access_deny
  176. return false
  177. end
  178. user = User.lookup( :id => session[:switched_from_user_id] )
  179. if !user
  180. render(
  181. :json => {},
  182. :status => :not_found
  183. )
  184. return false
  185. end
  186. # rememeber current user
  187. current_session_user = current_user
  188. # remove switched_from_user_id
  189. session[:switched_from_user_id] = nil
  190. # set old session user again
  191. current_user_set(user)
  192. # log end session
  193. current_session_user.activity_stream_log( 'ended switch to', user.id, true )
  194. redirect_to '/#'
  195. end
  196. def list
  197. return if deny_if_not_role('Admin')
  198. assets = {}
  199. sessions_clean = []
  200. SessionHelper.list.each {|session|
  201. next if !session.data['user_id']
  202. sessions_clean.push session
  203. if session.data['user_id']
  204. user = User.lookup( :id => session.data['user_id'] )
  205. assets = user.assets( assets )
  206. end
  207. }
  208. render :json => {
  209. :sessions => sessions_clean,
  210. :assets => assets,
  211. }
  212. end
  213. def delete
  214. return if deny_if_not_role('Admin')
  215. SessionHelper::destroy( params[:id] )
  216. render :json => {}
  217. end
  218. end