the_role 1.4.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/config/routes.rb CHANGED
@@ -1,18 +1,16 @@
1
1
  Rails.application.routes.draw do
2
2
  namespace :admin do
3
3
  resources :roles do
4
- member do
5
- get :new
6
- get :index
7
- post :new_role_section
8
- post :new_role_policy
9
- end
10
- resources :sections, :controller => :role_section do
4
+ resources :sections, :controller => :role_sections, :only => :none do
5
+ collection do
6
+ post :create
7
+ post :create_rule
8
+ end
11
9
  member do
12
- get :new_policy
13
- delete :delete_policy
10
+ delete :destroy
11
+ delete :destroy_rule
14
12
  end
15
- end#sections
16
- end#policy
17
- end#admin
13
+ end
14
+ end
15
+ end
18
16
  end
@@ -2,10 +2,10 @@ class CreateRoles < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :roles do |t|
4
4
 
5
- t.string :name
6
- t.string :title
7
- t.text :description
8
- t.text :the_role, :null => false
5
+ t.string :name, :null => false
6
+ t.string :title, :null => false
7
+ t.text :description, :null => false
8
+ t.text :the_role, :null => false
9
9
 
10
10
  t.timestamps
11
11
  end
data/lib/the_role.rb CHANGED
@@ -6,111 +6,16 @@ require "the_role/engine"
6
6
  require "the_role/version"
7
7
  require "the_role/the_class_exists"
8
8
 
9
+ require "the_role/modules/base"
10
+ require "the_role/modules/param_helper"
11
+ require "the_role/modules/user_model"
12
+ require "the_role/modules/role_model"
13
+ require "the_role/modules/controller_requires"
14
+
9
15
  module TheRole
16
+ # include TheRole::Base
10
17
  # include TheRole::Requires
11
18
  # include TheRole::UserModel
12
19
  # include TheRole::RoleModel
13
-
14
- NAME_SYMBOLS = /^[a-zA-Z][a-zA-Z0-9_\-]*[a-zA-Z0-9]$/
15
-
16
- # TheRole.get(@role.the_role)
17
- def self.get str
18
- str = str.is_a?(String) ? str : String.new
19
- hash = YAML::load(str)
20
- hash ? hash : Hash.new
21
- end
22
-
23
- module UserModel
24
- def self.included(base)
25
- base.class_eval do
26
- belongs_to :role
27
- attr_accessible :role
28
- # when user changed - @the_role should be reload
29
- after_save { |user| user.instance_variable_set(:@the_role, nil) }
30
- end
31
- end
32
-
33
- def the_role
34
- @the_role ||= self.role ? TheRole.get(self.role.the_role) : Hash.new
35
- end
36
-
37
- def admin?
38
- role = self.the_role[:system] ? self.the_role[:system][:administrator] : false
39
- role && role.is_a?(TrueClass)
40
- end
41
-
42
- def moderator? section
43
- return true if self.admin?
44
- role = self.the_role[:moderator] ? self.the_role[:moderator][section.to_sym] : false
45
- role && role.is_a?(TrueClass)
46
- end
47
-
48
- # TRUE if user has role - administartor of system
49
- # TRUE if user is moderator of this section (controller_name)
50
- # FALSE when this section (or role) is nil
51
- # return current value of role (TRUE|FALSE) if it exists
52
- def has_role?(section, policy)
53
- return true if self.admin?
54
- return true if self.moderator? section
55
- if self.the_role[section.to_sym] && self.the_role[section.to_sym][policy.to_sym]
56
- self.the_role[section.to_sym][policy.to_sym].is_a?(TrueClass)
57
- else
58
- false
59
- end
60
- end
61
-
62
- # FALSE if object is nil
63
- # If object is a USER - check for youself
64
- # Check for owner field - :user_id
65
- # Check for owner _object_ if owner field is not :user_id
66
- def owner?(obj)
67
- return false unless obj
68
- return true if self.admin?
69
- return true if self.moderator? obj.class.to_s.tableize # moderator? 'pages'
70
- return self.id == obj.id if obj.is_a?(User)
71
- return self.id == obj[:user_id] if obj[:user_id]
72
- return self.id == obj[:user][:id] if obj[:user]
73
- false
74
- end
75
- end#UserModel
76
-
77
- module RoleModel
78
- def self.included(base)
79
- base.class_eval do
80
- has_many :users
81
- validates :name, :presence => {:message => I18n.translate('the_role.name_presence')}
82
- validates :title, :presence => {:message => I18n.translate('the_role.title_presence')}
83
- end
84
- end
85
- end#RoleModel
86
-
87
- # for application controller
88
- # @the_role_object should be defined with before_filter
89
- # @the_role_object = @page
90
- module Requires
91
- private
92
-
93
- def the_role_access_denied
94
- flash[:error] = t('the_role.access_denied')
95
- redirect_to root_path
96
- end
97
-
98
- # before_filter :role_require
99
- def the_role_require
100
- the_role_access_denied unless current_user.has_role?(controller_name, action_name)
101
- end
102
-
103
- # before_filter :the_role_object
104
- # define class variable for *the_owner_require* filter with Controller class name
105
- # @the_role_object = @article
106
- def the_role_object
107
- variable_name = self.class.to_s.tableize.split('_').first.singularize.split('/').last
108
- @the_role_object = self.instance_variable_get("@#{variable_name}")
109
- end
110
-
111
- # before_filter :the_owner_require
112
- def the_owner_require
113
- the_role_access_denied unless current_user.owner?(@the_role_object)
114
- end
115
- end#Requires
116
- end#TheRole
20
+ # include TheRole::ParamHelper
21
+ end
data/lib/the_role/hash.rb CHANGED
@@ -1,25 +1,29 @@
1
1
  class Hash
