Class: Ibrain::User

Inherits:
Base
  • Object
show all
Includes:
Devise::JWT::RevocationStrategies::JTIMatcher
Defined in:
app/models/ibrain/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jwt_tokenObject

Returns the value of attribute jwt_token.



5
6
7
# File 'app/models/ibrain/user.rb', line 5

def jwt_token
  @jwt_token
end

Class Method Details

.create_with_line!(params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'app/models/ibrain/user.rb', line 61

def create_with_line!(params)
  user = created!({
    uid: params['uid'],
    provider: 'line',
    remote_avatar_url: params['info']['image']
  })

  user.skip_confirmation! unless user&.confirmed?
  user
end

.ibrain_find(params, available_columns) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/ibrain/user.rb', line 40

def ibrain_find(params, available_columns)
  matched_value = params[:username] || params[:email]

  if matched_value.present?
    query = available_columns.map do |column_name|
      <<~RUBY
        #{column_name} = '#{matched_value}'
      RUBY
    end.join(' OR ')

    where(query).first
  end
end

.random_passwordObject



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

def random_password
  (('A'..'Z').to_a.sample(4) + ["~", "!", "@", "#", "$", "%", "^", "&", "*", "_", "-"].sample(1) + ('0'..'9').to_a.sample(2) + ('a'..'z').to_a.sample(4)).join
end

.social_find_or_initialize(params) ⇒ Object



54
55
56
57
58
59
# File 'app/models/ibrain/user.rb', line 54

def social_find_or_initialize(params)
  user = find_by(provider: params[:provider], uid: params[:uid])
  return user if user.present?

  create!(params)
end

Instance Method Details

#can_skip_confirmation?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/ibrain/user.rb', line 35

def can_skip_confirmation?
  try(:is_admin?) || email.blank?
end

#jwt_payloadObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/ibrain/user.rb', line 22

def jwt_payload
  # for hasura
  hasura_keys = {
      'https://hasura.io/jwt/claims': {
      'x-hasura-allowed-roles': Ibrain.user_class.roles.keys,
      'x-hasura-default-role': role,
      'x-hasura-user-id': id.to_s
    }
  }

  super.merge({ 'role' => role }, hasura_keys)
end