Class: NextcloudClient::Ocs::GroupFolder

Inherits:
NextcloudClient::OcsApi show all
Includes:
Helpers
Defined in:
lib/nextcloud-client/ocs/group_folder.rb

Overview

Group Folder class used for interfering with group folders

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 NextcloudClient::OcsApi

#app, #file_sharing, #group, #group_folder, #user

Methods inherited from Api

#ocs, #request, #webdav

Constructor Details

#initialize(args) ⇒ GroupFolder

Application initializer

Options Hash (args):

  • :url (String)

    Nextcloud instance URL

  • :username (String)

    Nextcloud instance user username

  • :password (String)

    Nextcloud instance user password



18
19
20
21
22
23
24
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 18

def initialize(args)
  super(args)

  @url = URI(
    @url.scheme + "://" + @url.host + "/apps/groupfolders/"
  )
end

Instance Attribute Details

#metaHash



7
8
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
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 7

class GroupFolder < OcsApi
  include Helpers

  attr_accessor :meta

  # Application initializer
  #
  # @param [Hash] args authentication credentials.
  # @option args [String] :url Nextcloud instance URL
  # @option args [String] :username Nextcloud instance user username
  # @option args [String] :password Nextcloud instance user password
  def initialize(args)
    super(args)

    @url = URI(
      @url.scheme + "://" + @url.host + "/apps/groupfolders/"
    )
  end

  # List all folders
  #
  # @return [Array] All group folders
  def folders
    response = request(:get, "folders")
    h = doc_to_hash(response, "//data").try(:[], "data").try(:[], "element")
    h = [h] if h.class == Hash
    add_meta(response, h)
  end

  # Return ID of folder given by name
  #
  # @param name [String] Folder name
  # @return [Integer] Folder ID
  def get_folder_id(name)
    response = request(:get, "folders")
    h = doc_to_hash(response, "//data").try(:[], "data").try(:[], "element")
    group = h.select { |folder| folder["mount_point"] == name }&.first
    unless group.nil?
      return group["id"]
    end
  end

  # Get information about a group folder
  #
  # @param folderid [Integer]
  # @return [Hash] Information about a group folder
  def find(folderid)
    response = request(:get, "folders/#{folderid}")
    h = doc_to_hash(response, "//data").try(:[], "data")
    add_meta(response, h)
  end

  # Create a group folder
  #
  # @param mountpoint [String] Mountpoint
  # @return [Hash] Hash with created group folder
  def create(mountpoint)
    args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
    response = request(:post, "folders", args)
    h = doc_to_hash(response, "//data").try(:[], "data")
    add_meta(response, h)
  end

  # Destroy a group folder
  #
  # @param mountpoint [String] Mountpoint
  # @return [Bool] True on success
  def destroy(folderid)
    response = request(:delete, "folders/#{folderid}")
    h = doc_to_hash(response, "//data").try(:[], "data")
    return h == "1"
  end

  # Give a group access to a folder
  #
  # @param folderid [Integer] FolderId to modify
  # @param group [String] Group which should get access to the folder
  # @return [Bool] True on success
  def give_access(folderid, group)
    args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
    response = request(:post, "folders/#{folderid}/groups", args)
    h = doc_to_hash(response, "//data").try(:[], "data")
    return h == "1"
  end

  # Remove access from a group to a folder
  # @param folderid [Integer] FolderId to modify
  # @param group [String] Group which should be removed to get access to the folder
  # @return [Bool] True on success
  def remove_access(folderid, group)
    response = request(:delete, "folders/#{folderid}/groups/#{group}")
    h = doc_to_hash(response, "//data").try(:[], "data")
    return h == "1"
  end

  # Set the permissions a group has in a folder
  #   PERMISSION_CREATE = 4;
  #   PERMISSION_READ = 1;
  #   PERMISSION_UPDATE = 2;
  #   PERMISSION_DELETE = 8;
  #   PERMISSION_SHARE = 16;
  #   PERMISSION_ALL = 31;
  #
  # @param folderid [Integer] FolderId to modify
  # @param group [String] Group for which the permissions should be changed
  # @param permissions [String] The new permissions for the group as bitmask of permissions constants
  # @return [Bool] True on success
  def set_permissions(folderid, group, permissions)
    args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
    response = request(:post, "folders/#{folderid}/groups/#{group}", args)
    h = doc_to_hash(response, "//data").try(:[], "data")
    return h == "1"
  end

  # Set the quota for a folder
  #
  # @param folderid [Integer] FolderId to modify
  # @param quota [Integer] The new quota for the folder in bytes, use -3 for unlimited
  # @return [Bool] True on success
  def set_quota(folderid, quota)
    args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
    response = request(:post, "folders/#{folderid}/quota", args)
    h = doc_to_hash(response, "//data").try(:[], "data")
    return h == "1"
  end

  # Change the name of a folder
  #
  # @param folderid [Integer] FolderId to modify
  # @param mountpoint [String] The new folder name
  # @return [Bool] True on success
  def rename_folder(folderid, mountpoint)
    args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
    response = request(:post, "folders/#{folderid}/mountpoint", args)
    h = doc_to_hash(response, "//data").try(:[], "data")
    return h == "1"
  end
