Class: Nextcloud::Ocs::User

Inherits:
Nextcloud::OcsApi show all
Includes:
Helpers
Defined in:
lib/nextcloud/ocs/user.rb

Overview

Class includes User provisioning fetures, including User group operations

Direct Known Subclasses

Group

Defined Under Namespace

Classes: Group

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#add_meta, #doc_to_hash, #get_meta, #has_dav_errors, #parse_dav_response, #parse_with_meta, #path_from_href

Methods inherited from Nextcloud::OcsApi

#app, #file_sharing, #user

Methods inherited from Api

#request

Constructor Details

#initialize(args, userid = nil) ⇒ User

Class initializer

Parameters:

  • api (Object)

    Api instance

  • userid (String, nil) (defaults to: nil)

    User identifier


18
19
20
21
22
23
24
25
26
27
# File 'lib/nextcloud/ocs/user.rb', line 18

def initialize(args, userid = nil)
  @userid = userid if userid

  if args.class == Nextcloud::OcsApi
    @api = args
  else
    super(args)
    @api = self
  end
end

Instance Attribute Details

#metaHash

Returns Information about API response.

Returns:

  • (Hash)

    Information about API response


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
102
103
104
105
106
107
108
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
134
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/nextcloud/ocs/user.rb', line 9

class User < OcsApi
  include Helpers

  attr_accessor :meta, :userid

  # Class initializer
  #
  # @param api [Object] Api instance
  # @param userid [String,nil] User identifier
  def initialize(args, userid = nil)
    @userid = userid if userid

    if args.class == Nextcloud::OcsApi
      @api = args
    else
      super(args)
      @api = self
    end
  end

  # Sets user (useful if class is initiated without OcsApi.user)
  #
  # @param userid [String] User identifier
  # @return [Obeject] self
  def set(userid)
    @userid = userid
    self
  end

  # Retrieve information about an user
  #
  # @param userid [String]
  # @return [Object] User instance
  def find(userid)
    response = @api.request(:get, "users/#{userid}")

    enabled = response.xpath("//data/enabled").text
    id = response.xpath("//data/id").text
    quota = response.xpath("//data/quota/*").each_with_object({}) do |node, quota|
      quota[node.name] = node.text
    end
    email = response.xpath("//data/email").text
    displayname = response.xpath("//data/displayname").text
    phone = response.xpath("//data/phone").text
    address = response.xpath("//data/address").text
    website = response.xpath("//data/website").text
    twitter = response.xpath("//data/twitter").text
    groups = []
    response.xpath("//data/groups/element").each do |prop|
      groups << prop.text
    end

    language = response.xpath("//data/language").text

    user = Nextcloud::Models::User.new(enabled: enabled, id: id, quota: quota, email: email,
                                       displayname: displayname, phone: phone, address: address, website: website,
                                       twitter: twitter, groups: groups, language: language)
    (user.meta = get_meta(response)) && user
  end

  # Retrieve all users
  #
  # @return [Array] List of all users
  def all
    response = @api.request(:get, "users")

    users = [].tap do |users|
      response.xpath("//element").each do |prop|
        id = prop.text
        users << Nextcloud::Models::User.new(id: id)
      end
    end

    meta = get_meta(response)

    users.send(:define_singleton_method, :meta) { meta } && users
  end

  # Add a new user
  #
  # @param userid [String] User identifier
  # @param password [String] User password
  # @return [Object] Instance with meta response
  def create(userid, password)
    response = @api.request(:post, "users", userid: userid, password: password)
    (@meta = get_meta(response)) && self
  end

  # Update a parameter of an user
  #
  # @param userid [String] User identifier
  # @param key [String] Parameter to update. Can be quota, displayname, phone, address, website, twitter or password
  # @param value [String] Value to update to
  # @return [Object] Instance with meta information
  def update(userid, key, value)
    response = @api.request(:put, "users/#{userid}", key: key, value: value)
    (@meta = get_meta(response)) && self
  end

  # Disable an user
  #
  # @param userid [String] User identifier
  # @return [Object] Instance with meta information
  def disable(userid)
    response = @api.request(:put, "users/#{userid}/disable")
    (@meta = get_meta(response)) && self
  end

  # Enable an user
  #
  # @param userid [String] User identifier
  # @return [Object] Instance with meta information
  def enable(userid)
    response = @api.request(:put, "users/#{userid}/enable")
    (@meta = get_meta(response)) && self
  end

  # Remove an user account
  #
  # @param userid [String] User identifier
  # @return [Object] Instance with meta information
  def destroy(userid)
    response = @api.request(:delete, "users/#{userid}")
    (@meta = get_meta(response)) && self
  end

  # Class covering User group operations
  #
  # @!attribute [rw] meta
  #   @return [Hash] Information about API response
  # @!attribute [rw] groupid
  #   @return [String,nil] Group identifier
  class Group < User
    include Helpers

    attr_accessor :userid, :groupid, :meta

    # Initializes an User Group instance
    #
    # @param api [Object] Api instance
    # @param userid [String] User identifier
    # @param groupid [String,nil] Group identifier
    def initialize(api, userid, groupid = nil)
      @api = api
      @userid = userid
      @groupid = groupid if groupid
    end

    # Add an user to a group
    #
    # @param groupid [String] Group to add user to
    # @return [Object] Instance with meta information
    def create(groupid)
      response = @api.request(:post, "users/#{@userid}/groups", groupid: groupid)
      (@meta = get_meta(response)) && self
    end

    # Remove user from a group
    #
    # @param groupid [String] Group to remove user from
    # @return [Object] Instance with meta information
    def destroy(groupid)
      response = @api.request(:delete, "users/#{@userid}/groups", groupid: groupid)
      (@meta = get_meta(response)) && self
    end

    # Make an user subadmin of a group
    #
    # @return [Object] Instance with meta information
    def promote
      response = @api.request(:post, "users/#{@userid}/subadmins", groupid: @groupid)
      (@meta = get_meta(response)) && self
    end

    # Remove an user from group subadmins
    #
    # @return [Object] Instance with meta information
    def demote
      response = @api.request(:delete, "users/#{@userid}/subadmins", groupid: @groupid)
      (@meta = get_meta(response)) && self
    end
  end

  # Initialize a group class
  #
  # @param groupid [String,nil] Group identifier
  def group(groupid = nil)
    Group.new(@api, @userid, groupid)
  end

  # List groups that user belongs to
  #
  # @return [Array] User groups
  def groups
    response = @api.request(:get, "users/#{@userid}/groups")
    parse_with_meta(response, "//data/groups/element")
  end

  # List groups that user sub-admins
  #
  # @return [Array] User sub-admin groups
  def subadmin_groups
    response = @api.request(:get, "users/#{@userid}/subadmins")
    parse_with_meta(response, "//data/element")
  end

  # Resend welcome e-mail letter to a user
  #
  # @param userid [String]
  # @return [Object] Instance with meta information
  def resend_welcome(userid)
    response = @api.request(:post, "users/#{userid}/welcome")
    (@meta = get_meta(response)) && self
  end
