Class: User

Inherits:
ActiveRecord::Base
  • Object
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

#passwordObject

Returns the value of attribute password


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

def password
  @password
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


204
205
206
207
208
209
210
211
# File 'app/models/user.rb', line 204

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

.natural_orderObject

sorted by display name


70
71
72
73
74
75
76
77
# File 'app/models/user.rb', line 70

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


80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/user.rb', line 80

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)

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

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

215
216
217
218
219
220
221
222
# File 'app/models/user.rb', line 215

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.


113
114
115
# File 'app/models/user.rb', line 113

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

#localeObject


92
93
94
# File 'app/models/user.rb', line 92

def locale
  settings.profile['language']
end

#mark_as_deletedObject


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

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)

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

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=>“”)


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

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

#nameObject


96
97
98
# File 'app/models/user.rb', line 96

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

#new_random_password(size = 3) ⇒ Object

Returns a random password.


118
119
120
121
122
123
124
125
126
127
# File 'app/models/user.rb', line 118

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


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

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

#receive_email?Boolean

Returns:

  • (Boolean)

100
101
102
# File 'app/models/user.rb', line 100

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


131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/user.rb', line 131

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


200
201
202
# File 'app/models/user.rb', line 200

def restore
  update_column :deleted_at, nil
end

#role_admin?Boolean

Checks the admin role

Returns:

  • (Boolean)

144
145
146
# File 'app/models/user.rb', line 144

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

#role_article_meta?Boolean

Checks the article_meta role

Returns:

  • (Boolean)

159
160
161
# File 'app/models/user.rb', line 159

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

#role_finance?Boolean

Checks the finance role

Returns:

  • (Boolean)

149
150
151
# File 'app/models/user.rb', line 149

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

#role_invoices?Boolean

Checks the invoices role

Returns:

  • (Boolean)

154
155
156
# File 'app/models/user.rb', line 154

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

#role_orders?Boolean

Checks the orders role

Returns:

  • (Boolean)

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

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

#role_pickups?Boolean

Checks the invoices role

Returns:

  • (Boolean)

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

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

#role_suppliers?Boolean

Checks the suppliers role

Returns:

  • (Boolean)

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

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

#set_passwordObject

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


105
106
107
108
109
110
# File 'app/models/user.rb', line 105

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


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

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