checks_user_columns_fillup.rb 1.8 KB

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