end

#useridString?

Returns User identifier.

Returns:

  • (String, nil)

    User identifier


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
102
103
104
105
106
107
108
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
134
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/nextcloud/ocs/user.rb', line 9

class User < OcsApi
  include Helpers

  attr_accessor :meta, :userid

  # Class initializer
  #
  # @param api [Object] Api instance
  # @param userid [String,nil] User identifier
  def initialize(args, userid = nil)
    @userid = userid if userid

    if args.class == Nextcloud::OcsApi
      @api = args
    else
      super(args)
      @api = self
    end
  end

  # Sets user (useful if class is initiated without OcsApi.user)
  #
  # @param userid [String] User identifier
  # @return [Obeject] self
  def set(userid)
    @userid = userid
    self
  end

  # Retrieve information about an user
  #
  # @param userid [String]
  # @return [Object] User instance
  def find(userid)
    response = @api.request(:get, "users/#{userid}")

    enabled = response.xpath("//data/enabled").text
    id = response.xpath("//data/id").text
    quota = response.xpath("//data/quota/*").each_with_object({}) do |node, quota|
      quota[node.name] = node.text
    end
    email = response.xpath("//data/email").text
    displayname = response.xpath("//data/displayname").text
    phone = response.xpath("//data/phone").text
    address = response.xpath("//data/address").text
    website = response.xpath("//data/website").text
    twitter = response.xpath("//data/twitter").text
    groups = []
    response.xpath("//data/groups/element").each do |prop|
      groups << prop.text
    end

    language = response.xpath("//data/language").text

    user = Nextcloud::Models::User.new(enabled: enabled, id: id, quota: quota, email: email,
                                       displayname: displayname, phone: phone, address: address, website: website,
                                       twitter: twitter, groups: groups, language: language)
    (user.meta = get_meta(response)) && user
  end

  # Retrieve all users
  #
  # @return [Array] List of all users
  def all
    response = @api.request(:get, "users")

    users = [].tap do |users|
      response.xpath("//element").each do |prop|
        id = prop.text
        users << Nextcloud::Models::User.new(id: id)
      end
    end

    meta = get_meta(response)

    users.send(:define_singleton_method, :meta) { meta } && users
  end

  # Add a new user
  #
  # @param userid [String] User identifier
  # @param password [String] User password
  # @return [Object] Instance with meta response
  def create(userid, password)
    response = @api.request(:post, "users", userid: userid, password: password)
    (@meta = get_meta(response)) && self
  end

  # Update a parameter of an user
  #
  # @param userid [String] User identifier
  # @param key [String] Parameter to update. Can be quota, displayname, phone, address, website, twitter or password
  # @param value [String] Value to update to
  # @return [Object] Instance with meta information
  def update(userid, key, value)
    response = @api.request(:put, "users/#{userid}", key: key, value: value)
    (@meta = get_meta(response)) && self
  end

  # Disable an user
  #
  # @param userid [String] User identifier
  # @return [Object] Instance with meta information
  def disable(userid)
    response = @api.request(:put, "users/#{userid}/disable")
    (@meta = get_meta(response)) && self
  end

  # Enable an user
  #
  # @param userid [String] User identifier
  # @return [Object] Instance with meta information
  def enable(userid)
    response = @api.request(:put, "users/#{userid}/enable")
    (@meta = get_meta(response)) && self
  end

  # Remove an user account
  #
  # @param userid [String] User identifier
  # @return [Object] Instance with meta information
  def destroy(userid)
    response = @api.request(:delete, "users/#{userid}")
    (@meta = get_meta(response)) && self
  end

  # Class covering User group operations
  #
  # @!attribute [rw] meta
  #   @return [Hash] Information about API response
  # @!attribute [rw] groupid
  #   @return [String,nil] Group identifier
  class Group < User
    include Helpers

    attr_accessor :userid, :groupid, :meta

    # Initializes an User Group instance
    #
    # @param api [Object] Api instance
    # @param userid [String] User identifier
    # @param groupid [String,nil] Group identifier
    def initialize(api, userid, groupid = nil)
      @api = api
      @userid = userid
      @groupid = groupid if groupid
    end

    # Add an user to a group
    #
    # @param groupid [String] Group to add user to
    # @return [Object] Instance with meta information
    def create(groupid)
      response = @api.request(:post, "users/#{@userid}/groups", groupid: groupid)
      (@meta = get_meta(response)) && self
    end

    # Remove user from a group
    #
    # @param groupid [String] Group to remove user from
    # @return [Object] Instance with meta information
    def destroy(groupid)
      response = @api.request(:delete, "users/#{@userid}/groups", groupid: groupid)
      (@meta = get_meta(response)) && self
    end

    # Make an user subadmin of a group
    #
    # @return [Object] Instance with meta information
    def promote
      response = @api.request(:post, "users/#{@userid}/subadmins", groupid: @groupid)
      (@meta = get_meta(response)) && self
    end

    # Remove an user from group subadmins
    #
    # @return [Object] Instance with meta information
    def demote
      response = @api.request(:delete, "users/#{@userid}/subadmins", groupid: @groupid)
      (@meta = get_meta(response)) && self
    end
  end

  # Initialize a group class
  #
  # @param groupid [String,nil] Group identifier
  def group(groupid = nil)
    Group.new(@api, @userid, groupid)
  end

  # List groups that user belongs to
  #
  # @return [Array] User groups
  def groups
    response = @api.request(:get, "users/#{@userid}/groups")
    parse_with_meta(response, "//data/groups/element")
  end

  # List groups that user sub-admins
  #
  # @return [Array] User sub-admin groups
  def subadmin_groups
    response = @api.request(:get, "users/#{@userid}/subadmins")
    parse_with_meta(response, "//data/element")
  end

  # Resend welcome e-mail letter to a user
  #
  # @param userid [String]
  # @return [Object] Instance with meta information
  def resend_welcome(userid)
    response = @api.request(:post, "users/#{userid}/welcome")
    (@meta = get_meta(response)) && self
  end
