Module: RepositoryManager::HasRepository::LocalInstanceMethods
- Defined in:
- lib/repository_manager/has_repository.rb
Instance Method Summary collapse
- #add_members_to(sharing, members, options = RepositoryManager.default_sharing_permissions) ⇒ Object
-
#add_members_to!(sharing, members, options = RepositoryManager.default_sharing_permissions) ⇒ Object
You can here add new members in the sharing Param member could be an object or an array of object.
-
#can_add_to?(sharing) ⇒ Boolean
Return true if you can add a member in this sharing, false else.
-
#can_create?(repo_item, permissions = nil) ⇒ Boolean
Return true if you can create in the repo, false else.
-
#can_delete?(repo_item, permissions = nil) ⇒ Boolean
Returns true if you can delete the repo, false else.
-
#can_download?(repo_item, permissions = nil) ⇒ Boolean
Return true if you can download the repo, else false Read = Download for the moment.
-
#can_read?(repo_item, permissions = nil) ⇒ Boolean
Return true if you can read the repo, else false.
-
#can_remove_from?(sharing) ⇒ Boolean
Return true if you can remove a member in this sharing, false else.
-
#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.
-
#can_update?(repo_item, permissions = nil) ⇒ Boolean
Returns true if you can edit the repo, false else.
- #copy_repo_item(repo_item, target = nil, options = {}) ⇒ Object
-
#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).
- #create_file(file, options = {}) ⇒ Object
-
#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).
-
#create_folder(name = '', options = {}) ⇒ Object
Like create_folder! Returns false if the folder is not created instead of an exception.
-
#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.
-
#delete_download_path ⇒ Object
Delete the download folder of the user.
- #delete_repo_item(repo_item) ⇒ Object
-
#delete_repo_item!(repo_item) ⇒ Object
Delete the repo_item.
- #download(repo_item, options = {}) ⇒ Object
-
#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.
-
#get_default_download_path(prefix = 'download/') ⇒ Object
Get the download path of the member.
-
#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).
-
#get_sharing_permissions(sharing) ⇒ Object
Return the permissions of the sharing (can_add, can_remove).
- #move_repo_item(repo_item, target = nil) ⇒ Object
-
#move_repo_item!(repo_item, target = nil) ⇒ Object
Move the repo_item.
- #remove_members_from(sharing, members) ⇒ Object
-
#remove_members_from!(sharing, members) ⇒ Object
You can here remove members in the sharing Param member could be an object or an array of object.
-
#rename_repo_item(repo_item, new_name) ⇒ Object
Rename the repo_item with the new_name.
-
#rename_repo_item!(repo_item, new_name) ⇒ Object
Rename the repo_item with the new_name.
-
#repo_item_name_exist_in_root?(name) ⇒ Boolean
Returns true of false if the name exist in the root path of this instance.
- #share(repo_item, members, options = {}) ⇒ Object
-
#share!(repo_item, members, options = {}) ⇒ Object
Sharing the repo_item with the members, with the options options contains :
:can_read- Member can download the repo_item:can_create- Member can create a new repo_item on it:can_edit- Member can edit the repo_item:can_delete- Member can delete the repo_item:can_share- Member can share the repo_item options contains ::can_add- Specify if the member can add objects to the sharing:can_remove- Specify if the member can remove object to the sharing.
Instance Method Details
#add_members_to(sharing, members, options = RepositoryManager.default_sharing_permissions) ⇒ Object
439 440 441 442 443 444 445 |
# File 'lib/repository_manager/has_repository.rb', line 439 def add_members_to(sharing, members, = RepositoryManager.) begin add_members_to!(sharing, members, = RepositoryManager.) rescue RepositoryManager::PermissionException false end end |
#add_members_to!(sharing, members, options = RepositoryManager.default_sharing_permissions) ⇒ Object
You can here add new members in the sharing Param member could be an object or an array of object
429 430 431 432 433 434 435 436 437 |
# File 'lib/repository_manager/has_repository.rb', line 429 def add_members_to!(sharing, members, = RepositoryManager.) = (sharing) if can_add_to?(sharing) = (, ) sharing.add_members(members, ) else 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
418 419 420 |
# File 'lib/repository_manager/has_repository.rb', line 418 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
387 388 389 |
# File 'lib/repository_manager/has_repository.rb', line 387 def can_create?(repo_item, = nil) can_do?('create', repo_item, ) end |
#can_delete?(repo_item, permissions = nil) ⇒ Boolean
Returns true if you can delete the repo, false else
397 398 399 |
# File 'lib/repository_manager/has_repository.rb', line 397 def can_delete?(repo_item, = nil) can_do?('delete', repo_item, ) end |
#can_download?(repo_item, permissions = nil) ⇒ Boolean
Return true if you can download the repo, else false Read = Download for the moment
382 383 384 |
# File 'lib/repository_manager/has_repository.rb', line 382 def can_download?(repo_item, = nil) can_do?('read', repo_item, ) end |
#can_read?(repo_item, permissions = nil) ⇒ Boolean
Return true if you can read the repo, else false
376 377 378 |
# File 'lib/repository_manager/has_repository.rb', line 376 def can_read?(repo_item, = nil) can_do?('read', repo_item, ) end |
#can_remove_from?(sharing) ⇒ Boolean
Return true if you can remove a member in this sharing, false else
423 424 425 |
# File 'lib/repository_manager/has_repository.rb', line 423 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
371 372 373 |
# File 'lib/repository_manager/has_repository.rb', line 371 def can_share?(repo_item, = nil) can_do?('share', repo_item, ) end |
#can_update?(repo_item, permissions = nil) ⇒ Boolean
Returns true if you can edit the repo, false else
392 393 394 |
# File 'lib/repository_manager/has_repository.rb', line 392 def can_update?(repo_item, = nil) can_do?('update', repo_item, ) end |
#copy_repo_item(repo_item, target = nil, options = {}) ⇒ Object
351 352 353 354 355 356 357 |
# File 'lib/repository_manager/has_repository.rb', line 351 def copy_repo_item(repo_item, target = nil, = {}) begin copy_repo_item!(repo_item, target, ) rescue RepositoryManager::PermissionException, RepositoryManager::RepositoryManagerException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved 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)
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 331 def copy_repo_item!(repo_item, target = nil, = {}) unless can_read?(repo_item) 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) 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: [:sender]) end |
#create_file(file, options = {}) ⇒ Object
214 215 216 217 218 219 220 |
# File 'lib/repository_manager/has_repository.rb', line 214 def create_file(file, = {}) begin create_file!(file, ) rescue RepositoryManager::PermissionException, RepositoryManager::RepositoryManagerException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved 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)
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
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 |
# File 'lib/repository_manager/has_repository.rb', line 175 def create_file!(file, = {}) source_folder = [: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 repo_file.owner = self repo_file.sender = [: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::RepositoryManagerException.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
140 141 142 143 144 145 146 |
# File 'lib/repository_manager/has_repository.rb', line 140 def create_folder(name = '', = {}) begin create_folder!(name, ) rescue RepositoryManager::PermissionException, RepositoryManager::RepositoryManagerException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved 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
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 |
# File 'lib/repository_manager/has_repository.rb', line 103 def create_folder!(name = '', = {}) source_folder = [: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 = [: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::RepositoryManagerException.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_path ⇒ Object
Delete the download folder of the user
360 361 362 |
# File 'lib/repository_manager/has_repository.rb', line 360 def delete_download_path FileUtils.rm_rf(self.get_default_download_path()) end |
#delete_repo_item(repo_item) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/repository_manager/has_repository.rb', line 157 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
149 150 151 152 153 154 155 |
# File 'lib/repository_manager/has_repository.rb', line 149 def delete_repo_item!(repo_item) if can_delete?(repo_item) repo_item.destroy else 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
263 264 265 266 267 268 269 |
# File 'lib/repository_manager/has_repository.rb', line 263 def download(repo_item, = {}) begin download!(repo_item, ) 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'
253 254 255 256 257 258 259 260 261 |
# File 'lib/repository_manager/has_repository.rb', line 253 def download!(repo_item, = {}) if can_download?(repo_item) path = [:path] if [:path] repo_item.download!({object: self, path: path}) else 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 = 'download/') ⇒ Object
Get the download path of the member
466 467 468 |
# File 'lib/repository_manager/has_repository.rb', line 466 def get_default_download_path(prefix = '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)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/repository_manager/has_repository.rb', line 227 def (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)
365 366 367 |
# File 'lib/repository_manager/has_repository.rb', line 365 def (sharing) sharing.(self) end |
#move_repo_item(repo_item, target = nil) ⇒ Object
319 320 321 322 323 324 325 |
# File 'lib/repository_manager/has_repository.rb', line 319 def move_repo_item(repo_item, target = nil) begin move_repo_item!(repo_item, target) rescue RepositoryManager::RepositoryManagerException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved false end end |
#move_repo_item!(repo_item, target = nil) ⇒ Object
Move the repo_item. If you let all options empty, the item is moving into the self.root target => move into this source_folder if target == nil, move to the root
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/repository_manager/has_repository.rb', line 291 def move_repo_item!(repo_item, target = nil) if !can_read?(repo_item) 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) 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) 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) raise RepositoryManager::PermissionException.new("move repo_item failed. You don't have the permission to create in the source_folder '#{[: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) 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
457 458 459 460 461 462 463 |
# File 'lib/repository_manager/has_repository.rb', line 457 def remove_members_from(sharing, members) begin remove_members_from!(sharing, members) rescue RepositoryManager::PermissionException false end end |
#remove_members_from!(sharing, members) ⇒ Object
You can here remove members in the sharing Param member could be an object or an array of object
449 450 451 452 453 454 455 |
# File 'lib/repository_manager/has_repository.rb', line 449 def remove_members_from!(sharing, members) if can_remove_from?(sharing) sharing.remove_members(members) else 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
280 281 282 283 284 285 286 |
# File 'lib/repository_manager/has_repository.rb', line 280 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
272 273 274 275 276 277 |
# File 'lib/repository_manager/has_repository.rb', line 272 def rename_repo_item!(repo_item, new_name) unless can_update?(repo_item) 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
471 472 473 |
# File 'lib/repository_manager/has_repository.rb', line 471 def repo_item_name_exist_in_root?(name) RepoItem.where('name = ? OR file = ?', name, name).where(owner: self).where(ancestry: nil).first ? true : false end |
#share(repo_item, members, options = {}) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/repository_manager/has_repository.rb', line 87 def share(repo_item, members, = {}) begin share!(repo_item, members, ) rescue RepositoryManager::PermissionException, RepositoryManager::NestedSharingException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved 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 |
# File 'lib/repository_manager/has_repository.rb', line 46 def share!(repo_item, members, = {}) # 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? raise RepositoryManager::NestedSharingException.new("sharing failed. Another sharing already exist on the subtree or an ancestor of '#{repo_item.name}'") end end = (repo_item) # Here we look if the instance has the permission for making a sharing if can_share?(nil, ) # We put the default options = RepositoryManager. = RepositoryManager. # If there is options, we have to take it = [:repo_item_permissions] if [:repo_item_permissions] = [:sharing_permissions] if [:sharing_permissions] # Correct the item permission with accepted permissions = (, ) sharing = RepositoryManager::Sharing.new() sharing.owner = self sharing.creator = [:creator] sharing.add_members(members, ) repo_item.sharings << sharing repo_item.save! sharing else # No permission => No sharing raise RepositoryManager::PermissionException.new("sharing failed. You don't have the permission to share the repo_item '#{repo_item.name}'") end end |