Class: Ddr::Auth::GrouperGateway

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

Constant Summary collapse

REPOSITORY_GROUP_FILTER =
"duke:library:repository:ddr:"
SUBJECT_ID_RE =
Regexp.new('[^@]+(?=@duke\.edu)')

Instance Method Summary collapse

Constructor Details

#initializeGrouperGateway

Returns a new instance of GrouperGateway.



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

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

Instance Method Details

#repository_group_namesObject



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

def repository_group_names
  repository_groups.collect { |g| g["name"] }
end

#repository_groupsObject

List of all grouper groups for the repository



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

def repository_groups
  repo_groups = groups(REPOSITORY_GROUP_FILTER)
  ok? ? repo_groups : []
end

#user_group_names(user) ⇒ Object



53
54
55
# File 'lib/ddr/auth/grouper_gateway.rb', line 53

def user_group_names(user)
  user_groups(user).collect { |g| g["name"] }
end

#user_groups(user) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ddr/auth/grouper_gateway.rb', line 28

def user_groups(user)
  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
    Rails.logger.error e
  end
  groups
end