20210414000000_init_session_timeout.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class InitSessionTimeout < ActiveRecord::Migration[5.2]
  3. def change
  4. return if !Setting.exists?(name: 'system_init_done')
  5. change_setting_prio('user_create_account', 10)
  6. change_setting_prio('user_lost_password', 20)
  7. two_days = 2.days.seconds
  8. Setting.create_if_not_exists(
  9. title: 'Session Timeout',
  10. name: 'session_timeout',
  11. area: 'Security::Base',
  12. description: 'Defines the session timeout for inactivity of users (in seconds).',
  13. options: {
  14. form: [
  15. {
  16. display: 'Default',
  17. null: false,
  18. name: 'default',
  19. tag: 'input',
  20. },
  21. {
  22. display: 'admin',
  23. null: false,
  24. name: 'admin',
  25. tag: 'input',
  26. },
  27. {
  28. display: 'ticket.agent',
  29. null: false,
  30. name: 'ticket.agent',
  31. tag: 'input',
  32. },
  33. {
  34. display: 'ticket.customer',
  35. null: false,
  36. name: 'ticket.customer',
  37. tag: 'input',
  38. },
  39. ],
  40. },
  41. preferences: {
  42. prio: 30,
  43. },
  44. state: {
  45. 'default' => two_days,
  46. 'admin' => two_days,
  47. 'ticket.agent' => two_days,
  48. 'ticket.customer' => two_days,
  49. },
  50. frontend: true
  51. )
  52. Scheduler.create_or_update(
  53. name: 'Cleanup dead sessions.',
  54. method: 'SessionTimeoutJob.perform_now',
  55. period: 1.minute,
  56. last_run: 1.day.from_now,
  57. prio: 2,
  58. active: true,
  59. updated_by_id: 1,
  60. created_by_id: 1,
  61. )
  62. end
  63. def change_setting_prio(name, prio)
  64. setting = Setting.find_by(name: name)
  65. setting.preferences[:prio] = prio
  66. setting.save
  67. end
  68. end