rolify 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +6 -0
- data/LICENSE +20 -0
- data/README.rdoc +80 -0
- data/Rakefile +2 -0
- data/lib/generators/rolify/role/role_generator.rb +34 -0
- data/lib/generators/rolify/role/templates/migration.rb +15 -0
- data/lib/generators/rolify/role/templates/role.rb +3 -0
- data/lib/rolify.rb +3 -0
- data/lib/rolify/version.rb +3 -0
- data/rolify.gemspec +21 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_helper.rb +4 -0
- metadata +67 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Florent Monbillard
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
= rolify
|
2
|
+
|
3
|
+
Very simple Roles library without any authorization enforcement (built to use with cancan) supporting scope on object.
|
4
|
+
|
5
|
+
Let's see an example:
|
6
|
+
|
7
|
+
user.is_moderator?(Forum.first) => # return false if user is moderator of another Forum
|
8
|
+
|
9
|
+
This library was intended to be used with CanCan[https://github.com/ryanb/cancan] and devise[https://github.com/plataformatec/devise/] but should be generic enough to be used by any other authentication/authorization solutions.
|
10
|
+
|
11
|
+
== Requirements
|
12
|
+
|
13
|
+
* >= Rails 3
|
14
|
+
* ActiveRecord ORM
|
15
|
+
|
16
|
+
== Installation
|
17
|
+
|
18
|
+
In <b>Rails 3</b>, add this to your Gemfile and run the +bundle+ command.
|
19
|
+
|
20
|
+
gem "rolify"
|
21
|
+
|
22
|
+
Alternatively, you can install it as a plugin.
|
23
|
+
|
24
|
+
rails plugin install git://github.com/EppO/rolify.git
|
25
|
+
|
26
|
+
== Getting Started
|
27
|
+
|
28
|
+
=== 1. Generate Role Modle
|
29
|
+
|
30
|
+
First, create your Role model and migration file using this generator:
|
31
|
+
|
32
|
+
rails g rolify:role Role User
|
33
|
+
|
34
|
+
Role and User classes are the default. You can specify any Role class name you want. This is completly a new file so any name can do the job.
|
35
|
+
For the User class name, you would probably use the one provided by your authentication solution. rolify just adds some class methods in an existing User class.
|
36
|
+
|
37
|
+
=== 2. Run the migration
|
38
|
+
|
39
|
+
Let's migrate !
|
40
|
+
|
41
|
+
rake db:migrate
|
42
|
+
|
43
|
+
=== 3. Add a role to a user
|
44
|
+
|
45
|
+
To define a global role:
|
46
|
+
|
47
|
+
user = User.find(1)
|
48
|
+
user.has_role "admin"
|
49
|
+
|
50
|
+
To define a role scoped to a resource
|
51
|
+
|
52
|
+
user = User.find(2)
|
53
|
+
user.has_role "moderator", Forum.first
|
54
|
+
|
55
|
+
That's it !
|
56
|
+
|
57
|
+
=== 4. Check roles
|
58
|
+
|
59
|
+
To check if a user has a global role
|
60
|
+
|
61
|
+
user = User.find(1)
|
62
|
+
user.has_role? "admin"
|
63
|
+
# => true
|
64
|
+
|
65
|
+
To check if a user has a role scoped to a resource
|
66
|
+
|
67
|
+
user = User.find(2)
|
68
|
+
user.has_role "moderator", Forum.first
|
69
|
+
# => true
|
70
|
+
user.has_role "moderator", Forum.last
|
71
|
+
# => false
|
72
|
+
|
73
|
+
== Questions or Problems?
|
74
|
+
|
75
|
+
If you have any issues with rolify which you cannot find the solution to in the tiny README[https://github.com/EppO/rolify], please add an {issue on GitHub}[https://github.com/EppO/rolify/issues] or fork the project and send a pull request.
|
76
|
+
|
77
|
+
== TODO
|
78
|
+
|
79
|
+
* Write specs
|
80
|
+
* Write code
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
module Rolify
|
4
|
+
module Generators
|
5
|
+
class RoleGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
argument :role_cname, :type => :string, :default => "Role"
|
10
|
+
argument :user_cname, :type => :string, :default => "User"
|
11
|
+
|
12
|
+
desc "Generates a model with the given NAME and a migration file."
|
13
|
+
|
14
|
+
def generate_role
|
15
|
+
template "role.rb", "app/models/role.rb"
|
16
|
+
inject_into_class(model_path, user_cname.camelize) do
|
17
|
+
" has_and_belongs_to_many :#{role_cname.tableize}\n"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def copy_role_file
|
22
|
+
migration_template "migration.rb", "db/migrate/rolify_create_#{role_cname.tableize}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def model_path
|
26
|
+
File.join("app", "models", "#{user_cname.underscore}.rb")
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.next_migration_number(path)
|
30
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class RolifyCreate<%= role_cname.camelize %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table(<%= role_cname.tableize.to_sym %>) do |t|
|
4
|
+
t.string :name
|
5
|
+
t.references :resource, :polymorphic => true
|
6
|
+
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table(<%= (user_cname.tableize + "_" + role_cname.tableize).to_sym %>, :id => false) do |t|
|
11
|
+
t.references :<%= user_cname.underscore.singularize %>
|
12
|
+
t.references :<%= role_cname.underscore.singularize %>
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/rolify.rb
ADDED
data/rolify.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rolify/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rolify"
|
7
|
+
s.version = Rolify::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Florent Monbillard"]
|
10
|
+
s.email = ["f.monbillard@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/EppO/rolify"
|
12
|
+
s.summary = %q{Roles library with object scoping}
|
13
|
+
s.description = %q{Very simple Roles library without any authorization enforcement (built to use with cancan) supporting scope on object: user.is_moderator?(Forum.first) => # return false if user is moderator of another Forum }
|
14
|
+
|
15
|
+
s.rubyforge_project = s.name
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/spec/spec.opts
ADDED
File without changes
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rolify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Florent Monbillard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-03 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "Very simple Roles library without any authorization enforcement (built to use with cancan) supporting scope on object: user.is_moderator?(Forum.first) => # return false if user is moderator of another Forum "
|
17
|
+
email:
|
18
|
+
- f.monbillard@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- Gemfile
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- lib/generators/rolify/role/role_generator.rb
|
32
|
+
- lib/generators/rolify/role/templates/migration.rb
|
33
|
+
- lib/generators/rolify/role/templates/role.rb
|
34
|
+
- lib/rolify.rb
|
35
|
+
- lib/rolify/version.rb
|
36
|
+
- rolify.gemspec
|
37
|
+
- spec/spec.opts
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
homepage: https://github.com/EppO/rolify
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project: rolify
|
62
|
+
rubygems_version: 1.7.2
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Roles library with object scoping
|
66
|
+
test_files: []
|
67
|
+
|