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)


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

def effective_devise_user?; true; end

#filter_parametersObject



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

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



226
227
228
229
230
231
232
233
234
# File 'app/models/concerns/effective_devise_user.rb', line 226

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



199
200
201
202
203
204
205
206
# File 'app/models/concerns/effective_devise_user.rb', line 199

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



209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/concerns/effective_devise_user.rb', line 209

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/concerns/effective_devise_user.rb', line 140

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)


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

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



103
104
105
106
# File 'app/models/concerns/effective_devise_user.rb', line 103

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



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/concerns/effective_devise_user.rb', line 184

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