sharepoint_api 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8f8c46ee759a2cb1b84211e88137406512de21a48a4a759d109097c54273259f
4
+ data.tar.gz: 444dbfaee8eff930e0a030ad5cb5450cedc71268eb0ff05ab346af8297c032c9
5
+ SHA512:
6
+ metadata.gz: e1f6ff11efc7b37027928325908e1b05cb5318d1ff4f84fb0a0a10b2253544e54606c7c7043ea1d40257469443412dddebb268629a9cd118331fa4453702b5cb
7
+ data.tar.gz: 9f7b3f87f4eaeaf4271a4266ae5f4a054805a5967c471bffde71ca10649e099ce917db9c1aede8e3d8e8dd099ce091f510149ef110a0df3276cf1189bb505b55
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ sharepoint_api-*.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,90 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+ Exclude:
7
+ - bin/*
8
+ TargetRubyVersion: 2.6
9
+
10
+ Layout/DotPosition:
11
+ EnforcedStyle: trailing
12
+
13
+ Layout/EmptyLinesAroundAccessModifier:
14
+ Enabled: false
15
+
16
+ Layout/EmptyLinesAroundAttributeAccessor:
17
+ Enabled: true
18
+
19
+ Layout/EmptyLinesAroundBlockBody:
20
+ Enabled: false
21
+
22
+ Layout/EmptyLinesAroundClassBody:
23
+ Enabled: false
24
+
25
+ Layout/EmptyLinesAroundMethodBody:
26
+ Enabled: false
27
+
28
+ Layout/EmptyLinesAroundModuleBody:
29
+ Enabled: false
30
+
31
+ Layout/LineLength:
32
+ Enabled: false
33
+
34
+ Layout/SpaceAroundMethodCallOperator:
35
+ Enabled: true
36
+
37
+ Lint/DeprecatedOpenSSLConstant:
38
+ Enabled: true
39
+
40
+ Lint/RaiseException:
41
+ Enabled: true
42
+
43
+ Lint/StructNewOverride:
44
+ Enabled: true
45
+
46
+ Metrics/BlockLength:
47
+ Exclude: ['**/*.rake']
48
+ IgnoredMethods: ['describe', 'context', 'it', 'FactoryBot.define', 'factory']
49
+
50
+ Metrics/MethodLength:
51
+ Max: 20
52
+
53
+ Style/AndOr:
54
+ EnforcedStyle: conditionals
55
+
56
+ Style/BlockDelimiters:
57
+ EnforcedStyle: braces_for_chaining
58
+
59
+ Style/ClassAndModuleChildren:
60
+ Enabled: false
61
+
62
+ Style/Documentation:
63
+ Enabled: false
64
+
65
+ Style/ExponentialNotation:
66
+ Enabled: true
67
+
68
+ Style/FrozenStringLiteralComment:
69
+ Enabled: false
70
+
71
+ Style/HashEachMethods:
72
+ Enabled: true
73
+
74
+ Style/HashTransformKeys:
75
+ Enabled: true
76
+
77
+ Style/HashTransformValues:
78
+ Enabled: true
79
+
80
+ Style/MultilineBlockChain:
81
+ Enabled: false
82
+
83
+ Style/SlicingWithRange:
84
+ Enabled: true
85
+
86
+ RSpec/LetSetup:
87
+ Enabled: false
88
+
89
+ RSpec/NestedGroups:
90
+ Max: 4
@@ -0,0 +1,2 @@
1
+ ## [1.0.0] - 2020-12-07
2
+ * Initial Release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at andrew.kalek@anlek.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sharepoint_api.gemspec
6
+ gemspec
7
+
8
+ gem 'faker'
9
+ gem 'rake', '~> 13.0'
10
+ gem 'rspec', '~> 3.6'
11
+ gem 'rspec-collection_matchers'
12
+ gem 'rubocop', '~> 1.5'
13
+ gem 'rubocop-rspec', '~> 2.0'
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ SharepointApi (0.1.0)
5
+ addressable (~> 2.7)
6
+ proof-sharepoint-ruby (~> 1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ ast (2.4.1)
14
+ concurrent-ruby (1.1.7)
15
+ curb (0.9.10)
16
+ diff-lcs (1.4.4)
17
+ faker (2.15.1)
18
+ i18n (>= 1.6, < 2)
19
+ i18n (1.8.5)
20
+ concurrent-ruby (~> 1.0)
21
+ parallel (1.20.1)
22
+ parser (2.7.2.0)
23
+ ast (~> 2.4.1)
24
+ proof-sharepoint-ruby (1.0.0)
25
+ curb (~> 0.8, <= 0.9.10)
26
+ public_suffix (4.0.6)
27
+ rainbow (3.0.0)
28
+ rake (13.0.1)
29
+ regexp_parser (2.0.0)
30
+ rexml (3.2.4)
31
+ rspec (3.10.0)
32
+ rspec-core (~> 3.10.0)
33
+ rspec-expectations (~> 3.10.0)
34
+ rspec-mocks (~> 3.10.0)
35
+ rspec-collection_matchers (1.2.0)
36
+ rspec-expectations (>= 2.99.0.beta1)
37
+ rspec-core (3.10.0)
38
+ rspec-support (~> 3.10.0)
39
+ rspec-expectations (3.10.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.10.0)
42
+ rspec-mocks (3.10.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.10.0)
45
+ rspec-support (3.10.0)
46
+ rubocop (1.5.2)
47
+ parallel (~> 1.10)
48
+ parser (>= 2.7.1.5)
49
+ rainbow (>= 2.2.2, < 4.0)
50
+ regexp_parser (>= 1.8, < 3.0)
51
+ rexml
52
+ rubocop-ast (>= 1.2.0, < 2.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 2.0)
55
+ rubocop-ast (1.3.0)
56
+ parser (>= 2.7.1.5)
57
+ rubocop-rspec (2.0.1)
58
+ rubocop (~> 1.0)
59
+ rubocop-ast (>= 1.1.0)
60
+ ruby-progressbar (1.10.1)
61
+ unicode-display_width (1.7.0)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ SharepointApi!
68
+ faker
69
+ rake (~> 13.0)
70
+ rspec (~> 3.6)
71
+ rspec-collection_matchers
72
+ rubocop (~> 1.5)
73
+ rubocop-rspec (~> 2.0)
74
+
75
+ BUNDLED WITH
76
+ 1.17.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Andrew Kalek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # SharepointApi
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sharepoint_api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sharepoint_api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sharepoint_api
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/anlek/sharepoint_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/anlek/sharepoint_api/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the SharepointApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/anlek/sharepoint_api/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sharepoint_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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,108 @@
1
+ require 'addressable'
2
+ require 'forwardable'
3
+ require 'sharepoint-http-auth'
4
+ require 'logger'
5
+ require 'sharepoint-ruby'
6
+
7
+ require_relative 'sharepoint_api/sharepoint_error'
8
+ require_relative 'sharepoint_api/version'
9
+ require_relative 'sharepoint_api/items'
10
+ require_relative 'sharepoint_api/file_info'
11
+ require_relative 'sharepoint_api/filename_cleaner'
12
+ require_relative 'sharepoint_api/file_system'
13
+ require_relative 'sharepoint_api/permissions'
14
+ require_relative 'sharepoint_api/users'
15
+ require_relative 'sharepoint_api/version_converter'
16
+
17
+ class SharepointApi
18
+ extend Forwardable
19
+ include SharepointApi::Items
20
+ include SharepointApi::FileSystem
21
+ include SharepointApi::Permissions
22
+ include SharepointApi::Users
23
+
24
+ class << self
25
+ attr_writer :logger
26
+
27
+ def logger
28
+ @logger ||= begin
29
+ logger = Logger.new($stdout)
30
+ logger.level = Logger::WARN
31
+ logger
32
+ end
33
+ end
34
+
35
+ def log_as(method, exception_or_message, level: :info)
36
+ message = if exception_or_message.respond_to?(:message)
37
+ exception_or_message.message
38
+ else
39
+ exception_or_message
40
+ end
41
+
42
+ logger.send(level, "#{name}: #{method}: #{message}")
43
+ end
44
+ end
45
+
46
+ attr_accessor :library_name
47
+ attr_reader :site, :host
48
+
49
+ def_delegators :site, :protocol
50
+ def_delegators 'self.class', :logger, :log_as, :logger=
51
+
52
+ def initialize(library_name: '', **config)
53
+ @library_name = library_name
54
+ @host = config.fetch(:host)
55
+ @username = config.fetch(:username)
56
+ @password = config.fetch(:password)
57
+ @site_name = config[:site_name] || ''
58
+ @ntlm = config[:ntlm] || false
59
+ @verbose = config[:verbose] || false
60
+ @login_name_extractor = config[:login_name_extractor]
61
+
62
+ @site = build_connection
63
+ end
64
+
65
+ def site_relative_path(path, preview: false)
66
+ file_path = [
67
+ library_name, path
68
+ ].reject{ |p| p.nil? || p == '' }.join('/')
69
+ encode_path(file_path, preview: preview)
70
+ end
71
+
72
+ def server_relative_path(path)
73
+ "/#{site_path}/#{site_relative_path(path)}"
74
+ end
75
+
76
+ def site_path
77
+ @site.name
78
+ end
79
+
80
+ def base_path
81
+ "#{protocol}://#{host}/#{site_path}"
82
+ end
83
+
84
+ def encode_path(path, preview: false)
85
+ path = Addressable::URI.encode(path)
86
+ path.gsub!(/'/, '%27%27') unless preview
87
+ path.gsub!('+', '%2B')
88
+ path
89
+ end
90
+
91
+ def login_name(*args, **kwargs)
92
+ @login_name_extractor.call(*args, **kwargs)
93
+ end
94
+
95
+ #######
96
+ private
97
+ #######
98
+
99
+ def build_connection
100
+ site = Sharepoint::Site.new(@host, @site_name)
101
+ site.verbose = @verbose
102
+
103
+ site.session = Sharepoint::HttpAuth::Session.new(site) if @ntlm
104
+ site.session.authenticate(@username, @password) # custom STS?
105
+
106
+ site
107
+ end
108
+ end
@@ -0,0 +1,67 @@
1
+ class SharepointApi
2
+ class FileInfo
3
+ extend Forwardable
4
+ attr_reader :version, :version_id, :file, :updated_at
5
+
6
+ def_delegator :file, :name
7
+
8
+ def self.wrap(sharepoint_file)
9
+ return nil unless sharepoint_file
10
+
11
+ new(sharepoint_file)
12
+ end
13
+
14
+ def initialize(file)
15
+ @version = file.major_version
16
+ @version_id = SharepointApi::VersionConverter.to_ui(file.major_version)
17
+ @updated_at = Time.parse(file.time_last_modified)
18
+ @file = file
19
+ end
20
+
21
+ def list_item_id
22
+ @list_item_id ||= file.list_item_all_fields.id
23
+ end
24
+
25
+ def versions
26
+ return @all_versions if @all_versions
27
+
28
+ @all_versions = []
29
+
30
+ @all_versions << make_version_object(
31
+ version_id,
32
+ created_at: updated_at,
33
+ current: true,
34
+ creator_login: file.modified_by.login_name
35
+ )
36
+
37
+ return @all_versions unless version > 1
38
+
39
+ @all_versions += file.versions.map do |file_version|
40
+ make_version_object(
41
+ file_version.id,
42
+ created_at: Time.parse(file_version.created),
43
+ current: file_version.is_current_version,
44
+ creator_login: file_version.creator.login_name
45
+ )
46
+ end
47
+
48
+ @all_versions = @all_versions.sort_by(&:number)
49
+ end
50
+
51
+ def to_h
52
+ { version: version, version_id: version_id, name: name, list_item_id: list_item_id }
53
+ end
54
+
55
+ #######
56
+ private
57
+ #######
58
+
59
+ def make_version_object(version_id, **attrs)
60
+ OpenStruct.new(
61
+ number: SharepointApi::VersionConverter.to_major(version_id),
62
+ version_id: version_id,
63
+ **attrs
64
+ )
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,187 @@
1
+ require_relative 'sharepoint_error'
2
+ require_relative 'items'
3
+
4
+ class SharepointApi
5
+ module FileSystem
6
+ class LockError < SharepointError; end
7
+ class FileNotFoundError < SharepointError; end
8
+
9
+ def add_file(path, content)
10
+ folder_path = File.dirname(path)
11
+ file_name = encode_path(File.basename(path))
12
+ begin
13
+ retries ||= 0
14
+ FileInfo.wrap(
15
+ site.query(
16
+ :post,
17
+ "GetFolderByServerRelativeUrl('#{site_relative_path(folder_path)}')/Files" \
18
+ "/Add(overwrite=true,url='#{file_name}')",
19
+ content
20
+ )
21
+ )
22
+ rescue Sharepoint::SPException => e
23
+ reraise_if_lock_error(e)
24
+
25
+ log_as(__method__, e)
26
+ log_as(__method__, "adding folder with path #{folder_path}")
27
+ add_folder(folder_path)
28
+ retry if (retries += 1) < 2
29
+ log_as(__method__, e, level: :warn)
30
+ nil
31
+ end
32
+ end
33
+
34
+ def file_exists?(path)
35
+ server_path = server_relative_path(path)
36
+ site.query(:get, "GetFileByServerRelativeUrl('#{server_path}')/Exists")
37
+ rescue Sharepoint::SPException => e
38
+ log_as(__method__, e)
39
+ false
40
+ end
41
+
42
+ def find_file(path)
43
+ server_path = server_relative_path(path)
44
+ FileInfo.wrap(
45
+ site.query(:get, "GetFileByServerRelativeUrl('#{server_path}')")
46
+ )
47
+ rescue Sharepoint::SPException => e
48
+ log_as(__method__, e)
49
+ nil
50
+ end
51
+
52
+ def find_file_version(path, version_id)
53
+ server_path = server_relative_path(path)
54
+ site.query(:get, "GetFileByServerRelativeUrl('#{server_path}')/versions(#{version_id})")
55
+ rescue Sharepoint::SPException => e
56
+ log_as(__method__, e)
57
+ nil
58
+ end
59
+
60
+ def move_file(path, new_path)
61
+ server_path = server_relative_path(path)
62
+ new_server_path = server_relative_path(new_path)
63
+
64
+ site.query(
65
+ :post,
66
+ "GetFileByServerRelativeUrl(@v)/MoveTo?@v='#{server_path}'&newurl='#{new_server_path}'&flags=0"
67
+ )
68
+
69
+ true
70
+ rescue Sharepoint::SPException => e
71
+ log_as(__method__, e)
72
+ false
73
+ end
74
+
75
+ def rename_file(path, rename_to)
76
+ validate_update_list_item(
77
+ path,
78
+ {
79
+ 'FileLeafRef' => rename_to
80
+ }
81
+ )
82
+ rescue SharepointApi::ItemUpdateError => _e
83
+ false
84
+ end
85
+
86
+ def remove_file(path)
87
+ site.query(
88
+ :post,
89
+ "GetFileByServerRelativeUrl('#{server_relative_path(path)}')/Recycle"
90
+ )
91
+ rescue Sharepoint::SPException => e
92
+ reraise_if_lock_error(e)
93
+
94
+ log_as(__method__, e)
95
+ nil
96
+ end
97
+
98
+ def remove_file_version(path, version_id)
99
+ site.query(
100
+ :post,
101
+ "GetFileByServerRelativeUrl('#{server_relative_path(path)}')/Versions/DeleteById(vid=#{version_id})"
102
+ )
103
+ rescue Sharepoint::SPException => e
104
+ log_as(__method__, e)
105
+ nil
106
+ end
107
+
108
+ def retrieve_file_content(path)
109
+ server_path = server_relative_path(path)
110
+ site.query(:get, "GetFileByServerRelativeUrl('#{server_path}')/$value", { binaryStringResponseBody: true }.to_json, true)
111
+ rescue Sharepoint::SPException => e
112
+ reraise_if_lock_error(e)
113
+
114
+ log_as(__method__, e)
115
+ raise FileNotFoundError, "The file #{path} does not exist."
116
+ end
117
+
118
+ def files_in_folder(path)
119
+ server_path = server_relative_path(path)
120
+ site.query(:get, "GetFolderByServerRelativeUrl('#{server_path}')/Files").
121
+ map { |file| FileInfo.new(file) }
122
+ rescue Sharepoint::SPException => e
123
+ log_as(__method__, e)
124
+ nil
125
+ end
126
+
127
+ ##
128
+ # Perhaps this should support a recursive option to
129
+ # create all parents as needed?
130
+ def add_folder(path)
131
+ site.query(:post, "Folders/Add('#{server_relative_path(path)}')")
132
+ rescue Sharepoint::SPException => e
133
+ log_as(__method__, e)
134
+ nil
135
+ end
136
+
137
+ def find_folder(path)
138
+ server_path = server_relative_path(path)
139
+ site.query(:get, "GetFolderByServerRelativeUrl('#{server_path}')")
140
+ rescue Sharepoint::SPException => e
141
+ log_as(__method__, e)
142
+ false
143
+ end
144
+
145
+ def remove_folder(path)
146
+ server_path = server_relative_path(path)
147
+ site.query(:post, "GetFolderByServerRelativeUrl('#{server_path}')/Recycle")
148
+ rescue Sharepoint::SPException => e
149
+ log_as(__method__, e)
150
+ nil
151
+ end
152
+
153
+ def add_library(name, description: nil)
154
+ site.query(:post, 'lists', {
155
+ '__metadata' => { 'type' => 'SP.List' },
156
+ 'Title' => name,
157
+ 'Description' => description || "Document library for #{name}.",
158
+ 'BaseTemplate' => Sharepoint::LIST_TEMPLATE_TYPE[:DocumentLibrary],
159
+ 'AllowContentTypes' => true,
160
+ 'ContentTypesEnabled' => true,
161
+ 'EnableVersioning' => true
162
+ }.to_json)
163
+ rescue Sharepoint::SPException => e
164
+ log_as(__method__, e)
165
+ false
166
+ end
167
+
168
+ def find_library(name)
169
+ encoded_name = encode_path(name)
170
+ site.query :get, "Lists/GetByTitle('#{encoded_name}')"
171
+ rescue Sharepoint::SPException => e
172
+ log_as(__method__, e)
173
+ nil
174
+ end
175
+
176
+ #######
177
+ private
178
+ #######
179
+
180
+ def reraise_if_lock_error(error)
181
+ return unless error.message =~ /is locked for shared use/
182
+
183
+ log_as(__method__, error, level: :warn)
184
+ raise(LockError, error.message)
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,22 @@
1
+ class SharepointApi
2
+ class FilenameCleaner
3
+ # Windows doesn't allow these characters in file names.
4
+ INVALID_CHARS_REGEX = %r([~"#%&*:<>?/\\{\|}]).freeze
5
+
6
+ def self.call(filename, invalid_character_regex: nil)
7
+ new(invalid_character_regex).call(filename)
8
+ end
9
+
10
+ def initialize(invalid_character_regex = nil)
11
+ @invalid_character_regex = invalid_character_regex || INVALID_CHARS_REGEX
12
+ end
13
+
14
+ def call(filename)
15
+ if @invalid_character_regex
16
+ filename.gsub(@invalid_character_regex, '')
17
+ else
18
+ filename
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,71 @@
1
+ require_relative 'sharepoint_error'
2
+
3
+ class SharepointApi
4
+ module Items
5
+ class ItemUpdateError < SharepointError; end
6
+
7
+ def validate_update_list_item(path, field_name_field_value_pairs)
8
+ server_path = server_relative_path(path)
9
+ uri = "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields/" \
10
+ 'ValidateUpdateListItem'
11
+
12
+ form_values = field_name_field_value_pairs.map do |key, value|
13
+ { 'FieldName' => key, 'FieldValue' => value }
14
+ end
15
+ body = { 'formValues' => form_values, 'bNewDocumentUpdate' => false }
16
+
17
+ response = site.query(:post, uri, body.to_json, true)
18
+ hoist_complex_errors!(response, uri, body)
19
+
20
+ true
21
+ rescue Sharepoint::SharepointError => e
22
+ raise SharepointApi::SharepointError, e.message
23
+ end
24
+
25
+ def fetch_content_types
26
+ encoded_name = encode_path(library_name)
27
+ response = site.query(
28
+ :get,
29
+ "Lists/GetByTitle('#{encoded_name}')/ContentTypes?" \
30
+ '$select=Name,StringId'
31
+ )
32
+ response.map { |type| type.data.except('__metadata') }
33
+ rescue Sharepoint::SPException => e
34
+ log_as(__method__, e)
35
+
36
+ nil
37
+ end
38
+
39
+ #######
40
+ private
41
+ #######
42
+
43
+ def hoist_complex_errors!(response, uri, body)
44
+ data = JSON.parse(response)
45
+
46
+ if data['error']
47
+ error = Sharepoint::SPException.new(data, uri, body)
48
+ reraise_if_lock_error(error)
49
+
50
+ log_as(__method__, error)
51
+ raise ItemUpdateError, error.message
52
+ end
53
+
54
+ errors = data.dig('d', 'ValidateUpdateListItem', 'results')&.
55
+ select { |result| result['HasException'] }&.
56
+ map { |result| result.slice('FieldName', 'FieldValue', 'ErrorMessage') }
57
+
58
+ return if errors.nil? || errors.count.zero?
59
+
60
+ log_as(__method__, errors)
61
+ raise ItemUpdateError, errors
62
+ end
63
+
64
+ def reraise_if_lock_error(error)
65
+ return unless error.message =~ /is locked for shared use/
66
+
67
+ log_as(__method__, error, level: :warn)
68
+ raise(LockError, error.message)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,135 @@
1
+ require_relative 'sharepoint_error'
2
+
3
+ class SharepointApi
4
+ module Permissions
5
+ def list_item_for(path)
6
+ server_path = server_relative_path(path)
7
+ site.query(:get, "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields")
8
+ rescue Sharepoint::SPException => e
9
+ log_as(__method__, e)
10
+ nil
11
+ end
12
+
13
+ ##
14
+ # NO SPACES in url or Addressable::URI.encode the url.
15
+ def break_permission_inheritance_for(path, copy_role_assignments: false, clear_subscopes: true)
16
+ server_path = server_relative_path(path)
17
+ site.query(
18
+ :post,
19
+ "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields/" \
20
+ "BreakRoleInheritance(CopyRoleAssignments=#{copy_role_assignments},ClearSubscopes=#{clear_subscopes})"
21
+ )
22
+
23
+ true # Anything other than an error is success
24
+ rescue Sharepoint::SPException => e
25
+ log_as(__method__, e)
26
+ nil
27
+ end
28
+
29
+ def find_group(group_name)
30
+ escaped_group_name = encode_path(group_name)
31
+ site.query(:get, "SiteGroups/GetByName('#{escaped_group_name}')")
32
+ rescue Sharepoint::SPException => e
33
+ log_as(__method__, e)
34
+ nil
35
+ end
36
+
37
+ def add_group(group_name)
38
+ site.query(:post, 'SiteGroups', {
39
+ '__metadata' => { 'type': 'SP.Group' },
40
+ 'Title' => group_name,
41
+ 'Description' => "Access Group for #{group_name}"
42
+ }.to_json)
43
+ rescue Sharepoint::SPException => e
44
+ log_as(__method__, e)
45
+ false
46
+ end
47
+
48
+ ##
49
+ # This also removes any role definitions the groups is using.
50
+ def remove_group(group_name)
51
+ site.query(:post, "SiteGroups/RemoveByLoginName('#{group_name}')")
52
+ rescue Sharepoint::SPException => e
53
+ log_as(__method__, e)
54
+ false
55
+ end
56
+
57
+ def add_user_to_group(login_name, group_name)
58
+ user = site.query(:post, "SiteGroups/GetByName('#{group_name}')/Users", {
59
+ '__metadata' => { 'type' => 'SP.User' }, 'LoginName' => login_name
60
+ }.to_json)
61
+
62
+ !user.nil?
63
+ rescue Sharepoint::SPException => e
64
+ log_as(__method__, e)
65
+ false
66
+ end
67
+
68
+ ##
69
+ # Addressable::URI.encode does not encode `:#.` characters,
70
+ # which is a must for login names.
71
+ def remove_user_from_group(login_name, group_name)
72
+ encoded_login_name = ERB::Util.url_encode(login_name)
73
+ site.query(
74
+ :post,
75
+ "SiteGroups/GetByName('#{group_name}')/" \
76
+ "Users/RemoveByLoginName(@v)?@v='#{encoded_login_name}'"
77
+ )
78
+
79
+ true
80
+ rescue Sharepoint::SPException => e
81
+ log_as(__method__, e)
82
+ false
83
+ end
84
+
85
+ def users_in_group(group_name)
86
+ site.query(:get, "SiteGroups/GetByName('#{group_name}')/Users")
87
+ rescue Sharepoint::SPException => e
88
+ log_as(__method__, e)
89
+ nil
90
+ end
91
+
92
+ def find_role(role_name = 'Edit')
93
+ escaped_role_name = encode_path(role_name)
94
+ site.query :get, "RoleDefinitions/GetByName('#{escaped_role_name}')"
95
+ rescue Sharepoint::SPException => e
96
+ log_as(__method__, e)
97
+ false
98
+ end
99
+
100
+ ##
101
+ # Alternate version would be:
102
+ # def add_role_assignment(library_guid:, list_item_id:, principal_id:, role_id:)
103
+ # list_item_path = "Lists(guid'#{@library_guid}')/Items(#{@list_item_id})"
104
+ # You would do it the above way if you had a problem with the file names being too long.
105
+ def add_role_assignment(path, principal_id, role_id)
106
+ server_path = server_relative_path(path)
107
+ list_item_path = "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields"
108
+
109
+ site.query( # returns nothing on success
110
+ :post,
111
+ "#{list_item_path}/RoleAssignments/AddRoleAssignment(PrincipalId=#{principal_id},RoleDefId=#{role_id})"
112
+ )
113
+
114
+ true
115
+ rescue Sharepoint::SPException => e
116
+ log_as(__method__, e)
117
+ false
118
+ end
119
+
120
+ def remove_role_assignment(path, principal_id, role_id)
121
+ server_path = server_relative_path(path)
122
+ list_item_path = "GetFolderByServerRelativeUrl('#{server_path}')/ListItemAllFields"
123
+
124
+ site.query( # returns nothing on success
125
+ :post,
126
+ "#{list_item_path}/RoleAssignments/RemoveRoleAssignment(PrincipalId=#{principal_id},RoleDefId=#{role_id})"
127
+ )
128
+
129
+ true
130
+ rescue Sharepoint::SPException => e
131
+ log_as(__method__, e)
132
+ false
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,3 @@
1
+ class SharepointApi
2
+ class SharepointError < RuntimeError; end
3
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'sharepoint_error'
2
+
3
+ class SharepointApi
4
+ module Users
5
+ def ensure_user(login_name)
6
+ body = {
7
+ 'logonName' => login_name
8
+ }
9
+ site.query(:post, 'ensureUser', body.to_json)
10
+ rescue Sharepoint::SPException => e
11
+ log_as(__method__, e)
12
+ nil
13
+ end
14
+
15
+ def fetch_user_id_from_login_name(login_name)
16
+ encoded_login_name = ERB::Util.url_encode(login_name)
17
+
18
+ url = "/SiteUsers/GetByLoginName(@v)?@v='#{encoded_login_name}'&$select=Id"
19
+ site.query(:get, url)&.id
20
+ rescue Sharepoint::SPException => e
21
+ log_as(__method__, e)
22
+ nil
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ class SharepointApi
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,12 @@
1
+ class SharepointApi
2
+ class VersionConverter
3
+ VERSION_UNIT = 512
4
+ def self.to_major(ui_version)
5
+ ui_version.to_i / VERSION_UNIT
6
+ end
7
+
8
+ def self.to_ui(major_version)
9
+ major_version.to_i * VERSION_UNIT
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/sharepoint_api/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'sharepoint_api'
5
+ spec.version = SharepointApi::VERSION
6
+ spec.authors = ['Andrew Kalek', 'Marlen Brunner']
7
+ spec.email = ['akalek@proofgov.com', 'mbrunner@proofgov.com']
8
+
9
+ spec.summary = 'SharepointApi allows you to interact with sharepoint in a simpler DSL.'
10
+ spec.description = 'A tool to make it easier to talk to sharepoint (via the proof-sharepoint-ruby gem)'
11
+ spec.homepage = 'https://github.com/proofgov/sharepoint_api'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/proofgov/sharepoint_api'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/proofgov/sharepoint_api/blob/master/CHANGELOG.md'
18
+
19
+ spec.add_runtime_dependency('proof-sharepoint-ruby', '~> 1.0')
20
+ spec.add_runtime_dependency('addressable', '~> 2.7')
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+
30
+ spec.require_paths = ['lib']
31
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sharepoint_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kalek
8
+ - Marlen Brunner
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-12-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: proof-sharepoint-ruby
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: addressable
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '2.7'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.7'
42
+ description: A tool to make it easier to talk to sharepoint (via the proof-sharepoint-ruby
43
+ gem)
44
+ email:
45
+ - akalek@proofgov.com
46
+ - mbrunner@proofgov.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".gitignore"
52
+ - ".rspec"
53
+ - ".rubocop.yml"
54
+ - CHANGELOG.md
55
+ - CODE_OF_CONDUCT.md
56
+ - Gemfile
57
+ - Gemfile.lock
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - bin/console
62
+ - bin/setup
63
+ - lib/sharepoint_api.rb
64
+ - lib/sharepoint_api/file_info.rb
65
+ - lib/sharepoint_api/file_system.rb
66
+ - lib/sharepoint_api/filename_cleaner.rb
67
+ - lib/sharepoint_api/items.rb
68
+ - lib/sharepoint_api/permissions.rb
69
+ - lib/sharepoint_api/sharepoint_error.rb
70
+ - lib/sharepoint_api/users.rb
71
+ - lib/sharepoint_api/version.rb
72
+ - lib/sharepoint_api/version_converter.rb
73
+ - sharepoint_api.gemspec
74
+ homepage: https://github.com/proofgov/sharepoint_api
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/proofgov/sharepoint_api
79
+ source_code_uri: https://github.com/proofgov/sharepoint_api
80
+ changelog_uri: https://github.com/proofgov/sharepoint_api/blob/master/CHANGELOG.md
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.6.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.1.4
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: SharepointApi allows you to interact with sharepoint in a simpler DSL.
100
+ test_files: []