end

Instance Method Details

#allArray

Retrieve all users

Returns:

  • (Array)

    List of all users


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nextcloud/ocs/user.rb', line 72

def all
  response = @api.request(:get, "users")

  users = [].tap do |users|
    response.xpath("//element").each do |prop|
      id = prop.text
      users << Nextcloud::Models::User.new(id: id)
    end
  end

  meta = get_meta(response)

  users.send(:define_singleton_method, :meta) { meta } && users
end

#create(userid, password) ⇒ Object

Add a new user

Parameters:

  • userid (String)

    User identifier

  • password (String)

    User password

Returns:

  • (Object)

    Instance with meta response


92
93
94
95
# File 'lib/nextcloud/ocs/user.rb', line 92

def create(userid, password)
  response = @api.request(:post, "users", userid: userid, password: password)
  (@meta = get_meta(response)) && self
end

#destroy(userid) ⇒ Object

Remove an user account

Parameters:

  • userid (String)

    User identifier

Returns:

  • (Object)

    Instance with meta information


130
131
132
133
# File 'lib/nextcloud/ocs/user.rb', line 130

def destroy(userid)
  response = @api.request(:delete, "users/#{userid}")
  (@meta = get_meta(response)) && self
end

#disable(userid) ⇒ Object

Disable an user

