repository-manager 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/Gemfile.lock +9 -12
  2. data/README.md +152 -115
  3. data/app/models/{app_file.rb → repo_file.rb} +3 -3
  4. data/app/models/repo_folder.rb +60 -0
  5. data/app/models/{repository.rb → repo_item.rb} +10 -10
  6. data/app/models/sharing.rb +58 -0
  7. data/app/models/sharings_member.rb +6 -0
  8. data/app/uploaders/{repository_uploader.rb → repo_file_uploader.rb} +1 -1
  9. data/db/migrate/20131018214212_create_repository_manager.rb +9 -17
  10. data/lib/generators/repository_manager/install_generator.rb +1 -3
  11. data/lib/generators/repository_manager/templates/initializer.rb +5 -10
  12. data/lib/repository-manager.rb +4 -4
  13. data/lib/repository_manager/engine.rb +0 -1
  14. data/lib/repository_manager/has_repository.rb +140 -129
  15. data/lib/repository_manager/version.rb +1 -1
  16. data/repository-manager.gemspec +4 -5
  17. data/spec/dummy/config/locales/en.yml +1 -8
  18. data/spec/dummy/db/development.sqlite3 +0 -0
  19. data/spec/dummy/db/migrate/20131016193722_create_users.rb +11 -11
  20. data/spec/dummy/db/migrate/20131016193834_create_groups.rb +10 -10
  21. data/spec/dummy/db/migrate/20131016194207_create_groups_users.rb +5 -5
  22. data/spec/dummy/db/migrate/20131018214212_create_repository_manager.rb +9 -17
  23. data/spec/dummy/db/schema.rb +16 -16
  24. data/spec/dummy/db/test.sqlite3 +0 -0
  25. data/spec/factories/{app_file.rb → repo_file.rb} +1 -1
  26. data/spec/factories/{folder.rb → repo_folder.rb} +1 -1
  27. data/spec/has_repository_spec.rb +107 -107
  28. data/spec/models/associations_spec.rb +45 -45
  29. data/spec/models/repository_spec.rb +51 -27
  30. data/spec/models/share_spec.rb +39 -39
  31. metadata +15 -37
  32. data/app/models/folder.rb +0 -54
  33. data/app/models/share.rb +0 -58
  34. data/app/models/shares_item.rb +0 -10
  35. data/db/migrate/20131025085844_add_file_to_repositories.rb +0 -8
  36. data/db/migrate/20131025085845_add_file_to_files.rb +0 -5
  37. data/spec/dummy/db/migrate/20131025085844_add_file_to_repositories.rb +0 -8
  38. data/spec/dummy/db/migrate/20131025085845_add_file_to_files.rb +0 -5
data/Gemfile.lock CHANGED
@@ -1,10 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- repository-manager (0.0.6)
4
+ repository-manager (0.0.7)
5
5
  ancestry
6
6
  carrierwave
7
- enumerize
8
7
  rails (> 3.0.0)
9
8
  rubyzip
10
9
 
@@ -44,14 +43,12 @@ GEM
44
43
  activemodel (>= 3.2.0)
45
44
  activesupport (>= 3.2.0)
46
45
  json (>= 1.7)
47
- diff-lcs (1.2.4)
48
- enumerize (0.7.0)
49
- activesupport (>= 3.2)
46
+ diff-lcs (1.2.5)
50
47
  erubis (2.7.0)
51
- factory_girl (4.2.0)
48
+ factory_girl (4.3.0)
52
49
  activesupport (>= 3.0.0)
53
- factory_girl_rails (4.2.1)
54
- factory_girl (~> 4.2.0)
50
+ factory_girl_rails (4.3.0)
51
+ factory_girl (~> 4.3.0)
55
52
  railties (>= 3.0.0)
56
53
  hike (1.2.3)
57
54
  i18n (0.6.5)
@@ -80,10 +77,10 @@ GEM
80
77
  rake (>= 0.8.7)
81
78
  thor (>= 0.18.1, < 2.0)
