darkhelmet-role_on 0.1.1
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 +59 -0
- data/Rakefile +56 -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 +21 -0
- data/init.rb +1 -0
- data/lib/role_on.rb +38 -0
- data/test/role_on_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +67 -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,59 @@
|
|
|
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, :on => [:edit,:update])
|
|
52
|
+
|
|
53
|
+
Can also use except
|
|
54
|
+
|
|
55
|
+
role_on(:admin, :except => [:index,:show])
|
|
56
|
+
|
|
57
|
+
# License
|
|
58
|
+
|
|
59
|
+
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/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.1
|
|
@@ -0,0 +1,21 @@
|
|
|
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'], :name => 'index_roles_on_name'
|
|
9
|
+
|
|
10
|
+
create_table :user_roles, :id => false, :force => true do |t|
|
|
11
|
+
t.integer :role_id, :user_id
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
r = Role.create(:name => 'admin')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.down
|
|
18
|
+
drop_table :roles
|
|
19
|
+
drop_table :roles_users
|
|
20
|
+
end
|
|
21
|
+
end
|
data/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'role_on'
|
data/lib/role_on.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module RoleOn
|
|
2
|
+
module RoleOnControllerMethods
|
|
3
|
+
def role_on(role, options = {})
|
|
4
|
+
before_filter do |c|
|
|
5
|
+
action = c.params[:action].intern
|
|
6
|
+
user_roles = c.current_user.roles.map(&:name).map(&:intern)
|
|
7
|
+
restricted_actions = if options.include?(:on)
|
|
8
|
+
[options[:on]].flatten
|
|
9
|
+
elsif options.include?(:except)
|
|
10
|
+
c.class.action_methods.to_a.map(&:intern) - [options[:except]].flatten
|
|
11
|
+
else
|
|
12
|
+
c.class.action_methods.to_a.map(&:intern)
|
|
13
|
+
end
|
|
14
|
+
if restricted_actions.include?(action) && !user_roles.include?(role)
|
|
15
|
+
c.send(:access_denied)
|
|
16
|
+
false
|
|
17
|
+
end
|
|
18
|
+
true
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
module RoleOnUserMethods
|
|
24
|
+
def has_role?(role)
|
|
25
|
+
return false if roles.nil?
|
|
26
|
+
roles.include?(Role[role])
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.included(klass)
|
|
31
|
+
if User == klass
|
|
32
|
+
klass.send(:extend, RoleOnUserMethods)
|
|
33
|
+
klass.send(:has_and_belongs_to_many, :roles, :join_table => 'user_roles')
|
|
34
|
+
elsif ApplicationController == klass
|
|
35
|
+
klass.send(:extend, RoleOnControllerMethods)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: darkhelmet-role_on
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Daniel Huckstep
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-07-13 00:00:00 -07: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
|
+
- VERSION
|
|
32
|
+
- generators/role_on/role_on_generator.rb
|
|
33
|
+
- generators/role_on/templates/app/models/role.rb
|
|
34
|
+
- generators/role_on/templates/db/migrate/migration.rb
|
|
35
|
+
- init.rb
|
|
36
|
+
- lib/role_on.rb
|
|
37
|
+
- test/role_on_test.rb
|
|
38
|
+
- test/test_helper.rb
|
|
39
|
+
has_rdoc: false
|
|
40
|
+
homepage: http://github.com/darkhelmet/role_on
|
|
41
|
+
post_install_message:
|
|
42
|
+
rdoc_options:
|
|
43
|
+
- --charset=UTF-8
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: "0"
|
|
51
|
+
version:
|
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: "0"
|
|
57
|
+
version:
|
|
58
|
+
requirements: []
|
|
59
|
+
|
|
60
|
+
rubyforge_project:
|
|
61
|
+
rubygems_version: 1.2.0
|
|
62
|
+
signing_key:
|
|
63
|
+
specification_version: 3
|
|
64
|
+
summary: Really simple roles
|
|
65
|
+
test_files:
|
|
66
|
+
- test/test_helper.rb
|
|
67
|
+
- test/role_on_test.rb
|