Class: User

Inherits:
Usman::ApplicationRecord show all
Extended by:
KuppayamValidators, Usman::ImportErrorHandler
Defined in:
app/models/user.rb

Constant Summary collapse

PENDING =

Constants

"pending"
APPROVED =
"approved"
SUSPENDED =
"suspended"
STATUS =
{ 
  PENDING => "Pending", 
  APPROVED => "Approved", 
  SUSPENDED => "Suspended"
}
STATUS_REVERSE =
{ 
  "Pending" => PENDING, 
  "Approved" => APPROVED, 
  "Suspended" => SUSPENDED
}
EXCLUDED_JSON_ATTRIBUTES =
[:confirmation_token, :password_digest, :reset_password_token, :unlock_token, :status, :reset_password_sent_at, :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :confirmed_at, :confirmation_sent_at, :unconfirmed_email, :failed_attempts, :locked_at, :created_at, :updated_at]
DEFAULT_PASSWORD =
"Password@1"
SESSION_TIME_OUT =
30.minutes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Usman::ImportErrorHandler

import_from_csv

Class Method Details

.find_by_email_or_username(query) ⇒ Object


Class Methods




53
54
55
# File 'app/models/user.rb', line 53

def self.find_by_email_or_username(query)
  self.where("LOWER(email) = LOWER('#{query}') OR LOWER(username) = LOWER('#{query}')").first
end

.save_row_data(row, base_path) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/user.rb', line 74

def self.save_row_data(row, base_path)

  image_base_path = base_path + "images/"

  row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }

  return if row[:name].blank?

  user = User.find_by_username(row[:username]) || User.new
  user.name = row[:name]
  user.username = row[:username]
  user.designation = row[:designation]
  user.email = row[:email]
  user.phone = row[:phone]

  user.super_admin = ["true", "t","1","yes","y"].include?(row[:super_admin].to_s.downcase.strip)

  user.status = row[:status]
  user.assign_default_password

  # Initializing error hash for displaying all errors altogether
  error_object = Usman::ErrorHash.new

  if user.valid?
    user.save!
  else
    summary = "Error while saving user: #{user.name}"
    details = "Error! #{user.errors.full_messages.to_sentence}"
    error_object.errors << { summary: summary, details: details }
  end

  ## Adding a profile picture
  begin
    image_path = image_base_path + "users/#{user.username}.png"
    image_path = image_base_path + "users/#{user.username}.jpg" unless File.exists?(image_path)
    if File.exists?(image_path)
      user.build_profile_picture
      user.profile_picture.image = File.open(image_path)
      if user.profile_picture.valid?
        user.profile_picture.save
      else
        summary = "Error while saving user: #{user.name}"
        details = "Error! #{user.errors.full_messages.to_sentence}"
        details << ", #{user.profile_picture.errors.full_messages.to_sentence}" if user.profile_picture
        error_object.errors << { summary: summary, details: details }
      end
    else
      summary = "Profile Picture not found for user: #{user.name}"
      details = "#{image_path}/png doesn't exists"
      error_object.warnings << { summary: summary, details: details }
    end
  rescue => e
    summary = "Error during processing: #{$!}"
    details = "User: #{user.name}, Image Path: #{image_path}"
    stack_trace = "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
    error_object.errors << { summary: summary, details: details, stack_trace: stack_trace }
  end if user.profile_picture.blank?

  return error_object
end

Instance Method Details

#approve!Object

change the status to :approved Return the status

Examples

>>> user.approve!
=> "approved"


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

def approve!
  self.update_attribute(:status, APPROVED)
end

#approved?Boolean

  • Return true if the user is not approved, else false.

Examples

>>> user.approved?
=> true

Returns:

  • (Boolean)


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

def approved?
  (status == APPROVED)
end

#assign_default_passwordObject



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

def assign_default_password
  self.password = DEFAULT_PASSWORD
  self.password_confirmation = DEFAULT_PASSWORD
end

#can_be_approved?Boolean

Returns:

  • (Boolean)


291
292
293
# File 'app/models/user.rb', line 291

def can_be_approved?
  pending? or suspended?
end

#can_be_deleted?Boolean

Returns:

  • (Boolean)


303
304
305
# File 'app/models/user.rb', line 303

def can_be_deleted?
  return true
end

#can_be_edited?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'app/models/user.rb', line 307

def can_be_edited?
  !suspended?
end

#can_be_marked_as_pending?Boolean

Returns:

  • (Boolean)


