roled 0.0.3 → 0.0.4

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.
@@ -17,6 +17,10 @@ module Roled
17
17
  def copy_migrations
18
18
  migration_template "migration.rb", "db/migrate/install_roled.rb"
19
19
  end
20
+
21
+ def copy_initializer
22
+ copy_file "roled.rb", "config/initializers/roled.rb"
23
+ end
20
24
  end
21
25
  end
22
26
  end
@@ -0,0 +1,3 @@
1
+ Roled::Core.user = :current_user
2
+ #want to make sure that a module gets skipped?
3
+ #Roled::Core.skip_module = [:devise]
@@ -2,6 +2,7 @@ require 'roled/rake'
2
2
  require 'roled/models/aco'
3
3
  require 'roled/models/aro'
4
4
  require 'roled/models/acos_aro'
5
+ require 'roled/exceptions'
5
6
  require 'roled/acts_as'
6
7
  require 'roled/core'
7
8
 
@@ -6,29 +6,21 @@ module Roled
6
6
  end
7
7
 
8
8
  def permission?(controller, action)
9
- return has_permission_to?(controller,action)
9
+ has_permission_to?(controller,action)
10
10
  end
11
11
 
12
12
  def has_permission_to?(controller, action)
13
- if !controller || !action
14
- return false
15
- end
16
-
13
+ return false if !controller || !action
17
14
  options = self.class.global_options
18
- groups = []
19
- if !options.blank?
15
+ if !options.blank?
16
+ groups = []
20
17
  options.each do |key,name|
21
- if key == :belongs_to
22
- if(defined?(self.send(name)))
23
- groups << self.send(name)
24
- end
25
- end
18
+ groups << self.send(name) if key == :belongs_to && defined?(self.send(name))
26
19
  end
27
20
  end
28
-
29
21
  parent = "#{controller}_controller"
30
22
  if !Aco.where(:parent => parent, :action => action).exists?
31
- return false
23
+ raise UnknownAro, "Action is undefined, run rake acos:generate"
32
24
  end
33
25
 
34
26
  ids = Aco.where("parent IN (?) AND action is NULL OR parent = ? AND action = ?", [parent,"all"], parent, action).order('action desc')
@@ -5,24 +5,36 @@ module Roled
5
5
  included do
6
6
  end
7
7
 
8
- def authorize_user!(user, options = {})
9
- if defined?(options[:skip_module])
10
- if params[:controller] =~ /#{options[:skip_module]}\/\.*/
8
+ def authorize_user!
9
+ if defined?(@@modules)
10
+ current = params[:controller].split('/', 2)[0]
11
+ if @@modules.include?(current.to_sym)
11
12
  return
12
13
  end
13
14
  end
15
+
14
16
  if(!user.blank?)
15
- action = params[:action]
16
- if(!action)
17
- action = "index"
18
- end
17
+ action = params[:action] || "index"
19
18
  request = user.has_permission_to? params[:controller], action
20
19
  if(request != nil && request)
21
20
  else
22
- redirect_to root_url, :flash => {:error => "Unauthorized"}
21
+ raise Unauthorized
23
22
  end
24
23
  end
25
24
  end
26
25
 
26
+ def self.user=(user)
27
+ @@user = user
28
+ end
29
+
30
+ def self.skip_module=(modules)
31
+ @@modules = modules
32
+ end
33
+
34
+ def user
35
+ send(@@user)
36
+ end
37
+
38
+
27
39
  end
28
40
  end
@@ -0,0 +1,23 @@
1
+ module Roled
2
+
3
+ class Error < StandardError; end
4
+
5
+ class UnknownAro < Error; end
6
+
7
+ class Unauthorized < Error
8
+ attr_reader :action, :subject
9
+ attr_writer :default_message
10
+
11
+ def initialize(message = nil, action = nil, subject = nil)
12
+ @message = message
13
+ @action = action
14
+ @subject = subject
15
+ @default_message = I18n.t(:"unauthorized.default", :default => "You are not authorized to access this page.")
16
+ end
17
+
18
+ def to_s
19
+ @message || @default_message
20
+ end
21
+ end
22
+
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Roled
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roled
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-18 00:00:00.000000000 Z
12
+ date: 2013-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -53,8 +53,10 @@ extra_rdoc_files: []
53
53
  files:
54
54
  - lib/generators/roled/install_generator.rb
55
55
  - lib/generators/roled/templates/migration.rb
56
+ - lib/generators/roled/templates/roled.rb
56
57
  - lib/roled/acts_as.rb
57
58
  - lib/roled/core.rb
59
+ - lib/roled/exceptions.rb
58
60
  - lib/roled/models/aco.rb
59
61
  - lib/roled/models/acos_aro.rb
60
62
  - lib/roled/models/aro.rb