Module: SharepointApi::Permissions
- Included in:
- SharepointApi
- Defined in:
- lib/sharepoint_api/permissions.rb
Instance Method Summary collapse
- #add_group(group_name) ⇒ Object
-
#add_role_assignment(path, principal_id, role_id) ⇒ Object
Alternate version would be: def add_role_assignment(library_guid:, list_item_id:, principal_id:, role_id:) list_item_path = “Lists(guid’#@library_guid‘)/Items(#@list_item_id)” You would do it the above way if you had a problem with the file names being too long.
- #add_user_to_group(login_name, group_name) ⇒ Object
-
#break_permission_inheritance_for(path, copy_role_assignments: false, clear_subscopes: true) ⇒ Object
NO SPACES in url or Addressable::URI.encode the url.
- #fetch_role_assignments(path, principal_id: nil) ⇒ Object
- #find_group(group_name) ⇒ Object
- #find_role(role_name = 'Edit') ⇒ Object
- #list_item_for(path) ⇒ Object
-
#remove_group(group_name) ⇒ Object
This also removes any role definitions the groups is using.
- #remove_role_assignment(path, principal_id, role_id) ⇒ Object
-
#remove_user_from_group(login_name, group_name) ⇒ Object
Addressable::URI.encode does not encode ‘:#.` characters, which is a must for login names.
-
#rename_group(old_group_name, new_group_name) ⇒ Object
to rename a sp group when the acronym changes.
- #users_in_group(group_name) ⇒ Object
Instance Method Details
#add_group(group_name) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sharepoint_api/permissions.rb', line 37 def add_group(group_name) site.query(:post, 'SiteGroups', { '__metadata' => { 'type': 'SP.Group' }, 'Title' => group_name, 'Description' => "Access Group for #{group_name}" }.to_json) rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#add_role_assignment(path, principal_id, role_id) ⇒ Object
Alternate version would be: def add_role_assignment(library_guid:, list_item_id:, principal_id:, role_id:)
list_item_path = "Lists(guid'#{@library_guid}')/Items(#{@list_item_id})"
You would do it the above way if you had a problem with the file names being too long.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/sharepoint_api/permissions.rb', line 151 def add_role_assignment(path, principal_id, role_id) server_path = server_relative_path(path) list_item_path = "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields" site.query( # returns nothing on success :post, "#{list_item_path}/RoleAssignments/AddRoleAssignment(PrincipalId=#{principal_id},RoleDefId=#{role_id})" ) true rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#add_user_to_group(login_name, group_name) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sharepoint_api/permissions.rb', line 73 def add_user_to_group(login_name, group_name) user = site.query(:post, "SiteGroups/GetByName('#{group_name}')/Users", { '__metadata' => { 'type' => 'SP.User' }, 'LoginName' => login_name }.to_json) !user.nil? rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#break_permission_inheritance_for(path, copy_role_assignments: false, clear_subscopes: true) ⇒ Object
NO SPACES in url or Addressable::URI.encode the url.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sharepoint_api/permissions.rb', line 15 def (path, copy_role_assignments: false, clear_subscopes: true) server_path = server_relative_path(path) site.query( :post, "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields/" \ "BreakRoleInheritance(CopyRoleAssignments=#{copy_role_assignments},ClearSubscopes=#{clear_subscopes})" ) true # Anything other than an error is success rescue Sharepoint::SPException => e log_as(__method__, e) nil end |
#fetch_role_assignments(path, principal_id: nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/sharepoint_api/permissions.rb', line 116 def fetch_role_assignments(path, principal_id: nil) server_path = server_relative_path(path) list_item_path = "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields" query = '$expand=Member,RoleDefinitionBindings&$select=RoleDefinitionBindings/Name&$select=RoleDefinitionBindings/Id,PrincipalId,Member/LoginName' unless principal_id.nil? principal_filter = encode_path("PrincipalId eq #{principal_id}") query += "&$filter=#{principal_filter}" end response = site.query(:get, "#{list_item_path}/RoleAssignments?#{query}") formatted = response.map do |assignment| data = assignment.data current_roles = data['RoleDefinitionBindings']['results'].map { |rdb| { role_id: rdb['Id'], role_name: rdb['Name'] } } { login_name: data['Member']['LoginName'], principal_id: data['PrincipalId'], roles: current_roles } end principal_id.nil? ? formatted : formatted.first rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#find_group(group_name) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/sharepoint_api/permissions.rb', line 29 def find_group(group_name) escaped_group_name = encode_path(group_name) site.query(:get, "SiteGroups/GetByName('#{escaped_group_name}')") rescue Sharepoint::SPException => e log_as(__method__, e) nil end |
#find_role(role_name = 'Edit') ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/sharepoint_api/permissions.rb', line 108 def find_role(role_name = 'Edit') escaped_role_name = encode_path(role_name) site.query :get, "RoleDefinitions/GetByName('#{escaped_role_name}')" rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#list_item_for(path) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/sharepoint_api/permissions.rb', line 5 def list_item_for(path) server_path = server_relative_path(path) site.query(:get, "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields") rescue Sharepoint::SPException => e log_as(__method__, e) nil end |
#remove_group(group_name) ⇒ Object
This also removes any role definitions the groups is using.
66 67 68 69 70 71 |
# File 'lib/sharepoint_api/permissions.rb', line 66 def remove_group(group_name) site.query(:post, "SiteGroups/RemoveByLoginName('#{group_name}')") rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#remove_role_assignment(path, principal_id, role_id) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/sharepoint_api/permissions.rb', line 166 def remove_role_assignment(path, principal_id, role_id) server_path = server_relative_path(path) list_item_path = "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields" site.query( # returns nothing on success :post, "#{list_item_path}/RoleAssignments/RemoveRoleAssignment(PrincipalId=#{principal_id},RoleDefId=#{role_id})" ) true rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#remove_user_from_group(login_name, group_name) ⇒ Object
Addressable::URI.encode does not encode ‘:#.` characters, which is a must for login names.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sharepoint_api/permissions.rb', line 87 def remove_user_from_group(login_name, group_name) encoded_login_name = ERB::Util.url_encode(login_name) site.query( :post, "SiteGroups/GetByName('#{group_name}')/" \ "Users/RemoveByLoginName(@v)?@v='#{encoded_login_name}'" ) true rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#rename_group(old_group_name, new_group_name) ⇒ Object
to rename a sp group when the acronym changes
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sharepoint_api/permissions.rb', line 49 def rename_group(old_group_name, new_group_name) escaped_group_name = encode_path(old_group_name) site.query(:post, "SiteGroups/GetByName('#{escaped_group_name}')", { '__metadata' => { 'type': 'SP.Group' }, 'Title' => new_group_name, 'Description' => "Access Group for #{new_group_name}" }.to_json) do |curl| curl.headers['X-HTTP-Method'] = 'MERGE' end true rescue Sharepoint::SPException => e log_as(__method__, e) false end |
#users_in_group(group_name) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/sharepoint_api/permissions.rb', line 101 def users_in_group(group_name) site.query(:get, "SiteGroups/GetByName('#{group_name}')/Users") rescue Sharepoint::SPException => e log_as(__method__, e) nil end |