Ruby on Rails plugin (gem) for managing repositories (files/folders/permissions/sharing).
Repository Manager 
This gem add functionalities to manage files and folders (repositories). Each instance (users, groups, etc..) has it own repository (with files and folders). It can manage them (create, edit, remove, copy, move, etc) and share them with other objects.
This project is based on the need for a repository manager system for Collaide. A system for easily create/delete files and folders in a repository. For sharing these "repo items" easily with other object with a flexible and complete permissions management.
Instead of creating my core repository manager system heavily dependent on our development, I'm trying to implement a generic and potent repository gem.
After looking for a good gem to use I noticed the lack of repository gems and flexibility in them. Repository Manager tries to be the more easy, light and flexible possible.
This gem is my informatics project for the Master in University of Lausanne (CH).
Installation
Add to your Gemfile:
gem 'repository-manager'
Then run:
$ bundle update
Run install script:
$ rails g repository_manager:install
And don't forget to migrate your database:
$ rake db:migrate
Settings
You can edit the RepositoryManager settings in the initializer (config/initializer/repository_manager.rb).
RepositoryManager.setup do |config|
config.default_repo_item_permissions = { can_read: true, can_create: false, can_update: false, can_delete: false, can_sharing: false }
config.default_sharing_permissions = { can_add: false, can_remove: false }
config.has_paper_trail = false
config.auto_overwrite_item = false
end
For instance, if you want that a default sharing is totally free (for edit, delete, etc), just put all default parameters to true
. We want that paper_trail is activate:
RepositoryManager.setup do |config|
config.default_repo_item_permissions = { can_read: true, can_create: true, can_update: true, can_delete: true, can_share: true }
config.default_sharing_permissions = { can_add: true, can_remove: true }
config.has_paper_trail = true
end
NOTE : If you want to add paper_trail to the repo_item model. You first have to install PaperTrail in you project.
More informations on the documentation of the PaperTrail gem.
See the chapter Permissions for more details about the permissions.
See the chapter Overwrite for more details about the overwrite.
Preparing your models
You can choose witch model can have repository.
In your model:
class User < ActiveRecord::Base
has_repository
end
You are not limited to the User model. You can use RepositoryManager in any other model and use it in several different models. If you have Groups and Houses in your application and you want to exchange repo_items
as if they were the same, just add has_repository
to each one and you will be able to sharing repo_files
/repo_folders
groups-groups, groups-users, users-groups and users-users. Of course, you can extend it for as many classes as you need.
Example:
class User < ActiveRecord::Base
has_repository
end
class Group < ActiveRecord::Base
has_repository
end
How to use Repository Manager
Introduction
A repo_item
is an item in a repository, it can be:
- A file (
repo_file
, class name : RepositoryManager::RepoFile
)
- A folder (
repo_folder
, class name : RepositoryManager::RepoFolder
).
A folder can contains files and folders. Like in a real tree files and folders.
A few methods are written in those two ways :
- method(arg, options)
- method!(arg, options) (note the "!")
The two methods do the same, but the one with the "!" returns an Exception error if it is a problem (PermissionException or RepositoryManagerException for instance) and the method without "!" return false if it has a problem.
How can I manage a repo_item (file or folder)
You just have to call the has_repository
methods create_file
, create_folder
, move_repo_item
, copy_repo_item
, rename_repo_item
or delete_repo_item
.
source_folder = user1.create_folder('Root folder')
the_new_folder = user1.create_folder('The new folder', source_folder: source_folder)
user1.create_file(params[:file], source_folder: the_new_folder)
user1.create_file(File.open('somewhere'), source_folder: the_new_folder)
repo_file = RepositoryManager::RepoFile.new
repo_file.file = your_file
user1.create_file(repo_file, source_folder: the_new_folder)
file2 = user1.create_file(params[:file2], filename: 'specific_name.jpg')
test_folder = user1.create_folder('Test folder')
user1.move_repo_item(the_new_folder, source_folder: test_folder)
user1.rename_repo_item(the_new_folder, 'The renamed folder')
user1.copy_repo_item(source_folder, source_folder: test_folder)
user1.delete_repo_item(test_folder)
user1.delete_repo_item(file2)
Owner and Sender
If a user (sender of the item) send a file or folder into a group (owner of this item), you can specify the owner and the sender like this :
folder = group1.create_folder('Folder created by user1', sender: user1)
folder.owner
folder.sender
file = group1.create_file(params[:file], source_folder: folder, sender: user1)
file.owner
file.sender
WARNING : There is no verification if the user1 has the permission to create a file or folder into this group. You have to check this in your controller ! The fact that user1 is the sender of this folder gives him NO MORE PERMISSION on it !
Unzip an archive
You can send a zipped file in your application and unzip it on it. Repository Manager will automaticaly create the Repo Items
needed. This is very usefull when you want to send a folder (with sub files and folders), just zip it, send it, and unzip it in the application. You can use the has_repository
method unzip_repo_item(repo_item, options)
.
repo_file_archive = @user.create_file(the_file_sended)
@user.unzip_repo_item(repo_file_archive)
@group.unzip_repo_item(repo_file_archive, source_folder: target_folder, overwrite: true, sender: @user)
Overwrite
You can overwrite a file or a folder. You juste have to pass the overwrite: true
option in method : create_file
, create_folder
, copy_repo_item
, move_repo_item
. See those examples below :
@user.create_file(the_new_file, source_folder: specific_folder, overwrite: true)
@user.create_folder("Folder Name", overwrite: true)
NOTE : If you overwrite a folder, the old folder will be destroyed ! If you overwrite a file, the existing file will be updated with the new file (better for versioning).
How can I share a repo_item (file/folder)
Now, user1 want to share his folder 'The new folder' with a Group object group1
and another User object user2
. You can use the has_repository
method share_repo_item(repo_item, member, options = nil)
.
members = []
members << group1
members << user2
sharing = user1.share_repo_item(the_new_folder, members)
options = {
sharing_permissions: {
can_add: true,
can_remove: false
},
repo_item_permissions: {
can_read: true,
can_create: true,
can_update: true,
can_delete: false,
can_share: true
}
}
sharing = user1.share_repo_item(the_new_folder, members, options)
repo_item_permissions
specifies what kind of permissions you give on the repo_item in a specific sharing.
sharing_permissions
specifies if the member of the sharing can add or remove other members in this sharing.
See the chapter Permissions for more details.
Repository Manager and the nested sharing
Repository Manager actually don't accept nested sharing.
parent = @user1.create_folder('Parent')
nested = @user1.create_folder('Nested', parent)
children = @user1.create_folder('Children', nested)
@user1.share_repo_item(nested, @user2)
nested.can_be_shared_without_nesting?
parent.can_be_shared_without_nesting?
children.can_be_shared_without_nesting?
@user1.share_repo_item(parent, @user2)
@user1.share_repo_item!(parent, @user2)
@user1.share_repo_item!(children, @user2)
How can I see my repo_items
You can have two kind of repo_items:
- Your own repo_items
- The repo_items shared with you.
You can get all the items of only these who are in the root.
user1.repo_items.all
user2.root_repo_items.all
user2.shared_repo_items.all
user2.root_shared_repo_items.all
If you only want to have the folders or the files, you can do it like that:
user1.repo_items.folders.to_a
user2.shared_repo_items.files.to_a
Recall: a repo_item can be:
if repo_item.is_folder?
repo_item.name
elsif repo_item.is_file?
repo_item.name
repo_item.file.url
repo_item.file.current_path
else
end
For file details, more infos on the documentation of the carrierwave gem.
How can I manage a sharing
If it has the permission, an object can add members to a sharing.
user1.can_add_to?(sharing)
members = []
members << user3
members << group2
...
user1.add_members_to(sharing, members)
options = {can_add: true, can_remove: false}
user1.add_members_to(sharing, members, options)
group2.can_add_to?(sharing)
group2.can_remove_from?(sharing)
If an object has the permission, it can remove members from a sharing, else the method return false
(or raise an PermissionException if you user the remove_members_from!
method).
user1.remove_members_from(sharing, group2)
You can directly work with the sharing
. Be careful, there is NO permission control !
sharing.add_members(member)
sharing.add_members(member, {can_add: true, can_remove: false})
sharing.remove_members([user2, group1])
Permissions
Repository permissions
The owner of a repo_item
(file or folder) has all the permissions on it. When he share this repo_item
, he can choose what permission he gives to the share. The permissions are :
can_read?(repo_item)
: The member can read (=download) this file/folder.
can_create?(repo_item)
: Can create in the repo_item (Note: if repo_item is nil (= own root), always true).
can_update?(repo_item)
: Can update a repo_item (ex: rename).
can_delete?(repo_item)
: Can delete a repo_item.
can_share?(repo_item)
: Can share a repo_item.
To check if a user has one of this permission, you just have to write : user1.can_read?(repo_item)
, user1.can_share?(repo_item)
, etc (it returns true
or false
).
NOTICE : An object who can share a repo_item, can't set new permissions that it doesn't have.
For instance: user3
has a sharing
of repo_item1
in which it :can_delete => false
and :can_share => true
. He can share repo_item1
with user4
, but he can't put :can_delete => true
in the repo_item_permission
of this new share.
You can get all the permissions of an object
in a repo_item
with this method: object.get_permissions(repo_item)
def get_permissions(repo_item = nil)
[...]
end
Sharing permissions
You can manage the permissions of a member in a sharing. The owner of the sharing has all the permissions. The sharing permissions are:
can_add_to?(sharing)
: The member can add a new instance in this sharing.
can_remove_from?(sharing)
: Can remove an instance from this sharing.
To check if the object can add or remove an instance in the sharing, just write : group1.can_add_to?(sharing)
or group1.can_remove_from?(sharing)
(it returns true
or false
).
Like the repo_item permissions, you can get the sharing permissions of an object
in a sharing
with : object.get_sharing_permissions(sharing)
.
Download a repository
RepositoryManager make the download of a repo_item
easy. If the user want to download a file, use the has_repository
method : download_repo_item
. This method returns you the path of the file (if the user can_read
it).
If the repo_item
is a file, the method returns you the path of this file.
If the repo_item
is a folder, it automatically generates a zip file with all the constant that the user can_read
. The method returns the path of this zip file.
path_to_file = user1.download_repo_item(the_file)
send_file path_to_file, filename: the_file.name
path_to_zip = user1.download_repo_item(the_folder)
send_file path_to_zip
user1.delete_download_path()
You can directly download the folder (without permission control):
path = the_folder.download
the_folder.delete_zip
Errors handling
When an error happen, you (and the user also) want to know what is the source of the problem. I tried to make it the most simple as possible.
For the two has_repository
methods create_file
and create_folder
, the errors are pushed into the options
hash parameter with the key errors
(options[:errors]
)
options = {source_folder: repo_item, sender: current_user}
if @item = @group.create_folder(params[:repo_folder][:name], options)
redirect_to back, notice: 'Folder created'
else
redirect_to back, alert: options[:errors].first
options[:errors]
options = {source_folder: repo_item, sender: current_user}
if @item = @group.create_file(params[:repo_file][:file], options)
redirect_to back, notice: 'File created'
else
redirect_to back, alert: options[:errors].first
options[:errors]
For the other has_repository
methods, the errors are added in the first object passed in parameter (for instance: repo_item
or sharing
)
if @group.delete_repo_item(@repo_item)
redirect_to :back, notice: 'Item deleted'
else
redirect_to :back, alert: repo_item.errors.messages[:delete].first
end
Documentation
For more documentation go on the Repository Manager Doc.
TODO
- Write the methods : share_link.
- Versioning
- Snapshot the file if possible
- ...
License
This project rocks and uses MIT-LICENSE.
Created by Yves Baumann.