Class: StandardFile::AbstractUserManager

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_file/abstract/user_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(user_class) ⇒ AbstractUserManager

Returns a new instance of AbstractUserManager.



4
5
6
# File 'lib/standard_file/abstract/user_manager.rb', line 4

def initialize(user_class)
  @user_class = user_class
end

Instance Method Details

#auth_params(email) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/standard_file/abstract/user_manager.rb', line 39

def auth_params(email)
  user = @user_class.find_by_email(email)

  if !user
    return nil
  end

  auth_params = {
    :identifier => user.email,
    :pw_cost => user.pw_cost,
    :pw_nonce => user.pw_nonce,
    :version => user.version
  }

  if user.pw_salt
    #v002 only
    auth_params[:pw_salt] = user.pw_salt
  end

  if user.pw_func
    #v001 only
    auth_params[:pw_func] = user.pw_func
    auth_params[:pw_alg] = user.pw_alg
    auth_params[:pw_key_size] = user.pw_key_size
  end

  return auth_params
end

#change_pw(user, password, params) ⇒ Object



28
29
30
31
32
# File 'lib/standard_file/abstract/user_manager.rb', line 28

def change_pw(user, password, params)
  user.encrypted_password = hash_password(password)
  user.update!(registration_params(params))
  return { user: user, token: jwt(user) }
end

#register(email, password, params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/standard_file/abstract/user_manager.rb', line 17

def register(email, password, params)
  user = @user_class.find_by_email(email)
  if user
    return {:error => {:message => "This email is already registered.", :status => 401}}
  else
    user = @user_class.new(:email => email, :encrypted_password => hash_password(password))
    user.update!(registration_params(params))
    return { user: user, token: jwt(user) }
  end
end

#sign_in(email, password) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/standard_file/abstract/user_manager.rb', line 8

def (email, password)
  user = @user_class.find_by_email(email)
  if user and test_password(password, user.encrypted_password)
    return { user: user, token: jwt(user) }
  else
    return {:error => {:message => "Invalid email or password.", :status => 401}}
  end
end

#update(user, params) ⇒ Object



34
35
36
37
# File 'lib/standard_file/abstract/user_manager.rb', line 34

def update(user, params)
  user.update!(registration_params(params))
  return { user: user, token: jwt(user) }
end