Class: User
Overview
License
Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2019 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(FALSE), 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
invitation_accepted_at :datetime
invitation_created_at :datetime
invitation_limit :integer
invitation_sent_at :datetime
invitation_token :string
invitations_count :integer default(0)
invited_by_id :integer
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
provider :string
remember_created_at :datetime
reset_password_sent_at :datetime
reset_password_token :string
rights :text
role_id :integer
sign_in_count :integer default(0)
signup_at :datetime
team_id :integer
uid :string
unconfirmed_email :string
unlock_token :string
updated_at :datetime not null
updater_id :integer
Constant Summary
collapse
- PREFERENCE_SHOW_MAP_INTERVENTION_FORM =
No point accepted in preference name
'show_map_on_intervention_form'.freeze
- PREFERENCE_SHOW_EXPORT_PREVIEW =
'show_export_preview'.freeze
- PREFERENCE_SHOW_COMPARE_REALISED_PLANNED =
'compare_planned_and_realised'.freeze
- PREFERENCES =
{
PREFERENCE_SHOW_MAP_INTERVENTION_FORM => :boolean,
PREFERENCE_SHOW_EXPORT_PREVIEW => :boolean,
PREFERENCE_SHOW_COMPARE_REALISED_PLANNED => :boolean
}.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Rightable
#each_right, #resource_actions, #right_exist?, #rights_array
#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?
included
included
Class Method Details
.generate_authentication_token ⇒ Object
362
363
364
365
366
367
|
# File 'app/models/user.rb', line 362
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
370
371
372
|
# File 'app/models/user.rb', line 370
def self.give_password(length = 8, mode = :complex)
generate_password(length, mode)
end
|
.notify_administrators(*args) ⇒ Object
Notify all administrators
235
236
237
238
239
|
# File 'app/models/user.rb', line 235
def self.notify_administrators(*args)
User.administrators.each do |user|
user.notify(*args)
end
end
|
Instance Method Details
#active_for_authentication? ⇒ Boolean
249
250
251
|
# File 'app/models/user.rb', line 249
def active_for_authentication?
super && approved?
end
|
#approved? ⇒ Boolean
245
246
247
|
# File 'app/models/user.rb', line 245
def approved?
!pending_approval?
end
|
#authorization(controller_name, action_name, rights_list = nil) ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'app/models/user.rb', line 261
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 & %i[__minimum__ __public__]).empty? && (rights_list & rights).empty? && !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
205
206
207
208
209
|
# File 'app/models/user.rb', line 205
def avatar_url(options = {})
size = options[:size] || 200
hash = Digest::MD5.hexdigest(email)
"https://secure.gravatar.com/avatar/#{hash}?size=#{size}"
end
|
#can?(action, resource) ⇒ Boolean
274
275
276
|
# File 'app/models/user.rb', line 274
def can?(action, resource)
administrator? || right_exist?(action, resource)
end
|
#can_access?(url) ⇒ Boolean
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'app/models/user.rb', line 278
def can_access?(url)
return true if administrator?
if url.is_a?(Hash)
unless url[:controller] && 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
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
|
#card ⇒ Object
358
359
360
|
# File 'app/models/user.rb', line 358
def card
nil
end
|
#current_campaign ⇒ Object
307
308
309
310
311
312
313
314
315
|
# File 'app/models/user.rb', line 307
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_campaign
prefer!('current_campaign.id', campaign.id)
end
campaign
end
|
#current_campaign=(campaign) ⇒ Object
317
318
319
|
# File 'app/models/user.rb', line 317
def current_campaign=(campaign)
prefer!('current_campaign.id', campaign.id, :integer)
end
|
#current_financial_year ⇒ Object
321
322
323
324
325
326
327
328
329
|
# File 'app/models/user.rb', line 321
def current_financial_year
return nil unless default_financial_year = FinancialYear.on(Date.current)
preference = self.preference('current_financial_year', default_financial_year, :record)
unless financial_year = preference.value
financial_year = default_financial_year
prefer!('current_financial_year', financial_year)
end
financial_year
end
|
#current_financial_year=(financial_year) ⇒ Object
331
332
333
|
# File 'app/models/user.rb', line 331
def current_financial_year=(financial_year)
prefer!('current_financial_year', financial_year, :record)
end
|
#current_period ⇒ Object
343
344
345
|
# File 'app/models/user.rb', line 343
def current_period
preference('current_period', Date.today, :string).value
end
|
#current_period=(period) ⇒ Object
347
348
349
|
# File 'app/models/user.rb', line 347
def current_period=(period)
prefer!('current_period', period, :string)
end
|
#current_period_interval ⇒ Object
335
336
337
|
# File 'app/models/user.rb', line 335
def current_period_interval
preference('current_period_interval', :year, :string).value
end
|
#current_period_interval=(period_interval) ⇒ Object
339
340
341
|
# File 'app/models/user.rb', line 339
def current_period_interval=(period_interval)
prefer!('current_period_interval', period_interval, :string)
end
|
#full_name ⇒ Object
166
167
168
|
# File 'app/models/user.rb', line 166
def full_name
name
end
|
#inactive_message ⇒ Object
253
254
255
256
257
258
259
|
# File 'app/models/user.rb', line 253
def inactive_message
if !approved?
:not_approved
else
super
end
end
|
#invitation_status ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
|
# File 'app/models/user.rb', line 170
def invitation_status
if created_by_invite?
if invitation_accepted?
tc('invitation.accepted')
else
tc('invitation.pending')
end
else
tc('invitation.not_invited')
end
end
|
#label ⇒ Object
192
193
194
|
# File 'app/models/user.rb', line 192
def label
name
end
|
#lock ⇒ Object
298
299
300
|
# File 'app/models/user.rb', line 298
def lock
update_column(:locked, true)
end
|
#mask_lettered_items?(options = {}) ⇒ Boolean
351
352
353
354
355
356
|
# File 'app/models/user.rb', line 351
def mask_lettered_items?(options = {})
preference_name = options[:controller] || 'all'
preference_name << ".#{options[:context]}" if options[:context]
preference_name << '.lettered_items.masked'
preference(preference_name, false, :boolean).value
end
|
#name ⇒ Object
187
188
189
190
|
# File 'app/models/user.rb', line 187
def name
"#{first_name} #{last_name}"
end
|
#notify(message, interpolations = {}, options = {}) ⇒ Object
Create a notification with message for given user
229
230
231
232
|
# File 'app/models/user.rb', line 229
def notify(message, interpolations = {}, options = {})
attributes = options.slice(:target, :target_url, :level)
notifications.create!(attributes.merge(message: message, interpolations: interpolations))
end
|
#pending_approval? ⇒ Boolean
241
242
243
|
# File 'app/models/user.rb', line 241
def pending_approval?
signup_at.present?
end
|
#prefer!(name, value, nature = nil) ⇒ Object
220
221
222
223
224
225
226
|
# File 'app/models/user.rb', line 220
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
212
213
214
215
216
|
# File 'app/models/user.rb', line 212
def preference(name, default_value = nil, nature = nil)
p = preferences.find_by(name: name)
p ||= prefer!(name, default_value, nature)
p
end
|
#status ⇒ Object
182
183
184
185
|
# File 'app/models/user.rb', line 182
def status
return tc('status.invitation.pending') if created_by_invite? && !invitation_accepted?
return tc('status.registration.pending') if pending_approval?
end
|
#theme ⇒ Object
200
201
202
|
# File 'app/models/user.rb', line 200
def theme
preference(:theme).value
end
|
#theme=(value) ⇒ Object
196
197
198
|
# File 'app/models/user.rb', line 196
def theme=(value)
prefer!(:theme, value, :string)
end
|
#unlock ⇒ Object
303
304
305
|
# File 'app/models/user.rb', line 303
def unlock
update_column(:locked, false)
end
|