microslop_one_drive 4.1.0 → 5.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea6fe465bafb81fd55f7e6115d3c25684f8de6d4dd7f7a10a2c21cca5d194e7e
4
- data.tar.gz: 0a8109c8bb15d7660f9b6cdaa84f920dce01280d346971297da3456059d2d67b
3
+ metadata.gz: 3888f96cecbe9314e50eb6808fbd53af0ec0115905ac06c9c034abbee0871cf9
4
+ data.tar.gz: 9efebc0e59d585e66e8bc7778f593d4e299dafe37e7497319fb71a4bb928a8a7
5
5
  SHA512:
6
- metadata.gz: e85320b160e96a4b218856b9b5701d863e5d5960b6e4f67b8b72485a4205a672a19413f7a6b2e86ed11649a6925841dae3b52b869f2cabf8c14bc1ef0ae7dc84
7
- data.tar.gz: a6a89eb7938ec464bd5a63293f3627706d472295eff66428d1ccd14d3341dd5b6202706d9651d7a8a494d64a373811c7d6127e5b6aaa043c0ef06a4364b58700
6
+ metadata.gz: bc24edeb9829b2c982d6c629f82bd1d6a884fe90476bfa8cc9c49948b30c9f25bde0fe3bda934607aab8f1f64d993e3da7d68e8f363849a4a29af4e026b10b77
7
+ data.tar.gz: 6275971239665720766eff7629b3bfc3b375844a5f3f15f1b6c8c29569590730f3a8410c17bbdfbdd18ea54ae9cc53766ba055b97c7479ac3f6428db22b7ede5
data/README.md CHANGED
@@ -131,18 +131,30 @@ shared_items = drive_item_list.items.select(&:shared?)
131
131
  example_item = shared_items.first
132
132
 
133
133
  permission_list = client.permissions(item_id: example_item.id)
134
- permission = permission_list.first
135
-
136
- permission.role # => "write"
137
- permission.audience.type # => "user"
138
- permission.audience.id # => "person@example.com"
139
- permission.audience.display_name # => "Example Person"
140
- permission.audience.email_address # => "person@example.com"
134
+ # Or with a specific drive: client.permissions(item_id: example_item.id, drive_id: drive.id)
135
+
136
+ permissions = permission_list.permissions
137
+ # Each permission is a DirectPermission, SharingLink, or SharingInvitation.
138
+
139
+ # Example: direct permission (e.g. owner) use granted_to (a User)
140
+ direct = permissions.find { |p| p.is_a?(MicroslopOneDrive::Permissions::DirectPermission) }
141
+ direct.roles # => ["owner"]
142
+ direct.granted_to.display_name # => "Example Person"
143
+ direct.granted_to.email_address # => "person@example.com"
144
+ direct.granted_to.id # => "4"
145
+
146
+ # Example: sharing link — use granted_to_list and link
147
+ link_perm = permissions.find { |p| p.is_a?(MicroslopOneDrive::Permissions::SharingLink) }
148
+ link_perm.roles # => ["write"]
149
+ link_perm.granted_to_list # => array of Users
150
+ link_perm.link.web_url # => "https://1drv.ms/f/c/..."
151
+ link_perm.edit_link? # => true
152
+ link_perm.anonymous_link? # => true
141
153
  ```
142
154
 
143
155
  ### Get Permissions for multiple Drive Items
144
156
 
145
- Instead of calling `client.permissions(...)` for each item -- which would make N API calls for N items -- we use
157
+ Instead of calling `client.permissions(...)` for each item which would make N API calls for N items use
146
158
  Microsoft Graph API [batch](https://learn.microsoft.com/en-us/graph/json-batching?tabs=http) feature.
147
159
 
148
160
  ```rb
@@ -150,8 +162,18 @@ drive_item_list = client.delta(drive_id: drive.id)
150
162
  shared_items = drive_item_list.items.select(&:shared?)
151
163
 
152
164
  permissions = client.batch_permissions(item_ids: shared_items.map(&:id))