2
- def the_reset!(default_value= false)
3
- base= self
4
- base.each do |key, v|
5
- if base[key.to_sym].is_a?(Hash)
6
- base[key.to_sym]= base[key.to_sym].the_reset!(default_value)
7
- else
8
- base[key.to_sym]= default_value
9
- end
2
+ # load 'the_role/hash.rb' - UPDATE, BUT NOT RELOAD
3
+
4
+ # {'a b' => 1, "x y" => {'hello' => 1, :hello => 2} }.underscorify_keys => {:a_b=>1, :x_y=>{:hello=>2}}
5
+ def underscorify_keys
6
+ hash = {}
7
+ self.each do |key, value|
8
+ new_key = key.to_s.parameterize.underscore.to_sym
9
+ hash[new_key] = self[key].is_a?(Hash) ? self[key].underscorify_keys : value
10
10
  end
11
+ hash
11
12
  end
12
13
 
13
- def the_merge!(hash= nil, default_value= true)
14
- return self unless hash.is_a?(Hash)
15
- base= self
16
- hash.each do |key, v|
17
- if base[key.to_sym].is_a?(Hash) && hash[key.to_sym].is_a?(Hash)
18
- base[key.to_sym]= base[key.to_sym].the_merge!(hash[key.to_sym], default_value)
19
- else
20
- base[key.to_sym]= default_value
21
- end
14
+ def deep_reset(default = false)
15
+ hash = dup
16
+ hash.each do |key, value|
17
+ hash[key] = hash[key].is_a?(Hash) ? hash[key].deep_reset(default) : default
22
18
  end
23
- base.to_hash
19
+ hash
20
+ end
21
+
22
+ def underscorify_keys!
23
+ replace underscorify_keys
24
+ end
25
+
26
+ def deep_reset!(default = false)
27
+ replace deep_reset(default)
24
28
  end
25
29
  end
