Class: Warden::GitHub::User

Inherits:
Struct
  • Object
show all
Defined in:
lib/warden/github/user.rb

Constant Summary collapse

ATTRIBUTES =
%w[id login name gravatar_id avatar_url email company site_admin].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribsObject

Returns the value of attribute attribs

Returns:

  • (Object)

    the current value of attribs



3
4
5
# File 'lib/warden/github/user.rb', line 3

def attribs
  @attribs
end

#browser_session_idObject

Returns the value of attribute browser_session_id

Returns:

  • (Object)

    the current value of browser_session_id



3
4
5
# File 'lib/warden/github/user.rb', line 3

def browser_session_id
  @browser_session_id
end

#membershipsObject

Returns the value of attribute memberships.



6
7
8
# File 'lib/warden/github/user.rb', line 6

def memberships
  @memberships
end

#tokenObject

Returns the value of attribute token

Returns:

  • (Object)

    the current value of token



3
4
5
# File 'lib/warden/github/user.rb', line 3

def token
  @token
end

Class Method Details

.load(access_token, browser_session_id = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/warden/github/user.rb', line 8

def self.load(access_token, browser_session_id = nil)
  api  = Octokit::Client.new(access_token: access_token)
  data =  { }

  api.user.to_hash.each do |k,v|
    data[k.to_s] = v if ATTRIBUTES.include?(k.to_s)
  end

  new(data, access_token, browser_session_id)
end

Instance Method Details

#apiObject

Access the GitHub API from Octokit

Octokit is a robust client library for the GitHub API github.com/octokit/octokit.rb

Returns a cached client object for easy use



101
102
103
104
105
106
# File 'lib/warden/github/user.rb', line 101

def api
  # Don't cache instance for now because of a ruby marshaling bug present
  # in MRI 1.9.3 (Bug #7627) that causes instance variables to be
  # marshaled even when explicitly specifying #marshal_dump.
  Octokit::Client.new(login: , access_token: token)
end

#browser_session_valid?(since = 120) ⇒ Boolean

Identify if the browser session is still valid

Returns: true if the browser session is still active or the GitHub API is unavailable

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
# File 'lib/warden/github/user.rb', line 77

def browser_session_valid?(since = 120)
  return true unless using_single_sign_out?
  client = api
  client.get("/user/sessions/active", browser_session_id: browser_session_id)
  client.last_response.status == 204
rescue Octokit::ServerError # GitHub API unavailable
  true
rescue Octokit::ClientError => e # GitHub API failed
  false
end

#marshal_dumpObject



19
20
21
# File 'lib/warden/github/user.rb', line 19

def marshal_dump
  Hash[members.zip(values)]
end

#marshal_load(hash) ⇒ Object



23
24
25
# File 'lib/warden/github/user.rb', line 23

def marshal_load(hash)
  hash.each { |k,v| send("#{k}=", v) }
end

#organization_member?(org_name) ⇒ Boolean

See if the user is a member of the named organization

name - the organization name

Returns: true if the user has access, false otherwise

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/warden/github/user.rb', line 50

def organization_member?(org_name)
  membership_cache.fetch_membership(:org, org_name) do
    api.organization_member?(org_name, )
  end
end

#organization_public_member?(org_name) ⇒ Boolean Also known as: publicized_organization_member?

See if the user is a public member of the named organization

name - the organization name

Returns: true if the user is publicized as an org member

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/warden/github/user.rb', line 36

def organization_public_member?(org_name)
  membership_cache.fetch_membership(:org_pub, org_name) do
    api.organization_public_member?(org_name, )
  end
end

#site_admin?Boolean

Identify GitHub employees/staff members.

Returns: true if the authenticated user is a GitHub employee, false otherwise

Returns:

  • (Boolean)


70
71
72
# File 'lib/warden/github/user.rb', line 70

def site_admin?
  !!site_admin
end

#team_member?(team_id) ⇒ Boolean

See if the user is a member of the team id

team_id - the team’s id

Returns: true if the user has access, false otherwise

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/warden/github/user.rb', line 61

def team_member?(team_id)
  membership_cache.fetch_membership(:team, team_id) do
    api.team_member?(team_id, )
  end
end

#using_single_sign_out?Boolean

Identify if the user is on a GitHub SSO property

Returns: true if a browser_session_id is present, false otherwise.

Returns:

  • (Boolean)


91
92
93
# File 'lib/warden/github/user.rb', line 91

def using_single_sign_out?
  !(browser_session_id.nil? || browser_session_id == "")
end