Module: EffectiveDeviseUser::ClassMethods

Defined in:
app/models/concerns/effective_devise_user.rb

Instance Method Summary collapse

Instance Method Details

#effective_devise_user?Boolean

Returns:

  • (Boolean)


82
# File 'app/models/concerns/effective_devise_user.rb', line 82

def effective_devise_user?; true; end

#filter_parametersObject



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
# File 'app/models/concerns/effective_devise_user.rb', line 89

def filter_parameters
  [
    :encrypted_password,
    :reset_password_token,
    :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,
    :invitation_token,
    :invitation_created_at,
    :invitation_sent_at,
    :invitation_accepted_at,
    :invitation_limit,
    :invited_by_type,
    :invited_by_id,
    :invitations_count,
    :uid,
    :provider,
    :access_token,
    :refresh_token,
    :token_expires_at,
    :avatar_url,
    :roles_mask,
    :confirmation_sent_at,
    :confirmed_at,
    :unconfirmed_email
  ]
end

#find_by_any_email(value) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'app/models/concerns/effective_devise_user.rb', line 207

def find_by_any_email(value)
  email = value.to_s.strip.downcase

  if has_alternate_email?
    where(email: email).or(where(alternate_email: email)).first
  else
    where(email: email).first
  end
end

#find_first_by_auth_conditions(tainted_conditions, opts = {}) ⇒ Object



180
181
182
183
184
185
186
187
# File 'app/models/concerns/effective_devise_user.rb', line 180

def find_first_by_auth_conditions(tainted_conditions, opts = {})
  conditions = devise_parameter_filter.filter(tainted_conditions).merge(opts)

  user = to_adapter.find_first(conditions)
  return user if user.present? && user.persisted?

  to_adapter.find_first(alternate_email: conditions[:email]) if has_alternate_email?
end

#find_for_database_authentication(warden_conditions) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/concerns/effective_devise_user.rb', line 190

def find_for_database_authentication(warden_conditions)
  conditions = warden_conditions.dup.presence || {}

  email = conditions.delete(:email).to_s.strip.downcase
  raise "Expected an email condition but got #{conditions} instead" unless email.present?

  if has_alternate_email?
    where(conditions).where('email = :email OR alternate_email = :email', email: email).first
  else
    where(conditions).where(email: email).first
  end
end

#from_omniauth(auth, params) ⇒ Object



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
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/concerns/effective_devise_user.rb', line 121

def from_omniauth(auth, params)
  invitation_token = (params.presence || {})['invitation_token']

  email = (auth.info.email.presence || "#{auth.uid}@#{auth.provider}.none").downcase
  image = auth.info.image
  name = auth.info.name || auth.dig(:extra, :raw_info, :login)

  user = if invitation_token
    find_by_invitation_token(invitation_token, false) || raise(ActiveRecord::RecordNotFound)
  else
    where(uid: auth.uid).or(where(email: email)).first || self.new()
  end

  user.assign_attributes(
    uid: auth.uid,
    provider: auth.provider,
    email: email,
    avatar_url: image,
    name: name,
    first_name: (auth.info.first_name.presence || name.split(' ').first.presence || 'First'),
    last_name: (auth.info.last_name.presence || name.split(' ').last.presence || 'Last')
  )

  if auth.respond_to?(:credentials)
    user.assign_attributes(
      access_token: auth.credentials.token,
      refresh_token: auth.credentials.refresh_token,
      token_expires_at: Time.zone.at(auth.credentials.expires_at), # We are given integer datetime e.g. '1549394077'
    )
  end

  # Make a password
  user.password = Devise.friendly_token[0, 20] if user.encrypted_password.blank?

  # Devise Invitable
  invitation_token ? user.accept_invitation! : user.save!

  # Devise Confirmable
  user.confirm if user.respond_to?(:confirm)

  user
end

#has_alternate_email?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'app/models/concerns/effective_devise_user.rb', line 203

def has_alternate_email?
  column_names.include?('alternate_email')
end

#permitted_sign_up_paramsObject

Should contain all fields as per views/users/_sign_up_fields



84
85
86
87
# File 'app/models/concerns/effective_devise_user.rb', line 84

def  # Should contain all fields as per views/users/_sign_up_fields
  raise('please define a self.permitted_sign_up_params')
  [:email, :password, :password_confirmation, :first_name, :last_name, :name, :login]
end

#send_reset_password_instructions(attributes = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/models/concerns/effective_devise_user.rb', line 165

def send_reset_password_instructions(attributes = {})
  recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
  return recoverable unless recoverable.persisted?

  # Add custom errors and require a confirmation if previous sign in was provider
  if recoverable.provider.present? && attributes[:confirm_new_password].blank?
    recoverable.errors.add(:email, "previous sign in was with #{recoverable.provider}")
    recoverable.errors.add(:confirm_new_password, 'please confirm to proceed')
  end

  recoverable.send_reset_password_instructions if recoverable.errors.blank?
  recoverable
end