Class: Communication::SessionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/communication/session_manager.rb

Overview

Manages user session

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_credentials) ⇒ SessionManager

Returns a new instance of SessionManager.



24
25
26
# File 'lib/communication/session_manager.rb', line 24

def initialize()
  self.credentials = 
end

Class Method Details

.logged_in?Boolean

Returns returns true if user is logged in.

Returns:

  • (Boolean)

    True if user is logged in, false otherwise.



84
85
86
87
88
# File 'lib/communication/session_manager.rb', line 84

def self.logged_in?
  Utilities.cookie_exists? && !CredentialsConfiguration.new..nil?
rescue StandardError
  false
end

.logoutObject

Log out the currently logged in user.



72
73
74
75
76
77
78
79
# File 'lib/communication/session_manager.rb', line 72

def self.logout
  raise LogworkException::UserNotLoggedIn.new, "You are not logged in." unless logged_in?

  Communicator.instance.delete("/rest/auth/#{Constants::JIRA_AUTH_VERSION}/session") do
    Utilities.remove_cookie
    CredentialsConfiguration.new.(nil, nil)
  end
end

Instance Method Details

#handle_login_success(body) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/communication/session_manager.rb', line 53

def (body)
  unless credentials.is_stored
    # store credentials
    Configuration::CredentialsConfiguration.new.(credentials.username,
                                                                         credentials.password)
  end

  store_cookie(body)
  yield if block_given?
end

#login(force: false) ⇒ Object

Logs in a user or prompts with login credentials if it's not logged in.

Parameters:

  • force (Bool) (defaults to: false)

    Pass true to force login regardleess of login state.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/communication/session_manager.rb', line 31

def (force: false)
  if SessionManager.logged_in? && !force
    raise LogworkException::UserAlreadyLoggedIn.new,
          "You are already logged in."
  end

  params = {
    "username" => credentials.username,
    "password" => credentials.password
  }

  Communicator.instance.post("/rest/auth/#{Constants::JIRA_AUTH_VERSION}/session", params) do |body, res|
    (body, res) { yield if block_given? }
  end
end

#myselfHash

Returns information about the logged in user.

Returns:

  • (Hash)

    User info.



93
94
95
96
97
98
99
# File 'lib/communication/session_manager.rb', line 93

def myself
  Communicator.instance.get("#{Communicator.base_api_url}/myself") do |body, _|
    raise LogworkException::InvalidURL unless body.is_a?(Hash)

    { full_name: body[:displayName] }
  end
end

#parse_login_response(body, _) ⇒ Object



47
48
49
50
51
# File 'lib/communication/session_manager.rb', line 47

def (body, _)
  (body) do
    yield if block_given?
  end
end


64
65
66
67
68
69
# File 'lib/communication/session_manager.rb', line 64

def store_cookie(body)
  return unless body.is_a?(Hash)

  cookie = "#{body[:session][:name]}=#{body[:session][:value]}"
  Communicator.instance.store_cookie(cookie)
end