153
- # Returns a flat array of MicroslopOneDrive::Permission (batches of 20 are made under the hood).
154
- permissions.each { |p| puts "#{p.audience.display_name}: #{p.role}" }
165
+ # Or with a specific drive: client.batch_permissions(item_ids: shared_items.map(&:id), drive_id: drive.id)
166
+ # Returns a flat array of permission objects (DirectPermission, SharingLink, SharingInvitation).
167
+ # Batches of 20 are made under the hood.
168
+
169
+ permissions.each do |p|
170
+ case p
171
+ when MicroslopOneDrive::Permissions::DirectPermission
172
+ puts "#{p.granted_to.display_name}: #{p.roles.join(', ')}"
173
+ when MicroslopOneDrive::Permissions::SharingLink
174
+ puts "Link (#{p.link.type}): #{p.roles.join(', ')}"
175
+ end
176
+ end
155
177
  ```
156
178
 
157
179
  ### Checking if the user's accounts supports SharePoint Sites
@@ -16,6 +16,7 @@ module MicroslopOneDrive
16
16
  include Endpoints::BatchPermissions
17
17
  include Endpoints::SupportsSites
18
18
  include Endpoints::DeletePermission
19
+ include Endpoints::RevokeGrants
19
20
 
20
21
  # @param access_token [String] OAuth access token for Microsoft Graph.
21
22
  # @param logger [Object, nil] Optional logger (e.g. Rails.logger) that responds to +#info+, +#debug+, +#warn+, +#error+.
@@ -0,0 +1,17 @@
1
+ module MicroslopOneDrive
2
+ module Deserializers
3
+ class GrantedToDeserializer
4
+ def self.create_from_hash(granted_to_hash)
5
+ granted_to_hash = Utils.deep_symbolize_keys(granted_to_hash)
6
+
7
+ if granted_to_hash.key?(:siteUser)
8
+ UserDeserializer.create_from_hash(granted_to_hash[:siteUser])
9
+ elsif granted_to_hash.key?(:group)
10
+ raise NotImplementedError, "Group permissions are not supported yet"
11
+ else
12
+ raise NotImplementedError, "Unknown granted to type for hash: #{granted_to_hash.inspect}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module MicroslopOneDrive
2
+ module Deserializers
3
+ class LinkDeserializer
4
+ def self.create_from_hash(link_hash)
5
+ link_hash = Utils.deep_symbolize_keys(link_hash)
6
+
7
+ MicroslopOneDrive::Permissions::Link.new(
8
+ web_url: link_hash.fetch(:webUrl, nil),
9
+ scope: link_hash.fetch(:scope, nil),
10
+ type: link_hash.fetch(:type, nil),
11
+ prevents_download: link_hash.fetch(:preventsDownload, false)
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,76 @@
1
+ module MicroslopOneDrive
2
+ module Deserializers
3
+ class PermissionDeserializer
4
+ # Deserializes a hash into a Permission object. Permissions can be SharingLinks or SharingInvitations.
5
+ def self.create_from_hash(permission_hash)
6
+ permission_hash = Utils.deep_symbolize_keys(permission_hash)
7
+
8
+ if sharing_link?(permission_hash)
9
+ build_sharing_link(permission_hash)
10
+ elsif sharing_invitation?(permission_hash)
11
+ build_sharing_invitation(permission_hash)
12
+ elsif direct_permission?(permission_hash)
13
+ build_direct_permission(permission_hash)
14
+ else
15
+ raise NotImplementedError, "Unknown permission type for hash: #{permission_hash.inspect}"
16
+ end
17
+ end
18
+
19
+ def self.build_common_parameters(permission_hash)
20
+ link_hash = permission_hash.fetch(:link, nil)
21
+ link = LinkDeserializer.create_from_hash(link_hash) if link_hash
22
+
23
+ {
24
+ id: permission_hash.fetch(:id, nil),
25
+ roles: permission_hash.fetch(:roles, []),
26
+ share_id: permission_hash.fetch(:shareId, nil),
27
+ link: link
28
+ }
29
+ end
30
+
31
+ def self.sharing_link?(permission_hash)
32
+ link = permission_hash.fetch(:link, nil)
33
+ return false if link.nil?
34
+
35
+ link.key?(:scope) && link.key?(:type)
36
+ end
37
+
38
+ def self.sharing_invitation?(permission_hash)
39
+ permission_hash.key?(:invitation)
40
+ end
41
+
42
+ def self.direct_permission?(permission_hash)
43
+ permission_hash.key?(:grantedToV2)
44
+ end
45
+
46
+ def self.build_sharing_link(permission_hash)
47
+ common_parameters = build_common_parameters(permission_hash)
48
+
49
+ granted_to_hash_list = permission_hash.fetch(:grantedToIdentitiesV2, [])
50
+ granted_to_list = granted_to_hash_list.map { GrantedToDeserializer.create_from_hash(it) }
51
+
52
+ has_password = permission_hash.fetch(:hasPassword, false)
53
+
54
+ MicroslopOneDrive::Permissions::SharingLink.new(
55
+ **common_parameters,
56
+ granted_to_list: granted_to_list,
57
+ has_password: has_password
58
+ )
59
+ end
60
+
61
+ def self.build_sharing_invitation(permission_hash)
62
+ common_parameters = build_common_parameters(permission_hash)
63
+ MicroslopOneDrive::Permissions::SharingInvitation.new(**common_parameters)
64
+ end
65
+
66
+ def self.build_direct_permission(permission_hash)
67
+ common_parameters = build_common_parameters(permission_hash)
68
+
69
+ granted_to_hash = permission_hash.fetch(:grantedToV2, nil)
70
+ granted_to = GrantedToDeserializer.create_from_hash(granted_to_hash)
71
+
72
+ MicroslopOneDrive::Permissions::DirectPermission.new(**common_parameters, granted_to: granted_to)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -21,7 +21,8 @@ module MicroslopOneDrive
21
21
  mobile_phone: user_hash.fetch(:mobilePhone, nil),
22
22
  job_title: user_hash.fetch(:jobTitle, nil),
23
23
  office_location: user_hash.fetch(:officeLocation, nil),
24
- business_phones: user_hash.fetch(:businessPhones, [])
24
+ business_phones: user_hash.fetch(:businessPhones, []),
25
+ login_name: user_hash.fetch(:loginName, nil)
25
26
  )
26
27
  end
27
28
  end
@@ -17,7 +17,7 @@ module MicroslopOneDrive
17
17
 
18
18
  response = get(path: url, query: {})
19
19
 
20
- if response.code == 404
20
+ if response.not_found?
21
21
  return MicroslopOneDrive::ListResponses::PermissionList.new(
22
22
  drive_item_id: item_id,
23
23
  parsed_response: {"value" => []}
@@ -0,0 +1,30 @@
1
+ module MicroslopOneDrive
2
+ module Endpoints
3
+ module RevokeGrants
4
+ # Revokes a grant from a drive item.
5
+ #
6
+ # NOTE: This is from the beta version of the OneDrive API.
7
+ # https://learn.microsoft.com/en-us/graph/api/permission-revokegrants
8
+ #
9
+ # @param item_id [String] The ID of the DriveItem to revoke the grant from.
10
+ # @param permission_id [String] The ID of the Permission to revoke.
11
+ # @param grantees [Array<Hash>] The grantees to revoke the grant from. Each grantee should be a hash with one
12
+ # (and only one) of the following keys:
13
+ #
14
+ # - email: The email address of the grantee.
15
+ # - alias: The alias of the grantee.
16
+ # - objectId: The object ID of the grantee.
17
+ #
18
+ # @return [boolean] True if successful, else raises an error.
19
+ def revoke_grants(item_id:, permission_id:, grantees:)
20
+ url = "me/drive/items/#{item_id}/permissions/#{permission_id}/revokeGrants"
21
+
22
+ response = post(path: url, body: {grantees: grantees}.to_json)
23
+
24
+ return true if response.success?
25
+
26
+ handle_error(response)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -12,15 +12,15 @@ module MicroslopOneDrive
12
12
  private
13
13
 
14
14
  def build_permissions(drive_item_id, parsed_response)
15
- permission_sets = parsed_response.fetch("value", []).map do
16
- MicroslopOneDrive::PermissionSet.new(drive_item_id: drive_item_id, parsed_response: it)
15
+ permissions = parsed_response.fetch("value", []).map do
16
+ MicroslopOneDrive::Deserializers::PermissionDeserializer.create_from_hash(it)
17
17
  end
18
18
 
19
- # At this stage, the permissions could contain multiple Audiences for the same Permission.
20
- # This is because OneDrive can return multiple permissions for the same thing.
21
- # For example, a file shared with one person and a public link will return in a single permission object.
22
- # We therefore need to "explode" the permission sets into multiple permissions, one for each Audience.
23
- permission_sets.flat_map(&:to_permissions)
19
+ permissions.each do
20
+ it.drive_item_id = drive_item_id
21
+ end
22
+
23
+ permissions
24
24
  end
25
25
  end
26
26
  end
@@ -0,0 +1,16 @@
1
+ module MicroslopOneDrive
2
+ module Permissions
3
+ class BasePermission
4
+ attr_accessor :drive_item_id
5
+ attr_reader :id, :roles, :share_id, :link
6
+
7
+ def initialize(id:, roles:, share_id:, link:, drive_item_id: nil)
8
+ @id = id
9
+ @roles = roles
10
+ @share_id = share_id
11
+ @link = link
12
+ @drive_item_id = drive_item_id
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module MicroslopOneDrive
2
+ module Permissions
3
+ class DirectPermission < MicroslopOneDrive::Permissions::BasePermission
4
+ # A direct permission is a permission that is granted to a specific user or group.
5
+ attr_reader :granted_to
6
+
7
+ def initialize(id:, roles:, share_id:, link:, granted_to:)
8
+ super(id: id, roles: roles, share_id: share_id, link: link)
9
+ @granted_to = granted_to
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module MicroslopOneDrive
2
+ module Permissions
3
+ class Link
4
+ attr_reader :web_url, :scope, :type, :prevents_download
5
+
6
+ def initialize(web_url:, scope:, type:, prevents_download:)
7
+ @web_url = web_url
8
+ @scope = scope
9
+ @type = type
10
+ @prevents_download = prevents_download
11
+ end
12
+
13
+ def view_link?
14
+ type == "view"
15
+ end
16
+
17
+ def edit_link?
18
+ type == "edit"
19
+ end
20
+
21
+ def specific_people_link?
22
+ scope == "users"
23
+ end
24
+
25
+ def anonymous_link?
26
+ scope == "anonymous"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ module MicroslopOneDrive
2
+ module Permissions
3
+ class SharingInvitation < MicroslopOneDrive::Permissions::BasePermission
4
+ # From https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/permission?view=odsp-graph-online#sharing-invitations:
5
+ #
6
+ # > Permissions sent by the invite API may have additional information in the invitation facet. If an invitation
7
+ # > was sent to an email address that doesn't match a known account, the grantedTo property may not be set until
8
+ # > the invitation is redeemed, which occurs the first time the user clicks the link and signs in.
9
+ #
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,43 @@
1
+ module MicroslopOneDrive
2
+ module Permissions
3
+ class SharingLink < MicroslopOneDrive::Permissions::BasePermission
4
+ # From https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/permission?view=odsp-graph-online#sharing-links:
5
+ #
6
+ # > Permissions with a link facet represent sharing links created on the item. These are the most common kinds of
7
+ # > permissions. Sharing links provide a unique URL that can be used to access a file or folder. They can be set
8
+ # > up to grant access in a variety of ways. For example, you can use the createLink API to create a link that
9
+ # > works for anyone signed into your organization, or you can create a link that works for anyone, without
10
+ # > needing to sign in. You can use the invite API to create a link that only works for specific people, whether
11
+ # > they're in your company or not.
12
+ #
13
+ # Concrete examples:
14
+ # 1. https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/permission?view=odsp-graph-online#view-link
15
+ # 2. https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/permission?view=odsp-graph-online#edit-link
16
+ # 3. https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/permission?view=odsp-graph-online#specific-people-link
17
+ #
18
+ attr_reader :has_password, :granted_to_list
19
+
20
+ def initialize(id:, roles:, share_id:, link:, has_password:, granted_to_list:)
21
+ super(id: id, roles: roles, share_id: share_id, link: link)
22
+ @has_password = has_password
23
+ @granted_to_list = granted_to_list
24
+ end
25
+
26
+ def view_link?
27
+ link.view_link?
28
+ end
29
+
30
+ def edit_link?
31
+ link.edit_link?
32
+ end
33
+
34
+ def specific_people_link?
35
+ link.specific_people_link?
36
+ end
37
+
38
+ def anonymous_link?
39
+ link.anonymous_link?
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,7 +1,7 @@
1
1
  module MicroslopOneDrive
2
2
  class User
3
3
  attr_reader :principal_name, :id, :display_name, :surname, :given_name, :preferred_language, :mail, :mobile_phone,
4
- :job_title, :office_location, :business_phones
4
+ :job_title, :office_location, :business_phones, :login_name
5
5
 
6
6
  def initialize(
7
7
  principal_name: nil,
@@ -14,7 +14,8 @@ module MicroslopOneDrive
14
14
  mobile_phone: nil,
15
15
  job_title: nil,
16
16
  office_location: nil,
17
- business_phones: []
17
+ business_phones: [],
18
+ login_name: nil
18
19
  )
19
20
  @principal_name = principal_name
20
21
  @id = id
@@ -27,14 +28,19 @@ module MicroslopOneDrive
27
28
  @job_title = job_title
28
29
  @office_location = office_location
29
30
  @business_phones = business_phones
31
+ @login_name = login_name
32
+ end
33
+
34
+ def email
35
+ @mail
30
36
  end
31
37
 
32
38
  def email_address
33
- mail
39
+ @mail
34
40
  end
35
41
 
36
42
  def last_name
37
- surname
43
+ @surname
38
44
  end
39
45
  end
40
46
  end
@@ -1,3 +1,3 @@
1
1
  module MicroslopOneDrive
2
- VERSION = "4.1.0".freeze
2
+ VERSION = "5.1.0".freeze
3
3
  end
@@ -11,6 +11,9 @@ require_relative "microslop_one_drive/deserializers/quota_deserializer"
11
11
  require_relative "microslop_one_drive/deserializers/drive_item_deserializer"
12
12
  require_relative "microslop_one_drive/deserializers/parent_reference_deserializer"
13
13
  require_relative "microslop_one_drive/deserializers/shared_with_me_item_deserializer"
14
+ require_relative "microslop_one_drive/deserializers/permission_deserializer"
15
+ require_relative "microslop_one_drive/deserializers/link_deserializer"
16
+ require_relative "microslop_one_drive/deserializers/granted_to_deserializer"
14
17
 
15
18
  # Drive items
16
19
  require_relative "microslop_one_drive/file"
@@ -33,11 +36,13 @@ require_relative "microslop_one_drive/batch/response"
33
36
  require_relative "microslop_one_drive/user"
34
37
  require_relative "microslop_one_drive/parent_reference"
35
38
  require_relative "microslop_one_drive/drive"
36
- require_relative "microslop_one_drive/permission_set"
37
- require_relative "microslop_one_drive/permission"
38
- require_relative "microslop_one_drive/audience"
39
39
  require_relative "microslop_one_drive/quota"
40
40
  require_relative "microslop_one_drive/shared_with_me_item"
41
+ require_relative "microslop_one_drive/permissions/base_permission"
42
+ require_relative "microslop_one_drive/permissions/direct_permission"
43
+ require_relative "microslop_one_drive/permissions/sharing_link"
44
+ require_relative "microslop_one_drive/permissions/sharing_invitation"
45
+ require_relative "microslop_one_drive/permissions/link"
41
46
 
42
47
  # Endpoints
43
48
  require_relative "microslop_one_drive/endpoints/me"
@@ -52,6 +57,7 @@ require_relative "microslop_one_drive/endpoints/batch"
52
57
  require_relative "microslop_one_drive/endpoints/batch_permissions"
53
58
  require_relative "microslop_one_drive/endpoints/delete_permission"
54
59
  require_relative "microslop_one_drive/endpoints/supports_sites"
60
+ require_relative "microslop_one_drive/endpoints/revoke_grants"
55
61
 
56
62
  # Client
57
63
  require_relative "microslop_one_drive/errors/client_error"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: microslop_one_drive
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bradley Marques
@@ -33,13 +33,15 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - README.md
35
35
  - lib/microslop_one_drive.rb
36
- - lib/microslop_one_drive/audience.rb
37
36
  - lib/microslop_one_drive/batch/batch_response.rb
38
37
  - lib/microslop_one_drive/batch/response.rb
39
38
  - lib/microslop_one_drive/client.rb
40
39
  - lib/microslop_one_drive/deserializers/drive_deserializer.rb
41
40
  - lib/microslop_one_drive/deserializers/drive_item_deserializer.rb
41
+ - lib/microslop_one_drive/deserializers/granted_to_deserializer.rb
42
+ - lib/microslop_one_drive/deserializers/link_deserializer.rb
42
43
  - lib/microslop_one_drive/deserializers/parent_reference_deserializer.rb
44
+ - lib/microslop_one_drive/deserializers/permission_deserializer.rb
43
45
  - lib/microslop_one_drive/deserializers/quota_deserializer.rb
44
46
  - lib/microslop_one_drive/deserializers/shared_with_me_item_deserializer.rb
45
47
  - lib/microslop_one_drive/deserializers/user_deserializer.rb
@@ -56,6 +58,7 @@ files:
56
58
  - lib/microslop_one_drive/endpoints/drive_item_exists.rb
57
59
  - lib/microslop_one_drive/endpoints/me.rb
58
60
  - lib/microslop_one_drive/endpoints/permissions.rb
61
+ - lib/microslop_one_drive/endpoints/revoke_grants.rb
59
62
  - lib/microslop_one_drive/endpoints/supports_sites.rb
60
63
  - lib/microslop_one_drive/errors/client_error.rb
61
64
  - lib/microslop_one_drive/file.rb
@@ -66,8 +69,11 @@ files:
66
69
  - lib/microslop_one_drive/list_responses/permission_list.rb
67
70
  - lib/microslop_one_drive/list_responses/shared_with_me_list.rb
68
71
  - lib/microslop_one_drive/parent_reference.rb
69
- - lib/microslop_one_drive/permission.rb
70
- - lib/microslop_one_drive/permission_set.rb
72
+ - lib/microslop_one_drive/permissions/base_permission.rb
73
+ - lib/microslop_one_drive/permissions/direct_permission.rb
74
+ - lib/microslop_one_drive/permissions/link.rb
75
+ - lib/microslop_one_drive/permissions/sharing_invitation.rb
76
+ - lib/microslop_one_drive/permissions/sharing_link.rb
71
77
  - lib/microslop_one_drive/quota.rb
72
78
  - lib/microslop_one_drive/root_folder.rb
73
79
  - lib/microslop_one_drive/shared_with_me_item.rb
@@ -1,32 +0,0 @@
1
- module MicroslopOneDrive
2
- class Audience
3
- attr_reader :type, :id, :display_name, :email_address
4
-
5
- def initialize(type:, id:, display_name:, email_address: nil)
6
- @type = type
7
- @id = id
8
- @display_name = display_name
9
- @email_address = email_address
10
- end
11
-
12
- # Converts a "siteUser" object into an OneDriveAudience object.
13
- #
14
- # A siteUser object looks like this:
15
- #
16
- # {
17
- # "displayName" => "Bob Bentley",
18
- # "email" => "bob@haikucode.dev",
19
- # "id" => "12",
20
- # "loginName" => "i:0#.f|membership|bob@haikucode.dev"
21
- # }
22
- #
23
- def self.from_site_user(site_user)
24
- new(
25
- type: "user",
26
- id: site_user.fetch("email"),
27
- display_name: site_user.fetch("displayName"),
28
- email_address: site_user.fetch("email")
29
- )
30
- end
31
- end
32
- end
@@ -1,12 +0,0 @@
1
- module MicroslopOneDrive
2
- class Permission
3
- attr_reader :id, :drive_item_id, :role, :audience
4
-
5
- def initialize(id:, drive_item_id:, role:, audience:)
6
- @id = id
7
- @drive_item_id = drive_item_id
8
- @role = role
9
- @audience = audience
10
- end
11
- end
12
- end
@@ -1,59 +0,0 @@
1
- module MicroslopOneDrive
2
- class PermissionSet
3
- attr_reader :id, :role, :audiences
4
-
5
- def initialize(drive_item_id:, parsed_response:)
6
- @drive_item_id = drive_item_id
7
- @id = parsed_response.fetch("id", nil)
8
- @role = build_role(parsed_response)
9
- @audiences = build_audiences(parsed_response)
10
- end
11
-
12
- def to_permissions
13
- @audiences.map { Permission.new(id: @id, drive_item_id: @drive_item_id, role: @role, audience: it) }
14
- end
15
-
16
- private
17
-
18
- def build_role(parsed_response)
19
- roles = parsed_response.fetch("roles", [])
20
- roles.is_a?(Array) ? roles.first : roles
21
- end
22
-
23
- def build_audiences(parsed_response)
24
- audiences = []
25
-
26
- audiences += audiences_from_site_users(parsed_response)
27
- audiences += audiences_from_anonymous_links(parsed_response)
28
-
29
- audiences.compact
30
- end
31
-
32
- def audiences_from_site_users(parsed_response)
33
- site_users = []
34
- site_users += parsed_response.fetch("grantedToIdentitiesV2", []).flat_map { it.fetch("siteUser", nil) }
35
- site_users << parsed_response.dig("grantedToV2", "siteUser")
36
- site_users.compact!
37
-
38
- site_users.map { Audience.from_site_user(it) }
39
- end
40
-
41
- def audiences_from_anonymous_links(parsed_response)
42
- link = parsed_response.fetch("link", nil)
43
- return [] if link.nil? || (link.respond_to?(:empty?) && link.empty?)
44
-
45
- link_scope = link.fetch("scope", nil)
46
-
47
- return [] unless link_scope == "anonymous" # I.e. an "anyone with the link" audience
48
-
49
- [
50
- Audience.new(
51
- type: "anyone",
52
- id: "anyone_with_the_link",
53
- display_name: "Anyone with the link",
54
- email_address: nil
55
- )
56
- ]
57
- end
58
- end
59
- end