simple-navigation-acl 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +171 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/simple_navigation_acl.js +15 -0
  6. data/app/assets/stylesheets/simple_navigation_acl.css +58 -0
  7. data/app/controllers/simple_navigation_acl/application_controller.rb +5 -0
  8. data/app/controllers/simple_navigation_acl/rules_controller.rb +49 -0
  9. data/app/helpers/simple_navigation_acl/application_helper.rb +20 -0
  10. data/app/models/simple_navigation_acl/acl_rule.rb +6 -0
  11. data/app/views/simple_navigation_acl/_form.haml +6 -0
  12. data/app/views/simple_navigation_acl/_tree.haml +6 -0
  13. data/app/views/simple_navigation_acl/_tree_item.haml +12 -0
  14. data/app/views/simple_navigation_acl/rules/edit.html.erb +11 -0
  15. data/app/views/simple_navigation_acl/rules/show.html.erb +9 -0
  16. data/config/initializers/assets.rb +1 -0
  17. data/config/initializers/inflections.rb +5 -0
  18. data/config/locales/simple_navigation_acl.en.yml +4 -0
  19. data/config/routes.rb +17 -0
  20. data/lib/generators/simple_navigation_acl/dictionary_generator.rb +12 -0
  21. data/lib/generators/simple_navigation_acl/install_generator.rb +41 -0
  22. data/lib/generators/templates/create_acl_rules.rb +18 -0
  23. data/lib/generators/templates/generic_dictionary.erb +11 -0
  24. data/lib/generators/templates/initializer.rb +1 -0
  25. data/lib/simple-navigation-acl.rb +1 -0
  26. data/lib/simple_navigation_acl.rb +8 -0
  27. data/lib/simple_navigation_acl/base.rb +79 -0
  28. data/lib/simple_navigation_acl/engine.rb +4 -0
  29. data/lib/simple_navigation_acl/override.rb +12 -0
  30. data/lib/simple_navigation_acl/version.rb +3 -0
  31. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01d931fc73723efb53a9738a8e81fbbd6822c233
