cantango-permits 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/Gemfile +39 -0
- data/Gemfile.lock +155 -0
- data/MIT-LICENSE +20 -0
- data/README.mdown +362 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/cantango/executor.rb +5 -0
- data/lib/cantango/executor/base.rb +9 -0
- data/lib/cantango/executor/permit.rb +7 -0
- data/lib/cantango/executor/permit/abstract.rb +27 -0
- data/lib/cantango/executor/permit/base.rb +19 -0
- data/lib/cantango/executor/permit/special.rb +9 -0
- data/lib/cantango/executor/permit_type.rb +50 -0
- data/lib/cantango/license.rb +19 -0
- data/lib/cantango/license/rules.rb +17 -0
- data/lib/cantango/permit.mdown +4 -0
- data/lib/cantango/permit.rb +5 -0
- data/lib/cantango/permit/account_type.rb +44 -0
- data/lib/cantango/permit/attribute.rb +71 -0
- data/lib/cantango/permit/base.rb +94 -0
- data/lib/cantango/permit/class_methods.rb +49 -0
- data/lib/cantango/permit/helper.rb +11 -0
- data/lib/cantango/permit/helper/execution.rb +38 -0
- data/lib/cantango/permit/helper/host.rb +13 -0
- data/lib/cantango/permit/helper/license.rb +34 -0
- data/lib/cantango/permit/helper/naming.rb +38 -0
- data/lib/cantango/permit/helper/state.rb +21 -0
- data/lib/cantango/permit/special.rb +17 -0
- data/lib/cantango/permit/user.rb +36 -0
- data/lib/cantango/permit/user_type.rb +34 -0
- data/lib/cantango/permits.rb +20 -0
- data/lib/cantango/permits_ext.rb +7 -0
- data/lib/cantango/permits_ext/ability.rb +7 -0
- data/lib/cantango/permits_ext/ability/helper.rb +9 -0
- data/lib/cantango/permits_ext/ability/helper/permits.rb +8 -0
- data/lib/cantango/permits_ext/builder.rb +7 -0
- data/lib/cantango/permits_ext/builder/permit.rb +9 -0
- data/lib/cantango/permits_ext/builder/permit/account_type.rb +13 -0
- data/lib/cantango/permits_ext/builder/permit/base.rb +94 -0
- data/lib/cantango/permits_ext/builder/permit/special.rb +13 -0
- data/lib/cantango/permits_ext/builder/permit/user_type.rb +7 -0
- data/lib/cantango/permits_ext/class_methods.rb +28 -0
- data/lib/cantango/permits_ext/config.rb +11 -0
- data/lib/cantango/permits_ext/config/engines.rb +9 -0
- data/lib/cantango/permits_ext/config/engines/permit.rb +20 -0
- data/lib/cantango/permits_ext/config/permits.rb +43 -0
- data/lib/cantango/permits_ext/config/permits/accounts.rb +15 -0
- data/lib/cantango/permits_ext/config/permits/disabling.rb +22 -0
- data/lib/cantango/permits_ext/config/permits/enabling.rb +14 -0
- data/lib/cantango/permits_ext/config/permits/execution.rb +21 -0
- data/lib/cantango/permits_ext/config/permits/key.rb +19 -0
- data/lib/cantango/permits_ext/config/permits/registration.rb +33 -0
- data/lib/cantango/permits_ext/config/permits/tracking.rb +19 -0
- data/lib/cantango/permits_ext/config/permits/types.rb +25 -0
- data/lib/cantango/permits_ext/engine.rb +7 -0
- data/lib/cantango/permits_ext/engine/permits.rb +75 -0
- data/lib/cantango/permits_ext/factory.rb +7 -0
- data/lib/cantango/permits_ext/factory/permits.rb +40 -0
- data/lib/cantango/permits_ext/finder.rb +7 -0
- data/lib/cantango/permits_ext/finder/base.rb +35 -0
- data/lib/cantango/permits_ext/finder/permit.rb +7 -0
- data/lib/cantango/permits_ext/finder/permit/account.rb +47 -0
- data/lib/cantango/permits_ext/finder/permit/base.rb +53 -0
- data/lib/cantango/permits_ext/loader.rb +7 -0
- data/lib/cantango/permits_ext/loader/categories.rb +50 -0
- data/lib/cantango/permits_ext/loader/license.rb +19 -0
- data/lib/cantango/permits_ext/macros.rb +7 -0
- data/lib/cantango/permits_ext/macros/permit.rb +32 -0
- data/lib/cantango/permits_ext/parser.rb +7 -0
- data/lib/cantango/permits_ext/parser/categories.rb +15 -0
- data/lib/cantango/permits_ext/registry.rb +7 -0
- data/lib/cantango/permits_ext/registry/permit.rb +45 -0
- data/lib/generators/cantango/account_permit/account_permit_generator.rb +37 -0
- data/lib/generators/cantango/account_permit/templates/account_permit.erb +23 -0
- data/lib/generators/cantango/base.rb +71 -0
- data/lib/generators/cantango/basic.rb +41 -0
- data/lib/generators/cantango/license/license_generator.rb +29 -0
- data/lib/generators/cantango/license/templates/license.erb +10 -0
- data/lib/generators/cantango/license_base.rb +15 -0
- data/lib/generators/cantango/licenses/licenses_generator.rb +26 -0
- data/lib/generators/cantango/permit_generator.rb +58 -0
- data/lib/generators/cantango/role_permit/role_permit_generator.rb +39 -0
- data/lib/generators/cantango/role_permit/templates/account_permit.erb +4 -0
- data/lib/generators/cantango/role_permit/templates/role_group_permit.erb +24 -0
- data/lib/generators/cantango/role_permit/templates/role_permit.erb +23 -0
- data/lib/generators/cantango/role_permits/role_permits_generator.rb +45 -0
- data/lib/generators/cantango/user_permit/templates/account_permit.erb +5 -0
- data/lib/generators/cantango/user_permit/templates/user_permit.erb +23 -0
- data/lib/generators/cantango/user_permit/user_permit_generator.rb +36 -0
- data/lib/tasks/permits_tasks.rake +4 -0
- data/spec/cantango/ability/helper/permits_spec.rb +14 -0
- data/spec/cantango/builder/permit/account_type_spec.rb +25 -0
- data/spec/cantango/builder/permit/base_spec.rb +30 -0
- data/spec/cantango/builder/permit/special_spec.rb +25 -0
- data/spec/cantango/builder/permit/user_type_spec.rb +27 -0
- data/spec/cantango/config/engines/permit_spec.rb +14 -0
- data/spec/cantango/config/engines_spec.rb +142 -0
- data/spec/cantango/config/permit_registry_ex.rb +9 -0
- data/spec/cantango/config/permit_registry_spec.rb +8 -0
- data/spec/cantango/config/permits/accounts_spec.rb +23 -0
- data/spec/cantango/config/permits/disabling_spec.rb +38 -0
- data/spec/cantango/config/permits/enabling_spec.rb +19 -0
- data/spec/cantango/config/permits/execution_spec.rb +27 -0
- data/spec/cantango/config/permits/registration_spec.rb +15 -0
- data/spec/cantango/config/permits/tracking_spec.rb +19 -0
- data/spec/cantango/config/permits/types_spec.rb +27 -0
- data/spec/cantango/config/permits_spec.rb +76 -0
- data/spec/cantango/engine/permit_cached_spec.rb +52 -0
- data/spec/cantango/engine/permit_spec.rb +58 -0
- data/spec/cantango/engine/permit_static_dynamic_spec.rb +65 -0
- data/spec/cantango/executor/custom_permit_spec.rb +65 -0
- data/spec/cantango/executor/more_permit_spec.rb +45 -0
- data/spec/cantango/executor/permit_base_spec.rb +46 -0
- data/spec/cantango/executor/permit_spec.rb +53 -0
- data/spec/cantango/executor/special_permit_spec.rb +36 -0
- data/spec/cantango/factory/permits_spec.rb +70 -0
- data/spec/cantango/finder/account_permit_spec.rb +30 -0
- data/spec/cantango/finder/permit_spec.rb +23 -0
- data/spec/cantango/license/save_license_spec.rb +24 -0
- data/spec/cantango/license_spec.rb +0 -0
- data/spec/cantango/loader/categories_spec.rb +47 -0
- data/spec/cantango/loader/license_spec.rb +7 -0
- data/spec/cantango/macros/permit_spec.rb +38 -0
- data/spec/cantango/parser/categories_spec.rb +19 -0
- data/spec/cantango/permit/account_type_spec.rb +34 -0
- data/spec/cantango/permit/attribute_permit_spec.rb +65 -0
- data/spec/cantango/permit/base_spec.rb +106 -0
- data/spec/cantango/permit/class_methods_spec.rb +34 -0
- data/spec/cantango/permit/helper/execution_spec.rb +54 -0
- data/spec/cantango/permit/helper/host_spec.rb +29 -0
- data/spec/cantango/permit/helper/license_spec.rb +37 -0
- data/spec/cantango/permit/helper/naming_spec.rb +56 -0
- data/spec/cantango/permit/helper/state_spec.rb +24 -0
- data/spec/cantango/permit/special_default_spec.rb +2 -0
- data/spec/cantango/permit/special_first_spec.rb +2 -0
- data/spec/cantango/permit/user_type_spec.rb +78 -0
- data/spec/cantango/registry/permit_spec.rb +67 -0
- data/spec/fixtures/config/cantango_permissions.yml +49 -0
- data/spec/fixtures/config/categories.yml +6 -0
- data/spec/fixtures/config/evaluator_fixtures.yml +18 -0
- data/spec/fixtures/config/licenses.yml +4 -0
- data/spec/fixtures/config/permissions.yml +60 -0
- data/spec/fixtures/config/role_group.yml +4 -0
- data/spec/fixtures/config/roles.yml +4 -0
- data/spec/fixtures/config/test_permissions.yml +55 -0
- data/spec/fixtures/config/user_permissions.yml +8 -0
- data/spec/fixtures/models.rb +15 -0
- data/spec/fixtures/models/admin.rb +2 -0
- data/spec/fixtures/models/admin_account.rb +22 -0
- data/spec/fixtures/models/items.rb +11 -0
- data/spec/fixtures/models/permission.rb +12 -0
- data/spec/fixtures/models/simple_roles.rb +49 -0
- data/spec/fixtures/models/user.rb +52 -0
- data/spec/fixtures/models/user_account.rb +21 -0
- data/spec/fixtures/tango_fixtures.rb +29 -0
- data/spec/generators/cantango/account_role_permit_generator_spec.rb +35 -0
- data/spec/generators/cantango/account_role_permits_generator_spec.rb +59 -0
- data/spec/generators/cantango/license_generator_spec.rb +33 -0
- data/spec/generators/cantango/licenses_generator_spec.rb +58 -0
- data/spec/generators/cantango/role_permit_generator_spec.rb +35 -0
- data/spec/generators/cantango/role_permits_generator_spec.rb +58 -0
- data/spec/helpers/current_user_accounts.rb +20 -0
- data/spec/helpers/current_users.rb +10 -0
- data/spec/spec_helper.rb +24 -0
- metadata +325 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Finder
|
3
|
+
class Base
|
4
|
+
include CanTango::Helpers::Debug
|
5
|
+
|
6
|
+
attr_reader :type, :name
|
7
|
+
|
8
|
+
def initialize name, options
|
9
|
+
@name = name.to_s.underscore.to_sym
|
10
|
+
@type = options[:type]
|
11
|
+
raise ArgumentError, "Missing name of permit to find" if !name
|
12
|
+
raise ArgumentError, "Missing type of permit to find" if !type
|
13
|
+
end
|
14
|
+
|
15
|
+
def permit
|
16
|
+
debug permit_msg(found_permit)
|
17
|
+
found_permit
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def permit_msg
|
23
|
+
found_permit.nil? ? "no #{type} permits found named #{name}" : "#{type} permit found named: #{name} -> #{found_permit}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def permits
|
27
|
+
registered_permits.registered_for(type)
|
28
|
+
end
|
29
|
+
|
30
|
+
def registered_permits
|
31
|
+
CanTango.config.permits
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Finder
|
3
|
+
module Permit
|
4
|
+
class Account < Base
|
5
|
+
attr_reader :user_account
|
6
|
+
|
7
|
+
def initialize name, options = {}
|
8
|
+
super
|
9
|
+
@account = options[:account]
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_permit
|
13
|
+
retrieve_permit
|
14
|
+
end
|
15
|
+
|
16
|
+
def permits
|
17
|
+
account_registry.registered_for(type)
|
18
|
+
end
|
19
|
+
|
20
|
+
def account_registry
|
21
|
+
CanTango.config.permits.accounts.registry_for(account)
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def found_permit
|
27
|
+
@found_permit ||= permits.registered[name]
|
28
|
+
end
|
29
|
+
|
30
|
+
def permits_to_try
|
31
|
+
[permit]
|
32
|
+
end
|
33
|
+
|
34
|
+
# this is used to namespace role permits for a specific type of user account
|
35
|
+
# this allows role permits to be defined differently for each user account (and hence sub application) if need be
|
36
|
+
# otherwise it will fall back to the generic role permit (the one which is not wrapped in a user account namespace)
|
37
|
+
def permit_class
|
38
|
+
[permit_ns , permit_class].join('::')
|
39
|
+
end
|
40
|
+
|
41
|
+
def permit_ns
|
42
|
+
"#{account}Permits"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Finder
|
3
|
+
module Permit
|
4
|
+
class Base < CanTango::Finder::Base
|
5
|
+
include CanTango::Helpers::Debug
|
6
|
+
|
7
|
+
# This class is used to find the right permit, possible scoped for a specific user account
|
8
|
+
attr_reader :account
|
9
|
+
|
10
|
+
def initialize name, options = {}
|
11
|
+
super
|
12
|
+
@account = options[:account]
|
13
|
+
end
|
14
|
+
|
15
|
+
def find_permit
|
16
|
+
raise find_error if !retrieve_permit
|
17
|
+
retrieve_permit
|
18
|
+
end
|
19
|
+
|
20
|
+
def account_finder name
|
21
|
+
@account_finder ||= CanTango::Finder::Permit::Account.new name, :type => type, :account => account
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def permit_msg found
|
27
|
+
found.nil? ? "no permits found for #{name}" : "permits registered for name: #{name} -> #{found}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def found_permit
|
31
|
+
@found_permit ||= permits.registered.send(name)
|
32
|
+
end
|
33
|
+
alias_method :registered?, :found_permit
|
34
|
+
|
35
|
+
def account_permit
|
36
|
+
account_finder(name).find_permit
|
37
|
+
end
|
38
|
+
|
39
|
+
def find_error
|
40
|
+
"The #{type} Permit for #{name} could not be loaded. You need to define a coresponding Permit class"
|
41
|
+
end
|
42
|
+
|
43
|
+
def retrieve_permit
|
44
|
+
@found_permit ||= permits_to_try.first
|
45
|
+
end
|
46
|
+
|
47
|
+
def permits_to_try
|
48
|
+
@permits_to_try ||= [account_permit, permit].compact
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Loader
|
3
|
+
class Categories < Yaml
|
4
|
+
attr_reader :file_name, :categories
|
5
|
+
|
6
|
+
def initialize file = nil
|
7
|
+
begin
|
8
|
+
@file_name = file || categories_config_file
|
9
|
+
yml_content.each do |key, value|
|
10
|
+
parser.parse(categories, key, value)
|
11
|
+
end
|
12
|
+
|
13
|
+
rescue RuntimeError => e
|
14
|
+
raise "CanTango::Categories::Loader Error: The categories for the file #{file_name} could not be loaded - cause was #{e}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def category name
|
19
|
+
categories.category(name).subjects
|
20
|
+
rescue
|
21
|
+
[]
|
22
|
+
end
|
23
|
+
|
24
|
+
def categories
|
25
|
+
@categories ||= CanTango.config.categories
|
26
|
+
end
|
27
|
+
|
28
|
+
def parser
|
29
|
+
@parser ||= CanTango::Parser::Categories.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def load_categories name = nil
|
33
|
+
name ||= categories_config_file
|
34
|
+
CanTango::Loader::Categories.new name
|
35
|
+
end
|
36
|
+
|
37
|
+
def categories_config_file
|
38
|
+
get_config_file 'categories'
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_config_file name
|
42
|
+
File.join(config_path, "#{name}.yml")
|
43
|
+
end
|
44
|
+
|
45
|
+
def config_path
|
46
|
+
CanTango.config.engine(:permit).config_path
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Loader
|
3
|
+
class License
|
4
|
+
def load_rules name = nil
|
5
|
+
return if !licenses || licenses.permissions.empty?
|
6
|
+
name ||= self.class.to_s.gsub(/License$/, "").underscore
|
7
|
+
|
8
|
+
return if licenses.permissions[name].nil?
|
9
|
+
|
10
|
+
licenses.permissions[name].can_eval do |permission_statement|
|
11
|
+
instance_eval permission_statement
|
12
|
+
end
|
13
|
+
licenses.permissions[name].cannot_eval do |permission_statement|
|
14
|
+
instance_eval permission_statement
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Permit
|
3
|
+
class MissingTypeError < StandardError; end
|
4
|
+
class MissingNameError < StandardError; end
|
5
|
+
end
|
6
|
+
|
7
|
+
module Macros
|
8
|
+
module Permit
|
9
|
+
def tango_permit options = {}
|
10
|
+
name = options[:name] || CanTango::Macros::Permit.permit_name(self)
|
11
|
+
account = options[:account] || CanTango::Macros::Permit.account_name(self)
|
12
|
+
super_class = self.superclass
|
13
|
+
class_type = unless super_class == Object
|
14
|
+
super_class.respond_to?(:type) ? super_class.type : nil
|
15
|
+
end
|
16
|
+
type = options[:type] || class_type
|
17
|
+
|
18
|
+
raise CanTango::Permit::MissingNameError, "Name of permit could not be determined, try specifying a :name option" if name.nil?
|
19
|
+
raise CanTango::Permit::MissingTypeError, "Type of permit could not be determined, try specifying a :type option" if type.nil?
|
20
|
+
|
21
|
+
reg_options = {:name => name, :type => type, :account => account}
|
22
|
+
CanTango.config.permits.register self, reg_options
|
23
|
+
reg_options
|
24
|
+
end
|
25
|
+
extend CanTango::Permit::Helper::Naming
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Class
|
31
|
+
include CanTango::Macros::Permit
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Parser
|
3
|
+
class Categories
|
4
|
+
def parse categories, key, obj, &blk
|
5
|
+
case obj
|
6
|
+
when Array
|
7
|
+
categories[key] = obj
|
8
|
+
else
|
9
|
+
raise "Each key must have a YAML hash that defines which models make up the category (related kinds of items)"
|
10
|
+
end
|
11
|
+
yield categories if blk
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Registry
|
3
|
+
module Permit
|
4
|
+
def registry_for type
|
5
|
+
raise ArgumentError, "Not an available permit type, was: #{type} - available: #{available_types}" unless available_type? type
|
6
|
+
inst_var_name = "@#{type}"
|
7
|
+
instance_variable_set(inst_var_name, CanTango::Registry::Hash.new) if !instance_variable_get(inst_var_name)
|
8
|
+
instance_variable_get(inst_var_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
def registered_for type, name = nil
|
12
|
+
name ? registry_for(type)[name.to_s] : registry_for(type)
|
13
|
+
end
|
14
|
+
|
15
|
+
def all
|
16
|
+
(available_types - [:special]).map{|type| registry_for(type) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def show_all
|
20
|
+
(available_types - [:special]).map{|type| "#{type} -> #{show_hash_reg(registry_for(type))}" }.join("\n")
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def show_hash_reg reg
|
26
|
+
reg.registered.map {|key, value| "#{key} (#{value})" }.join(',')
|
27
|
+
end
|
28
|
+
|
29
|
+
def available_type? type
|
30
|
+
available_types.include? type
|
31
|
+
end
|
32
|
+
|
33
|
+
def available_types
|
34
|
+
CanTango.config.permits.types.available
|
35
|
+
end
|
36
|
+
|
37
|
+
class Base
|
38
|
+
include CanTango::Registry::Permit
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'generators/cantango/base'
|
2
|
+
require 'generators/cantango/license_base'
|
3
|
+
require 'generators/cantango/permit_generator'
|
4
|
+
|
5
|
+
module Cantango
|
6
|
+
module Generators
|
7
|
+
class AccountPermitGenerator < Cantango::Generators::Base
|
8
|
+
desc "Creates a Permit for an account in 'app/permits' with specific permissions and/or licenses"
|
9
|
+
|
10
|
+
argument :account, :type => :string, :desc => "Account class to create permit for"
|
11
|
+
|
12
|
+
class_option :licenses, :type => :array, :default => [], :desc => "Licenses to use in Permit"
|
13
|
+
|
14
|
+
class_option :account, :type => :string,
|
15
|
+
:desc => "Generate permits for a specific user account"
|
16
|
+
|
17
|
+
class_option :group, :type => :boolean, :default => false, :desc => "Generate permit for a role group"
|
18
|
+
|
19
|
+
source_root File.dirname(__FILE__) + '/templates'
|
20
|
+
|
21
|
+
def main_flow
|
22
|
+
template_permit account
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def is_account?
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
include Cantango::Generators::LicenseBase
|
32
|
+
include Cantango::Generators::PermitGenerator
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class <%= account.to_s.camelize %>AccountPermit < CanTango::AccountPermit
|
2
|
+
def initialize ability
|
3
|
+
super
|
4
|
+
end
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
def permit_rules
|
9
|
+
# insert your can, cannot and any other rule statements here
|
10
|
+
<%= permit_logic.strip %>
|
11
|
+
<%= license_logic %>
|
12
|
+
end
|
13
|
+
|
14
|
+
module Cached
|
15
|
+
def permit_rules
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module NonCached
|
20
|
+
def permit_rules
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'generators/cantango/basic'
|
2
|
+
|
3
|
+
module Cantango
|
4
|
+
module Generators
|
5
|
+
class Base < ::Rails::Generators::Base
|
6
|
+
|
7
|
+
include Cantango::Generators::Basic
|
8
|
+
|
9
|
+
CAN_ACTIONS = [:create, :update, :manage, :read, :access]
|
10
|
+
|
11
|
+
CAN_ACTIONS.each do |action|
|
12
|
+
class_eval %{
|
13
|
+
class_option :#{action}, :type => :array, :default => [], :desc => "Models allowed to #{action}"
|
14
|
+
class_option :not_#{action}, :type => :array, :default => [], :desc => "Models not allowed to #{action}"
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
[:user, :account].each do |name|
|
21
|
+
define_method :"#{name}?" do
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
CAN_ACTIONS.each do |action|
|
27
|
+
class_eval %{
|
28
|
+
def #{action}
|
29
|
+
options[:#{action}]
|
30
|
+
end
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
CAN_ACTIONS.each do |action|
|
35
|
+
class_eval %{
|
36
|
+
def not_#{action}
|
37
|
+
options[:not_#{action}]
|
38
|
+
end
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def rules_logic
|
43
|
+
can_logic
|
44
|
+
cannot_logic
|
45
|
+
end
|
46
|
+
|
47
|
+
def can_logic
|
48
|
+
CAN_ACTIONS.map do |action|
|
49
|
+
send(action).map do |c|
|
50
|
+
"can(:#{action}, #{act_model(c)})"
|
51
|
+
end.join("\n ")
|
52
|
+
end.join("\n")
|
53
|
+
end
|
54
|
+
|
55
|
+
def cannot_logic
|
56
|
+
CAN_ACTIONS.map do |action|
|
57
|
+
send(action).map do |c|
|
58
|
+
"can(:#{action}, #{act_model(c)})"
|
59
|
+
end.join("\n ")
|
60
|
+
end.join("\n")
|
61
|
+
end
|
62
|
+
|
63
|
+
def act_model name
|
64
|
+
return ':all' if name == 'all'
|
65
|
+
name.camelize
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|