82
79
  rake (10.1.0)
83
- rspec-core (2.14.5)
84
- rspec-expectations (2.14.3)
80
+ rspec-core (2.14.7)
81
+ rspec-expectations (2.14.4)
85
82
  diff-lcs (>= 1.1.3, < 2.0)
86
- rspec-mocks (2.14.3)
83
+ rspec-mocks (2.14.4)
87
84
  rspec-rails (2.14.0)
88
85
  actionpack (>= 3.0)
89
86
  activesupport (>= 3.0)
@@ -91,7 +88,7 @@ GEM
91
88
  rspec-core (~> 2.14.0)
92
89
  rspec-expectations (~> 2.14.0)
93
90
  rspec-mocks (~> 2.14.0)
94
- rubyzip (0.9.9)
91
+ rubyzip (1.1.0)
95
92
  sprockets (2.10.0)
96
93
  hike (~> 1.2)
97
94
  multi_json (~> 1.0)
data/README.md CHANGED
@@ -1,17 +1,18 @@
1
1
  WORK IN PROGRESS, but it already works !
2
2
 
3
- Ruby on Rails plugin (gem) for managing repositories (files/folders/permissions).
3
+ Ruby on Rails plugin (gem) for managing repositories (files/folders/permissions/sharing).
4
4
 
5
5
  # RepositoryManager
6
6
 
