Module: RepositoryManager::HasRepository::LocalInstanceMethods

Defined in:
lib/repository_manager/has_repository.rb

Instance Method Summary collapse

Instance Method Details

#add_members_to(sharing, members, options = RepositoryManager.default_sharing_permissions) ⇒ Object



480
481
482
483
484
485
486
# File 'lib/repository_manager/has_repository.rb', line 480

def add_members_to(sharing, members, options = RepositoryManager.default_sharing_permissions)
  begin
    add_members_to!(sharing, members, options = RepositoryManager.default_sharing_permissions)
  rescue RepositoryManager::PermissionException
    false
  end
end

#add_members_to!(sharing, members, options = RepositoryManager.default_sharing_permissions) ⇒ Object

Add new members in the sharing Param member could be an object or an array of object



469
470
471
472
473
474
475
476
477
478
# File 'lib/repository_manager/has_repository.rb', line 469

def add_members_to!(sharing, members, options = RepositoryManager.default_sharing_permissions)
  permissions = get_sharing_permissions(sharing)
  if can_add_to?(sharing)
    sharing_permissions = make_sharing_permissions(options, permissions)
    sharing.add_members(members, sharing_permissions)
  else
    sharing.errors.add(:add, I18n.t('repository_manager.errors.sharing.add.no_permission'))
    raise RepositoryManager::PermissionException.new("add members failed. You don't have the permission to add a member in this sharing")
  end
end

#can_add_to?(sharing) ⇒ Boolean

Return true if you can add a member in this sharing, false else

Returns:

  • (Boolean)


458
459
460
# File 'lib/repository_manager/has_repository.rb', line 458

def can_add_to?(sharing)
  can_do_to?('add', sharing)
end

#can_create?(repo_item, permissions = nil) ⇒ Boolean

Return true if you can create in the repo, false else

Returns:

  • (Boolean)


427
428
429
# File 'lib/repository_manager/has_repository.rb', line 427

def can_create?(repo_item, permissions = nil)
  can_do?('create', repo_item, permissions)
end

#can_delete?(repo_item, permissions = nil) ⇒ Boolean

Returns true if you can delete the repo, false else

Returns:

  • (Boolean)


437
438
439
# File 'lib/repository_manager/has_repository.rb', line 437

def can_delete?(repo_item, permissions = nil)
  can_do?('delete', repo_item, permissions)
end

#can_download?(repo_item, permissions = nil) ⇒ Boolean

Return true if you can download the repo, else false Read = Download for the moment

Returns:

  • (Boolean)


422
423
424
# File 'lib/repository_manager/has_repository.rb', line 422

def can_download?(repo_item, permissions = nil)
  can_do?('read', repo_item, permissions)
end

#can_read?(repo_item, permissions = nil) ⇒ Boolean

Return true if you can read the repo, else false

Returns:

  • (Boolean)


416
417
418
# File 'lib/repository_manager/has_repository.rb', line 416

def can_read?(repo_item, permissions = nil)
  can_do?('read', repo_item, permissions)
end

#can_remove_from?(sharing) ⇒ Boolean

Return true if you can remove a member in this sharing, false else

Returns:

  • (Boolean)


463
464
465
# File 'lib/repository_manager/has_repository.rb', line 463

def can_remove_from?(sharing)
  can_do_to?('remove', sharing)
end

#can_share?(repo_item, permissions = nil) ⇒ Boolean

Return true if you can share the repo, else false You can give the permissions or the repo_item as params

Returns:

  • (Boolean)


411
412
413
# File 'lib/repository_manager/has_repository.rb', line 411

def can_share?(repo_item, permissions = nil)
  can_do?('share', repo_item, permissions)
end

#can_update?(repo_item, permissions = nil) ⇒ Boolean

Returns true if you can edit the repo, false else

Returns:

  • (Boolean)


432
433
434
# File 'lib/repository_manager/has_repository.rb', line 432

def can_update?(repo_item, permissions = nil)
  can_do?('update', repo_item, permissions)
end

#copy_repo_item(repo_item, target = nil, options = {}) ⇒ Object



388
389
390
391
392
393
394
395
396
397
# File 'lib/repository_manager/has_repository.rb', line 388

def copy_repo_item(repo_item, target = nil, options = {})
  begin
    copy_repo_item!(repo_item, target, options)
  rescue RepositoryManager::PermissionException, RepositoryManager::ItemExistException
    false
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
    repo_item.errors.add(:copy, I18n.t('repository_manager.errors.repo_item.copy.not_copied'))
    false
  end
