Class: User

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

Overview

Informations

License

Ekylibre ERP - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2014 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(255)
commercial                             :boolean          not null
confirmation_sent_at                   :datetime
confirmation_token                     :string(255)
confirmed_at                           :datetime
created_at                             :datetime         not null
creator_id                             :integer
current_sign_in_at                     :datetime
current_sign_in_ip                     :string(255)
description                            :text
email                                  :string(255)      not null
employed                               :boolean          not null
employment                             :string(255)
encrypted_password                     :string(255)      default(""), not null
establishment_id                       :integer
failed_attempts                        :integer          default(0)
first_name                             :string(255)      not null
id                                     :integer          not null, primary key
language                               :string(3)        not null
last_name                              :string(255)      not null
last_sign_in_at                        :datetime
last_sign_in_ip                        :string(255)
lock_version                           :integer          default(0), not null
locked                                 :boolean          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(255)
rights                                 :text
role_id                                :integer          not null
sign_in_count                          :integer          default(0)
team_id                                :integer
unconfirmed_email                      :string(255)
unlock_token                           :string(255)
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

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, #old_record, #others, scope_with_registration, scopes, 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



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

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



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

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

Instance Method Details

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



176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/models/user.rb', line 176

def authorization(controller_name, action_name, rights_list=nil)
  rights_list = self.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? and (rights_list & rights).empty? and not self.administrator?
    message = :no_right_defined_for_this_part_of_the_application_and_this_user.tl
  end
  return message
end

#can?(right) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#can_access?(url) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/models/user.rb', line 193

def can_access?(url)
  return true if self.administrator?
  if url.is_a?(Hash)
    unless url[:controller] and url[:action]
      raise "Invalid URL for accessibility test: #{url.inspect}"
    end
    key = "#{url[:controller].to_s.gsub(/^\//, '')}##{url[:action]}"
  else
    key = url.to_s
  end
  unless list = Ekylibre::Access.reversed_list[key]
    logger.debug "Unable to check access for action: #{key}. #{url.inspect}"
    return true
  end
  list &= self.resource_actions
  return list.any?
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.



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

def interventions_paths(options = {})
  crumbs = self.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
  return crumbs.order(read_at: :asc).map(&:intervention_path)
end

#labelObject



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

def label
  self.name
end

#lockObject

Lock the user



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

def lock
  update_column(:locked, true)
end

#nameObject



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

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

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



167
168
169
170
171
172
173
174
# File 'app/models/user.rb', line 167

def prefer!(name, value, nature = :string)
  unless p = self.preferences.find_by(name: name)
    p = self.preferences.build(name: name, nature: nature.to_s)
  end
  p.value = value
  p.save!
  return p
end

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

Find or create preference for given name



157
158
159
160
161
162
163
164
# File 'app/models/user.rb', line 157

def preference(name, default_value = nil, nature = :string)
  prefs = self.preferences.select{ |p| p.name == name }
  return prefs.first if prefs.any?
  p = self.preferences.build(name: name, nature: nature.to_s)
  p.value  = default_value
  p.save!
  p
end

#unconverted_crumb_daysObject

Returns the days where the user has crumbs present



222
223
224
# File 'app/models/user.rb', line 222

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

#unlockObject

Unlock the user



217
218
219
# File 'app/models/user.rb', line 217

def unlock
  update_column(:locked, false)
end