Class: Notee::User

Inherits:
ApplicationRecord show all
Defined in:
app/models/notee/user.rb

Constant Summary collapse

SECURE =

constants

'SOFHGPOIJERPGOKSPDO2SPTI4RJ6POIFDJVS7ETJ1EITJHSPEKMVOEIGU'
CIPHER =
'aes-256-cbc'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#create_authority, #destroy_authority, #is_destroy?, #skip_callback_block, #update_authority

Instance Attribute Details

#editor_idObject

Returns the value of attribute editor_id.



15
16
17
# File 'app/models/notee/user.rb', line 15

def editor_id
  @editor_id
end

#fileObject

accessors



11
12
13
# File 'app/models/notee/user.rb', line 11

def file
  @file
end

#now_passwordObject

Returns the value of attribute now_password.



12
13
14
# File 'app/models/notee/user.rb', line 12

def now_password
  @now_password
end

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'app/models/notee/user.rb', line 13

def password
  @password
end

#password_confirmObject

Returns the value of attribute password_confirm.



14
15
16
# File 'app/models/notee/user.rb', line 14

def password_confirm
  @password_confirm
end

Class Method Details

.decrypt(password) ⇒ Object



41
42
43
44
# File 'app/models/notee/user.rb', line 41

def self.decrypt(password)
  crypt = ActiveSupport::MessageEncryptor.new(SECURE, CIPHER)
  crypt.decrypt_and_verify(password)
end

.root_user_settingObject



69
70
71
72
73
# File 'app/models/notee/user.rb', line 69

def self.root_user_setting
  if token = Token.create!(user_id: 0)
    Thread.current[:request].session[:access_token] = token.access_token
  end
end

.sign_in(name_or_email, password) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/notee/user.rb', line 46

def self.(name_or_email, password)

  # root-user login
  if Notee.notee_id == name_or_email && Notee.notee_password == password
    return root_user_setting
  end

  # other-user login
  user = not_trash.find_by(name: name_or_email)
  user = not_trash.find_by(email: name_or_email) unless user
  return false unless user
  return false if user.id == 0 # root_user
  return false unless password == decrypt(user.encrypted_password)

  user_setting(user)
end

.user_setting(user) ⇒ Object



63
64
65
66
67
# File 'app/models/notee/user.rb', line 63

def self.user_setting(user)
  if token = Token.create!(user_id: user.id)
    Thread.current[:request].session[:access_token] = token.access_token
  end
end

Instance Method Details

#encrypt(password) ⇒ Object



36
37
38
39
# File 'app/models/notee/user.rb', line 36

def encrypt(password)
  crypt = ActiveSupport::MessageEncryptor.new(SECURE, CIPHER)
  crypt.encrypt_and_sign(password)
end

#update_password(params) ⇒ Object



30
31
32
33
34
# File 'app/models/notee/user.rb', line 30

def update_password(params)
  return false unless params[:now_password] == User.decrypt(self.encrypted_password)
  return false unless params[:password] == params[:password_confirm]
  self.update(params)
end