4
+ data.tar.gz: 573121676f3c02e1831f9af7e42b20dab12fe40e
5
+ SHA512:
6
+ metadata.gz: 9225225ba2d5953d3638f095ad9f32b8f9029f9b9c4e43e187537f31819c97abd9652938be933dbd39dbeef2a706b726263740d7c4cf48e191f5875f69bba95c
7
+ data.tar.gz: 873dc6026fdea9ada2b007bfdfa0b122580e964675d2881c3e950c965f76149dca7c45d5bf4704d3fa6cfdae022772975eb97ff042ab101b0648af7051b2624e
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Daniel de Carvalho Passarini
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # Simple Navigation Acl
2
+
3
+ A great and easy condition builder to your rails project
4
+
5
+ ## How to install
6
+
7
+ Add it to your **Gemfile**:
8
+ ```ruby
9
+ gem 'simple_navigation_acl'
10
+ ```
11
+
12
+ Run the following command to install it:
13
+ ```sh
14
+ $ bundle install
15
+ $ rails generate simple_navigation_acl:install
16
+ $ rake db:migrate
17
+ ```
18
+
19
+ Add it to your **app/assets/stylesheets/application.css**
20
+ ```js
21
+ *= require simple_navigation_acl
22
+ ```
23
+
24
+ Add it to your **app/assets/javascripts/application.js**
25
+ ```js
26
+ //= require simple_navigation_acl
27
+ ```
28
+
29
+ Modify simple-navigation helper `render_navigation` in your views to simple-navigation-acl helper `render_navigation_acl` and add param `acl_id`, like:
30
+ ```haml
31
+ = render_navigation_acl acl_id: current_user.role, level:(1..3), :renderer => :bootstrap3, :expand_all => true
32
+ ```
33
+
34
+ ## ID and CONTEXT
35
+
36
+ The simple-navigation-acl have id and [context](https://github.com/codeplant/simple-navigation/wiki/Configuration) to create rules for you:
37
+
38
+ Example for roles:
39
+
40
+ **config/navigation.rb**
41
+
42
+ ```ruby
43
+ SimpleNavigation::Configuration.run do |navigation|
44
+ navigation.items do |primary|
45
+ primary.item :menu1, "Menu 1", root_path do |sub_nav|
46
+ sub_nav.item :sub_menu1, 'Sub Menu1', root_path
47
+ end
48
+ end
49
+ end
50
+ ```
51
+
52
+ **config/admin_navigation.rb**
53
+
54
+ ```ruby
55
+ SimpleNavigation::Configuration.run do |navigation|
56
+ navigation.items do |primary|
57
+ primary.item :adm_menu1, "Admin Menu 1", root_path do |sub_nav|
58
+ sub_nav.item :adm_sub_menu1, 'Admin Sub Menu1', root_path
59
+ end
60
+ end
61
+ end
62
+ ```
63
+
64
+ **table acl_rules**
65
+
66
+ id | context | key
67
+ --- | --- | ---
68
+ admin | default | :menu1
69
+ admin | default | :sub_menu1
70
+ admin | admin | :adm_menu1
71
+ admin | admin | :adm_sub_menu1
72
+ user | default | :menu1
73
+ ...
74
+
75
+ ## Routes
76
+
77
+ **Edit Acl Rules**
78
+
79
+ You can bind acl rule on resource, doesn't matter what resource it is.
80
+ Only pass `id` to the route and it will be binded.
81
+
82
+ ```ruby
83
+ @resource = User.first
84
+ # or
85
+ @resource = Role.last
86
+ # or
87
+ @resource = Wherever.find_by(id: 1)
88
+ ```
89
+
90
+ ```haml
91
+ = link_to 'Edit', simple_navigation_acl_edit_path(@resource)
92
+ = link_to 'Show', simple_navigation_acl_show_path(@resource)
93
+ ```
94
+
95
+ Or a String like `@role = 'admin'` then:
96
+ ```haml
97
+ = link_to 'Edit', simple_navigation_acl_edit_path(id: @role)
98
+ ```
99
+
100
+ And to save via HTTP PATCH, PUT or POST, like:
101
+ ```haml
102
+ = form_tag simple_navigation_acl_save_path(@resource), method: :put
103
+
104
+ ```
105
+
106
+ ### Example Form for ACL by Roles
107
+
108
+ ```haml
109
+ .row
110
+ .col-md-12
111
+ %table.table.table-hover{style: 'width: auto'}
112
+ %tr
113
+ %th Roles
114
+ %th
115
+ - [:admin, :user].each do |role|
116
+ %tr
117
+ %td= role
118
+ %td
119
+ = link_to 'Edit', simple_navigation_acl_edit_path(id: role)
120
+ \|
121
+ = link_to 'Edit', simple_navigation_acl_show_path(id: role)
122
+ ```
123
+
124
+ ```haml
125
+ = render_navigation_acl acl_id: current_user.role
126
+ ```
127
+
128
+ ### Example Form for ACL by User
129
+
130
+ ```haml
131
+ .row
132
+ .col-md-12
133
+ %table.table.table-hover{style: 'width: auto'}
134
+ %tr
135
+ %th Roles
136
+ %th
137
+ - User.all.each do |user|
138
+ %tr
139
+ %td= role
140
+ %td
141
+ = link_to 'Edit', simple_navigation_acl_edit_path(user)
142
+ \|
143
+ = link_to 'Edit', simple_navigation_acl_show_path(user)
144
+ ```
145
+
146
+ ```haml
147
+ = render_navigation_acl acl_id: current_user.id
148
+ ```
149
+
150
+ ## Bug reports
151
+
152
+ If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as
153
+ possible to help us fixing the possible bug. We also encourage you to help even more by forking and
154
+ sending us a pull request.
155
+
156
+ https://github.com/brunoporto/simple_navigation_acl/issues
157
+
158
+ ## Maintainers
159
+
160
+ * Bruno Porto (https://github.com/brunoporto)
161
+
162
+ ## License
163
+
164
+ The MIT License (MIT)
165
+ Copyright (c) 2016 Bruno Porto
166
+
167
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
168
+
169
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
170
+
171
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ # require 'rdoc/task'
8
+ # RDoc::Task.new(:rdoc) do |rdoc|
9
+ # rdoc.rdoc_dir = 'rdoc'
10
+ # rdoc.title = 'SimpleNavigationAcl'
11
+ # rdoc.options << '--line-numbers'
12
+ # rdoc.rdoc_files.include('README.rdoc')
13
+ # rdoc.rdoc_files.include('lib/**/*.rb')
14
+ # end
15
+
16
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
17
+ load 'rails/tasks/engine.rake'
18
+
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ # require 'rake/testtask'
25
+ #
26
+ # Rake::TestTask.new(:test) do |t|
27
+ # t.libs << 'lib'
28
+ # t.libs << 'test'
29
+ # t.pattern = 'test/**/*_test.rb'
30
+ # t.verbose = false
31
+ # end
32
+ #
33
+ #
34
+ # task default: :test
@@ -0,0 +1,15 @@
1
+ $(document).on('change','.acl_tree_checkbox', function(ev) {
2
+ var checkbox = $(this);
3
+ if (checkbox.prop('checked')==true) {
4
+ var acl_tree_parents = checkbox.parents('.acl_tree_item');
5
+ acl_tree_parents.find('> :checkbox').prop('checked', true);
6
+ } else {
7
+ var acl_tree_item = checkbox.parent('.acl_tree_item:first');
8
+ acl_tree_item.find(':checkbox').prop('checked', false);
9
+ // REMOVER O PRIMEIRO PAI QUANDO OS FILHOS ESTIVERM TODOS DESMARCADOS
10
+ // var acl_tree_parent = acl_tree_item.parents('.acl_tree_item:first');
11
+ // if (acl_tree_parent.find('.acl_tree_item > :checkbox:checked').length==0) {
12
+ // acl_tree_parent.find(':checkbox:first').prop('checked', false);
13
+ // }
14
+ }
15
+ });
@@ -0,0 +1,58 @@
1
+ /*
2
+ *= require_self
3
+ */
4
+
5
+ ul.acl_tree label {
6
+ cursor: pointer;
7
+ }
8
+
9
+ ul.acl_tree,
10
+ ul.acl_tree ul.acl_tree {
11
+ margin:0 0 0 1em; /* indentation */
12
+ padding:0;
13
+ list-style:none;
14
+ position:relative;
15
+ }
16
+
17
+ ul.acl_tree ul.acl_tree {margin-left:.5em} /* (indentation/2) */
18
+
19
+ ul.acl_tree:before,
20
+ ul.acl_tree ul:before {
21
+ content:"";
22
+ display:block;
23
+ width:0;
24
+ position:absolute;
25
+ top:0;
26
+ bottom:0;
27
+ left:0;
28
+ border-left: 1px solid;
29
+ color: #C5C5C5;
30
+ }
31
+
32
+ ul.acl_tree li.acl_tree_item {
33
+ margin:0;
34
+ padding:0 1.5em; /* indentation + .5em */
35
+ line-height:2em; /* default list item's `line-height` */
36
+ font-weight:bold;
37
+ position:relative;
38
+ }
39
+
40
+ ul.acl_tree li.acl_tree_item:before {
41
+ content:"";
42
+ display:block;
43
+ width:10px; /* same with indentation */
44
+ height:0;
45
+ border-top:1px solid;
46
+ margin-top:-2px; /* border top width */
47
+ position:absolute;
48
+ top:1em; /* (line-height/2) */
49
+ left:0;
50
+ color: #C5C5C5;
51
+ }
52
+
53
+ ul.acl_tree li.acl_tree_item:last-child:before {
54
+ background:white; /* same with body background */
55
+ height:auto;
56
+ top:1em; /* (line-height/2) */
57
+ bottom:0;
58
+ }
@@ -0,0 +1,5 @@
1
+ module SimpleNavigationAcl
2
+ class ApplicationController < ::ApplicationController #< ActionController::Base #ENGINE
3
+
4
+ end
5
+ end
@@ -0,0 +1,49 @@
1
+ module SimpleNavigationAcl
2
+ class RulesController < ApplicationController
3
+
4
+ before_action :set_rule, only: [:edit, :show]
5
+
6
+ def edit
7
+ end
8
+
9
+ def show
10
+ end
11
+
12
+ def update
13
+ errors = []
14
+
15
+ resource_id = params[:id]
16
+ acl_item = params[:acl_item]
17
+
18
+ SimpleNavigationAcl::AclRule.where(id: resource_id).delete_all
19
+
20
+ acl_item.each do |context, menus|
21
+ menus.each do |menu|
22
+ rule = SimpleNavigationAcl::AclRule.find_or_create_by(id: resource_id, context: context, key: menu)
23
+ if rule.errors.present?
24
+ errors = errors + rule.errors.full_messages
25
+ end
26
+ end
27
+ end
28
+
29
+ respond_to do |format|
30
+ if errors.blank?
31
+ flash[:notice] = I18n.t(:save, default: ['Save Successfully'], scope: [:simple_navigation_acl, :messages])
32
+ format.html { redirect_to simple_navigation_acl_show_path(id: resource_id) }
33
+ format.json { render json: acl_item, status: :ok, location: simple_navigation_acl_show_path(id: resource_id) }
34
+ else
35
+ flash[:error] = errors.join(', ')
36
+ format.html { render :edit }
37
+ format.json { render json: errors, status: :unprocessable_entity }
38
+ end
39
+ end
40
+ end
41
+
42
+ private
43
+ def set_rule
44
+ @id = params[:id]
45
+ @rules = SimpleNavigationAcl::AclRule.where(id: @id).pluck(:context, :key)
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,20 @@
1
+ module SimpleNavigationAcl
2
+ module ApplicationHelper
3
+
4
+ def navigations_from_context(context)
5
+ SimpleNavigationAcl::Base.navigations(self, context)[context] rescue []
6
+ end
7
+
8
+ def show_navigation_tree(navs, rules=[], readonly: false)
9
+ render partial: 'simple_navigation_acl/tree', locals: {navs: navs, rules: rules, readonly: readonly}
10
+ end
11
+
12
+ def render_navigation_acl(options = {}, &block)
13
+ # render_navigation(options, &block)
14
+ container = active_navigation_item_container(options, &block)
15
+ acl_id = options.key?(:acl_id) ? options[:acl_id].to_sym : nil
16
+ container && container.apply_acl(acl_id, options[:context]) && container.render(options)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ module SimpleNavigationAcl
2
+ class AclRule < ActiveRecord::Base
3
+ self.table_name = :acl_rules
4
+ validates :id, :context, :key, presence: true
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ - readonly=false if readonly.nil?
2
+ - SimpleNavigationAcl::Base.contexts.each do |context|
3
+ - navs = navigations_from_context(context)
4
+ %fieldset.simple_navigation_acl_fieldset
5
+ %legend= context.to_s.humanize
6
+ = show_navigation_tree(navs, @rules, readonly: readonly)
@@ -0,0 +1,6 @@
1
+ %ul.acl_tree
2
+ - navs = [] if navs.nil?
3
+ - rules = [] if rules.nil?
4
+ - readonly = true if readonly.nil?
5
+ - navs.each do |nav|
6
+ = render partial: 'simple_navigation_acl/tree_item', locals: {nav: nav, rules: rules, readonly: readonly}
@@ -0,0 +1,12 @@
1
+ - unless nav.nil? || nav[:key].to_s =~ /^divider/
2
+ %li.acl_tree_item
3
+ - rules = [] if rules.nil?
4
+ - readonly = true if readonly.nil?
5
+ - nav_id = "#{nav[:context]}_#{nav[:key]}"
6
+ - checked = rules.include?([nav[:context].to_s, nav[:key].to_s])
7
+ - if readonly
8
+ %span= "&#10004;".html_safe if checked
9
+ - else
10
+ %input.acl_tree_checkbox{type: 'checkbox', name: "acl_item[#{nav[:context]}][]", id: nav_id, value: nav[:key], checked: checked}
11
+ %label{for: nav_id}= nav[:name]
12
+ = show_navigation_tree(nav[:items], rules, readonly: readonly) if nav.key?(:items)
@@ -0,0 +1,11 @@
1
+ <h1>Edit ACL</h1>
2
+
3
+ <%= form_tag simple_navigation_acl_save_path do %>
4
+ <%= render 'simple_navigation_acl/form' %>
5
+ <hr />
6
+ <%= submit_tag 'Save' %>
7
+ or
8
+ <%= link_to 'Back', :back %>
9
+ |
10
+ <%= link_to 'Show', simple_navigation_acl_edit_path(id: @id) %>
11
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <h1>Show ACL</h1>
2
+
3
+ <%= render 'simple_navigation_acl/form', readonly: true %>
4
+
5
+ <hr />
6
+
7
+ <%= link_to 'Back', :back %>
8
+ |
9
+ <%= link_to 'Edit', simple_navigation_acl_edit_path(id: @id) %>
@@ -0,0 +1 @@
1
+ Rails.application.config.assets.precompile += %w( simple_navigation_acl.js simple_navigation_acl.css )
@@ -0,0 +1,5 @@
1
+ ActiveSupport::Inflector.inflections do |inflect|
2
+ # inflect.clear
3
+ inflect.irregular 'permission', 'permissions'
4
+ end
5
+
@@ -0,0 +1,4 @@
1
+ en:
2
+ simple_navigation_acl:
3
+ messages:
4
+ save: 'Save Successfully'
data/config/routes.rb ADDED
@@ -0,0 +1,17 @@
1
+ Rails.application.routes.draw do
2
+
3
+
4
+ namespace :simple_navigation_acl do
5
+
6
+ get 'rules/:id/edit', to: 'rules#edit', as: 'edit'
7
+ get 'rules/:id', to: 'rules#show', as: 'show'
8
+ match 'rules/:id', to: 'rules#update', via: [:patch, :put, :post], as: 'save'
9
+
10
+ end
11
+
12
+
13
+
14
+
15
+
16
+
17
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails/generators/base'
2
+
3
+ module SimpleNavigationAcl
4
+ class DictionaryGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path("../../templates", __FILE__)
6
+
7
+ def generate_dictionary
8
+ @dictionary_name = file_name.classify
9
+ template "generic_dictionary.erb", File.join('app/condition_dictionaries', "#{file_name.underscore}_dictionary.rb")
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,41 @@
1
+ require 'rails/generators/base'
2
+
3
+ module SimpleNavigationAcl
4
+ module Generators
5
+
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path("../../templates", __FILE__)
8
+
9
+ include Rails::Generators::Migration
10
+ class_option :orm
11
+
12
+ desc 'Migrations'
13
+ def self.next_migration_number(path)
14
+ unless @prev_migration_nr
15
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
16
+ else
17
+ @prev_migration_nr += 1
18
+ end
19
+ @prev_migration_nr.to_s
20
+ end
21
+
22
+ def create_migration_file
23
+ migration_template 'create_acl_rules.rb', 'db/migrate/create_acl_rules.rb'
24
+ end
25
+
26
+ def copy_initializer
27
+ copy_file 'initializer.rb', 'config/initializers/simple_navigation_acl.rb'
28
+ end
29
+
30
+ def copy_locales
31
+ directory File.expand_path("../../../../config/locales", __FILE__), 'config/locales'
32
+ end
33
+
34
+ def copy_views
35
+ directory File.expand_path("../../../../app/views/simple_navigation_acl/rules", __FILE__), 'app/views/simple_navigation_acl/rules'
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ class CreateAclRules < ActiveRecord::Migration
2
+
3
+ def up
4
+ unless table_exists?(:acl_rules)
5
+ create_table :acl_rules, id: false do |t|
6
+ t.string :id, null: false, index: true
7
+ t.string :context, null: false, default: 'default', index: true
8
+ t.string :key, null: false, index: true
9
+ end
10
+ end
11
+ add_index :acl_rules, [:id, :context, :key] unless index_exists?(:acl_rules, [:id, :context, :key])
12
+ end
13
+
14
+ def down
15
+ drop_table :acl_rules if table_exists? :acl_rules
16
+ end
17
+
18
+ end
@@ -0,0 +1,11 @@
1
+ class <%= @dictionary_name %>Dictionary < SimpleNavigationAcl::Dictionary
2
+
3
+ dictionary do
4
+ param :name
5
+ end
6
+
7
+ # dictionary :context_name do
8
+ # param :name
9
+ # end
10
+
11
+ end
@@ -0,0 +1 @@
1
+ #SimpleNavigationAcl::Base.contexts += %w(admin)
@@ -0,0 +1 @@
1
+ require 'simple_navigation_acl'
@@ -0,0 +1,8 @@
1
+ # require 'haml'
2
+ require 'simple_navigation_acl/engine'
3
+ require 'simple_navigation_acl/base'
4
+ require 'simple_navigation_acl/override'
5
+
6
+ module SimpleNavigationAcl
7
+ # Mime::Type.register "application/vnd.ms-excel", :xls
8
+ end
@@ -0,0 +1,79 @@
1
+ module SimpleNavigationAcl
2
+ class Base
3
+
4
+ @contexts = [:default]
5
+ @current_user_method = :current_user
6
+ @entity = 'Role'
7
+ @verify_method = 'role'
8
+
9
+ class << self
10
+
11
+ attr_accessor :current_user_method, :entity, :verify_method
12
+
13
+ attr_reader :contexts
14
+
15
+ def contexts=(contexts)
16
+ contexts = [contexts] unless contexts.is_a?(Array)
17
+ @contexts = contexts.map(&:to_sym)
18
+ @contexts.uniq!
19
+ end
20
+
21
+ def navigations(obj_caller=nil, navigation_context=nil)
22
+ navigations = {}
23
+ contexts = if navigation_context.nil?
24
+ SimpleNavigationAcl::Base.contexts
25
+ else
26
+ navigation_context.is_a?(Array) ? navigation_context : [navigation_context]
27
+ end
28
+ contexts.each do |context|
29
+ SimpleNavigation::Helpers.load_config({context: context}, obj_caller)
30
+ primary_navigation = SimpleNavigation.config.primary_navigation
31
+ navigations[context] = get_nav_items(primary_navigation, context)
32
+ end
33
+ navigations
34
+ end
35
+
36
+ def apply_acl(navigation, id, context)
37
+ context=:default if context.nil?
38
+ rules_keys = SimpleNavigationAcl::AclRule.where(id: id, context: context).pluck(:key)
39
+ container = navigation.is_a?(SimpleNavigation::Configuration) ? navigation.instance_variable_get(:@primary_navigation) : navigation
40
+ filter_simple_navigation_with_rules!(container, rules_keys)
41
+ true
42
+ end
43
+
44
+ def filter_simple_navigation_with_rules!(simple_navigation_item_container, keys)
45
+ simple_navigation_item_container.items.delete_if do |simple_navigation_item|
46
+ if keys.include?(simple_navigation_item.key.to_s)
47
+ sub_navigation = simple_navigation_item.sub_navigation
48
+ filter_simple_navigation_with_rules!(sub_navigation, keys) if sub_navigation
49
+ false
50
+ else
51
+ true
52
+ end
53
+ end
54
+ end
55
+
56
+ private
57
+ def get_nav_items(nav, context=:default)
58
+ nav.items.map do |item|
59
+ items = {key: item.key, name: item.name, url: item.url, level: nav.level, context: context}
60
+ items[:items] = get_nav_items(item.sub_navigation, context) if item.sub_navigation.present?
61
+ items
62
+ end
63
+ end
64
+
65
+ # def get_nav_items_inline(nav, parent_key=nil)
66
+ # items = []
67
+ # nav.items.each do |item|
68
+ # h_item = {key: item.key, name: item.name, url: item.url, level: nav.level}
69
+ # h_item[:parent_key] = parent_key unless parent_key.nil?
70
+ # items << h_item
71
+ # items = items + get_nav_items_inline(item.sub_navigation, item.key) if item.sub_navigation.present?
72
+ # end
73
+ # items
74
+ # end
75
+
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,4 @@
1
+ module SimpleNavigationAcl
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,12 @@
1
+ module SimpleNavigation
2
+ class Configuration
3
+ def apply_acl(id, context=:default)
4
+ SimpleNavigationAcl::Base.apply_acl(self, id, context)
5
+ end
6
+ end
7
+ class ItemContainer
8
+ def apply_acl(id, context=:default)
9
+ SimpleNavigationAcl::Base.apply_acl(self, id, context)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleNavigationAcl
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-navigation-acl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Porto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: haml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Great and easy way to control ACL with simple-navigation in your Rails
28
+ project
29
+ email:
30
+ - brunotporto@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - app/assets/javascripts/simple_navigation_acl.js
39
+ - app/assets/stylesheets/simple_navigation_acl.css
40
+ - app/controllers/simple_navigation_acl/application_controller.rb
41
+ - app/controllers/simple_navigation_acl/rules_controller.rb
42
+ - app/helpers/simple_navigation_acl/application_helper.rb
43
+ - app/models/simple_navigation_acl/acl_rule.rb
44
+ - app/views/simple_navigation_acl/_form.haml
45
+ - app/views/simple_navigation_acl/_tree.haml
46
+ - app/views/simple_navigation_acl/_tree_item.haml
47
+ - app/views/simple_navigation_acl/rules/edit.html.erb
48
+ - app/views/simple_navigation_acl/rules/show.html.erb
49
+ - config/initializers/assets.rb
50
+ - config/initializers/inflections.rb
51
+ - config/locales/simple_navigation_acl.en.yml
52
+ - config/routes.rb
53
+ - lib/generators/simple_navigation_acl/dictionary_generator.rb
54
+ - lib/generators/simple_navigation_acl/install_generator.rb
55
+ - lib/generators/templates/create_acl_rules.rb
56
+ - lib/generators/templates/generic_dictionary.erb
57
+ - lib/generators/templates/initializer.rb
58
+ - lib/simple-navigation-acl.rb
59
+ - lib/simple_navigation_acl.rb
60
+ - lib/simple_navigation_acl/base.rb
61
+ - lib/simple_navigation_acl/engine.rb
62
+ - lib/simple_navigation_acl/override.rb
63
+ - lib/simple_navigation_acl/version.rb
64
+ homepage: https://github.com/brunoporto/simple_navigation_acl
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.5.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Great and easy way to control ACL with simple-navigation
88
+ test_files: []