Class: User

Inherits:
Ekylibre::Record::Base show all
Includes:
Rightable
Defined in:
app/models/user.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2015 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: users

administrator                          :boolean          default(TRUE), not null
authentication_token                   :string
commercial                             :boolean          default(FALSE), not null
confirmation_sent_at                   :datetime
confirmation_token                     :string
confirmed_at                           :datetime
created_at                             :datetime         not null
creator_id                             :integer
current_sign_in_at                     :datetime
current_sign_in_ip                     :string
description                            :text
email                                  :string           not null
employed                               :boolean          default(FALSE), not null
employment                             :string
encrypted_password                     :string           default(""), not null
failed_attempts                        :integer          default(0)
first_name                             :string           not null
id                                     :integer          not null, primary key
language                               :string           not null
last_name                              :string           not null
last_sign_in_at                        :datetime
last_sign_in_ip                        :string
lock_version                           :integer          default(0), not null
locked                                 :boolean          default(FALSE), not null
locked_at                              :datetime
maximal_grantable_reduction_percentage :decimal(19, 4)   default(5.0), not null
person_id                              :integer
remember_created_at                    :datetime
reset_password_sent_at                 :datetime
reset_password_token                   :string
rights                                 :text
role_id                                :integer
sign_in_count                          :integer          default(0)
team_id                                :integer
unconfirmed_email                      :string
unlock_token                           :string
updated_at                             :datetime         not null
updater_id                             :integer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rightable

#each_right, #resource_actions, #rights_array

Methods inherited from Ekylibre::Record::Base

#already_updated?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, #custom_fields, custom_fields, #custom_value, #destroyable?, #editable?, has_human_attribute_name?, has_picture, #human_attribute_name, human_attribute_name_with_id, #method_missing, nomenclature_reflections, #old_record, #others, refers_to, reset_schema, scope_with_registration, simple_scopes, #updateable?, #validate_custom_fields

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ekylibre::Record::Base

Class Method Details

.generate_authentication_tokenObject



272
273
274
275
276
277
# File 'app/models/user.rb', line 272

def self.generate_authentication_token
  loop do
    token = Devise.friendly_token
    break token unless exists?(authentication_token: token)
  end
end

.give_password(length = 8, mode = :complex) ⇒ Object

Used for generic password creation



280
281
282
# File 'app/models/user.rb', line 280

def self.give_password(length = 8, mode = :complex)
  generate_password(length, mode)
end

.notify_administrators(*args) ⇒ Object

Notify all administrators



185
186
187
188
189
# File 'app/models/user.rb', line 185

def self.notify_administrators(*args)
  User.administrators.each do |user|
    user.notify(*args)
  end
end

Instance Method Details

#authorization(controller_name, action_name, rights_list = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/models/user.rb', line 191

def authorization(controller_name, action_name, rights_list = nil)
  rights_list = rights_array if rights_list.blank?
  message = nil
  if self.class.rights[controller_name.to_sym].nil?
    message = :no_right_defined_for_this_part_of_the_application.tl(controller: controller_name, action: action_name)
  elsif (rights = self.class.rights[controller_name.to_sym][action_name.to_sym]).nil?
    message = :no_right_defined_for_this_part_of_the_application.tl(controller: controller_name, action: action_name)
  elsif (rights & [:__minimum__, :__public__]).empty? && (rights_list & rights).empty? && !self.administrator?
    message = :no_right_defined_for_this_part_of_the_application_and_this_user.tl
  end
  message
end

#avatar_url(options = {}) ⇒ Object

Returns the URL of the avatar of the user



156
157
158
159
160
# File 'app/models/user.rb', line 156

def avatar_url(options = {})
  size = options[:size] || 200
  hash = Digest::MD5.hexdigest(email)
  "https://secure.gravatar.com/avatar/#{hash}?size=#{size}"
end

#can?(right) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
# File 'app/models/user.rb', line 204

def can?(right)
  self.administrator? || self.rights.match(/(^|\s)#{right}(\s|$)/)
end

#can_access?(url) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/models/user.rb', line 208

def can_access?(url)
  return true if self.administrator?
  if url.is_a?(Hash)
    unless url[:controller] && url[:action]
      fail "Invalid URL for accessibility test: #{url.inspect}"
    end
    key = "#{url[:controller].to_s.gsub(/^\//, '')}##{url[:action]}"
  else
    key = url.to_s
  end
  list = Ekylibre::Access.rights_of(key)
  if list.empty?
    logger.debug "Unable to check access for action: #{key}. #{url.inspect}".yellow
    return true
  end
  list &= resource_actions
  list.any?
end

#cardObject



268
269
270
# File 'app/models/user.rb', line 268

def card
  nil
end

#current_campaignObject



254
255
256
257
258
259
260
261
262
# File 'app/models/user.rb', line 254

def current_campaign
  return nil unless default_campaign = Campaign.order(harvest_year: :desc).first
  preference = self.preference('current_campaign.id', default_campaign.id, :integer)
  unless campaign = Campaign.find_by(id: preference.value)
    campaign = default
    self.prefer!('current_campaign.id', campaign.id)
  end
  campaign
end

#current_campaign=(campaign) ⇒ Object



264
265
266
# File 'app/models/user.rb', line 264

def current_campaign=(campaign)
  self.prefer!('current_campaign.id', campaign.id, :integer)
end

#full_nameObject



142
143
144
# File 'app/models/user.rb', line 142

def full_name
  name
end

#interventions_paths(options = {}) ⇒ Object

Returns all crumbs, grouped by interventions paths, for the current user. The result is an array of interventions paths. An intervention path is an array of crumbs, for a user, ordered by read_at, between a start crumb and a stop crumb.



246
247
248
249
250
251
252
# File 'app/models/user.rb', line 246

def interventions_paths(options = {})
  crumbs = reload.crumbs.unconverted.where(nature: :start)
  if options[:on]
    crumbs = crumbs.where(read_at: options[:on].beginning_of_day..options[:on].end_of_day)
  end
  crumbs.order(read_at: :asc).map(&:intervention_path)
end

#labelObject



151
152
153
# File 'app/models/user.rb', line 151

def label
  name
end

#lockObject

Lock the user



228
229
230
# File 'app/models/user.rb', line 228

def lock
  update_column(:locked, true)
end

#nameObject



146
147
148
149
# File 'app/models/user.rb', line 146

def name
  # TODO: I18nize the method User#name !
  "#{first_name} #{last_name}"
end

#notify(message, interpolations = {}, options = {}) ⇒ Object

Create a notification with message for given user



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

def notify(message, interpolations = {}, options = {})
  attributes = options.slice(:target, :target_url, :level)
  notifications.create!(attributes.merge(message: message, interpolations: interpolations))
end

#prefer!(name, value, nature = nil) ⇒ Object



170
171
172
173
174
175
176
# File 'app/models/user.rb', line 170

def prefer!(name, value, nature = nil)
  p = preferences.find_or_initialize_by(name: name)
  p.nature ||= nature if nature
  p.value = value
  p.save!
  p
end

#preference(name, default_value = nil, nature = nil) ⇒ Object Also known as: pref

Find or create preference for given name



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

def preference(name, default_value = nil, nature = nil)
  p = preferences.find_by(name: name)
  p ||= prefer!(name, default_value, nature)
  p
end

#unconverted_crumb_daysObject

Returns the days where the user has crumbs present



238
239
240
# File 'app/models/user.rb', line 238

def unconverted_crumb_days
  crumbs.unconverted.pluck(:read_at).map(&:to_date).uniq.sort
end

#unlockObject

Unlock the user



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

def unlock
  update_column(:locked, false)
end