Module: HasHelpers::UserMethods::InstanceMethods

Defined in:
app/models/has_helpers/user_methods.rb

Instance Method Summary collapse

Instance Method Details

#abbreviated_nameObject



126
127
128
129
# File 'app/models/has_helpers/user_methods.rb', line 126

def abbreviated_name
  receiver = respond_to?(:demographic) ? demographic : self
  "#{receiver.first_name}#{receiver.last_name.to_s.sub(/^(.).*/, ' \1.')}"
end

#cas_extra_attributes=(extra_attributes) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/has_helpers/user_methods.rb', line 41

def cas_extra_attributes=(extra_attributes)
  org_id = extra_attributes["organization_id_or_uuid"] || extra_attributes["organization_id"]
  org = ::HasHelpers::Organization.find_or_initialize_by(id: org_id)
  time_zone = ::TimeZone.find_by(name: extra_attributes["organization_time_zone_name"])
  if time_zone.nil?
    time_zone = org.time_zone
    data = {
      message: "TimeZone Lookup Failed",
      organization: org.inspect,
      user: self.inspect,
      extra_attributes: extra_attributes,
    }.inspect
    HasHelpers::LogNotifier.notify(
      HasHelpers::HQException.new(data, backtrace: caller)
    )
  end

  organization_attributes = {
    code_name: extra_attributes["organization_code_name"],
    domain: extra_attributes["organization_domain"],
    ip_whitelist: extra_attributes["organization_ip_whitelist"],
    is_use_roles_for_authorization: extra_attributes["organization_is_use_roles_for_authorization"],
    name: extra_attributes["organization_name"],
    time_zone_id: time_zone.id,
  }
  organization_attributes[:is_active] = extra_attributes["organization_is_active"] if ::HasHelpers::Organization.column_names.include?("is_active")
  organization_attributes[:start_date] = extra_attributes["organization_go_live_date"] if org.start_date.blank?

  if !org.update(organization_attributes) && ::Rails.application.config.x.support_email_address.present?
    notifier_body = {
      "User" => username,
      "Class" => self.class.name,
      "Org ID" => org.id,
      "Organization name" => org.name,
      "Org name of extra attrs" => extra_attributes["organization_name"] || "no attr found",
      "Test org name" => extra_attributes["organization_name_test"] || "no name found",
      "Created at" => extra_attributes["created_at_test"],
      "Errors" => org.errors.full_messages.join(", "),
      "Backtrace" => Thread.current.backtrace.first(100),
    }
    ::HasHelpers::EmailNotifier.system(subject: "Error synchronizing organization attributes", to: "tools@onehq.com", body: notifier_body).deliver_now
  end

  # Ensure there's a join
  link = user_organizations.find_or_initialize_by(organization_id: org.id)
  link.is_primary = true

  self.assign_attributes(
    applications_array: extra_attributes["user_applications_array"],
    import_key: extra_attributes["user_import_key"],
    ip_whitelist: extra_attributes["user_ip_whitelist"],
    is_support: extra_attributes["user_is_support"] ? true : nil, # bi-state bool here, regular bool in hqsso
    permissions_hash: extra_attributes["user_permissions_hash"],
    photo_content_type: extra_attributes["user_photo_content_type"],
    photo_file_name: extra_attributes["user_photo_file_name"],
    photo_file_size: extra_attributes["user_photo_file_size"],
    photo_updated_at: extra_attributes["user_photo_updated_at"],
    production_api_key: extra_attributes["user_production_api_key"],
    staging_api_key: extra_attributes["user_staging_api_key"],
    username: extra_attributes["user_username"],
  )

  self.first_name = extra_attributes["user_first_name"] if respond_to?(:first_name)
  self.last_name = extra_attributes["user_last_name"] if respond_to?(:last_name)

  # only for new users
  if new_record?
    self.id = extra_attributes["user_id"]
    self.created_by = self
    self.updated_by = self
  end

  # Do not push changes to HQAdmin since the latest changes are currently being applied.
  # This is not strictly necessary, but it currenty prevents a pointless API call.
  self.skip_update_master_record = true

  self.extra_attributes = extra_attributes.select { |attr_name, _| attr_name.start_with?("user") }
  org.extra_attributes = extra_attributes.select { |attr_name, _| attr_name.start_with?("organization") }
  self.send(:handle_extra_attributes) if self.respond_to?(:handle_extra_attributes, true)
  org.send(:handle_extra_attributes) if org.respond_to?(:handle_extra_attributes, true)
  link.save!
  # Make sure all other links are not primary
  user_organizations.where.not(id: link.id).update_all(is_primary: nil)
end

#is_admin?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/has_helpers/user_methods.rb', line 131

def is_admin?
  applications_array.include?(::HasHelpers::Application::HQADMIN.name)
end

#is_super_user?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/models/has_helpers/user_methods.rb', line 135

def is_super_user?
  is_admin? || is_support?
end