roles-management-api 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bdb03f8036e789d47ddb10560a3442c021f1fcda
4
+ data.tar.gz: 32b5243c81c75337261d8467c9f071b5bf7059b2
5
+ SHA512:
6
+ metadata.gz: d04851c32b1883bf512a805e923324dbc2aa968b5e0c61cea4a65a5d2afca0597b34fbec97350c7daf425c4bdec5a9aa261b5a2b85cdbd2050deb365894d4813
7
+ data.tar.gz: 02720c2cd409777fb71cc2873519ba1560cd071e3fe9c058a77371effb73bab3d869a1efc1658537bed622d5e7110d38f417434972194dd1d8a6228e2e7a717e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in roles-management-api.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Roles Management API
2
+
3
+ Ruby gem for interacting with the DSS IT Roles Management API system.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'roles-management-api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install roles-management-api
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ client = RolesManagementAPI.login("https://roles.installation.url/api", "Username", "API Key")
25
+ client.connected?
26
+
27
+ r = client.find_role_by_id(1234)
28
+ puts "Role has #{r.assignments.length} assignments."
29
+
30
+ p = client.find_person_by_loginid('somebody')
31
+ puts "'somebody' user has full name of #{p.name}"
32
+
33
+ # You can also do: p = client.find_entity_by_id(123) if you know the ID
34
+
35
+ puts "Removing first assignment from role ..."
36
+ r.assignments.delete(r.assignments[0])
37
+ client.save(r)
38
+
39
+ puts "Adding 'somebody' to role ..."
40
+ r.assignments << p
41
+ client.save(r)
42
+ ```
43
+
44
+ ## Development
45
+
46
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/dssit/roles-management-api/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "roles-management-api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ require "roles-management-api/version"
2
+ require "roles-management-api/person"
3
+ require "roles-management-api/role_assignment"
4
+ require "roles-management-api/role"
5
+ require "roles-management-api/client"
6
+
7
+ require "json"
8
+ require "net/http"
9
+ require "uri"
10
+
11
+ module RolesManagementAPI
12
+ # Creates an instance of the RolesManagementAPIClient upon successful login
13
+ # or returns false.
14
+ def RolesManagementAPI.login(url, username, api_key)
15
+ client = RolesManagementAPI::Client.new(url, username, api_key)
16
+
17
+ if client.connected?
18
+ return client
19
+ else
20
+ return false
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,111 @@
1
+ module RolesManagementAPI
2
+ @@client = nil
3
+
4
+ class Client
5
+ attr_accessor :url, :username, :api_key
6
+
7
+ @conn = nil
8
+ @uri = nil
9
+
10
+ def initialize(url, username, api_key)
11
+ @uri = URI.parse(url)
12
+
13
+ @conn = Net::HTTP.new(@uri.host, @uri.port)
14
+ @conn.use_ssl = true if @uri.scheme == "https"
15
+
16
+ @username = username
17
+ @api_key = api_key
18
+ end
19
+
20
+ # Returns true if the API endpoint and key are valid and can connect
21
+ def connected?
22
+ response = get_request("validate")
23
+
24
+ return response.code == "200"
25
+ end
26
+
27
+ def save(object)
28
+ if object.is_a? RolesManagementAPI::Role
29
+ response = put_request("roles/" + object.id.to_s + ".json", object.as_json)
30
+ return response
31
+ else
32
+ STDERR.puts "Cannot save object: type '#{object.class}' not supported."
33
+ return false
34
+ end
35
+ end
36
+
37
+ # Returns a Role object for the given role_id or nil on error / not found
38
+ def find_role_by_id(role_id)
39
+ response = get_request("roles/" + role_id.to_s + ".json")
40
+
41
+ return nil unless response.code == "200"
42
+
43
+ json = JSON.parse(response.body, symbolize_names: true)
44
+
45
+ return Role.new(role_id, json)
46
+ end
47
+
48
+ # Returns a Person object for the given loginid or nil on error / not found
49
+ def find_entity_by_id(id)
50
+ response = get_request("entities/" + id.to_s + ".json")
51
+
52
+ return nil unless response.code == "200"
53
+
54
+ json = JSON.parse(response.body, symbolize_names: true)
55
+
56
+ if json[:type] == "Person"
57
+ return Person.new(json)
58
+ else
59
+ STDERR.puts "Did not understand entity type returned by RM."
60
+ return nil
61
+ end
62
+ end
63
+
64
+ # Returns a Person object for the given loginid or nil on error / not found
65
+ def find_person_by_loginid(loginid)
66
+ response = get_request("people/" + loginid + ".json")
67
+
68
+ return nil unless response.code == "200"
69
+
70
+ json = JSON.parse(response.body, symbolize_names: true)
71
+
72
+ return Person.new(json)
73
+ end
74
+
75
+ private
76
+
77
+ # Performs a HTTP GET using the configured API endpoint and key
78
+ def get_request(url)
79
+ request_url = @uri.request_uri + "/" + url
80
+ request = Net::HTTP::Get.new(request_url)
81
+ request = sign_request(request)
82
+
83
+ response = @conn.request(request)
84
+
85
+ STDERR.puts "Error querying RM, response code was #{response.code} for URL #{request_url}." unless response.code == "200"
86
+
87
+ return response
88
+ end
89
+
90
+ # Performs a HTTP POST using the configured API endpoint and key
91
+ def put_request(url, data)
92
+ request = Net::HTTP::Put.new(@uri.request_uri + "/" + url, initheader = {'Content-Type' =>'application/json'})
93
+ request = sign_request(request)
94
+
95
+ request.body = data.to_json
96
+
97
+ return @conn.request(request)
98
+ end
99
+
100
+ # Adds the necessary HTTP headers and API key to a request
101
+ def sign_request(request)
102
+ # RM requires we specify which version of the API we want
103
+ request.add_field("Accept", "application/vnd.roles-management.v1")
104
+ # RM requires we provide the username and API key via HTTP Basic Auth
105
+ request.basic_auth(@username, @api_key)
106
+
107
+ return request
108
+ end
109
+
110
+ end
111
+ end
@@ -0,0 +1,24 @@
1
+ module RolesManagementAPI
2
+ class Person
3
+ attr_accessor :id, :loginid, :name, :role_assignments, :group_memberships
4
+
5
+ # Creates a new Person object from a JSON object
6
+ def initialize(json)
7
+ @id = json[:id]
8
+ @loginid = json[:loginid]
9
+ @name = json[:name]
10
+ @role_assignments = json[:role_assignments]
11
+ @group_memberships = json[:group_memberships]
12
+ end
13
+
14
+ def as_json
15
+ json = {}
16
+
17
+ json.merge({
18
+ id: @id,
19
+ loginid: @loginid,
20
+ name: @name
21
+ })
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,81 @@
1
+ module RolesManagementAPI
2
+ class Role
3
+ attr_accessor :id, :application_id, :description, :name, :token, :assignments, :members
4
+
5
+ # Creates a new Role object from a NET/HTTP response (see client.rb)
6
+ def initialize(role_id, json)
7
+ @id = role_id
8
+ @application_id = json[:application_id]
9
+ @description = json[:description]
10
+ @name = json[:name]
11
+ @token = json[:token]
12
+
13
+ @assignments = RoleAssignmentArray.new(role_id)
14
+ json[:role_assignments_attributes].each do |assignment_json|
15
+ @assignments << RoleAssignment.new(assignment_json)
16
+ end
17
+
18
+ @members = []
19
+ json[:members].each do |member|
20
+ @members << Person.new(member)
21
+ end
22
+ end
23
+
24
+ def as_json
25
+ {
26
+ role: {
27
+ role_assignments_attributes: @assignments.as_json
28
+ }
29
+ }
30
+ end
31
+ end
32
+
33
+ # RoleAssignmentArray is an array-like class used by Role for maintaining its
34
+ # assignment list. Its primary purpose is to add the Rails-required "_destroy: true"
35
+ # attribute in its as_json for any assignment which has been removed.
36
+ class RoleAssignmentArray
37
+ def initialize(role_id)
38
+ @assignments = []
39
+ @role_id = role_id
40
+ end
41
+
42
+ def <<(assignment)
43
+ raise "You may only add objects of type 'RoleAssignment' or 'Person'" unless assignment.is_a?(RoleAssignment) or assignment.is_a?(Person)
44
+
45
+ if assignment.is_a?(Person)
46
+ @assignments << RoleAssignment.new({entity_id: assignment.id, role_id: @role_id})
47
+ else
48
+ @assignments << assignment
49
+ end
50
+ end
51
+
52
+ def [](idx)
53
+ @assignments[idx]
54
+ end
55
+
56
+ def each &block
57
+ @assignments.each &block
58
+ end
59
+
60
+ def length
61
+ @assignments.length
62
+ end
63
+
64
+ def delete(member)
65
+ raise "You may only delete objects of type 'RoleAssignment'" unless member.is_a?(RoleAssignment)
66
+
67
+ @assignments.each do |assignment|
68
+ if (assignment.id == member.id) and (member.id != nil)
69
+ assignment.destroy = true
70
+ return true
71
+ end
72
+ end
73
+
74
+ return false
75
+ end
76
+
77
+ def as_json
78
+ @assignments.map{ |a| a.as_json }
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,24 @@
1
+ module RolesManagementAPI
2
+ class RoleAssignment
3
+ attr_accessor :id, :entity_id, :role_id, :destroy
4
+
5
+ # Creates a new Person object from a JSON object
6
+ def initialize(json)
7
+ @id = json[:id]
8
+ @entity_id = json[:entity_id]
9
+ @role_id = json[:role_id]
10
+ @destroy = false
11
+ end
12
+
13
+ def as_json
14
+ json = {}
15
+ json['_destroy'] = true if @destroy
16
+
17
+ json.merge({
18
+ id: @id,
19
+ entity_id: @entity_id,
20
+ role_id: @role_id
21
+ })
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module RolesManagementAPI
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'roles-management-api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "roles-management-api"
8
+ spec.version = RolesManagementAPI::VERSION
9
+ spec.authors = ["Christopher Thielen"]
10
+ spec.email = ["cmthielen@ucdavis.edu"]
11
+
12
+ spec.summary = %q{Ruby gem for consuming the DSS IT Roles Management API}
13
+ spec.description = %q{Ruby gem for consuming the RESTful JSON-based DSS IT Roles Management API.}
14
+ spec.homepage = "https://github.com/dssit/roles-management-api"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roles-management-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Thielen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Ruby gem for consuming the RESTful JSON-based DSS IT Roles Management
42
+ API.
43
+ email:
44
+ - cmthielen@ucdavis.edu
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/roles-management-api.rb
57
+ - lib/roles-management-api/client.rb
58
+ - lib/roles-management-api/person.rb
59
+ - lib/roles-management-api/role.rb
60
+ - lib/roles-management-api/role_assignment.rb
61
+ - lib/roles-management-api/version.rb
62
+ - roles-management-api.gemspec
63
+ homepage: https://github.com/dssit/roles-management-api
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.5.1
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Ruby gem for consuming the DSS IT Roles Management API
86
+ test_files: []