295
296
297
# File 'app/models/user.rb', line 295

def can_be_marked_as_pending?
  approved? or suspended?
end

#can_be_suspended?Boolean

Returns:

  • (Boolean)


299
300
301
# File 'app/models/user.rb', line 299

def can_be_suspended?
  approved? or pending?
end

#can_create?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_create?(feature_name)
  feature = get_feature(feature_name)

  permission = Permission.where("feature_id = ? AND user_id = ?", feature.id, self.id).first
  permission && permission.can_create?
end

#can_delete?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


284
285
286
287
288
289
# File 'app/models/user.rb', line 284

def can_delete?(feature_name)
  feature = get_feature(feature_name)

  permission = Permission.where("feature_id = ? AND user_id = ?", feature.id, self.id).first
  permission && permission.can_delete?
end

#can_read?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


270
271
272
273
274
275
# File 'app/models/user.rb', line 270

def can_read?(feature_name)
  feature = get_feature(feature_name)

  permission = Permission.where("feature_id = ? AND user_id = ?", feature.id, self.id).first
  permission && permission.can_read?
end

#can_update?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_update?(feature_name)
  feature = get_feature(feature_name)

  permission = Permission.where("feature_id = ? AND user_id = ?", feature.id, self.id).first
  permission && permission.can_update?
end

#default_image_url(size = "small") ⇒ Object



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

def default_image_url(size="small")
  "/assets/kuppayam/defaults/user-#{size}.png"
end

#display_nameObject

  • Return full name

Examples

>>> user.display_name
=> "Joe Black"


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

def display_name
  "#{name}"
end

#end_sessionObject



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

def end_session
  # Reseting the auth token for user when he logs out.
  # (Time.now - 1.second)
  self.update_attributes auth_token: SecureRandom.hex, token_created_at: nil
end

#generate_reset_password_tokenObject



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

def generate_reset_password_token
   self.reset_password_token = SecureRandom.hex unless self.reset_password_token
   self.reset_password_sent_at = Time.now unless self.reset_password_sent_at
end

#is_super_admin?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'app/models/user.rb', line 198

def is_super_admin?
  super_admin
end

#pending!Object

change the status to :pending Return the status

Examples

>>> user.pending!
=> "pending"


176
177
178
# File 'app/models/user.rb', line 176

def pending!
  self.update_attribute(:status, PENDING)
end

#pending?Boolean

  • Return true if the user is pending, else false.

Examples

>>> user.pending?
=> true

Returns:

  • (Boolean)


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

def pending?
  (status == PENDING)
end

#set_permission(feature_name, **options) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'app/models/user.rb', line 248

def set_permission(feature_name, **options)
  options.reverse_merge!(
    can_create: false,
    can_read: true,
    can_update: false,
    can_delete: false
  )

  feature = get_feature(feature_name)

  permission = Permission.where("user_id = ? AND feature_id = ?", self.id, feature.id).first || Permission.new(user: self, feature: feature)
  permission.assign_attributes(options)
  permission.save
end

#start_sessionObject



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/user.rb', line 202

def start_session
  # FIX ME - specs are not written to ensure that all these data are saved
  self.token_created_at = Time.now
  self. = self. ? self. + 1 : 1
  self. = self.
  self. = self.
  self. = self.token_created_at

  # FIX ME - pass remote_ip to this method.
  # Make necessary changes to authentication service to make it work
  # self.current_sign_in_ip = remote_ip if remote_ip
  self.save
end

#suspend!Object

change the status to :suspended Return the status

Examples

>>> user.suspend!
=> "suspended"


194
195
196
# File 'app/models/user.rb', line 194

def suspend!
  self.update_attribute(:status, SUSPENDED)
end

#suspended?Boolean

  • Return true if the user is suspended, else false.

Examples

>>> user.suspended?
=> true

Returns:

  • (Boolean)


167
168
169
# File 'app/models/user.rb', line 167

def suspended?
  (status == SUSPENDED)
end

#token_about_to_expire?Boolean

Returns:

  • (Boolean)


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

def token_about_to_expire?
  return self.token_created_at.nil? || (Time.now > self.token_created_at + (SESSION_TIME_OUT - 1.minute))
end

#token_expired?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'app/models/user.rb', line 235

def token_expired?
  return self.token_created_at.nil? || (Time.now > self.token_created_at + SESSION_TIME_OUT)
end

#update_tokenObject



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

def update_token
  self.update_attribute(:token_created_at, Time.now)
end