Parameters:

  • userid (String)

    User identifier

Returns:

  • (Object)

    Instance with meta information


112
113
114
115
# File 'lib/nextcloud/ocs/user.rb', line 112

def disable(userid)
  response = @api.request(:put, "users/#{userid}/disable")
  (@meta = get_meta(response)) && self
end

#enable(userid) ⇒ Object

Enable an user

Parameters:

  • userid (String)

    User identifier

Returns:

  • (Object)

    Instance with meta information


121
122
123
124
# File 'lib/nextcloud/ocs/user.rb', line 121

def enable(userid)
  response = @api.request(:put, "users/#{userid}/enable")
  (@meta = get_meta(response)) && self
end

#find(userid) ⇒ Object

Retrieve information about an user

Parameters:

  • userid (String)

Returns:

  • (Object)

    User instance


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nextcloud/ocs/user.rb', line 42

def find(userid)
  response = @api.request(:get, "users/#{userid}")

  enabled = response.xpath("//data/enabled").text
  id = response.xpath("//data/id").text
  quota = response.xpath("//data/quota/*").each_with_object({}) do |node, quota|
    quota[node.name] = node.text
  end
  email = response.xpath("//data/email").text
  displayname = response.xpath("//data/displayname").text
  phone = response.xpath("//data/phone").text
  address = response.xpath("//data/address").text
  website = response.xpath("//data/website").text
  twitter = response.xpath("//data/twitter").text
  groups = []
  response.xpath("//data/groups/element").each do |prop|
    groups << prop.text
  end

  language = response.xpath("//data/language").text

  user = Nextcloud::Models::User.new(enabled: enabled, id: id, quota: quota, email: email,
                                     displayname: displayname, phone: phone, address: address, website: website,
                                     twitter: twitter, groups: groups, language: language)
  (user.meta = get_meta(response)) && user
end

#group(groupid = nil) ⇒ Object

Initialize a group class

Parameters:

  • groupid (String, nil) (defaults to: nil)

    Group identifier


195
196
197
# File 'lib/nextcloud/ocs/user.rb', line 195

def group(groupid = nil)
  Group.new(@api, @userid, groupid)
end

#groupsArray

List groups that user belongs to

Returns:

  • (Array)

    User groups


202
203
204
205
# File 'lib/nextcloud/ocs/user.rb', line 202

def groups
  response = @api.request(:get, "users/#{@userid}/groups")
  parse_with_meta(response, "//data/groups/element")
end

#resend_welcome(userid) ⇒ Object

Resend welcome e-mail letter to a user

Parameters:

  • userid (String)

Returns:

  • (Object)

    Instance with meta information


219
220
221
222
# File 'lib/nextcloud/ocs/user.rb', line 219

def resend_welcome(userid)
  response = @api.request(:post, "users/#{userid}/welcome")
  (@meta = get_meta(response)) && self
end

#set(userid) ⇒ Obeject

Sets user (useful if class is initiated without OcsApi.user)

Parameters:

  • userid (String)

    User identifier

Returns:

  • (Obeject)

    self


33
34
35
36
# File 'lib/nextcloud/ocs/user.rb', line 33

def set(userid)
  @userid = userid
  self
end

#subadmin_groupsArray

List groups that user sub-admins

Returns:

  • (Array)

    User sub-admin groups


210
211
212
213
# File 'lib/nextcloud/ocs/user.rb', line 210

def subadmin_groups
  response = @api.request(:get, "users/#{@userid}/subadmins")
  parse_with_meta(response, "//data/element")
end

#update(userid, key, value) ⇒ Object

Update a parameter of an user

Parameters:

  • userid (String)

    User identifier

  • key (String)

    Parameter to update. Can be quota, displayname, phone, address, website, twitter or password

  • value (String)

    Value to update to

Returns:

  • (Object)

    Instance with meta information


103
104
105
106
# File 'lib/nextcloud/ocs/user.rb', line 103

def update(userid, key, value)
  response = @api.request(:put, "users/#{userid}", key: key, value: value)
  (@meta = get_meta(response)) && self
end