arpa 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +42 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +138 -0
  8. data/Rakefile +2 -0
  9. data/arpa.gemspec +33 -0
  10. data/lib/arpa.rb +91 -0
  11. data/lib/arpa/additions/resource.rb +29 -0
  12. data/lib/arpa/assets/stylesheets/ar_accordion.scss +80 -0
  13. data/lib/arpa/controllers/profiles_controller.rb +106 -0
  14. data/lib/arpa/controllers/resources_controller.rb +47 -0
  15. data/lib/arpa/controllers/roles_controller.rb +106 -0
  16. data/lib/arpa/data_mappers/action_mapper.rb +13 -0
  17. data/lib/arpa/data_mappers/base.rb +117 -0
  18. data/lib/arpa/data_mappers/profile_mapper.rb +14 -0
  19. data/lib/arpa/data_mappers/resource_mapper.rb +11 -0
  20. data/lib/arpa/data_mappers/role_mapper.rb +14 -0
  21. data/lib/arpa/entities/action.rb +24 -0
  22. data/lib/arpa/entities/permissions.rb +35 -0
  23. data/lib/arpa/entities/profile.rb +22 -0
  24. data/lib/arpa/entities/resource.rb +58 -0
  25. data/lib/arpa/entities/role.rb +28 -0
  26. data/lib/arpa/exceptions/record_invalid.rb +13 -0
  27. data/lib/arpa/repositories/actions/creator.rb +18 -0
  28. data/lib/arpa/repositories/actions/finder.rb +23 -0
  29. data/lib/arpa/repositories/actions/remover.rb +22 -0
  30. data/lib/arpa/repositories/actions/repository_action.rb +15 -0
  31. data/lib/arpa/repositories/base.rb +15 -0
  32. data/lib/arpa/repositories/profiles/creator.rb +18 -0
  33. data/lib/arpa/repositories/profiles/finder.rb +29 -0
  34. data/lib/arpa/repositories/profiles/remover.rb +29 -0
  35. data/lib/arpa/repositories/profiles/repository_profile.rb +12 -0
  36. data/lib/arpa/repositories/profiles/updater.rb +23 -0
  37. data/lib/arpa/repositories/registrator.rb +44 -0
  38. data/lib/arpa/repositories/resources/creator.rb +18 -0
  39. data/lib/arpa/repositories/resources/finder.rb +45 -0
  40. data/lib/arpa/repositories/resources/remover.rb +22 -0
  41. data/lib/arpa/repositories/resources/repository_resource.rb +12 -0
  42. data/lib/arpa/repositories/roles/creator.rb +18 -0
  43. data/lib/arpa/repositories/roles/finder.rb +29 -0
  44. data/lib/arpa/repositories/roles/remover.rb +29 -0
  45. data/lib/arpa/repositories/roles/repository_role.rb +13 -0
  46. data/lib/arpa/repositories/roles/updater.rb +23 -0
  47. data/lib/arpa/services/actions/create/action_creator.rb +50 -0
  48. data/lib/arpa/services/actions/remove/action_remover.rb +24 -0
  49. data/lib/arpa/services/base.rb +41 -0
  50. data/lib/arpa/services/profiles/create/profile_creator.rb +32 -0
  51. data/lib/arpa/services/profiles/profile_manager_creator.rb +22 -0
  52. data/lib/arpa/services/profiles/profile_manager_remover.rb +24 -0
  53. data/lib/arpa/services/profiles/profile_manager_updater.rb +22 -0
  54. data/lib/arpa/services/profiles/remove/profile_remover.rb +25 -0
  55. data/lib/arpa/services/profiles/update/profile_updater.rb +32 -0
  56. data/lib/arpa/services/resources/create/resource_creator.rb +42 -0
  57. data/lib/arpa/services/resources/remove/resource_remover.rb +31 -0
  58. data/lib/arpa/services/resources/resource_manager_creator.rb +50 -0
  59. data/lib/arpa/services/roles/create/role_creator.rb +33 -0
  60. data/lib/arpa/services/roles/remove/role_remover.rb +25 -0
  61. data/lib/arpa/services/roles/role_manager_creator.rb +23 -0
  62. data/lib/arpa/services/roles/role_manager_remover.rb +23 -0
  63. data/lib/arpa/services/roles/role_manager_updater.rb +22 -0
  64. data/lib/arpa/services/roles/update/role_updater.rb +32 -0
  65. data/lib/arpa/services/verifier.rb +24 -0
  66. data/lib/arpa/validators/action_validator.rb +17 -0
  67. data/lib/arpa/validators/profile_validator.rb +19 -0
  68. data/lib/arpa/validators/resource_validator.rb +17 -0
  69. data/lib/arpa/validators/role_validator.rb +20 -0
  70. data/lib/arpa/version.rb +3 -0
  71. data/lib/arpa/views/profiles/_form.html.erb +41 -0
  72. data/lib/arpa/views/profiles/edit.html.erb +6 -0
  73. data/lib/arpa/views/profiles/index.html.erb +29 -0
  74. data/lib/arpa/views/profiles/new.html.erb +5 -0
  75. data/lib/arpa/views/profiles/show.html.erb +37 -0
  76. data/lib/arpa/views/resources/index.html.erb +27 -0
  77. data/lib/arpa/views/resources/show.html.erb +33 -0
  78. data/lib/arpa/views/roles/_form.html.erb +55 -0
  79. data/lib/arpa/views/roles/edit.html.erb +6 -0
  80. data/lib/arpa/views/roles/index.html.erb +29 -0
  81. data/lib/arpa/views/roles/new.html.erb +5 -0
  82. data/lib/arpa/views/roles/show.html.erb +37 -0
  83. data/lib/config/locales/arpa.en.yml +38 -0
  84. data/lib/generators/ar/install_generator.rb +74 -0
  85. data/lib/generators/ar/templates/migration.rb +48 -0
  86. data/spec/factories/repository_actions.rb +19 -0
  87. data/spec/factories/repository_profiles.rb +11 -0
  88. data/spec/factories/repository_resources.rb +15 -0
  89. data/spec/factories/repository_roles.rb +11 -0
  90. data/spec/lib/ar/additions/resource_spec.rb +34 -0
  91. data/spec/lib/ar/data_mappers/action_mapper_spec.rb +46 -0
  92. data/spec/lib/ar/data_mappers/base_spec.rb +47 -0
  93. data/spec/lib/ar/data_mappers/profile_mapper_spec.rb +43 -0
  94. data/spec/lib/ar/data_mappers/resource_mapper_spec.rb +38 -0
  95. data/spec/lib/ar/data_mappers/role_mapper_spec.rb +47 -0
  96. data/spec/lib/ar/entities/action_spec.rb +19 -0
  97. data/spec/lib/ar/entities/permissions_spec.rb +61 -0
  98. data/spec/lib/ar/entities/resource_spec.rb +35 -0
  99. data/spec/lib/ar/repositories/actions/finder_spec.rb +40 -0
  100. data/spec/lib/ar/repositories/base_spec.rb +37 -0
  101. data/spec/lib/ar/repositories/profiles/finder_spec.rb +43 -0
  102. data/spec/lib/ar/repositories/profiles/remover_spec.rb +31 -0
  103. data/spec/lib/ar/repositories/resources/finder_spec.rb +92 -0
  104. data/spec/lib/ar/repositories/resources/remover_spec.rb +38 -0
  105. data/spec/lib/ar/repositories/roles/finder_spec.rb +43 -0
  106. data/spec/lib/ar/repositories/roles/remover_spec.rb +31 -0
  107. data/spec/lib/ar/requests/profiles/create_request_spec.rb +51 -0
  108. data/spec/lib/ar/requests/profiles/remove_request_spec.rb +36 -0
  109. data/spec/lib/ar/requests/resources/create_request_spec.rb +107 -0
  110. data/spec/lib/ar/requests/roles/create_request_spec.rb +40 -0
  111. data/spec/lib/ar/requests/roles/remove_request_spec.rb +23 -0
  112. data/spec/lib/ar/requests/roles/update_request_spec.rb +50 -0
  113. data/spec/lib/ar/services/actions/create/action_creator_spec.rb +88 -0
  114. data/spec/lib/ar/services/actions/remove/action_remover_spec.rb +36 -0
  115. data/spec/lib/ar/services/base_spec.rb +94 -0
  116. data/spec/lib/ar/services/profiles/create/profile_creator_spec.rb +68 -0
  117. data/spec/lib/ar/services/profiles/profile_manager_creator_spec.rb +31 -0
  118. data/spec/lib/ar/services/profiles/profile_manager_remover_spec.rb +44 -0
  119. data/spec/lib/ar/services/profiles/profile_manager_updater_spec.rb +31 -0
  120. data/spec/lib/ar/services/profiles/remove/profile_remover_spec.rb +45 -0
  121. data/spec/lib/ar/services/profiles/update/profile_updater_spec.rb +68 -0
  122. data/spec/lib/ar/services/resources/create/resource_creator_spec.rb +87 -0
  123. data/spec/lib/ar/services/resources/remove/resource_remover_spec.rb +50 -0
  124. data/spec/lib/ar/services/resources/resource_manager_creator_spec.rb +71 -0
  125. data/spec/lib/ar/services/roles/create/role_creator_spec.rb +68 -0
  126. data/spec/lib/ar/services/roles/remove/role_remover_spec.rb +50 -0
  127. data/spec/lib/ar/services/roles/role_manager_creator_spec.rb +31 -0
  128. data/spec/lib/ar/services/roles/role_manager_remover_spec.rb +32 -0
  129. data/spec/lib/ar/services/roles/role_manager_updater_spec.rb +31 -0
  130. data/spec/lib/ar/services/roles/update/role_updater_spec.rb +68 -0
  131. data/spec/lib/ar/services/verifier_spec.rb +44 -0
  132. data/spec/lib/ar/validators/action_validator_spec.rb +15 -0
  133. data/spec/lib/ar/validators/profile_validator_spec.rb +16 -0
  134. data/spec/lib/ar/validators/resource_validator_spec.rb +15 -0
  135. data/spec/lib/ar/validators/role_validator_spec.rb +16 -0
  136. data/spec/spec_helper.rb +33 -0
  137. data/spec/support/repositories/test_repository.rb +2 -0
  138. data/spec/support/schema.rb +58 -0
  139. metadata +380 -0