end

Instance Method Details

#create(mountpoint) ⇒ Hash

Create a group folder



63
64
65
66
67
68
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 63

def create(mountpoint)
  args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
  response = request(:post, "folders", args)
  h = doc_to_hash(response, "//data").try(:[], "data")
  add_meta(response, h)
end

#destroy(folderid) ⇒ Bool

Destroy a group folder



74
75
76
77
78
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 74

def destroy(folderid)
  response = request(:delete, "folders/#{folderid}")
  h = doc_to_hash(response, "//data").try(:[], "data")
  return h == "1"
end

#find(folderid) ⇒ Hash

Get information about a group folder



53
54
55
56
57
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 53

def find(folderid)
  response = request(:get, "folders/#{folderid}")
  h = doc_to_hash(response, "//data").try(:[], "data")
  add_meta(response, h)
end

#foldersArray

List all folders



29
30
31
32
33
34
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 29

def folders
  response = request(:get, "folders")
  h = doc_to_hash(response, "//data").try(:[], "data").try(:[], "element")
  h = [h] if h.class == Hash
  add_meta(response, h)
end

#get_folder_id(name) ⇒ Integer

Return ID of folder given by name



40
41
42
43
44
45
46
47
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 40

def get_folder_id(name)
  response = request(:get, "folders")
  h = doc_to_hash(response, "//data").try(:[], "data").try(:[], "element")
  group = h.select { |folder| folder["mount_point"] == name }&.first
  unless group.nil?
    return group["id"]
  end
end

#give_access(folderid, group) ⇒ Bool

Give a group access to a folder



85
86
87
88
89
90
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 85

def give_access(folderid, group)
  args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
  response = request(:post, "folders/#{folderid}/groups", args)
  h = doc_to_hash(response, "//data").try(:[], "data")
  return h == "1"
end

#remove_access(folderid, group) ⇒ Bool

Remove access from a group to a folder



96
97
98
99
100
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 96

def remove_access(folderid, group)
  response = request(:delete, "folders/#{folderid}/groups/#{group}")
  h = doc_to_hash(response, "//data").try(:[], "data")
  return h == "1"
end

#rename_folder(folderid, mountpoint) ⇒ Bool

Change the name of a folder



138
139
140
141
142
143
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 138

def rename_folder(folderid, mountpoint)
  args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
  response = request(:post, "folders/#{folderid}/mountpoint", args)
  h = doc_to_hash(response, "//data").try(:[], "data")
  return h == "1"
end

#set_permissions(folderid, group, permissions) ⇒ Bool

Set the permissions a group has in a folder

PERMISSION_CREATE = 4;
PERMISSION_READ = 1;
PERMISSION_UPDATE = 2;
PERMISSION_DELETE = 8;
PERMISSION_SHARE = 16;
PERMISSION_ALL = 31;


114
115
116
117
118
119
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 114

def set_permissions(folderid, group, permissions)
  args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
  response = request(:post, "folders/#{folderid}/groups/#{group}", args)
  h = doc_to_hash(response, "//data").try(:[], "data")
  return h == "1"
end

#set_quota(folderid, quota) ⇒ Bool

Set the quota for a folder



126
127
128
129
130
131
# File 'lib/nextcloud-client/ocs/group_folder.rb', line 126

def set_quota(folderid, quota)
  args = local_variables.reduce({}) { |c, i| c[i] = binding.local_variable_get(i); c }
  response = request(:post, "folders/#{folderid}/quota", args)
  h = doc_to_hash(response, "//data").try(:[], "data")
  return h == "1"
end