Class: User

Inherits:
ApplicationRecord
  • Object
show all
Includes:
ConfigManager, StringLengthLimit
Defined in:
app/models/user.rb

Overview

Publify user. TODO: Should belong to a blog

Constant Summary collapse

ADMIN =
"admin"
PUBLISHER =
"publisher"
CONTRIBUTOR =
"contributor"
STATUS =
%w(active inactive).freeze

Constants included from StringLengthLimit

StringLengthLimit::STRING_LIMIT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigManager

#canonicalize, included

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



38
39
40
# File 'app/models/user.rb', line 38

def filename
  @filename
end

Class Method Details

.saltObject



61
62
63
# File 'app/models/user.rb', line 61

def self.salt
  "20ac4d290c2293702c64b3b287ae5ea79b26a5c1"
end

.to_prefixObject



102
103
104
# File 'app/models/user.rb', line 102

def self.to_prefix
  "author"
end

Instance Method Details

#active_for_authentication?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/models/user.rb', line 94

def active_for_authentication?
  super && state == "active"
end

#admin?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/user.rb', line 124

def admin?
  profile == User::ADMIN
end

#article_counterObject



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

def article_counter
  articles.size
end

#devise_valid_password?Object

Authenticate users with old password hashes



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

alias devise_valid_password? valid_password?

#display_nameObject



110
111
112
113
114
115
116
117
118
# File 'app/models/user.rb', line 110

def display_name
  if nickname.present?
    nickname
  elsif name.present?
    name
  else
    
  end
end

#display_namesObject



71
72
73
74
# File 'app/models/user.rb', line 71

def display_names
  [:login, :nickname, :firstname, :lastname, :first_and_last_name].
    map { |f| send(f) }.delete_if(&:empty?)
end

#first_and_last_nameObject



65
66
67
68
69
# File 'app/models/user.rb', line 65

def first_and_last_name
  return "" unless firstname.present? && lastname.present?

  "#{firstname} #{lastname}"
end

#has_twitter_configured?Boolean

Returns:

  • (Boolean)


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

def has_twitter_configured?
  twitter_oauth_token.present? && twitter_oauth_token_secret.present?
end


120
121
122
# File 'app/models/user.rb', line 120

def permalink
  
end

#text_filterObject



98
99
100
# File 'app/models/user.rb', line 98

def text_filter
  TextFilter.make_filter(text_filter_name)
end

#update_twitter_profile_image(img) ⇒ Object



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

def update_twitter_profile_image(img)
  return if twitter_profile_image == img

  self.twitter_profile_image = img
  save
end

#valid_password?(password) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/user.rb', line 79

def valid_password?(password)
  devise_valid_password?(password)
rescue BCrypt::Errors::InvalidHash
  digest = Digest::SHA1.hexdigest("#{self.class.salt}--#{password}--")
  if digest == encrypted_password
    # Update old SHA1 password with new Devise ByCrypt password
    self.encrypted_password = password_digest(password)
    save
    true
  else
    # If not BCrypt password and not old SHA1 password deny access
    false
  end
end