role_on 0.2.7
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.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +21 -0
- data/README.md +79 -0
- data/Rakefile +56 -0
- data/TODO +1 -0
- data/VERSION +1 -0
- data/generators/role_on/role_on_generator.rb +8 -0
- data/generators/role_on/templates/app/models/role.rb +7 -0
- data/generators/role_on/templates/db/migrate/migration.rb +24 -0
- data/init.rb +1 -0
- data/lib/role_on.rb +51 -0
- data/role_on.gemspec +47 -0
- metadata +68 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009 Daniel Huckstep
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# role_on
|
2
|
+
|
3
|
+
Really Simple Roles
|
4
|
+
|
5
|
+
# Assumptions
|
6
|
+
|
7
|
+
I assume you have a model called User for your user authentication stuff.
|
8
|
+
|
9
|
+
# Usage
|
10
|
+
|
11
|
+
config.gem 'darkhelmet-role_on', :lib => 'role_on', :source => 'http://gems.github.com'
|
12
|
+
|
13
|
+
Add
|
14
|
+
|
15
|
+
def store_location
|
16
|
+
session[:return_to] = request.request_uri
|
17
|
+
end
|
18
|
+
|
19
|
+
def redirect_back_or_default(default)
|
20
|
+
redirect_to(session[:return_to] || default)
|
21
|
+
session[:return_to] = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def access_denied
|
25
|
+
flash[:error] = 'You are not authorized to perform this action'
|
26
|
+
redirect_back_or_default '/'
|
27
|
+
end
|
28
|
+
|
29
|
+
Or similar to you application controller, and setup store_location as an after_fitler, and all of them as helper methods
|
30
|
+
|
31
|
+
after_filter :store_location
|
32
|
+
helper_method :store_location, :redirect_back_or_default, :access_denied
|
33
|
+
|
34
|
+
Include RoleOn in your application controller and User model
|
35
|
+
|
36
|
+
include RoleOn
|
37
|
+
|
38
|
+
Generate model and migration
|
39
|
+
|
40
|
+
./script/generate role_on
|
41
|
+
|
42
|
+
Migrate
|
43
|
+
|
44
|
+
rake db:migrate
|
45
|
+
|
46
|
+
Do your own thing for managing roles.
|
47
|
+
|
48
|
+
Start locking down your controllers
|
49
|
+
|
50
|
+
role_on(:admin, :on => [:new,:create,:destroy])
|
51
|
+
role_on(:regular, :only => [:edit,:update])
|
52
|
+
|
53
|
+
Add your views
|
54
|
+
|
55
|
+
if current_user.has_role?(:admin) # do stuff
|
56
|
+
|
57
|
+
Can also use except
|
58
|
+
|
59
|
+
role_on(:admin, :except => [:index,:show])
|
60
|
+
|
61
|
+
Can specify :sa to allow a 'superadmin' to gain access even if they don't have the specific role
|
62
|
+
|
63
|
+
role_on(:foo_admin, :sa => :super_admin)
|
64
|
+
|
65
|
+
In that case, users who either have the :foo_admin or :super_admin role will have access
|
66
|
+
|
67
|
+
You can also define a role_on_defaults method on things to define default arguments
|
68
|
+
|
69
|
+
In application_controller:
|
70
|
+
|
71
|
+
def role_on_defaults
|
72
|
+
{ :sa => :my_sa_role }
|
73
|
+
end
|
74
|
+
|
75
|
+
In all sub controllers, :my_sa_role will be the value of :sa. This can be overridden.
|
76
|
+
|
77
|
+
# License
|
78
|
+
|
79
|
+
See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "role_on"
|
8
|
+
gem.summary = %Q{Really simple roles}
|
9
|
+
gem.email = "darkhelmet@darkhelmetlive.com"
|
10
|
+
gem.homepage = "http://github.com/darkhelmet/role_on"
|
11
|
+
gem.authors = ["Daniel Huckstep"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
end
|
14
|
+
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
if File.exist?('VERSION.yml')
|
45
|
+
config = YAML.load(File.read('VERSION.yml'))
|
46
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
47
|
+
else
|
48
|
+
version = ""
|
49
|
+
end
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "role_on #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
56
|
+
|
data/TODO
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
* class methods on user to add helper methods for finding admins, etc
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.7
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class SetupRoleOn < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :roles, :force => true do |t|
|
4
|
+
t.string :name
|
5
|
+
t.timestamps
|
6
|
+
end
|
7
|
+
|
8
|
+
add_index :roles, :name
|
9
|
+
|
10
|
+
create_table :user_roles, :id => false, :force => true do |t|
|
11
|
+
t.integer :role_id, :user_id
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index :user_roles, :role_id
|
15
|
+
add_index :user_roles, :user_id
|
16
|
+
|
17
|
+
r = Role.create(:name => 'admin')
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.down
|
21
|
+
drop_table :roles
|
22
|
+
drop_table :roles_users
|
23
|
+
end
|
24
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'role_on'
|
data/lib/role_on.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module RoleOn
|
2
|
+
module RoleOnControllerMethods
|
3
|
+
def role_on(role, options = {})
|
4
|
+
before_filter do |c|
|
5
|
+
options = c.__send__(:role_on_defaults).merge(options) if (c.methods | c.protected_methods | c.private_methods).include?('role_on_defaults')
|
6
|
+
action = c.params[:action].intern
|
7
|
+
user_roles = c.__send__(:current_user).roles.map(&:name).map(&:intern)
|
8
|
+
restricted_actions = if options.include?(:on)
|
9
|
+
[options[:on]].flatten
|
10
|
+
elsif options.include?(:only)
|
11
|
+
[options[:only]].flatten
|
12
|
+
elsif options.include?(:except)
|
13
|
+
c.class.action_methods.to_a.map(&:intern) - [options[:except]].flatten
|
14
|
+
else
|
15
|
+
c.class.action_methods.to_a.map(&:intern)
|
16
|
+
end
|
17
|
+
if restricted_actions.include?(action) && !user_roles.include?(role) && (options.include?(:sa) ? !user_roles.include?(options[:sa]) : false)
|
18
|
+
c.__send__(:access_denied)
|
19
|
+
false
|
20
|
+
else
|
21
|
+
true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module RoleOnUserInstanceMethods
|
28
|
+
def has_role?(*roles)
|
29
|
+
return false if self.roles.empty?
|
30
|
+
roles.reject { |r| self.roles.include?(Role[r]) }.empty?
|
31
|
+
end
|
32
|
+
alias :has_roles? :has_role?
|
33
|
+
end
|
34
|
+
|
35
|
+
module RoleOnUserClassMethods
|
36
|
+
def helper_for(role,name = role.to_s.pluralize)
|
37
|
+
named_scope(name, lambda { { :conditions => ['roles.id = ?', Role[role].id], :joins => :roles } })
|
38
|
+
named_scope("non_#{name}", lambda { { :conditions => [ 'roles.id is ? or roles.id != ?', nil, Role[role].id ], :include => :roles } })
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.included(klass)
|
43
|
+
if User == klass
|
44
|
+
klass.send(:include, RoleOnUserInstanceMethods)
|
45
|
+
klass.send(:extend, RoleOnUserClassMethods)
|
46
|
+
klass.send(:has_and_belongs_to_many, :roles, :join_table => 'user_roles')
|
47
|
+
elsif ApplicationController == klass
|
48
|
+
klass.send(:extend, RoleOnControllerMethods)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/role_on.gemspec
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
3
|
+
# -*- encoding: utf-8 -*-
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = %q{role_on}
|
7
|
+
s.version = "0.2.7"
|
8
|
+
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.authors = ["Daniel Huckstep"]
|
11
|
+
s.date = %q{2009-08-24}
|
12
|
+
s.email = %q{darkhelmet@darkhelmetlive.com}
|
13
|
+
s.extra_rdoc_files = [
|
14
|
+
"LICENSE",
|
15
|
+
"README.md"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".document",
|
19
|
+
".gitignore",
|
20
|
+
"LICENSE",
|
21
|
+
"README.md",
|
22
|
+
"Rakefile",
|
23
|
+
"TODO",
|
24
|
+
"VERSION",
|
25
|
+
"generators/role_on/role_on_generator.rb",
|
26
|
+
"generators/role_on/templates/app/models/role.rb",
|
27
|
+
"generators/role_on/templates/db/migrate/migration.rb",
|
28
|
+
"init.rb",
|
29
|
+
"lib/role_on.rb",
|
30
|
+
"role_on.gemspec"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/darkhelmet/role_on}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.3}
|
36
|
+
s.summary = %q{Really simple roles}
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
40
|
+
s.specification_version = 3
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
43
|
+
else
|
44
|
+
end
|
45
|
+
else
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: role_on
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Huckstep
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-24 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: darkhelmet@darkhelmetlive.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- TODO
|
32
|
+
- VERSION
|
33
|
+
- generators/role_on/role_on_generator.rb
|
34
|
+
- generators/role_on/templates/app/models/role.rb
|
35
|
+
- generators/role_on/templates/db/migrate/migration.rb
|
36
|
+
- init.rb
|
37
|
+
- lib/role_on.rb
|
38
|
+
- role_on.gemspec
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/darkhelmet/role_on
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --charset=UTF-8
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.5
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Really simple roles
|
67
|
+
test_files: []
|
68
|
+
|