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"- STATUS =
{ PENDING => "Pending", APPROVED => "Approved", SUSPENDED => "Suspended" }
- STATUS_REVERSE =
{ "Pending" => PENDING, "Approved" => APPROVED, "Suspended" => SUSPENDED }
- 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, :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
Instance Method Summary collapse
-
#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
-
#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_reset_password_token ⇒ Object
- #generate_username_and_password ⇒ Object
- #has_role?(role) ⇒ Boolean
-
#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
.find_by_email_or_username(query) ⇒ Object
398 399 400 |
# File 'app/models/user.rb', line 398 def self.find_by_email_or_username(query) self.where("LOWER(email) = LOWER('#{query}') OR LOWER(username) = LOWER('#{query}')").first end |
.save_row_data(hsh) ⇒ Object
Import Methods
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 134 135 136 |
# File 'app/models/user.rb', line 102 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.message}" 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.full_messages.to_sentence}" error_object.errors << { summary: summary, details: details } end return error_object end |
Instance Method Details
#add_role(role) ⇒ Object
Role Methods
324 325 326 327 328 329 330 331 332 333 |
# File 'app/models/user.rb', line 324 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"
183 184 185 |
# File 'app/models/user.rb', line 183 def approve! self.update_attribute(:status, APPROVED) end |
#approved? ⇒ Boolean
-
Return true if the user is not approved, else false.
Examples
>>> user.approved?
=> true
149 150 151 |
# File 'app/models/user.rb', line 149 def approved? (status == APPROVED) end |
#as_json(options = {}) ⇒ Object
Exclude some attributes info from json output.
69 70 71 72 73 74 75 76 |
# File 'app/models/user.rb', line 69 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
245 246 247 248 |
# File 'app/models/user.rb', line 245 def assign_default_password self.password = DEFAULT_PASSWORD self.password_confirmation = DEFAULT_PASSWORD end |
#can_be_approved? ⇒ Boolean
301 302 303 |
# File 'app/models/user.rb', line 301 def can_be_approved? pending? or suspended? end |
#can_be_deleted? ⇒ Boolean
313 314 315 |
# File 'app/models/user.rb', line 313 def can_be_deleted? suspended? end |
#can_be_edited? ⇒ Boolean
317 318 319 |
# File 'app/models/user.rb', line 317 def can_be_edited? !suspended? end |
#can_be_marked_as_pending? ⇒ Boolean
305 306 307 |
# File 'app/models/user.rb', line 305 def can_be_marked_as_pending? approved? or suspended? end |
#can_be_suspended? ⇒ Boolean
309 310 311 |
# File 'app/models/user.rb', line 309 def can_be_suspended? approved? or pending? end |
#can_create?(feature_name) ⇒ Boolean
273 274 275 276 277 278 |
# File 'app/models/user.rb', line 273 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
294 295 296 297 298 299 |
# File 'app/models/user.rb', line 294 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
280 281 282 283 284 285 |
# File 'app/models/user.rb', line 280 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
287 288 289 290 291 292 |
# File 'app/models/user.rb', line 287 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
361 362 363 |
# File 'app/models/user.rb', line 361 def default_image_url(size="small") "/assets/kuppayam/defaults/user-#{size}.png" end |
#display_name ⇒ Object
-
Return full name
Examples
>>> user.display_name
=> "Joe Black"
357 358 359 |
# File 'app/models/user.rb', line 357 def display_name "#{name}" end |
#end_session ⇒ Object
235 236 237 238 239 240 241 242 243 |
# File 'app/models/user.rb', line 235 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
211 212 213 |
# File 'app/models/user.rb', line 211 def female? (gender == FEMALE) end |
#generate_reset_password_token ⇒ Object
250 251 252 253 |
# File 'app/models/user.rb', line 250 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
365 366 367 368 369 370 371 |
# File 'app/models/user.rb', line 365 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_role?(role) ⇒ Boolean
340 341 342 343 344 345 346 347 348 |
# File 'app/models/user.rb', line 340 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 |
#male? ⇒ Boolean
-
Return true if the user is male, else false.
Examples
>>> user.male?
=> true
203 204 205 |
# File 'app/models/user.rb', line 203 def male? (gender == MALE) end |
#nogender? ⇒ Boolean
-
Return true if the user is nogender, else false.
Examples
>>> user.nogender?
=> true
219 220 221 |
# File 'app/models/user.rb', line 219 def nogender? (gender == NOGENDER) end |
#pending! ⇒ Object
change the status to :pending Return the status
Examples
>>> user.pending!
=> "pending"
174 175 176 |
# File 'app/models/user.rb', line 174 def pending! self.update_attribute(:status, PENDING) end |
#pending? ⇒ Boolean
-
Return true if the user is pending, else false.
Examples
>>> user.pending?
=> true
157 158 159 |
# File 'app/models/user.rb', line 157 def pending? (status == PENDING) end |
#remove_role(role) ⇒ Object
335 336 337 338 |
# File 'app/models/user.rb', line 335 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
258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'app/models/user.rb', line 258 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
226 227 228 229 230 231 232 233 |
# File 'app/models/user.rb', line 226 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"
192 193 194 |
# File 'app/models/user.rb', line 192 def suspend! self.update_attribute(:status, SUSPENDED) end |
#suspended? ⇒ Boolean
-
Return true if the user is suspended, else false.
Examples
>>> user.suspended?
=> true
165 166 167 |
# File 'app/models/user.rb', line 165 def suspended? (status == SUSPENDED) end |