end

#copy_repo_item!(repo_item, target = nil, options = {}) ⇒ Object

Copy the repo_item in the source_folder or in own root target => the folder in witch we want to copy the repo item options

:sender => the new sender (by default => still the old sender)


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/repository_manager/has_repository.rb', line 366

def copy_repo_item!(repo_item, target = nil, options = {})
  unless can_read?(repo_item)
    repo_item.errors.add(:copy, I18n.t('repository_manager.errors.repo_item.copy.no_permission'))
    raise RepositoryManager::PermissionException.new("copy repo_item failed. You don't have the permission to read the repo_item '#{repo_item.name}'")
  end

  if target &&  !can_create?(target)
    repo_item.errors.add(:copy, I18n.t('repository_manager.errors.repo_item.copy.no_permission'))
    raise RepositoryManager::PermissionException.new("copy repo_item failed. You don't have the permission to create in the source_folder '#{target.name}'")
  end

  # The new owner
  if target
    owner = target.owner
  else
    owner = self
  end

  # If it has the permission, we move the repo_item in the source_folder
  repo_item.copy!(source_folder: target, owner: owner, sender: options[:sender])
end

#create_file(file, options = {}) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/repository_manager/has_repository.rb', line 231

def create_file(file, options = {})
  options[:errors] = []
  begin
    create_file!(file, options)
  rescue RepositoryManager::PermissionException
    options[:errors].push(I18n.t'repository_manager.errors.repo_item.repo_file.create.no_permission')
    false
  rescue RepositoryManager::ItemExistException
    options[:errors].push(I18n.t'repository_manager.errors.repo_item.repo_file.item_exist')
    false
  rescue RepositoryManager::RepositoryManagerException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
    options[:errors].push(I18n.t'repository_manager.errors.repo_item.repo_file.create.not_created')
    false
  end
end

#create_file!(file, options = {}) ⇒ Object

Create the file (file) in the directory (source_folder) options :

:source_folder = The directory in with the folder is created
:sender = The object of the sender (ex : current_user)
:filename = The name of the file (if you want to rename it directly)

Param file can be a File, or a instance of RepoFile Returns the object of the file created if it is ok Returns an Exception if the folder is not created

RepositoryManagerException if the file already exist
PermissionException if the object don't have the permission


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
224
225
226
227
228
229
# File 'lib/repository_manager/has_repository.rb', line 190

def create_file!(file, options = {})
  source_folder = options[:source_folder]
  if source_folder
    unless source_folder.is_folder?
      raise RepositoryManager::RepositoryManagerException.new("create file failed. The source folder must be a repo_folder.")
    end
  end

  # If he want to create a file in a directory, we have to check if he have the permission
  if can_create?(source_folder)

    if file.class.name == 'RepositoryManager::RepoFile'
      repo_file = file
    elsif file.class.name == 'File' || file.class.name == 'ActionDispatch::Http::UploadedFile'
      repo_file = RepositoryManager::RepoFile.new()
      repo_file.file = file
    else # "ActionController::Parameters"
      repo_file = RepositoryManager::RepoFile.new(file)
    end

    options[:filename] ? repo_file.name = options[:filename] : repo_file.name = repo_file.file.url.split('/').last

    repo_file.owner = self
    repo_file.sender = options[:sender]


    # If we are in root path we check if we can add this file name
    if !source_folder && repo_item_name_exist_in_root?(repo_file.file.identifier)
      raise RepositoryManager::ItemExistException.new("create file failed. The repo_item '#{repo_file.file.identifier}' already exist in the root folder.")
    end

    # It raise an error if name already exist and destroy the file
    source_folder.add!(repo_file) if source_folder

    repo_file.save!
  else
    raise RepositoryManager::PermissionException.new("create_file failed. You don't have the permission to create a file")
  end
  repo_file
end

#create_folder(name = '', options = {}) ⇒ Object

Like create_folder! Returns false if the folder is not created instead of an exception



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/repository_manager/has_repository.rb', line 145

def create_folder(name = '', options = {})
  options[:errors] = []
  begin
    create_folder!(name, options)
  rescue RepositoryManager::PermissionException
    options[:errors].push(I18n.t'repository_manager.errors.repo_item.repo_folder.create.no_permission')
    false
  rescue RepositoryManager::ItemExistException
    options[:errors].push(I18n.t'repository_manager.errors.repo_item.repo_folder.item_exist')
    false
  rescue RepositoryManager::RepositoryManagerException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
    options[:errors].push(I18n.t'repository_manager.errors.repo_item.repo_folder.create.not_created')
    false
  end