7
- This project is based on the need for a repository manager system for [Collaide](https://github.com/facenord-sud/collaide). A system for easily create/delete files and folders in a repository. For share these repositories easily with other object with a flexible and complete authorisations management.
7
+ This gem add fonctionalities to manage repositories. Each instance (users, groups, etc..) can have it own repository (with files and folders). It can manage them (edit, remove, add, etc) and share them with other objects.
8
8
 
9
- Instead of creating my core repository manager system heavily
9
+ This project is based on the need for a repository manager system for [Collaide](https://github.com/facenord-sud/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 authorisations management.
10
+
11
+ Instead of creating my core repository manager system heavily
10
12
  dependent on our development, I'm trying to implement a generic and potent repository gem.
11
13
 
12
14
  After looking for a good gem to use I noticed the lack of repository gems
13
15
  and flexibility in them. RepositoryManager tries to be the more flexible possible.
14
- Each instance (users, groups, etc..) can have it own repositories (with files and folders). It can manage them easily (edit, remove, add, etc) and share them with other instance.
15
16
 
16
17
  This gem is my informatics project for the Master in [University of Lausanne (CH)](http://www.unil.ch/index.html).
17
18
 
@@ -42,6 +43,32 @@ And don't forget to migrate your database:
42
43
  $ rake db:migrate
43
44
  ```
44
45
 
46
+ ## Settings
47
+
48
+ You can edit the RepositoryManager settings in the initializer (/config/initializer/repository_manager.rb).
49
+
50
+ ```ruby
51
+ RepositoryManager.setup do |config|
52
+
53
+ # Default permissions that an object has on the repo_item after a sharing.
54
+ config.default_repo_item_permissions = { can_read: true, can_create: false, can_update: false, can_delete: false, can_sharing: false }
55
+
56
+ # Default permissions that an object has when he is added in a sharing.
57
+ config.default_sharing_permissions = { can_add: false, can_remove: false }
58
+ end
59
+ ```
60
+
61
+ For instance, if you want that a default sharing is totaly free, just put all default parameters to `true` :
62
+ ```ruby
63
+ RepositoryManager.setup do |config|
64
+ config.default_repo_item_permissions = { can_read: true, can_create: true, can_update: true, can_delete: true, can_share: true }
65
+ config.default_sharing_permissions = { can_add: true, can_remove: true }
66
+ end
67
+ ```
68
+
69
+
70
+ See the chapter [Authorisations](#authorisations) for more details about the permissions.
71
+
45
72
  ## Preparing your models
46
73
 
47
74
  You can choose wich model can have repository.
@@ -54,7 +81,7 @@ class User < ActiveRecord::Base
54
81
  end
55
82
  ```
56
83
 
57
- You are not limited to the User model. You can use RepositoryManager in any other model and use it in serveral different models. If you have Groups and Houses in your application and you want to exchange repositories as if they were the same, just add `has_repository` to each one and you will be able to share files/folders groups-groups, groups-users, users-groups and users-users. Of course, you can extend it for as many classes as you need.
84
+ You are not limited to the User model. You can use RepositoryManager in any other model and use it in serveral 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.
58
85
 
59
86
  Example:
60
87
 
@@ -72,9 +99,17 @@ end
72
99
 
73
100
  ## How to use RepositoryManager
74
101
 
75
- ### How can I create/delete a repository (file or folder)
102
+ ### Introduction
103
+
104
+ A `repo_item` is an item in a repository, it can be:
105
+ - A file (`repo_file`, class name : `RepoFile`)
106
+ - A folder (`repo_folder`, class name : `RepoFolder`).
107
+
108
+ A folder can contains files and folders. Like in a real tree files and folders.
109
+
110
+ ### How can I create/delete a repo_item (file or folder)
76
111
 
77
- You just have to call the `has_repository` methods `create_file`, `create_folder`, or `delete_repository`.
112
+ You just have to call the `has_repository` methods `create_file`, `create_folder`, or `delete_repo_item`.
78
113
 
79
114
  ```ruby
80
115
  # user1 wants to create a folder in his repository
@@ -83,195 +118,197 @@ You just have to call the `has_repository` methods `create_file`, `create_folder
83
118
  source_folder = user1.create_folder('Root folder')
84
119
 
85
120
  # user1 own repository :
86
- # 'Root folder'
121
+ # -- 'Root folder'
87
122
 
88
- # sourceFolder is the directory in wich user1 wants to create the folder 'The new folder'
89
- name = 'The new folder'
90
- the_folder = user1.create_folder(name, source_folder)
123
+ # source_folder is the directory in wich user1 wants to create the folder 'The new folder'
124
+ the_new_folder = user1.create_folder('The new folder', source_folder)
91
125
 
92
126
  # user1 own repository :
93
- # 'Root folder'
94
- # 'The new folder'
127
+ # |-- 'Root folder'
128
+ # | |-- 'The new folder'
95
129
 
96
- # Now we want to add a file into the_folder
97
- # Note : user1 needs the ':can_create => true' permission in the folder : the_folder.
98
- user1.create_file(params[:file], the_folder)
130
+ # Now we want to add a file into the_new_folder
131
+ # Note : user1 needs the ':can_create => true' permission in the folder : the_new_folder (else the method returns `false`).
132
+ user1.create_file(params[:file], the_new_folder)
99
133
  # OR
100
- user1.create_file(File.open('somewhere'), the_folder)
134
+ user1.create_file(File.open('somewhere'), the_new_folder)
101
135
 
102
136
  # user1 own repository :
103
- # 'Root folder'
104
- # 'The new folder'
105
- # 'file'
137
+ # |-- 'Root folder'
138
+ # | |-- 'The new folder'
139
+ # | | |-- 'file'
106
140
 
141
+ # user1 want to create a file on the root of his repository
107
142
  file2 = user1.create_file(params[:file2])
108
143
 
109
144
  # user1 own repository :
110
- # 'Root folder'
111
- # 'The new folder'
112
- # 'file'
113
- # 'file2'
145
+ # |-- 'Root folder'
146
+ # | |-- 'The new folder'
147
+ # | | |-- 'file'
148
+ # |-- 'file2'
114
149
 
115
- # Delete a repository
116
- # Note : user1 needs the ':can_delete => true' permission in the folder : the_folder
117
- user1.delete_repository(the_folder)
150
+ # Delete a repo_item
151
+ # Note : user1 needs the ':can_delete => true' permission in the folder : the_new_folder (else the method returns `false`).
152
+ user1.delete_repo_item(the_new_folder)
118
153
 
119
154
  # user1 own repository :
120
- # 'Root folder'
121
- # 'file2'
155
+ # |-- 'Root folder'
156
+ # |-- 'file2'
122
157
 
123
- user1.delete_repository(file2)
158
+ user1.delete_repo_item(file2)
124
159
 
125
160
  # user1 own repository :
126
- # 'Root folder'
161
+ # |-- 'Root folder'
127
162
 
128
163
  ```
129
164
 
130
- ### How can I share a repository (file/folder)
165
+ ### How can I share a repo_item (file/folder)
131
166
 
132
- Now, user1 want to share his folder 'The new folder' with a Group object et another User object. You can use the `has_repository` method `share(repository, to, options = nil)`.
167
+ Now, user1 want to share his folder 'The new folder' with a Group object `group1` et another User object `user2`. You can use the `has_repository` method `share(repo_item, member, options = nil)`.
133
168
 
134
169
  ```ruby
135
- # user1 wants to share the_folder with group1 and user2
136
-
137
- items = []
138
- # You can add other instance (who has_repository) in this array to share with more than one instance
139
- item << group1
140
- items << user2
141
-
142
- share = user1.share(the_folder, items)
143
-
170
+ # user1 wants to share the_new_folder with group1 and user2
144
171
 
145
- # If you want to customize your share options, you can do it like this:
172
+ members = []
173
+ # You can add other instances (who `has_repository`) in this array to share with more than one instance
174
+ member << group1
175
+ members << user2
146
176
 
147
- # Default shares permisions are : {can_add: false, can_remove: false}
148
- share_permissions = {can_add: true, can_remove: true}
149
- # Default reposiroty permissions are: {can_read: true, can_create: false, can_update: false, can_delete: false, can_share: false}
150
- repo_permissions = {can_read: true, can_create: true, can_update: true, can_delete: true, can_share: true}
177
+ sharing = user1.share(the_new_folder, members)
151
178
 
152
- options = {share_permissions: share_permissions, repo_permissions: repo_permissions}
179
+ # If you want to customize your sharing options, you can do it like this:
180
+ options = {sharing_permissions: {can_add: true, can_remove: true}, repo_item_permissions: {can_read: true, can_create: true, can_update: true, can_delete: true, can_share: true}}
153
181
 
154
- share = user1.share(the_folder, items, options)
182
+ sharing = user1.share(the_new_folder, members, options)
155
183
  ```
156
184
 
157
- `share_permissions` specifies if the item who receive the share can add or remove items in this share.
185
+ `repo_item_permissions` specifies what kind of permissions you give on the repo_item in a specific sharing.
158
186
 
159
- `repo_permissions` specifies what kind of permission do you give at this share. If all the params are false, the share is useless, because the items have no more permissions in the repository selectionned.
187
+ `sharing_permissions` specifies if the member selectionned can add or remove other members in this sharing.
160
188
 
161
- See the chapter Authorisations for more details.
189
+ See the chapter [Authorisations](#authorisations) for more details.
162
190
 
163
- ### How can I see my repository
191
+ ### How can I see my repo_items
164
192
 
165
- There is two king of repository:
166
- - Your own repositories
167
- - The repositories shared with you.
193
+ You can have two kind of repo_items:
194
+ - Your own repo_items
195
+ - The repo_items shared with you.
168
196
 
169
197
  ```ruby
170
198
  # user1 want to get his own repository
171
- user1.repositories.all # => You get the repository that user1 has created
199
+ user1.repo_items.all # => You get the repo_items that user1 has created
172
200
 
173
- # user2 want to get his shared repository
174
- user2.shared_repositories.all
201
+ # user2 want to get his shared repo_items
202
+ user2.shared_repo_items.all
175
203
  ```
176
204
 
177
- A repository can be:
205
+ Recall: a repo_item can be:
178
206
  - A file
179
207
  - A folder
180
208
 
181
209
  ```ruby
182
- # We want to know if the object repository is a file or a folder:
183
- if repository.type == 'Folder'
184
- repository.name #=> Returns the name of the folder ('New folder').
185
- elsif repository.type == 'AppFile'
186
- repository.name #=> Returns the name of the file ('file.png').
210
+ # We want to know if the object repo_item is a file or a folder:
211
+ if repo_item.type == 'RepoFolder'
212
+ repo_item.name #=> Returns the name of the folder ('New folder').
213
+ elsif repo_item.type == 'RepoFile'
214
+ repo_item.name #=> Returns the name of the file ('file.png').
187
215
  # Here is the file
188
- repository.file.url # => '/url/to/file.png'
189
- repository.file.current_path # => 'path/to/file.png'
190
- repository.file.identifier # => 'file.png'
216
+ repo_item.file.url # => '/url/to/file.png'
217
+ repo_item.file.current_path # => 'path/to/file.png'
191
218
  end
192
219
  ```
193
220
 
221
+ For file informations, more infos on [the documentation of the carrierwave gem](https://github.com/carrierwaveuploader/carrierwave).
194
222
 
195
- ### How can I manage a share
196
223
 
224
+ ### How can I manage a sharing
197
225
 
198
- If it has the authorisation, an object can add items to a share.
226
+
227
+ If it has the authorisation, an object can add members to a sharing.
199
228
 
200
229
  ```ruby
201
- # user1 want to add items to his share (the actions are done only if user1 has the ':can_add' permission)
202
- user1.can_add_to?(share) # => true
230
+ # user1 want to add members to his sharing
231
+ # NOTE: The action is done only if user1 has the ':can_add' permission.
232
+ user1.can_add_to?(sharing) # => true
233
+
234
+ # Add members
235
+ members = []
236
+ members << user3
237
+ members << group2
238
+ ...
203
239
 
240
+ user1.add_members_to(sharing, members)
241
+
242
+ # You can change the default sharing permissions options :
204
243
  options = {can_add: true, can_remove: false}
205
- # Add items
206
- items = []
207
- items << user3
208
- items << group2
209
- ...
210
- user1.add_items_to(share, items, options)
244
+ user1.add_members_to(sharing, members, options)
211
245
 
212
- # Here user3 and group2 can add items in this share, but they can't remove an item.
213
- group2.can_add_to?(share) # => true
214
- group2.can_remove_from?(share) # => false
215
246
 
216
- # If user2 add an item in the share, he can choose if the permission ':can_add' is true or false, but he can't put ':can_remove' to true (because he don't have this permission himself).
247
+ # Here user3 and group2 can add members in this sharing, but they can't remove a member.
248
+ group2.can_add_to?(sharing) # => true
249
+ group2.can_remove_from?(sharing) # => false
250
+
251
+ # If user2 add a member in the sharing, he can choose if the permission ':can_add' is true or false, but he can't put ':can_remove' to true (because he don't have this permission himself).
217
252
  ```
218
253
 
219
- If it has the authorisation, an object can remove items from a share.
254
+ If an object has the authorisation, it can remove members from a sharing, else the method return `false`.
220
255
 
221
256
  ```ruby
222
- # user1 want to remove group2 from this share
223
- user1.remove_items_from(share, group2)
257
+ # user1 want to remove group2 from the sharing `sharing`
258
+ user1.remove_members_from(sharing, group2) # The second parameter can be an object or an array of object
224
259
  ```
225
260
 
226
- As admin, you can directly work with the share. Be carefull, there is NO authorisation verification !
261
+ You can directly work with the `sharing`. Be carefull, there is NO authorisation control !
227
262
 
228
263
  ```ruby
229
- # Add an item to the share
230
- share.add_items(item, share_permissions)
264
+ # Add a member to the sharing `sharing`
265
+ sharing.add_members(member)
266
+ # Or with options :
267
+ sharing.add_members(member, {can_add: true, can_remove: false})
268
+
231
269
 
232
- # Delete items from the share
233
- share.remove_items(items)
270
+ # Remove members from the sharing
271
+ sharing.remove_members({user2, group1})
234
272
  ```
235
273
 
236
274
  ### Authorisations
237
275
 
238
276
  #### Repository authorisations
239
277
 
240
- The owner of a repository (file or folder) has all the authorisations on it. When he share this repository, he can choose what authorisation he gives to the share. The authorisations are :
241
- - can_read?(repository) : The item can read (=download) this file/folder.
242
- - can_create?(repository) : Can create in the repository (if repository is nil (= root), always true).
243
- - can_update?(repository) : Can update a repository.
244
- - can_delete?(repository) : Can delete a repository.
245
- - can_share?(repository) : Can share a repository.
278
+ The owner of a `repo_item` (file or folder) has all the authorisations on it. When he share this `repo_item`, he can choose what authorisation he gives to the share. The authorisations are :
279
+ - can_read?(repo_item) : The member can read (=download) this file/folder.
280
+ - can_create?(repo_item) : Can create in the repo_item (Note: if repo_item is nil (= root), always true).
281
+ - can_update?(repo_item) : Can update a repo_item.
282
+ - can_delete?(repo_item) : Can delete a repo_item.
283
+ - can_share?(repo_item) : Can share a repo_item.
246
284
 
247
- To check if a user has one of this authorisation, you just have to write : `user1.can_read?(repository)`, `user1.can_share?(repository)`, etc (it returns `true` or `false`).
285
+ To check if a user has one of this authorisation, you just have to write : `user1.can_read?(repo_item)`, `user1.can_share?(repo_item)`, etc (it returns `true` or `false`).
248
286
 
249
- NOTICE : An object who can share a repository, can't set new permissions that it doesn't have.
250
- For instance, `user3` has a share of `repository1` with `:can_delete => false` and `:can_share => true`. He can share `repository1` with `user4`, but he can't put `:can_delete => true` in this new share.
287
+ NOTICE : An object who can share a repo_item, can't set new permissions that it doesn't have.
288
+ For instance: `user3` has a sharing of `repo_item1` with `:can_delete => false` and `:can_share => true`. He can share `repo_item1` with `user4`, but he can't put `:can_delete => true` in this new share.
251
289
 
252
- You can get all the authorisations with this method: `user1.get_authorisations(repository)`
290
+ You can get all the authorisations with this method: `user1.get_authorisations(repo_item)`
253
291
 
254
292
  ```ruby
255
- # Returns false if the object has no authorisation in this repository
256
- # Returns true if the object has all the authorisations
257
- # Returns a Hash if the entity has custums authorisations
258
- # Exemple
259
- # {can_read: true, can_create: true, can_update:true, can_delete:false, can_share: true}
260
- # Returns true if the repository is nil (because an object has all authorisations on his root folder)
261
- def get_authorisations(repository=nil)
262
- [...]
263
- end
293
+ # Gets the repo authorisations
294
+ # Return false if the entity has not the authorisation to share this rep
295
+ # Return true if the entity can share this rep with all the authorisations
296
+ # Return an Array if the entity can share but with restriction
297
+ # Return true if the repo_item is nil (he as all authorisations on his own rep)
298
+ def get_authorisations(repo_item = nil)
299
+ [...]
300
+ end
264
301
  ```
265
302
 
266
- #### Share permissions
303
+ #### Sharing permissions
267
304
 
268
- You can manage the permissions of an instance in a share. The owner of the share has all the permissions. The permissions are:
269
- - can_add_to?(share) : The item can add a new instance in this share.
270
- - can_remove_from?(share) : Can remove an instance from this share.
305
+ You can manage the permissions of a member in a sharing. The owner of the sharing has all the permissions. The permissions are:
306
+ - can_add_to?(sharing) : The member can add a new instance in this sharing.
307
+ - can_remove_from?(sharing) : Can remove an instance from this sharing.
271
308
 
272
- To check if the object can add or remove an instance in the share, just write : `group1.can_add_to?(share)` or `group1.can_remove_from?(share)` (it returns `true` or `false`).
309
+ 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`).
273
310
 
274
- Like the repository authorisations, you can get the share authorisations with : `group1.get_share_authorisations(share)`.
311
+ Like the repo_item authorisations, you can get the sharing authorisations with : `group1.get_sharing_authorisations(sharing)`.
275
312
 
276
313
  ## TODO
277
314
 
@@ -1,13 +1,13 @@
1
- class AppFile < Repository
1
+ class RepoFile < RepoItem
2
2
  attr_accessible :file, :content_type, :file_size if RepositoryManager.protected_attributes?
3
3
 
4
4
  validates :file, presence: true
5
- mount_uploader :file, RepositoryUploader
5
+ mount_uploader :file, RepoFileUploader
6
6
  before_save :update_asset_attributes
7
7
 
8
8
  # Return the name of the file with his extension
9
9
  def name
10
- file.identifier
10
+ file.url.split('/').last
11
11
  end
12
12
 
13
13
  # Downloading this file
@@ -0,0 +1,60 @@
1
+ require 'zip'
2
+
3
+ class RepoFolder < RepoItem
4
+ attr_accessible :name if RepositoryManager.protected_attributes?
5
+
6
+ validates :name, presence: true
7
+
8
+ # Add a repo_item in the folder.
9
+ def add(repo_item)
10
+ # We check if this name already exist
11
+ if RepoItem.where(name: repo_item.name).where(id: child_ids).first
12
+ #raise "add failed. The repo_item '#{repo_item.name}' already exist in the folder '#{name}'"
13
+ false
14
+ else
15
+ repo_item.update_attribute :parent, self
16
+ end
17
+ end
18
+
19
+ # Download this folder (zip it first)
20
+ # If object = nil, it download all the folder
21
+ # if object is set, it download only the folder that the object can_read.
22
+ def download(object = nil)
23
+ # Get all the children
24
+ children = RepoItem.find(child_ids)
25
+
26
+ # If something is in the array to add, we zip it
27
+ if children.length > 0
28
+ # We create the zip here
29
+ Zip::File.open("#{name}.zip", Zip::File::CREATE) { |zf|
30
+ add_repo_item_to_zip(children, zf, object)
31
+ }
32
+ else
33
+ # Nothing to download here
34
+ #raise "download failed. Folder #{name} is empty"
35
+ false
36
+ end
37
+
38
+ end
39
+
40
+ private
41
+
42
+ def add_repo_item_to_zip(children, zf, object = nil, prefix = nil)
43
+ children.each do |child|
44
+ # If this is a file, we just add this file to the zip
45
+ if child.type == 'RepoFile'
46
+ # Add the file in the zip if the object is authorised to read it.
47
+ zf.add("#{prefix}#{child.name}", child.file.current_path) if object == nil || object.can_read?(child)
48
+ elsif child.type == 'RepoFolder'
49
+ # If this folder has children, we do it again with it children
50
+ if child.has_children?
51
+ # We go in this new directory and add it repo_items
52
+ add_repo_item_to_zip(RepoItem.find(child.child_ids), zf, object, "#{prefix}#{child.name}/")
53
+ else
54
+ # We just create the folder if it is empty
55
+ zf.mkdir(child.name) if object == nil || object.can_read?(child)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,4 +1,4 @@
1
- class Repository < ActiveRecord::Base
1
+ class RepoItem < ActiveRecord::Base
2
2
  attr_accessible :type if RepositoryManager.protected_attributes?
3
3
 
4
4
 
@@ -6,16 +6,16 @@ class Repository < ActiveRecord::Base
6
6
 
7
7
  # Associate with the User Classe
8
8
  belongs_to :owner, :polymorphic => true
9
- has_many :shares, :dependent => :destroy
10
- #has_many :items, through: :shares
9
+ has_many :sharings, :dependent => :destroy
10
+ #has_many :members, through: :sharings
11
11
 
12
12
  if Rails::VERSION::MAJOR == 4
13
- scope :files, -> { where type: 'AppFile' }
14
- scope :folders, -> { where type: 'Folder' }
13
+ scope :files, -> { where type: 'RepoFile' }
14
+ scope :folders, -> { where type: 'RepoFolder' }
15
15
  else
16
16
  # Rails 3 does it this way
17
- scope :files, where(type: 'AppFile')
18
- scope :folders, where(type: 'Folder')
17
+ scope :files, where(type: 'RepoFile')
18
+ scope :folders, where(type: 'RepoFolder')
19
19
  end
20
20
 
21
21
  def copy(target_folder)
@@ -30,10 +30,10 @@ class Repository < ActiveRecord::Base
30
30
  #new_file
31
31
  end
32
32
 
33
- # Move the repository into the target_folder
34
- # TODO à tester
33
+ # Move the repo_item into the target_folder
34
+ # TODO à faire
35
35
  def move(target_folder)
36
- if target_folder.type == 'Folder'
36
+ if target_folder.type == 'RepoFolder'
37
37
  self.update_attribute :parent, target_folder
38
38
  end
39
39
  end
@@ -0,0 +1,58 @@
1
+ class Sharing < ActiveRecord::Base
2
+ attr_accessible :can_read, :can_create, :can_update, :can_delete, :can_share if RepositoryManager.protected_attributes?
3
+
4
+
5
+ has_many :sharings_members, :dependent => :destroy
6
+ belongs_to :owner, :polymorphic => true
7
+ belongs_to :repo_item
8
+
9
+ #scope :recipient, lambda { |recipient|
10
+ # joins(:receipts).where('receipts.receiver_id' => recipient.id,'receipts.receiver_type' => recipient.class.base_class.to_s)
11
+ #}
12
+ scope :members, lambda { |member|
13
+ joins(:sharings_members).where('sharings_members.member_id' => member.id,'sharings_members.member_type' => member.class.base_class.to_s)
14
+ }
15
+
16
+ # Return the authorisations of the sharing for the member
17
+ def get_authorisations(member)
18
+ # If the member is the owner, he can do what he want !
19
+ if self.owner == member
20
+ return true
21
+ elsif i = self.sharings_members.where(member_id: member.id, member_type: member.class.base_class.to_s).first
22
+ return {can_add: i.can_add, can_remove: i.can_remove}
23
+ else
24
+ return false
25
+ end
26
+ end
27
+
28
+ # Add members to the sharing
29
+ def add_members(members, sharing_permissions = nil)
30
+ if members.kind_of?(Array)
31
+ # Add each member to this sharing
32
+ members.each do |i|
33
+ sharing_member = SharingsMember.new(sharing_permissions)
34
+ sharing_member.member = i
35
+ # Add the sharings members in the sharing
36
+ self.sharings_members << sharing_member
37
+ end
38
+ else
39
+ sharing_member = SharingsMember.new(sharing_permissions)
40
+ sharing_member.member = members
41
+ # Add the sharings members in the sharing
42
+ self.sharings_members << sharing_member
43
+ end
44
+ end
45
+
46
+ # Remove members to the sharing
47
+ def remove_members(members)
48
+ if members.kind_of?(Array)
49
+ # Add each member to this sharing
50
+ members.each do |member|
51
+ self.sharings_members.where(:member_id => member.id, :member_type => member.class.base_class.to_s).first.destroy
52
+ end
53
+ else
54
+ self.sharings_members.where(:member_id => members.id, :member_type => members.class.base_class.to_s).first.destroy
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,6 @@
1
+ class SharingsMember < ActiveRecord::Base
2
+ attr_accessible :can_add, :can_remove if RepositoryManager.protected_attributes?
3
+
4
+ belongs_to :member, polymorphic: true
5
+ belongs_to :sharing
6
+ end
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require 'carrierwave/processing/mime_types'
3
3
 
4
- class RepositoryUploader < CarrierWave::Uploader::Base
4
+ class RepoFileUploader < CarrierWave::Uploader::Base
5
5
  include CarrierWave::MimeTypes
6
6
 
7
7
  process :set_content_type