Class: Ddr::Auth::GrouperGateway

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/ddr/auth/grouper_gateway.rb

Constant Summary collapse

SUBJECT_ID_RE =
Regexp.new('[^@]+(?=@duke\.edu)')
DEFAULT_TIMEOUT =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGrouperGateway

Returns a new instance of GrouperGateway.



29
30
31
32
33
34
# File 'lib/ddr/auth/grouper_gateway.rb', line 29

def initialize
  super Grouper::Rest::Client::Resource.new(ENV["GROUPER_URL"],
                                            user: ENV["GROUPER_USER"],
                                            password: ENV["GROUPER_PASSWORD"],
                                            timeout: ENV.fetch("GROUPER_TIMEOUT", DEFAULT_TIMEOUT).to_i)
end

Class Method Details

.const_missing(name) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ddr/auth/grouper_gateway.rb', line 12

def self.const_missing(name)
  if name == :REPOSITORY_GROUP_FILTER
    Deprecation.warn(self, "The constant `#{name}` is deprecated and will be removed in ddr-models 3.0." \
                           " Use `Ddr::Auth.repository_group_filter` instead.")
    return Ddr::Auth.repository_group_filter
  end
  super
end

.repository_groups(*args) ⇒ Object



21
22
23
# File 'lib/ddr/auth/grouper_gateway.rb', line 21

def self.repository_groups(*args)
  new.repository_groups(*args)
end

.user_groups(*args) ⇒ Object



25
26
27
# File 'lib/ddr/auth/grouper_gateway.rb', line 25

def self.user_groups(*args)
  new.user_groups(*args)
end

Instance Method Details

#repository_group_namesObject

Deprecated.

Use #repository_groups instead.



50
51
52
53
54
# File 'lib/ddr/auth/grouper_gateway.rb', line 50

def repository_group_names
  Deprecation.warn(self.class, "`Ddr::Auth::GrouperGateway#repository_group_names` is deprecated." \
                               " Use `#repository_groups` instead.")
  repository_groups
end

#repository_groups(raw = false) ⇒ Object

List of all grouper groups for the repository



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ddr/auth/grouper_gateway.rb', line 37

def repository_groups(raw = false)
  repo_groups = groups(REPOSITORY_GROUP_FILTER)
  if ok?
    return repo_groups if raw
    repo_groups.map do |g|
      Group.new(g["name"], label: g["displayExtension"])
    end
  else
    []
  end
end

#user_group_names(user) ⇒ Object

Deprecated.

Use #user_groups instead.



86
87
88
89
90
# File 'lib/ddr/auth/grouper_gateway.rb', line 86

def user_group_names(user)
  Deprecation.warn(self.class, "`Ddr::Auth::GrouperGateway#user_group_names` is deprecated." \
                               " Use `#user_groups` instead.")
  user_groups(user)
end

#user_groups(user, raw = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ddr/auth/grouper_gateway.rb', line 56

def user_groups(user, raw = false)
  groups = []
  subject_id = user.principal_name.scan(SUBJECT_ID_RE).first
  return groups unless subject_id
  begin
    request_body = {
      "WsRestGetGroupsRequest" => {
        "subjectLookups" => [{"subjectIdentifier" => subject_id}]
      }
    }
    # Have to use :call b/c grouper-rest-client :subjects method doesn't support POST
    response = call("subjects", :post, request_body)
    if ok?
      result = response["WsGetGroupsResults"]["results"].first
      # Have to manually filter results b/c Grouper WS version 1.5 does not support filter parameter
      if result && result["wsGroups"]
        groups = result["wsGroups"].select { |g| g["name"] =~ /^#{REPOSITORY_GROUP_FILTER}/ }
      end
    end
  rescue StandardError => e
    # XXX Should we raise a custom exception?
    Rails.logger.error e
  end
  return groups if raw
  groups.map do |g|
    Group.new(g["name"], label: g["displayExtension"])
  end
end