end

#create_folder!(name = '', options = {}) ⇒ Object

Create a folder with the name (name) options :

:source_folder = The directory in with the folder is created
:sender = The object of the sender (ex : current_user)

Returns the object of the folder created if it is ok Returns an Exception if the folder is not created

RepositoryManagerException if the name already exist
PermissionException if the object don't have the permission


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
# File 'lib/repository_manager/has_repository.rb', line 108

def create_folder!(name = '', options = {})
  source_folder = options[:source_folder]
  if source_folder
    unless source_folder.is_folder?
      raise RepositoryManager::RepositoryManagerException.new("create folder failed. The source folder must be a repo_folder.")
    end
  end

  # If he want to create a folder in a directory, we have to check if he have the permission
  if can_create?(source_folder)

    folder = RepoFolder.new
    if name == '' || name == nil || name == false || name.blank?
      folder.name = default_folder_name(source_folder)
    else
      folder.name = name
    end
    folder.owner = self
    folder.sender = options[:sender]

    # If we are in root path we check if we can add this folder name
    if !source_folder && repo_item_name_exist_in_root?(name)
      raise RepositoryManager::ItemExistException.new("create folder failed. The repo_item '#{name}' already exist in the root folder.")
    end

    # It raise an error if name already exist and destroy the folder
    source_folder.add!(folder) if source_folder

    folder.save!
  else
    raise RepositoryManager::PermissionException.new("create_folder failed. You don't have the permission to create a folder in '#{source_folder.name}'")
  end
  folder
end

#delete_download_pathObject

Delete the download folder of the user



400
401
402
# File 'lib/repository_manager/has_repository.rb', line 400

def delete_download_path
  FileUtils.rm_rf(self.get_default_download_path())
end

#delete_repo_item(repo_item) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/repository_manager/has_repository.rb', line 171

def delete_repo_item(repo_item)
  begin
    delete_repo_item!(repo_item)
  rescue RepositoryManager::PermissionException
    false
  end
end

#delete_repo_item!(repo_item) ⇒ Object

Delete the repo_item



162
163
164
165
166
167
168
169
# File 'lib/repository_manager/has_repository.rb', line 162

def delete_repo_item!(repo_item)
  if can_delete?(repo_item)
    repo_item.destroy
  else
    repo_item.errors.add(:delete, I18n.t('repository_manager.errors.repo_item.delete.no_permission'))
    raise RepositoryManager::PermissionException.new("delete_repo_item failed. You don't have the permission to delete the repo_item '#{repo_item.name}'")
  end
end

#download(repo_item, options = {}) ⇒ Object



289
290
291
292
293
294
295
# File 'lib/repository_manager/has_repository.rb', line 289

def download(repo_item, options = {})
  begin
    download!(repo_item, options)
  rescue RepositoryManager::PermissionException
    false
  end
end

#download!(repo_item, options = {}) ⇒ Object

Download a repo_item if the object can_read it If it is a file, he download the file If it is a folder, we check witch repo_item is in it, and witch he can_read We zip all the content that the object has access. options

:path => 'path/to/zip'


278
279
280
281
282
283
284
285
286
287
# File 'lib/repository_manager/has_repository.rb', line 278

def download!(repo_item, options = {})
  if can_download?(repo_item)
    path = options[:path] if options[:path]

    repo_item.download!({object: self, path: path})
  else
    repo_item.errors.add(:download, I18n.t('repository_manager.errors.repo_item.download.no_permission'))
    raise RepositoryManager::PermissionException.new("download failed. You don't have the permission to download the repo_item '#{repo_item.name}'")
  end
end

#get_default_download_path(prefix = "#{Rails.root.join('download')}/") ⇒ Object

Get the download path of the member



508
509
510
# File 'lib/repository_manager/has_repository.rb', line 508

def get_default_download_path(prefix = "#{Rails.root.join('download')}/")
  "#{prefix}#{self.class.base_class.to_s.underscore}/#{self.id}/"
end

#get_permissions(repo_item = nil) ⇒ Object

