Class: Monban::Repository::Auth::Sequel

Inherits:
Getto::Repository::Sequel
  • Object
show all
Defined in:
lib/monban/repository/auth.rb

Instance Method Summary collapse

Instance Method Details

#account_id_by_email(email:) ⇒ Object



18
19
20
21
22
23
# File 'lib/monban/repository/auth.rb', line 18

def (email:)
  db[:account_reset_password_emails]
    .where(email: email)
    .select(:account_id)
    .map{|hash| hash[:account_id] }.first
end

#account_id_by_login_id(login_id:) ⇒ Object



25
26
27
28
29
30
# File 'lib/monban/repository/auth.rb', line 25

def (login_id:)
  db[:account_login_ids]
    .where(login_id: )
    .select(:account_id)
    .map{|hash| hash[:account_id] }.first
end

#account_id_by_public_id(public_id:, now:) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/monban/repository/auth.rb', line 10

def (public_id:, now:)
  db[:account_public_ids]
    .where(public_id: public_id)
    .where(::Sequel[:account_public_ids][:expired_at] > now)
    .select(:account_id)
    .map{|hash| hash[:account_id] }.first
end

#authy_id(account_id:) ⇒ Object



91
92
93
94
95
96
# File 'lib/monban/repository/auth.rb', line 91

def authy_id(account_id:)
  db[:account_authy_ids]
    .where(account_id: )
    .select(:authy_id)
    .map{|hash| hash[:authy_id]}.first
end

#delete_reset_password_token(account_id:) ⇒ Object



114
115
116
117
118
# File 'lib/monban/repository/auth.rb', line 114

def delete_reset_password_token(account_id:)
  db[:account_reset_password_tokens]
    .where(account_id: )
    .delete
end

#insert_public_id(account_id:, public_id:, created_at:, expired_at:) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/monban/repository/auth.rb', line 80

def insert_public_id(account_id:, public_id:, created_at:, expired_at:)
  db[:account_public_ids].insert(
    account_id: ,
    public_id:  public_id,
    created_at: created_at,
    expired_at: expired_at,
    original_created_at: created_at,
  )
end

#insert_reset_password_token(account_id:, reset_token:, created_at:, expired_at:) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/monban/repository/auth.rb', line 126

def insert_reset_password_token(account_id:, reset_token:, created_at:, expired_at:)
  db[:account_reset_password_tokens].insert(
    account_id:  ,
    reset_token: reset_token,
    created_at:  created_at,
    expired_at:  expired_at,
  )
end

#login_id(account_id:) ⇒ Object



32
33
34
35
36
37
# File 'lib/monban/repository/auth.rb', line 32

def (account_id:)
  db[:account_login_ids]
    .where(account_id: )
    .select(:login_id)
    .map{|hash| hash[:login_id]}.first
end

#login_type(account_id:) ⇒ Object



39
40
41
42
# File 'lib/monban/repository/auth.rb', line 39

def (account_id:)
  # all account login with 'authy' in this implement
  "authy"
end

#password_hash_match?(account_id:, password_hash:) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/monban/repository/auth.rb', line 159

def password_hash_match?(account_id:, password_hash:)
  not db[:account_password_hashes]
    .where(account_id: , password_hash: password_hash)
    .empty?
end

#password_salt(account_id:) ⇒ Object



152
153
154
155
156
157
# File 'lib/monban/repository/auth.rb', line 152

def password_salt(account_id:)
  db[:account_password_hashes]
    .select(::Sequel.function(:substring, ::Sequel[:password_hash], 1, 30).as(:salt))
    .where(account_id: )
    .map{|hash| hash[:salt]}.first
end

#preserve_public_id_original_created_at(public_id:, original_created_at:) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/monban/repository/auth.rb', line 66

def preserve_public_id_original_created_at(public_id:, original_created_at:)
  db[:account_public_ids]
    .where(public_id: public_id)
    .update(
      original_created_at: original_created_at,
    )
end

#public_id_exists?(public_id:) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/monban/repository/auth.rb', line 74

def public_id_exists?(public_id:)
  not db[:account_public_ids]
    .where(public_id: public_id)
    .empty?
end

#public_id_original_created_at(public_id:) ⇒ Object



59
60
61
62
63
64
# File 'lib/monban/repository/auth.rb', line 59

def public_id_original_created_at(public_id:)
  db[:account_public_ids]
    .where(public_id: public_id)
    .select(:original_created_at)
    .map{|hash| hash[:original_created_at]}.first
end

#public_id_renew_enabled?(public_id:, original_created_at:) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/monban/repository/auth.rb', line 52

def public_id_renew_enabled?(public_id:, original_created_at:)
  not db[:account_public_ids]
    .where(public_id: public_id)
    .where(::Sequel[:account_public_ids][:original_created_at] > original_created_at)
    .empty?
end

#reset_password_token_exists?(reset_token:) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
# File 'lib/monban/repository/auth.rb', line 120

def reset_password_token_exists?(reset_token:)
  not db[:account_reset_password_tokens]
    .where(reset_token: reset_token)
    .empty?
end

#roles(account_id:) ⇒ Object



44
45
46
47
48
49
# File 'lib/monban/repository/auth.rb', line 44

def roles(account_id:)
  db[:account_roles]
    .where(account_id: )
    .select(:role)
    .map{|r| r[:role]}
end

#update_authy_id(account_id:, authy_id:, now:) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/monban/repository/auth.rb', line 98

def update_authy_id(account_id:, authy_id:, now:)
  if db[:account_authy_ids].where(account_id: ).empty?
    db[:account_authy_ids].insert(
      account_id: ,
      authy_id:   authy_id,
      created_at: now,
    )
  else
    db[:account_authy_ids].where(account_id: ).update(
      authy_id:   authy_id,
      created_at: now,
    )
  end
end

#update_password_hash(account_id:, password_hash:, now:) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/monban/repository/auth.rb', line 165

def update_password_hash(account_id:, password_hash:, now:)
  if db[:account_password_hashes].where(account_id: ).empty?
    db[:account_password_hashes].insert(
      account_id:    ,
      password_hash: password_hash,
      created_at:    now,
      updated_at:    now,
    )
  else
    db[:account_password_hashes].where(account_id: ).update(
      password_hash: password_hash,
      updated_at:    now,
    )
  end
end

#valid_reset_password_token?(account_id:, reset_token:, now:) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
147
148
149
# File 'lib/monban/repository/auth.rb', line 141

def valid_reset_password_token?(account_id:, reset_token:, now:)
  not db[:account_reset_password_tokens]
    .where(
      account_id: ,
      reset_token: reset_token,
    )
    .where(::Sequel[:account_reset_password_tokens][:expired_at] > now)
    .empty?
end

#wipe_old_reset_password_token(now:) ⇒ Object



135
136
137
138
139
# File 'lib/monban/repository/auth.rb', line 135

def wipe_old_reset_password_token(now:)
  db[:account_reset_password_tokens]
    .where(::Sequel[:account_reset_password_tokens][:expired_at] <= now)
    .delete
end