Class: User

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

Overview

Informations

License

Ekylibre - Simple ERP Copyright (C) 2009-2012 Brice Texier, Thibaud Merigon 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 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 General Public License for more details.

You should have received a copy of the GNU 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 inherited from Ekylibre::Record::Base

attr_readonly_with_conditions, columns_definition, complex_scopes, #custom_fields, custom_fields, #custom_value, #destroyable?, human_attribute_name_with_id, #old_record, #others, scope_with_registration, scopes, simple_scopes, #updateable?, #validate_custom_fields

Class Method Details

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

Used for generic password creation



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

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

.minimum_rightObject



106
# File 'app/models/user.rb', line 106

def minimum_right; :__minimum__; end

.rightsObject



107
# File 'app/models/user.rb', line 107

def rights; @@rights; end

.rights_fileObject



105
# File 'app/models/user.rb', line 105

def rights_file; Rails.root.join("config", "rights.yml"); end

.rights_listObject



108
# File 'app/models/user.rb', line 108

def rights_list; @@rights_list; end

Instance Method Details

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

# Find and check user account def self.authenticate(user_name, password)

if user = self.find_by_user_name_and_loggable(user_name.to_s.downcase, true)
  if user.locked or !user.authenticated?(password.to_s)
    user = nil
  end
end
return user

end



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

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 = tc(:no_right_defined_for_this_part_of_the_application, :controller => controller_name, :action => action_name)
  elsif (rights = self.class.rights[controller_name.to_sym][action_name.to_sym]).nil?
    message = tc(:no_right_defined_for_this_part_of_the_application, :controller => controller_name, :action => action_name)
  elsif (rights & [:__minimum__, :__public__]).empty? and (rights_list & rights).empty? and not self.administrator?
    message = tc(:no_right_defined_for_this_part_of_the_application_and_this_user)
  end
  return message
end

#can?(right) ⇒ Boolean

Returns:



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

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

#diff_less(right_markup = 'div', separator = '') ⇒ Object



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

def diff_less(right_markup = 'div', separator='')
  return '' if self.administrator?
  (self.role.rights_array-self.rights_array).select{|x| self.class.rights_list.include?(x)}.collect{|x| "<#{right_markup}>"+::I18n.t("rights.#{x}")+"</#{right_markup}>"}.join(separator).html_safe
end

#diff_more(right_markup = 'div', separator = '') ⇒ Object



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

def diff_more(right_markup = 'div', separator='')
  return '<div>&infin;</div>'.html_safe if self.administrator?
  (self.rights_array-self.role.rights_array).select{|x| self.class.rights_list.include?(x)}.collect{|x| "<#{right_markup}>"+::I18n.t("rights.#{x}")+"</#{right_markup}>"}.join(separator).html_safe
end

#labelObject



134
135
136
# File 'app/models/user.rb', line 134

def label
  self.full_name
end

#nameObject



130
131
132
# File 'app/models/user.rb', line 130

def name
  self.full_name
end

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



173
174
175
176
177
178
179
180
181
182
# File 'app/models/user.rb', line 173

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

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

Find or create preference for given name



161
162
163
164
165
166
167
168
169
170
# File 'app/models/user.rb', line 161

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

#rights_arrayObject



138
139
140
# File 'app/models/user.rb', line 138

def rights_array
  self.rights.to_s.split(/\s+/).collect{|x| x.to_sym}
end

#rights_array=(array) ⇒ Object



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

def rights_array=(array)
  narray = array.select{|x| self.class.rights_list.include? x.to_sym}.collect{|x| x.to_sym}
  self.rights = narray.join(" ")
  return narray
end