Gets the repo permissions Return false if the entity has not the permission to share this rep Return true if the entity can share this rep with all the permissions Return an Array if the entity can share but with restriction Return true if the repo_item is nil (he as all permissions on his own rep)



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/repository_manager/has_repository.rb', line 252

def get_permissions(repo_item = nil)
  # If repo_item is nil, he can do what he want
  return true if repo_item == nil

  # If the member is the owner, he can do what he want !
  if repo_item.owner == self
    # You can do what ever you want :)
    return true
  # Find if a sharing of this rep exist for the self instance or it ancestors
  else
    path_ids = repo_item.path_ids
    # Check the nearest sharing if it exist
    if s = self.sharings.where(repo_item_id: path_ids).last
      return {can_share: s.can_share, can_read: s.can_read, can_create: s.can_create, can_update: s.can_update, can_delete: s.can_delete}
    end
  end
  # Else, false
  return false
end

#get_sharing_permissions(sharing) ⇒ Object

Return the permissions of the sharing (can_add, can_remove)



405
406
407
# File 'lib/repository_manager/has_repository.rb', line 405

def get_sharing_permissions(sharing)
  sharing.get_permissions(self)
end

#move_repo_item(repo_item, target = nil) ⇒ Object



351
352
353
354
355
356
357
358
359
360
# File 'lib/repository_manager/has_repository.rb', line 351

def move_repo_item(repo_item, target = nil)
  begin
    move_repo_item!(repo_item, target)
  rescue RepositoryManager::PermissionException, RepositoryManager::ItemExistException
    false
  rescue RepositoryManager::RepositoryManagerException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
    repo_item.errors.add(:move, I18n.t('repository_manager.errors.repo_item.move.not_moved'))
    false
  end
end

#move_repo_item!(repo_item, target = nil) ⇒ Object

Move the repo_item target => move into this source_folder if target == nil, move to the root



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/repository_manager/has_repository.rb', line 318

def move_repo_item!(repo_item, target = nil)
  if !can_read?(repo_item)
    repo_item.errors.add(:move, I18n.t('repository_manager.errors.repo_item.move.no_permission'))
    raise RepositoryManager::PermissionException.new("move repo_item failed. You don't have the permission to read the repo_item '#{repo_item.name}'")
  end
  # If we want to change the owner we have to have the can_delete permission
  if target
    # If want to change the owner, we have to check if we have the permission
    if target.owner != repo_item.owner && !can_delete?(repo_item)
      repo_item.errors.add(:move, I18n.t('repository_manager.errors.repo_item.move.no_permission'))
      raise RepositoryManager::PermissionException.new("move repo_item failed. You don't have the permission to delete the repo_item '#{repo_item.name}'")
    end
    # If we don't want to change the owner, we look if we can_update
    if target.owner == repo_item.owner && !can_update?(repo_item)
      repo_item.errors.add(:move, I18n.t('repository_manager.errors.repo_item.move.no_permission'))
      raise RepositoryManager::PermissionException.new("move repo_item failed. You don't have the permission to update the '#{repo_item.name}'")
    end
    # We check if we can_create in the source_folder
    unless can_create?(target)
      repo_item.errors.add(:move, I18n.t('repository_manager.errors.repo_item.move.no_permission'))
      raise RepositoryManager::PermissionException.new("move repo_item failed. You don't have the permission to create in the source_folder '#{options[:source_folder].name}'")
    end
  else
    # Else if there is no source_folder, we check if we can delete the repo_item, if the owner change
    if self != repo_item.owner && !can_delete?(repo_item)
      repo_item.errors.add(:move, I18n.t('repository_manager.errors.repo_item.move.no_permission'))
      raise RepositoryManager::PermissionException.new("move repo_item failed. You don't have the permission to delete the repo_item '#{repo_item.name}'")
    end
  end
  # If it has the permission, we move the repo_item in the source_folder
  repo_item.move!(source_folder: target)
end

#remove_members_from(sharing, members) ⇒ Object



499
500
501
502
503
504
505
# File 'lib/repository_manager/has_repository.rb', line 499

def remove_members_from(sharing, members)
  begin
    remove_members_from!(sharing, members)
  rescue RepositoryManager::PermissionException
    false
  end
end

#remove_members_from!(sharing, members) ⇒ Object

Remove members in the sharing Param member could be an object or an array of object



490
491
492
493
494
495
496
497
# File 'lib/repository_manager/has_repository.rb', line 490

