easy_roles 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ init.rb
5
+ lib/easy_roles.rb
data/README.rdoc ADDED
@@ -0,0 +1,120 @@
1
+ = Easy Roles
2
+
3
+ Simple rails gem for basic role authorization with ruby on rails.
4
+
5
+ == Install
6
+
7
+ gem install platform45-easy_roles --source http://gems.github.com
8
+
9
+ or it can be installed as a rails plugin.
10
+
11
+ == Basic Setup
12
+
13
+ Add the following to your enviroment.rb in the rails initializer block
14
+
15
+ config.gem 'platform45-easy_roles', :lib => 'easy_roles', :source => 'http://gems.github.com'
16
+
17
+ Firstly you need to add a "roles" column to your users model, and set the default value to "--- []". Please note you can call this column anything you like, I like to use the name "roles".
18
+
19
+ t.string :roles, :default => "--- []"
20
+
21
+ Then you need to add "easy_roles :column_name" to your model.
22
+
23
+ class User < ActiveRecord::Base
24
+ easy_roles :roles
25
+ end
26
+
27
+ And thats it.
28
+
29
+ == Usage
30
+
31
+ Easy roles extends your model, and adds a few methods needed for basic role authorization.
32
+
33
+ adding a role to a user
34
+ add_role 'role'
35
+
36
+ removing a role from a user
37
+ remove_role 'role'
38
+
39
+ check to see if a user has a certain role
40
+ has_role? 'role'
41
+ # or
42
+ is_role? # role being anything you like, for example 'is_admin?' or 'is_awesome?'
43
+
44
+ == Examples
45
+
46
+ @user = User.first
47
+
48
+ @user.add_role 'admin'
49
+
50
+ @user.is_admin?
51
+ => true
52
+
53
+ @user.has_role? 'admin'
54
+ => true
55
+
56
+ @user.is_awesome?
57
+ => false
58
+
59
+ @user.add_role 'awesome'
60
+
61
+ @user.is_awesome?
62
+ => true
63
+
64
+ @user.remove_role 'admin'
65
+
66
+ @user.is_admin?
67
+ => false
68
+
69
+ etc etc
70
+
71
+ == Protecting controllers
72
+
73
+ There are many ways to implement views for specific roles, so I did not specifically supply one. Here's an example on what you could do:
74
+
75
+ class ApplicationController < ActionController::Base
76
+
77
+ def admin_required
78
+ unless current_user && current_user.is_admin?
79
+ flash[:error] = "Sorry, you don't have access to that."
80
+ redirect_to root_url and return false
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ Then in your AdminsController or any controller that you only want admins to view:
87
+
88
+ class AdminsController < ApplicationController
89
+ before_filter :admin_required
90
+ end
91
+
92
+ class MarksController < ApplicationController
93
+ before_filter :admin_required, :only => :create, :update
94
+ end
95
+
96
+ check out http://blog.platform45.com/2009/10/06/howto-basic-roles-for-users for implementation!
97
+
98
+
99
+ == License
100
+
101
+ Copyright (c) 2009 Platform45
102
+
103
+ Permission is hereby granted, free of charge, to any person obtaining
104
+ a copy of this software and associated documentation files (the
105
+ "Software"), to deal in the Software without restriction, including
106
+ without limitation the rights to use, copy, modify, merge, publish,
107
+ distribute, sublicense, and/or sell copies of the Software, and to
108
+ permit persons to whom the Software is furnished to do so, subject to
109
+ the following conditions:
110
+
111
+ The above copyright notice and this permission notice shall be
112
+ included in all copies or substantial portions of the Software.
113
+
114
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
115
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
116
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
117
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
118
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
119
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
120
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('easy_roles', '0.1.0') do |p|
6
+ p.description = "Easy role authorization in rails"
7
+ p.url = "http://github.com/platform45/easy_roles"
8
+ p.author = "Platform45"
9
+ p.email = "ryan@platform45.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{easy_roles}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Platform45"]
9
+ s.date = %q{2009-10-07}
10
+ s.description = %q{Easy role authorization in rails}
11
+ s.email = %q{ryan@platform45.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/easy_roles.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/easy_roles.rb", "easy_roles.gemspec"]
14
+ s.homepage = %q{http://github.com/platform45/easy_roles}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Easy_roles", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{easy_roles}
18
+ s.rubygems_version = %q{1.3.4}
19
+ s.summary = %q{Easy role authorization in rails}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'easy_roles'
data/lib/easy_roles.rb ADDED
@@ -0,0 +1,56 @@
1
+ module EasyRoles
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+
5
+ base.send :alias_method_chain, :method_missing, :roles
6
+ end
7
+
8
+ module ClassMethods
9
+ def easy_roles(name)
10
+ serialize name.to_sym, Array
11
+ before_validation_on_create :make_default_roles
12
+
13
+ class_eval <<-EOC
14
+ def has_role?(role)
15
+ #{name}.include?(role)
16
+ end
17
+
18
+ def add_role(role)
19
+ self.#{name} << role
20
+ end
21
+
22
+ def remove_role(role)
23
+ self.#{name}.delete(role)
24
+ end
25
+
26
+ def clear_roles
27
+ self.#{name} = []
28
+ end
29
+
30
+ def make_default_roles
31
+ clear_roles if roles.nil?
32
+ end
33
+
34
+ private :make_default_roles
35
+ EOC
36
+ end
37
+ end
38
+
39
+ def method_missing_with_roles(method_id, *args, &block)
40
+ match = method_id.to_s.match(/^is_(\w+)[?]$/)
41
+ if match && respond_to?('has_role?')
42
+ class_eval <<-EOC
43
+ def is_#{match[1]}?
44
+ send :has_role?, '#{match[1]}'
45
+ end
46
+ EOC
47
+ send "is_#{match[1]}?"
48
+ else
49
+ method_missing_without_roles(method_id, *args, &block)
50
+ end
51
+ end
52
+ end
53
+
54
+ class ActiveRecord::Base
55
+ include EasyRoles
56
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_roles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Platform45
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-07 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Easy role authorization in rails
17
+ email: ryan@platform45.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/easy_roles.rb
25
+ files:
26
+ - Manifest
27
+ - README.rdoc
28
+ - Rakefile
29
+ - init.rb
30
+ - lib/easy_roles.rb
31
+ - easy_roles.gemspec
32
+ has_rdoc: true
33
+ homepage: http://github.com/platform45/easy_roles
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --line-numbers
39
+ - --inline-source
40
+ - --title
41
+ - Easy_roles
42
+ - --main
43
+ - README.rdoc
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: "1.2"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project: easy_roles
61
+ rubygems_version: 1.3.4
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Easy role authorization in rails
65
+ test_files: []
66
+