@@ -0,0 +1,32 @@
1
+ module Arpa
2
+ module Services
3
+ module Profiles
4
+ module Update
5
+ class ProfileUpdater
6
+
7
+ def update(params)
8
+ profile = profile_instance(params)
9
+ validate_profile(profile)
10
+ updater_repo.update(profile)
11
+ end
12
+
13
+ private
14
+
15
+ def profile_instance(params)
16
+ Arpa::Entities::Profile.new(params)
17
+ end
18
+
19
+ def validate_profile(profile)
20
+ validator = Arpa::Validators::ProfileValidator.new(profile)
21
+ raise Arpa::Exceptions::RecordInvalid.new(message: validator.errors.messages, errors: validator.errors) unless validator.valid?
22
+ end
23
+
24
+ def updater_repo
25
+ @updater_repo ||= Arpa::Repositories::Profiles::Updater.new
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,42 @@
1
+ module Arpa
2
+ module Services
3
+ module Resources
4
+ module Create
5
+ class ResourceCreator
6
+
7
+ def create(resourceable)
8
+ resource = resource_instance(resourceable)
9
+
10
+ resource_found = finder_repo.by_full_name(resource.full_name)
11
+ return resource_found if resource_found
12
+
13
+ validate_resource(resource)
14
+ creator_repo.create(resource)
15
+ end
16
+
17
+ private
18
+
19
+ def resource_instance(resourceable)
20
+ resource = Arpa::Entities::Resource.new(full_name: resourceable.to_s)
21
+ resource.build_correct_name
22
+ resource
23
+ end
24
+
25
+ def validate_resource(resource)
26
+ validator = Arpa::Validators::ResourceValidator.new(resource)
27
+ raise Arpa::Exceptions::RecordInvalid.new(message: validator.errors.messages, errors: validator.errors) unless validator.valid?
28
+ end
29
+
30
+ def finder_repo
31
+ @finder_repo ||= Arpa::Repositories::Resources::Finder.new
32
+ end
33
+
34
+ def creator_repo
35
+ @creator_repo ||= Arpa::Repositories::Resources::Creator.new
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ module Arpa
2
+ module Services
3
+ module Resources
4
+ module Remove
5
+ class ResourceRemover
6
+
7
+ def remove_nonexistent_resources(resourceables)
8
+ resourceables_names = resourceables.map { |r| r.to_s }
9
+
10
+ finder_repo.all.each do |resource|
11
+ exist = resourceables_names.include?(resource.full_name)
12
+ remover_repo.destroy(resource) unless exist
13
+ end
14
+
15
+ end
16
+
17
+ private
18
+
19
+ def finder_repo
20
+ @finder_repo ||= Arpa::Repositories::Resources::Finder.new
21
+ end
22
+
23
+ def remover_repo
24
+ @remover_repo ||= Arpa::Repositories::Resources::Remover.new
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,50 @@
1
+ module Arpa
2
+ module Services
3
+ module Resources
4
+ class ResourceManagerCreator
5
+ include Arpa::Services::Base
6
+
7
+ def create(params, callback)
8
+ manager_action callback do
9
+ resourceables = params[:resourceables]
10
+
11
+ resource_remover.remove_nonexistent_resources(resourceables)
12
+
13
+ resourceables.collect do |resourceable|
14
+ resource = resource_creator.create(resourceable)
15
+ action_params = action_params(resource, resourceable)
16
+
17
+ action_remover.remove_nonexistent_actions(action_params)
18
+ action_creator.create_many(action_params)
19
+
20
+ resource
21
+ end
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def resource_remover
28
+ @resource_remover ||= Arpa::Services::Resources::Remove::ResourceRemover.new
29
+ end
30
+
31
+ def action_remover
32
+ @action_remover ||= Arpa::Services::Actions::Remove::ActionRemover.new
33
+ end
34
+
35
+ def resource_creator
36
+ @resource_creator ||= Arpa::Services::Resources::Create::ResourceCreator.new
37
+ end
38
+
39
+ def action_creator
40
+ @action_creator ||= Arpa::Services::Actions::Create::ActionCreator.new
41
+ end
42
+
43
+ def action_params(resource, resourceable)
44
+ {resource: resource, actions_names: resourceable.action_methods}
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,33 @@
1
+ module Arpa
2
+ module Services
3
+ module Roles
4
+ module Create
5
+ class RoleCreator
6
+
7
+ def create(params)
8
+ role = role_instance(params)
9
+ validate_role(role)
10
+ creator_repo.create(role)
11
+ end
12
+
13
+ private
14
+
15
+ def role_instance(params)
16
+ Arpa::Entities::Role.new(params)
17
+ end
18
+
19
+ def validate_role(role)
20
+ validator = Arpa::Validators::RoleValidator.new(role)
21
+ raise Arpa::Exceptions::RecordInvalid.new(message: validator.errors.messages, errors: validator.errors) unless validator.valid?
22
+ end
23
+
24
+ def creator_repo
25
+ @creator_repo ||= Arpa::Repositories::Roles::Creator.new
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,25 @@
1
+ module Arpa
2
+ module Services
3
+ module Roles
4
+ module Remove
5
+ class RoleRemover
6
+
7
+ def remove(entity)
8
+ if entity.has_profile?
9
+ remover_repo.disable(entity)
10
+ else
11
+ remover_repo.destroy(entity)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def remover_repo
18
+ @remover_repo ||= Arpa::Repositories::Roles::Remover.new
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module Arpa
2
+ module Services
3
+ module Roles
4
+ class RoleManagerCreator
5
+ include Arpa::Services::Base
6
+
7
+ def create(params, callback)
8
+ manager_action callback do
9
+ role_creator.create(params[:role])
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def role_creator
16
+ @role_creator ||= Arpa::Services::Roles::Create::RoleCreator.new
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,23 @@
1
+ module Arpa
2
+ module Services
3
+ module Roles
4
+ class RoleManagerRemover
5
+ include Arpa::Services::Base
6
+
7
+ def remove(params, callback)
8
+ manager_action callback do
9
+ role = params[:role]
10
+ role_remover.remove(role)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def role_remover
17
+ @role_remover ||= Arpa::Services::Roles::Remove::RoleRemover.new
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module Arpa
2
+ module Services
3
+ module Roles
4
+ class RoleManagerUpdater
5
+ include Arpa::Services::Base
6
+
7
+ def update(params, callback)
8
+ manager_action callback do
9
+ role_updater.update(params[:role])
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def role_updater
16
+ @role_updater ||= Arpa::Services::Roles::Update::RoleUpdater.new
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ module Arpa
2
+ module Services
3
+ module Roles
4
+ module Update
5
+ class RoleUpdater
6
+
7
+ def update(params)
8
+ role = role_instance(params)
9
+ validate_role(role)
10
+ updater_repo.update(role)
11
+ end
12
+
13
+ private
14
+
15
+ def role_instance(params)
16
+ Arpa::Entities::Role.new(params)
17
+ end
18
+
19
+ def validate_role(role)
20
+ validator = Arpa::Validators::RoleValidator.new(role)
21
+ raise Arpa::Exceptions::RecordInvalid.new(message: validator.errors.messages, errors: validator.errors) unless validator.valid?
22
+ end
23
+
24
+ def updater_repo
25
+ @updater_repo ||= Arpa::Repositories::Roles::Updater.new
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ module Arpa
2
+ module Services
3
+ class Verifier
4
+
5
+ def initialize(session, current_user)
6
+ @session = session
7
+ @current_user = current_user
8
+ end
9
+
10
+ def has_access?(resource, action)
11
+ free_access_action = action.to_s.split('_')[0]
12
+ return true if free_access_action.empty?
13
+ @session[:entity_permissions] ||= resource_finder.permissions(@current_user.profile_ids)
14
+ @session[:entity_permissions].has_permission?(resource.to_s, action.to_s)
15
+ end
16
+
17
+ private
18
+
19
+ def resource_finder
20
+ @resource_finder ||= Arpa::Repositories::Resources::Finder.new
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module Arpa
2
+ module Validators
3
+ class ActionValidator
4
+ include ActiveModel::Validations
5
+
6
+ attr_reader :name, :resource_id
7
+
8
+ validates :name, :resource_id, presence: true
9
+
10
+ def initialize(action)
11
+ @name = action.name
12
+ @resource_id = action.resource_id
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module Arpa
2
+ module Validators
3
+ class ProfileValidator
4
+ include ActiveModel::Validations
5
+
6
+ attr_reader :name, :description, :role_ids
7
+
8
+ validates :name, :description, presence: true
9
+ validates :role_ids, presence: { message: :many_blank }
10
+
11
+ def initialize(profile)
12
+ @name = profile.name
13
+ @description = profile.description
14
+ @role_ids = profile.role_ids
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module Arpa
2
+ module Validators
3
+ class ResourceValidator
4
+ include ActiveModel::Validations
5
+
6
+ attr_reader :full_name, :name
7
+
8
+ validates :full_name, :name, presence: true
9
+
10
+ def initialize(resource)
11
+ @full_name = resource.full_name
12
+ @name = resource.name
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module Arpa
2
+ module Validators
3
+ class RoleValidator
4
+ include ActiveModel::Validations
5
+
6
+ attr_reader :name, :description, :action_ids
7
+
8
+ validates :name, :description, presence: true
9
+ validates :action_ids, presence: { message: :many_blank }
10
+
11
+
12
+ def initialize(role)
13
+ @name = role.name
14
+ @description = role.description
15
+ @action_ids = role.action_ids
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Arpa
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,41 @@
1
+ <%= form_for(:profile, url: {action: action}, method: method) do |f| %>
2
+ <% if @error.present? && @error.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@error.errors.count, "error") %> prohibited this user from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @error.errors.full_messages.each do |message| %>
8
+ <li><%= message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <%= f.hidden_field :id, value: @profile.id %>
15
+
16
+ <div class="field">
17
+ <%= f.label :name %><br>
18
+ <%= f.text_field :name %>
19
+ </div>
20
+
21
+ <div class="field">
22
+ <%= f.label :description %><br>
23
+ <%= f.text_field :description %>
24
+ </div>
25
+
26
+ <div class="field">
27
+
28
+ <% @roles.each do |role| %>
29
+ <div>
30
+ <input type="checkbox" name="profile[role_ids][]" value="<%= role.id %>" <%= checked="checked" if @profile.role_ids.include?(role.id) %> >
31
+ <%= "#{role.name} - #{role.description}"%>
32
+ </div>
33
+ <% end %>
34
+
35
+ </div>
36
+
37
+ <div class="actions">
38
+ <%= f.submit %>
39
+ </div>
40
+
41
+ <% end %>