def remove_members_from!(sharing, members)
  if can_remove_from?(sharing)
    sharing.remove_members(members)
  else
    sharing.errors.add(:remove, I18n.t('repository_manager.errors.sharing.remove.no_permission'))
    raise RepositoryManager::PermissionException.new("remove members failed. You don't have the permission to remove a member on this sharing")
  end
end

#rename_repo_item(repo_item, new_name) ⇒ Object

Rename the repo_item with the new_name



307
308
309
310
311
312
313
# File 'lib/repository_manager/has_repository.rb', line 307

def rename_repo_item(repo_item, new_name)
  begin
    rename_repo_item!(repo_item, new_name)
  rescue RepositoryManager::PermissionException
    false
  end
end

#rename_repo_item!(repo_item, new_name) ⇒ Object

Rename the repo_item with the new_name



298
299
300
301
302
303
304
# File 'lib/repository_manager/has_repository.rb', line 298

def rename_repo_item!(repo_item, new_name)
  unless can_update?(repo_item)
    repo_item.errors.add(:rename, I18n.t('repository_manager.errors.repo_item.rename.no_permission'))
    raise RepositoryManager::PermissionException.new("rename repo_item failed. You don't have the permission to update the repo_item '#{repo_item.name}'")
  end
  repo_item.rename!(new_name)
end

#repo_item_name_exist_in_root?(name) ⇒ Boolean

Returns true of false if the name exist in the root path of this instance

Returns:

  • (Boolean)


513
514
515
# File 'lib/repository_manager/has_repository.rb', line 513

def repo_item_name_exist_in_root?(name)
  RepoItem.where('name = ?', name).where(owner: self).where(ancestry: nil).first ? true : false
end

#share(repo_item, members, options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/repository_manager/has_repository.rb', line 89

def share(repo_item, members, options = {})
  begin
    share!(repo_item, members, options)
  rescue RepositoryManager::PermissionException, RepositoryManager::NestedSharingException, RepositoryManager::RepositoryManagerException
    false
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
    repo_item.errors.add(:sharing, I18n.t('repository_manager.errors.sharing.create.not_created'))
    false
  end
end

#share!(repo_item, members, options = {}) ⇒ Object

Sharing the repo_item with the members, with the options options contains :

<tt>:can_read</tt> - Member can download the repo_item
<tt>:can_create</tt> - Member can create a new repo_item on it
<tt>:can_edit</tt> - Member can edit the repo_item
<tt>:can_delete</tt> - Member can delete the repo_item
<tt>:can_share</tt> - Member can share the repo_item

options contains :

<tt>:can_add</tt> - Specify if the member can add objects to the sharing
<tt>:can_remove</tt> - Specify if the member can remove object to the sharing


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
# File 'lib/repository_manager/has_repository.rb', line 46

def share!(repo_item, members, options = {})

  # Nested sharing are not accepted
  if !RepositoryManager.accept_nested_sharing
    # Check if no other sharing exist in the path
    unless repo_item.can_be_shared_without_nesting?
      repo_item.errors.add(:sharing, I18n.t('repository_manager.errors.sharing.create.nested_sharing'))
      raise RepositoryManager::NestedSharingException.new("sharing failed. Another sharing already exist on the subtree or an ancestor of '#{repo_item.name}'")
    end
  end

  permissions = get_permissions(repo_item)

  # Here we look if the instance has the permission for making a sharing
  if can_share?(nil, permissions)

    # We put the default options
    repo_item_permissions = RepositoryManager.default_repo_item_permissions
    sharing_permissions = RepositoryManager.default_sharing_permissions

    # If there is options, we have to take it
    repo_item_permissions = options[:repo_item_permissions] if options[:repo_item_permissions]
    sharing_permissions = options[:sharing_permissions] if options[:sharing_permissions]

    # Correct the item permission with accepted permissions
    repo_item_permissions = make_repo_item_permissions(repo_item_permissions, permissions)

    sharing = RepositoryManager::Sharing.new(repo_item_permissions)
    sharing.owner = self
    sharing.creator = options[:creator]

    sharing.add_members(members, sharing_permissions)

    repo_item.sharings << sharing
    repo_item.save!
    sharing
  else
    # No permission => No sharing
    repo_item.errors.add(:sharing, I18n.t('repository_manager.errors.sharing.create.no_permission'))
    raise RepositoryManager::PermissionException.new("sharing failed. You don't have the permission to share the repo_item '#{repo_item.name}'")
  end
end