@@ -0,0 +1,23 @@
1
+ module TheRole
2
+ module Base
3
+ def has_role? section_name, rule_name
4
+ hash = role_hash
5
+ section_name = param_prepare(section_name)
6
+ rule_name = param_prepare(rule_name)
7
+ return true if hash[:system] and hash[:system][:administrator]
8
+ return true if hash[:moderator] and hash[:moderator][section_name]
9
+ return false unless hash[section_name]
10
+ return false unless hash[section_name].key? rule_name
11
+ hash[section_name][rule_name]
12
+ end
13
+
14
+ def moderator? section_name
15
+ section_name = param_prepare(section_name)
16
+ has_role? section_name, :any_crazy_name
17
+ end
18
+
19
+ def admin?
20
+ has_role? :any_crazy_name, :any_crazy_name
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ module TheRole
2
+ module Requires
3
+ private
4
+
5
+ def role_access_denied
6
+ flash[:error] = t('the_role.access_denied')
7
+ redirect_to root_path
8
+ end
9
+
10
+ # before_filter :role_require
11
+ def role_require
12
+ role_access_denied unless current_user.has_role?(controller_name, action_name)
13
+ end
14
+
15
+ # before_filter :simple_object_finder
16
+ # define class variable for *owner_require* filter with Controller class name
17
+ # @object_for_ownership_checking = @article
18
+ def simple_object_finder
19
+ variable_name = self.class.to_s.tableize.split('_').first.singularize.split('/').last
20
+ @object_for_ownership_checking = self.instance_variable_get("@#{variable_name}")
21
+ end
22
+
23
+ # before_filter :owner_require
24
+ def owner_require
25
+ role_access_denied unless current_user.owner?(@object_for_ownership_checking)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ module TheRole
2
+ module ParamHelper
3
+ def param_prepare param
4
+ param.to_s.parameterize.underscore.to_sym
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,121 @@
1
+ module TheRole
2
+ module RoleModel
3
+ include TheRole::Base
4
+ include TheRole::ParamHelper
5
+
6
+ def role_hash; to_hash; end
7
+ alias_method :has?, :has_role?
8
+
9
+ def has_section? section_name
10
+ section_name = param_prepare(section_name)
11
+ to_hash.key? section_name
12
+ end
13
+
14
+ def self.included(base)
15
+ base.class_eval do
16
+ has_many :users
17
+ validates :name, :presence => true, :uniqueness => true
18
+ validates :title, :presence => true, :uniqueness => true
19
+ validates :description, :presence => true
20
+ before_create do
21
+ self.name = param_prepare(name)
22
+ self.the_role = {}.to_yaml
23
+ end
24
+
25
+ # C
26
+
27
+ def create_section section_name = nil
28
+ return false unless section_name
29
+ role = to_hash
30
+ section_name = param_prepare(section_name)
31
+ return false if section_name.blank?
32
+ return true if role[section_name]
33
+ role[section_name] = {}
34
+ update_attributes(:the_role => role.to_yaml)
35
+ end
36
+
37
+ def create_rule section_name, rule_name
38
+ return false unless create_section(section_name)
39
+ role = to_hash
40
+ rule_name = param_prepare(rule_name)
41
+ section_name = param_prepare(section_name)
42
+ return true if role[section_name][rule_name]
43
+ role[section_name][rule_name] = false
44
+ update_attributes(:the_role => role.to_yaml)
45
+ end
46
+
47
+ # R
48
+
49
+ def to_hash
50
+ begin YAML::load(the_role) rescue {} end
51
+ end
52
+
53
+ def to_yaml
54
+ the_role
55
+ end
56
+
57
+ def to_s
58
+ the_role
59
+ end
60
+
61
+ # U
62
+
63
+ # source_hash will be reset to false
64
+ # except true items from new_role_hash
65
+ # all keys will become symbols
66
+ # look at lib/the_role/hash.rb to find definition of *underscorify_keys* method
67
+ def update_role new_role_hash
68
+ new_role_hash = new_role_hash.try(:to_hash) || {}
69
+ new_role = new_role_hash.underscorify_keys
70
+ role = to_hash.underscorify_keys.deep_reset
71
+ role.deep_merge! new_role
72
+ update_attributes(:the_role => role.to_yaml)
73
+ end
74
+
75
+ def rule_on section_name, rule_name
76
+ role = to_hash
77
+ rule_name = param_prepare(rule_name)
78
+ section_name = param_prepare(section_name)
79
+ return false unless role[section_name]
80
+ return false unless role[section_name].key? rule_name
81
+ return true if role[section_name][rule_name]
82
+ role[section_name][rule_name] = true
83
+ update_attributes(:the_role => role.to_yaml)
84
+ end
85
+
86
+ def rule_off section_name, rule_name
87
+ role = to_hash
88
+ rule_name = param_prepare(rule_name)
89
+ section_name = param_prepare(section_name)
90
+ return false unless role[section_name]
91
+ return false unless role[section_name].key? rule_name
92
+ return true unless role[section_name][rule_name]
93
+ role[section_name][rule_name] = false
94
+ update_attributes(:the_role => role.to_yaml)
95
+ end
96
+
97
+ # D
98
+
99
+ def delete_section section_name = nil
100
+ return false unless section_name
101
+ role = to_hash
102
+ section_name = param_prepare(section_name)
103
+ return false if section_name.blank?
104
+ return false unless role[section_name]
105
+ role.delete section_name
106
+ update_attributes(:the_role => role.to_yaml)
107
+ end
108
+
109
+ def delete_rule section_name, rule_name
110
+ role = to_hash
111
+ rule_name = param_prepare(rule_name)
112
+ section_name = param_prepare(section_name)
113
+ return false unless role[section_name]
114
+ return false unless role[section_name].key? rule_name
115
+ role[section_name].delete rule_name
116
+ update_attributes(:the_role => role.to_yaml)
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,32 @@
1
+ module TheRole
2
+ module UserModel
3
+ include TheRole::Base
4
+ include TheRole::ParamHelper
5
+ def role_hash; @role_hash ||= role.to_hash; end
6
+
7
+ # FALSE if object is nil
8
+ # If object is a USER - check for youself
9
+ # Check for owner field - :user_id
10
+ # Check for owner _object_ if owner field is not :user_id
11
+ def owner? obj
12
+ return false unless obj
13
+ return true if admin?
14
+
15
+ section_name = obj.class.to_s.tableize
16
+ return true if moderator?(section_name)
17
+
18
+ return id == obj.id if obj.is_a?(User)
19
+ return id == obj[:user_id] if obj[:user_id]
20
+ return id == obj[:user][:id] if obj[:user]
21
+ false
22
+ end
23
+
24
+ def self.included(base)
25
+ base.class_eval do
26
+ belongs_to :role
27
+ attr_accessible :role
28
+ after_save { |user| user.instance_variable_set(:@role_hash, nil) }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module TheRole
2
- VERSION = "1.4.1"
2
+ VERSION = "1.5.0"
3
3
  end
data/pic.png ADDED
Binary file
data/the_role.gemspec CHANGED
@@ -17,10 +17,9 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
-
21
- # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
24
- s.add_dependency 'haml', '~> 3.0'
20
+
21
+ s.add_dependency 'haml'
25
22
  s.add_dependency 'sass'
23
+ s.add_dependency 'sass-rails'
24
+ s.add_dependency 'coffee-rails'
26
25
  end