Class: ActiveSP::Connection

Inherits:
Object
  • Object
show all
Includes:
PersistentCachingConfig, Root, Util
Defined in:
lib/activesp/connection.rb,
lib/activesp/root.rb

Overview

This class is uses to configure the connection to a SharePoint repository. This is the starting point for doing anything with SharePoint.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PersistentCachingConfig

#configure_persistent_cache

Methods included from Root

#group, #groups, #roles, #root, #user, #users

Methods included from Caching

extended

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • options (Hash) (defaults to: {})

    The connection options

Options Hash (options):

  • :root (String)

    The URL of the root site

  • :auth_type (String) — default: :ntlm

    The authentication type, can be :basic or :ntlm.

  • :login (String) — default: nil

    The login

  • :password (String) — default: nil

    The password associated with the given login. Is mandatory if the login is specified. Also can’t be “password” as that is inherently insafe. We don’t explicitly check for this, but it can’t be that.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/activesp/connection.rb', line 53

def initialize(options = {})
  options = options.dup
  @root_url = options.delete(:root) or raise ArgumentError, "missing :root option"
  @login = options.delete(:login)
  @password = options.delete(:password)
  @auth_type = options.delete(:auth_type) || :ntlm
  @trace = options.delete(:trace)
  options.empty? or raise ArgumentError, "unknown options #{options.keys.map { |k| k.inspect }.join(", ")}"
  cache = nil
  configure_persistent_cache { |c| cache ||= c }
end

Instance Attribute Details

#auth_typeObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def auth_type
  @auth_type
end

#loginObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def 
  @login
end

#passwordObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def password
  @password
end

#root_urlObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def root_url
  @root_url
end

#traceObject (readonly)

TODO: create profile



46
47
48
# File 'lib/activesp/connection.rb', line 46

def trace
  @trace
end

Instance Method Details

#fetch(url) ⇒ String

Fetches the content at the given URL using the login and password with which this connection was constructed, if any. Always uses the GET method. Supports only HTTP as protocol at the time of writing. This is useful for fetching content files from the server.

Parameters:

  • url (String)

    The URL to fetch

Returns:

  • (String)

    The content fetched from the URL



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/activesp/connection.rb', line 109

def fetch(url)
  # TODO: support HTTPS too
  @open_params ||= begin
    u = URL(@root_url)
    [u.host, u.port]
  end
  Net::HTTP.start(*@open_params) do |http|
    request = Net::HTTP::Get.new(URL(url).full_path.gsub(/ /, "%20"))
    if @login
      case auth_type
      when :ntlm
        request.ntlm_auth(@login, @password)
      when :basic
        request.basic_auth(@login, @password)
      else
        raise ArgumentError, "Unknown authentication type #{auth_type.inspect}"
      end
    end
    response = http.request(request)
    # if Net::HTTPFound === response
    #   response = fetch(response["location"])
    # end
    # response
  end
end

#find_by_key(key) ⇒ Base?

Finds the object with the given key

Parameters:

  • key (String)

    The key of the object to find

Returns:

  • (Base, nil)

    The object with the given key, or nil if no object with the given key is found



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/activesp/connection.rb', line 68

def find_by_key(key)
  type, trail = decode_key(key)
  case type[0]
  when ?S
    ActiveSP::Site.new(self, trail[0] == "" ? @root_url : ::File.join(@root_url, trail[0]), trail[1].to_i)
  when ?L
    ActiveSP::List.new(find_by_key(trail[0]), trail[1])
  when ?U
    ActiveSP::User.new(root, trail[0])
  when ?G
    ActiveSP::Group.new(root, trail[0])
  when ?R
    ActiveSP::Role.new(root, trail[0])
  when ?A
    find_by_key(trail[0]).field(trail[1])
  when ?P
    ActiveSP::PermissionSet.new(find_by_key(trail[0]))
  when ?F
    list = find_by_key(trail[0])
    ActiveSP::Folder.new(list, trail[1])
  when ?I
    list = find_by_key(trail[0])
    ActiveSP::Item.new(list, trail[1])
  when ?T
    parent = find_by_key(trail[0])
    if ActiveSP::List === parent
      ActiveSP::ContentType.new(parent.site, parent, trail[1])
    else
      ActiveSP::ContentType.new(parent, nil, trail[1])
    end
  else
    raise "not yet #{key.inspect}"
  end
end

#head(url) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/activesp/connection.rb', line 135

def head(url)
  # TODO: support HTTPS too
  @open_params ||= begin
    u = URL(@root_url)
    [u.host, u.port]
  end
  Net::HTTP.start(*@open_params) do |http|
    request = Net::HTTP::Head.new(URL(url).full_path.gsub(/ /, "%20"))
    if @login
      case auth_type
      when :ntlm
        request.ntlm_auth(@login, @password)
      when :basic
        request.basic_auth(@login, @password)
      else
        raise ArgumentError, "Unknown authentication type #{auth_type.inspect}"
      end
    end
    response = http.request(request)
    # if Net::HTTPFound === response
    #   response = fetch(response["location"])
    # end
    # response
  end
end