Class: User

Inherits:
ApplicationRecord show all
Includes:
CustomFields
Defined in:
app/models/user.rb

Overview

specific user rights through memberships (see Group)

Instance Attribute Summary collapse

Attributes included from CustomFields

#custom_fields

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#create_ordergroupObject

Returns the value of attribute create_ordergroup


23
24
25
# File 'app/models/user.rb', line 23

def create_ordergroup
  @create_ordergroup
end

#passwordObject

Returns the value of attribute password


23
24
25
# File 'app/models/user.rb', line 23

def password
  @password
end

#send_welcome_mailObject

Returns the value of attribute send_welcome_mail


23
24
25
# File 'app/models/user.rb', line 23

def send_welcome_mail
  @send_welcome_mail
end

#settings_attributesObject

Returns the value of attribute settings_attributes


23
24
25
# File 'app/models/user.rb', line 23

def settings_attributes
  @settings_attributes
end

Class Method Details

.authenticate(login, password) ⇒ Object


223
224
225
226
227
228
229
230
# File 'app/models/user.rb', line 223

def self.authenticate(, password)
  user = find_by_nick() || find_by_email()
  if user && password && user.has_password(password)
    user
  else
    nil
  end
end

.custom_fieldsObject


232
233
234
235
236
# File 'app/models/user.rb', line 232

def self.custom_fields
  fields = FoodsoftConfig[:custom_fields] && FoodsoftConfig[:custom_fields][:user]
  return [] unless fields
  fields.map(&:deep_symbolize_keys)
end

.natural_orderObject

sorted by display name


89
90
91
92
93
94
95
96
# File 'app/models/user.rb', line 89

def self.natural_order
  # would be sensible to match ApplicationController#show_user
  if FoodsoftConfig[:use_nick]
    order('nick')
  else
    order('first_name', 'last_name')
  end
end

.natural_search(q) ⇒ Object

search by (nick)name


99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/user.rb', line 99

def self.natural_search(q)
  q = q.strip
  users = User.arel_table
  # full string as nickname
  match_nick = users[:nick].matches("%#{q}%")
  # or each word matches either first or last name
  match_name = q.split.map do |a|
      users[:first_name].matches("%#{a}%").or users[:last_name].matches("%#{a}%")
  end.reduce(:and)
  User.where(match_nick.or match_name)
end

Instance Method Details

#deleted?Boolean

Returns:

  • (Boolean)

211
212
213
# File 'app/models/user.rb', line 211

def deleted?
  deleted_at.present?
end

#displayObject

XXX this is view-related; need to move out things like token_attributes

then this can be removed

240
241
242
243
244
245
246
247
# File 'app/models/user.rb', line 240

def display
  # would be sensible to match ApplicationHelper#show_user
  if FoodsoftConfig[:use_nick]
    nick.nil? ? I18n.t('helpers.application.nick_fallback') : nick
  else
    name
  end
end

#has_password(password) ⇒ Object

Returns true if the password argument matches the user's password.


132
133
134
# File 'app/models/user.rb', line 132

def has_password(password)
  Digest::SHA1.hexdigest(password + self.password_salt) == self.password_hash
end

#localeObject


111
112
113
# File 'app/models/user.rb', line 111

def locale
  settings.profile['language']
end

#mark_as_deletedObject


215
216
217
# File 'app/models/user.rb', line 215

def mark_as_deleted
  update_column :deleted_at, Time.now
end

#member_of?(group) ⇒ Boolean

returns true if user is a member of a given group

Returns:

  • (Boolean)

202
203
204
# File 'app/models/user.rb', line 202

def member_of?(group)
  group.users.exists?(self.id)
end

#member_of_groupsObject

Returns an array with the users groups (but without the Ordergroups -> because tpye=>“”)


207
208
209
# File 'app/models/user.rb', line 207

def member_of_groups()
   self.groups.where(type: '')
end

#nameObject


115
116
117
# File 'app/models/user.rb', line 115

def name
  [first_name, last_name].join(" ")
end

#new_random_password(size = 3) ⇒ Object

Returns a random password.


137
138
139
140
141
142
143
144
145
146
# File 'app/models/user.rb', line 137

