checks_user_columns_fillup.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module ApplicationModel::ChecksUserColumnsFillup
  3. extend ActiveSupport::Concern
  4. included do
  5. before_validation :fill_up_user_validate
  6. end
  7. def fill_up_user_validate
  8. return fill_up_user_create if new_record?
  9. fill_up_user_update
  10. end
  11. =begin
  12. set created_by_id & updated_by_id if not given based on UserInfo (current session)
  13. Used as before_create callback, no own use needed
  14. result = Model.fill_up_user_create(params)
  15. returns
  16. result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
  17. =end
  18. def fill_up_user_create
  19. if self.class.column_names.include?('updated_by_id') && UserInfo.current_user_id
  20. if updated_by_id && updated_by_id != UserInfo.current_user_id
  21. logger.info "NOTICE create - self.updated_by_id is different: #{updated_by_id}/#{UserInfo.current_user_id}"
  22. end
  23. self.updated_by_id = UserInfo.current_user_id
  24. end
  25. return true if self.class.column_names.exclude?('created_by_id')
  26. return true if !UserInfo.current_user_id
  27. if created_by_id && created_by_id != UserInfo.current_user_id
  28. logger.info "NOTICE create - self.created_by_id is different: #{created_by_id}/#{UserInfo.current_user_id}"
  29. end
  30. self.created_by_id = UserInfo.current_user_id
  31. true
  32. end
  33. =begin
  34. set updated_by_id if not given based on UserInfo (current session)
  35. Used as before_update callback, no own use needed
  36. result = Model.fill_up_user_update(params)
  37. returns
  38. result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
  39. =end
  40. def fill_up_user_update
  41. return true if self.class.column_names.exclude?('updated_by_id')
  42. return true if !UserInfo.current_user_id
  43. self.updated_by_id = UserInfo.current_user_id
  44. true
  45. end
  46. end