Class: User
- Inherits:
-
Usman::ApplicationRecord
- Object
- ActiveRecord::Base
- Usman::ApplicationRecord
- User
- Defined in:
- app/models/user.rb
Constant Summary collapse
- PENDING =
Constants
"pending"
- APPROVED =
"approved"
- SUSPENDED =
"suspended"
- DELETED =
"deleted"
- STATUS =
{ PENDING => "Pending", APPROVED => "Approved", SUSPENDED => "Suspended", DELETED => "Deleted" }
- STATUS_REVERSE =
{ "Pending" => PENDING, "Approved" => APPROVED, "Suspended" => SUSPENDED, "Deleted" => DELETED }
- MALE =
"male"
- FEMALE =
"female"
- NOGENDER =
"nogender"
- GENDER =
{ MALE => "Male", FEMALE => "Female", NOGENDER => "No Gender" }
- GENDER_REVERSE =
{ "Male" => MALE, "Female" => FEMALE, "No Gender" => NOGENDER }
- EXCLUDED_JSON_ATTRIBUTES =
[:confirmation_token, :password_digest, :reset_password_token, :auth_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 =
120.minutes
Class Method Summary collapse
-
.save_row_data(hsh) ⇒ Object
Import Methods.
Instance Method Summary collapse
-
#add_permission(feature_name, **options) ⇒ Object
Permission Methods ——————.
-
#add_role(role) ⇒ Object
Role Methods ————.
-
#approve! ⇒ Object
change the status to :approved Return the status == Examples >>> user.approve! => “approved”.
-
#approved? ⇒ Boolean
-
Return true if the user is not approved, else false.
-
-
#as_json(options = {}) ⇒ Object
Exclude some attributes info from json output.
- #assign_default_password ⇒ Object
- #can_be_approved? ⇒ Boolean
- #can_be_deleted? ⇒ Boolean
- #can_be_edited? ⇒ Boolean
- #can_be_marked_as_pending? ⇒ Boolean
- #can_be_suspended? ⇒ Boolean
- #can_create?(feature_name) ⇒ Boolean
- #can_delete?(feature_name) ⇒ Boolean
- #can_read?(feature_name) ⇒ Boolean
- #can_update?(feature_name) ⇒ Boolean
- #default_image_url(size = "small") ⇒ Object
-
#delete! ⇒ Object
change the status to :deleted Return the status == Examples >>> user.delete! => “deleted”.
-
#deleted? ⇒ Boolean
-
Return true if the user is deleted, else false.
-
-
#display_name ⇒ Object
-
Return full name == Examples >>> user.display_name => “Joe Black”.
-
- #end_session ⇒ Object
-
#female? ⇒ Boolean
-
Return true if the user is female, else false.
-
- #generate_dummy_data(registration) ⇒ Object
- #generate_reset_password_token ⇒ Object
- #generate_username_and_password ⇒ Object
- #has_create_permission?(class_name) ⇒ Boolean
- #has_delete_permission?(class_name) ⇒ Boolean
- #has_read_permission?(class_name) ⇒ Boolean
- #has_role?(role) ⇒ Boolean
- #has_update_permission?(class_name) ⇒ Boolean
-
#image_configuration ⇒ Object
Image Configuration ——————-.
-
#male? ⇒ Boolean
-
Return true if the user is male, else false.
-
-
#nogender? ⇒ Boolean
-
Return true if the user is nogender, else false.
-
-
#pending! ⇒ Object
change the status to :pending Return the status == Examples >>> user.pending! => “pending”.
-
#pending? ⇒ Boolean
-
Return true if the user is pending, else false.
-
- #remove_role(role) ⇒ Object
-
#set_permission(feature_name, **options) ⇒ Object
Permission Methods ——————.
-
#start_session(remote_ip) ⇒ Object
Authentication Methods ———————-.
-
#suspend! ⇒ Object
change the status to :suspended Return the status == Examples >>> user.suspend! => “suspended”.
-
#suspended? ⇒ Boolean
-
Return true if the user is suspended, else false.
-
Class Method Details
.save_row_data(hsh) ⇒ Object
Import Methods
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/models/user.rb', line 117 def self.save_row_data(hsh) return if hsh[:name].blank? user = User.find_by_username(hsh[:username]) || User.new user.name = hsh[:name] user.username = hsh[:username] user.designation = hsh[:designation] user.email = hsh[:email] user.phone = hsh[:phone] user.super_admin = ["true", "t","1","yes","y"].include?(hsh[:super_admin].to_s.downcase.strip) user.status = hsh[:status] user.assign_default_password # Initializing error hash for displaying all errors altogether error_object = Kuppayam::Importer::ErrorHash.new if user.valid? begin user.save! rescue Exception => e summary = "uncaught #{e} exception while handling connection: #{e.}" details = "Stack trace: #{e.backtrace.map {|l| " #{l}\n"}.join}" error_object.errors << { summary: summary, details: details } end else summary = "Error while saving user: #{user.name}" details = "Error! #{user.errors..to_sentence}" error_object.errors << { summary: summary, details: details } end return error_object end |
Instance Method Details
#add_permission(feature_name, **options) ⇒ Object
Permission Methods
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'app/models/user.rb', line 389 def (feature_name, **) .reverse_merge!( can_create: false, can_read: true, can_update: false, can_delete: false ) feature = Feature.find_by_name(feature_name) = self..where("feature_id = ?", feature.id).first || self..build .feature = feature .can_create = [:can_create] .can_read = [:can_read] .can_update = [:can_update] .can_delete = [:can_delete] .save end |
#add_role(role) ⇒ Object
Role Methods
360 361 362 363 364 365 366 367 368 369 |
# File 'app/models/user.rb', line 360 def add_role(role) return false unless self.approved? role = Role.find_by_name(role) if role.is_a?(String) if role self.roles << role unless self.has_role?(role) return true else return false end end |
#approve! ⇒ Object
change the status to :approved Return the status
Examples
>>> user.approve!
=> "approved"
207 208 209 210 |
# File 'app/models/user.rb', line 207 def approve! self.update_attribute(:status, APPROVED) self.registration.update_attribute(:status, Registration::VERIFIED) if self.registration end |
#approved? ⇒ Boolean
-
Return true if the user is not approved, else false.
Examples
>>> user.approved?
=> true
164 165 166 |
# File 'app/models/user.rb', line 164 def approved? (status == APPROVED) end |
#as_json(options = {}) ⇒ Object
Exclude some attributes info from json output.
79 80 81 82 83 84 85 86 |
# File 'app/models/user.rb', line 79 def as_json(={}) [:except] ||= EXCLUDED_JSON_ATTRIBUTES #options[:include] ||= [] #options[:methods] = [] #options[:methods] << :profile_image json = super() Hash[*json.map{|k, v| [k, v || ""]}.flatten] end |
#assign_default_password ⇒ Object
281 282 283 284 |
# File 'app/models/user.rb', line 281 def assign_default_password self.password = DEFAULT_PASSWORD self.password_confirmation = DEFAULT_PASSWORD end |
#can_be_approved? ⇒ Boolean
337 338 339 |
# File 'app/models/user.rb', line 337 def can_be_approved? pending? or suspended? end |
#can_be_deleted? ⇒ Boolean
349 350 351 |
# File 'app/models/user.rb', line 349 def can_be_deleted? suspended? end |
#can_be_edited? ⇒ Boolean
353 354 355 |
# File 'app/models/user.rb', line 353 def can_be_edited? !suspended? end |
#can_be_marked_as_pending? ⇒ Boolean
341 342 343 |
# File 'app/models/user.rb', line 341 def can_be_marked_as_pending? approved? or suspended? end |
#can_be_suspended? ⇒ Boolean
345 346 347 |
# File 'app/models/user.rb', line 345 def can_be_suspended? approved? or pending? end |
#can_create?(feature_name) ⇒ Boolean
309 310 311 312 313 314 |
# File 'app/models/user.rb', line 309 def can_create?(feature_name) feature = get_feature(feature_name) = Permission.where("feature_id = ? AND user_id = ?", feature.id, self.id).first && .can_create? end |
#can_delete?(feature_name) ⇒ Boolean
330 331 332 333 334 335 |
# File 'app/models/user.rb', line 330 def can_delete?(feature_name) feature = get_feature(feature_name) = Permission.where("feature_id = ? AND user_id = ?", feature.id, self.id).first && .can_delete? end |
#can_read?(feature_name) ⇒ Boolean
316 317 318 319 320 321 |
# File 'app/models/user.rb', line 316 def can_read?(feature_name) feature = get_feature(feature_name) = Permission.where("feature_id = ? AND user_id = ?", feature.id, self.id).first && .can_read? end |
#can_update?(feature_name) ⇒ Boolean
323 324 325 326 327 328 |
# File 'app/models/user.rb', line 323 def can_update?(feature_name) feature = get_feature(feature_name) = Permission.where("feature_id = ? AND user_id = ?", feature.id, self.id).first && .can_update? end |
#default_image_url(size = "small") ⇒ Object
462 463 464 |
# File 'app/models/user.rb', line 462 def default_image_url(size="small") "/assets/kuppayam/defaults/user-#{size}.png" end |
#delete! ⇒ Object
change the status to :deleted Return the status
Examples
>>> user.delete!
=> "deleted"
227 228 229 230 |
# File 'app/models/user.rb', line 227 def delete! self.update_attribute(:status, DELETED) self.registration.update_attribute(:status, DELETED) if self.registration end |
#deleted? ⇒ Boolean
-
Return true if the user is deleted, else false.
Examples
>>> user.deleted?
=> true
188 189 190 |
# File 'app/models/user.rb', line 188 def deleted? (status == DELETED) end |
#display_name ⇒ Object
-
Return full name
Examples
>>> user.display_name
=> "Joe Black"
458 459 460 |
# File 'app/models/user.rb', line 458 def display_name "#{name}" end |
#end_session ⇒ Object
271 272 273 274 275 276 277 278 279 |
# File 'app/models/user.rb', line 271 def end_session self.last_sign_in_at = self.current_sign_in_at self.last_sign_in_ip = self.current_sign_in_ip self.current_sign_in_at = nil self.current_sign_in_ip = nil self.save end |
#female? ⇒ Boolean
-
Return true if the user is female, else false.
Examples
>>> user.female?
=> true
247 248 249 |
# File 'app/models/user.rb', line 247 def female? (gender == FEMALE) end |
#generate_dummy_data(registration) ⇒ Object
474 475 476 477 478 479 480 481 |
# File 'app/models/user.rb', line 474 def generate_dummy_data(registration) generate_username_and_password self.email = "#{self.username}@donedealapps.com" self.name = "User #{registration.try(:id)}" self.country_id = registration.country_id self.city_id = registration.city_id self.dummy = true end |
#generate_reset_password_token ⇒ Object
286 287 288 289 |
# File 'app/models/user.rb', line 286 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 |
#generate_username_and_password ⇒ Object
466 467 468 469 470 471 472 |
# File 'app/models/user.rb', line 466 def generate_username_and_password self.username = SecureRandom.hex(4) unless self.username # Password should contain at least one special character, integer and one upper case character passwd = SecureRandom.hex(8) + "A@1" unless self.password self.password = passwd self.password_confirmation = passwd end |
#has_create_permission?(class_name) ⇒ Boolean
418 419 420 421 422 423 424 425 426 427 |
# File 'app/models/user.rb', line 418 def (class_name) return true if self.super_admin? feature = Feature.published.find_by_name(class_name.to_s) if feature = self..where("feature_id =?", feature.id).first return && .can_create? else return false end end |
#has_delete_permission?(class_name) ⇒ Boolean
440 441 442 443 444 445 446 447 448 449 |
# File 'app/models/user.rb', line 440 def (class_name) return true if self.super_admin? feature = Feature.published.find_by_name(class_name.to_s) if feature = self..where("feature_id =?", feature.id).first return && .can_delete? else return false end end |
#has_read_permission?(class_name) ⇒ Boolean
407 408 409 410 411 412 413 414 415 416 |
# File 'app/models/user.rb', line 407 def (class_name) return true if self.super_admin? feature = Feature.published.find_by_name(class_name.to_s) if feature = self..where("feature_id =?", feature.id).first return && .can_read? else return false end end |
#has_role?(role) ⇒ Boolean
376 377 378 379 380 381 382 383 384 |
# File 'app/models/user.rb', line 376 def has_role?(role) role = Role.find_by_name(role) if role.is_a?(String) if role && role.persisted? return true if self.super_admin self.roles.exists?(:id => [role.id]) else return false end end |
#has_update_permission?(class_name) ⇒ Boolean
429 430 431 432 433 434 435 436 437 438 |
# File 'app/models/user.rb', line 429 def (class_name) return true if self.super_admin? feature = Feature.published.find_by_name(class_name.to_s) if feature = self..where("feature_id =?", feature.id).first return && .can_update? else return false end end |
#image_configuration ⇒ Object
Image Configuration
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'app/models/user.rb', line 485 def image_configuration { "Image::ProfilePicture" => { max_upload_limit: 10485760, min_upload_limit: 1024, resolutions: [400, 400], form_upload_image_label: "Upload a new Image", form_title: "Upload an Image (Profile)", form_sub_title: "Please read the instructions below:", form_instructions: [ "the filename should be in .jpg / .jpeg or .png format", "the image resolutions should be <strong>400 x 400 Pixels</strong>", "the file size should be greater than 100 Kb and or lesser than <strong>10 MB</strong>" ] } } end |
#male? ⇒ Boolean
-
Return true if the user is male, else false.
Examples
>>> user.male?
=> true
239 240 241 |
# File 'app/models/user.rb', line 239 def male? (gender == MALE) end |
#nogender? ⇒ Boolean
-
Return true if the user is nogender, else false.
Examples
>>> user.nogender?
=> true
255 256 257 |
# File 'app/models/user.rb', line 255 def nogender? (gender == NOGENDER) end |
#pending! ⇒ Object
change the status to :pending Return the status
Examples
>>> user.pending!
=> "pending"
197 198 199 200 |
# File 'app/models/user.rb', line 197 def pending! self.update_attribute(:status, PENDING) self.registration.update_attribute(:status, PENDING) if self.registration end |
#pending? ⇒ Boolean
-
Return true if the user is pending, else false.
Examples
>>> user.pending?
=> true
172 173 174 |
# File 'app/models/user.rb', line 172 def pending? (status == PENDING) end |
#remove_role(role) ⇒ Object
371 372 373 374 |
# File 'app/models/user.rb', line 371 def remove_role(role) role = Role.find_by_name(role) if role.is_a?(String) self.roles.delete(role) if role end |
#set_permission(feature_name, **options) ⇒ Object
Permission Methods
294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'app/models/user.rb', line 294 def (feature_name, **) .reverse_merge!( can_create: false, can_read: true, can_update: false, can_delete: false ) feature = get_feature(feature_name) = Permission.where("user_id = ? AND feature_id = ?", self.id, feature.id).first || Permission.new(user: self, feature: feature) .assign_attributes() .save end |
#start_session(remote_ip) ⇒ Object
Authentication Methods
262 263 264 265 266 267 268 269 |
# File 'app/models/user.rb', line 262 def start_session(remote_ip) self.current_sign_in_at = Time.now self.current_sign_in_ip = remote_ip self.sign_in_count = self.sign_in_count ? self.sign_in_count + 1 : 1 self.save end |
#suspend! ⇒ Object
change the status to :suspended Return the status
Examples
>>> user.suspend!
=> "suspended"
217 218 219 220 |
# File 'app/models/user.rb', line 217 def suspend! self.update_attribute(:status, SUSPENDED) self.registration.update_attribute(:status, SUSPENDED) if self.registration end |
#suspended? ⇒ Boolean
-
Return true if the user is suspended, else false.
Examples
>>> user.suspended?
=> true
180 181 182 |
# File 'app/models/user.rb', line 180 def suspended? (status == SUSPENDED) end |