def new_random_password(size = 3)
  c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr)
  v = %w(a e i o u y)
  f, r = true, ''
  (size * 2).times do
    r << (f ? c[rand * c.size] : v[rand * v.size])
    f = !f
  end
  r
end

#ordergroupObject

has_one :ordergroup, :through => :memberships, :source => :group, :class_name => “Ordergroup”


12
13
14
# File 'app/models/user.rb', line 12

def ordergroup
  Ordergroup.joins(:memberships).where(memberships: {user_id: self.id}).first
end

#ordergroup_nameObject


197
198
199
# File 'app/models/user.rb', line 197

def ordergroup_name
  ordergroup ? ordergroup.name : I18n.t('model.user.no_ordergroup')
end

#receive_email?Boolean

Returns:

  • (Boolean)

119
120
121
# File 'app/models/user.rb', line 119

def receive_email?
  settings.messages['send_as_email'] && email.present?
end

#request_password_reset!Boolean

Generates password reset token and sends email

Returns:

  • (Boolean)

    Whether it succeeded or not


150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/user.rb', line 150

def request_password_reset!
  self.reset_password_token = new_random_password(16)
  self.reset_password_expires = Time.now.advance(days: 2)
  if save!
    Mailer.reset_password(self).deliver_now
    logger.debug("Sent password reset email to #{email}.")
    true
  else
    false
  end
end

#restoreObject


219
220
221
# File 'app/models/user.rb', line 219

def restore
  update_column :deleted_at, nil
end

#role_admin?Boolean

Checks the admin role

Returns:

  • (Boolean)

163
164
165
# File 'app/models/user.rb', line 163

def role_admin?
  groups.detect {|group| group.role_admin?}
end

#role_article_meta?Boolean

Checks the article_meta role

Returns:

  • (Boolean)

178
179
180
# File 'app/models/user.rb', line 178

def 
  FoodsoftConfig[:default_role_article_meta] || groups.detect {|group| group.}
end

#role_finance?Boolean

Checks the finance role

Returns:

  • (Boolean)

168
169
170
# File 'app/models/user.rb', line 168

def role_finance?
  FoodsoftConfig[:default_role_finance] || groups.detect {|group| group.role_finance?}
end

#role_invoices?Boolean

Checks the invoices role

Returns:

  • (Boolean)

173
174
175
# File 'app/models/user.rb', line 173

def role_invoices?
  FoodsoftConfig[:default_role_invoices] || groups.detect {|group| group.role_invoices?}
end

#role_orders?Boolean

Checks the orders role

Returns:

  • (Boolean)

193
194
195
# File 'app/models/user.rb', line 193

def role_orders?
  FoodsoftConfig[:default_role_orders] || groups.detect {|group| group.role_orders?}
end

#role_pickups?Boolean

Checks the invoices role

Returns:

  • (Boolean)

188
189
190
# File 'app/models/user.rb', line 188

def role_pickups?
  FoodsoftConfig[:default_role_pickups] || groups.detect {|group| group.role_pickups?}
end

#role_suppliers?Boolean

Checks the suppliers role

Returns:

  • (Boolean)

183
184
185
# File 'app/models/user.rb', line 183

def role_suppliers?
  FoodsoftConfig[:default_role_suppliers] || groups.detect {|group| group.role_suppliers?}
end

#send_welcome_mail?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'app/models/user.rb', line 84

def send_welcome_mail?
  ActiveRecord::Type::Boolean.new.type_cast_from_user(send_welcome_mail)
end

#set_passwordObject

Sets the user's password. It will be stored encrypted along with a random salt.


124
125
126
127
128
129
# File 'app/models/user.rb', line 124

def set_password
  unless password.blank?
    salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
    self.password_hash, self.password_salt = Digest::SHA1.hexdigest(password + salt), salt
  end
end

#token_attributesObject


249
250
251
252
253
# File 'app/models/user.rb', line 249

def token_attributes
  # would be sensible to match ApplicationController#show_user
  #   this should not be part of the model anyway
  {:id => id, :name => "#{display} (#{ordergroup.try(:name)})"}
end