jakewendt-simply_authorized 1.3.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +52 -0
- data/app/controllers/roles_controller.rb +38 -0
- data/app/models/role.rb +34 -0
- data/config/routes.rb +9 -0
- data/generators/simply_authorized/USAGE +0 -0
- data/generators/simply_authorized/simply_authorized_generator.rb +84 -0
- data/generators/simply_authorized/templates/autotest_simply_authorized.rb +2 -0
- data/generators/simply_authorized/templates/functional/roles_controller_test.rb +143 -0
- data/generators/simply_authorized/templates/migrations/create_roles.rb +14 -0
- data/generators/simply_authorized/templates/migrations/create_roles_users.rb +14 -0
- data/generators/simply_authorized/templates/simply_authorized.rake +8 -0
- data/generators/simply_authorized/templates/stylesheets/authorized.css +0 -0
- data/generators/simply_authorized/templates/unit/role_test.rb +30 -0
- data/lib/jakewendt-simply_authorized.rb +1 -0
- data/lib/simply_authorized.rb +41 -0
- data/lib/simply_authorized/authorization.rb +68 -0
- data/lib/simply_authorized/autotest.rb +26 -0
- data/lib/simply_authorized/controller.rb +87 -0
- data/lib/simply_authorized/core_extension.rb +16 -0
- data/lib/simply_authorized/factories.rb +15 -0
- data/lib/simply_authorized/factory_test_helper.rb +47 -0
- data/lib/simply_authorized/helper.rb +28 -0
- data/lib/simply_authorized/permissive_controller.rb +27 -0
- data/lib/simply_authorized/resourceful_controller.rb +83 -0
- data/lib/simply_authorized/tasks.rb +1 -0
- data/lib/simply_authorized/test_tasks.rb +47 -0
- data/lib/simply_authorized/user_model.rb +161 -0
- data/lib/tasks/application.rake +40 -0
- data/lib/tasks/database.rake +52 -0
- data/lib/tasks/documentation.rake +68 -0
- data/lib/tasks/rcov.rake +44 -0
- data/lib/tasks/simply_sessions.rake +5 -0
- data/rails/init.rb +4 -0
- data/test/app/controllers/application_controller.rb +16 -0
- data/test/app/controllers/home_controller.rb +10 -0
- data/test/app/controllers/users_controller.rb +43 -0
- data/test/app/models/user.rb +3 -0
- data/test/config/routes.rb +11 -0
- data/test/functional/authorized/roles_controller_test.rb +143 -0
- data/test/unit/authorized/role_test.rb +30 -0
- metadata +167 -0
data/README.rdoc
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
= SimplyAuthorized
|
2
|
+
|
3
|
+
This is a rails app built around a ruby gem for testing.
|
4
|
+
|
5
|
+
== ToDo
|
6
|
+
|
7
|
+
* merge authorized/controller.rb into authorized/permissive_controller.rb
|
8
|
+
* perhaps include authorized/resourceful_controller.rb as well
|
9
|
+
* remove hard coded :users from Role model
|
10
|
+
* build a full development testing app
|
11
|
+
|
12
|
+
== Required Gem Sources
|
13
|
+
|
14
|
+
== Required Gems
|
15
|
+
|
16
|
+
== Other Required
|
17
|
+
|
18
|
+
* current_user method
|
19
|
+
|
20
|
+
== Installation (as a plugin/engine)
|
21
|
+
|
22
|
+
config.gem "jakewendt-simply_authorized",
|
23
|
+
:source => 'http://rubygems.org'
|
24
|
+
|
25
|
+
class User
|
26
|
+
simply_authorized
|
27
|
+
end
|
28
|
+
|
29
|
+
script/generate simply_authorized
|
30
|
+
|
31
|
+
== Testing (as an app)
|
32
|
+
|
33
|
+
rake db:migrate
|
34
|
+
rake db:fixtures:load
|
35
|
+
rake test
|
36
|
+
script/server
|
37
|
+
|
38
|
+
== Gemified with Jeweler
|
39
|
+
|
40
|
+
vi Rakefile
|
41
|
+
rake version:write
|
42
|
+
|
43
|
+
rake version:bump:patch
|
44
|
+
rake version:bump:minor
|
45
|
+
rake version:bump:major
|
46
|
+
|
47
|
+
rake gemspec
|
48
|
+
|
49
|
+
rake install
|
50
|
+
rake release
|
51
|
+
|
52
|
+
Copyright (c) 2010 [Jake Wendt], released under the MIT license
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class RolesController < ApplicationController
|
2
|
+
|
3
|
+
before_filter :may_assign_roles_required
|
4
|
+
before_filter :user_id_required
|
5
|
+
before_filter :may_not_be_user_required
|
6
|
+
before_filter :id_required
|
7
|
+
|
8
|
+
def update
|
9
|
+
@user.roles << @role
|
10
|
+
flash[:notice] = 'User was successfully updated.'
|
11
|
+
redirect_to @user
|
12
|
+
end
|
13
|
+
|
14
|
+
def destroy
|
15
|
+
@user.roles.delete @role
|
16
|
+
flash[:notice] = 'User was successfully updated.'
|
17
|
+
redirect_to @user
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def user_id_required
|
23
|
+
if !params[:user_id].blank? and User.exists?(params[:user_id])
|
24
|
+
@user = User.find(params[:user_id])
|
25
|
+
else
|
26
|
+
access_denied("user id required!", users_path)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def id_required
|
31
|
+
if !params[:id].blank? and Role.exists?(:name => params[:id])
|
32
|
+
@role = Role.find_by_name(params[:id])
|
33
|
+
else
|
34
|
+
access_denied("id required!", @user)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/app/models/role.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# #82 new
|
2
|
+
# Roles and Users
|
3
|
+
#
|
4
|
+
# Reported by Magee | August 9th, 2010 @ 02:11 PM
|
5
|
+
#
|
6
|
+
# Currently we should have four roles (three in
|
7
|
+
# the system right now). They are effectively as follows:
|
8
|
+
#
|
9
|
+
# 1. Reader -- users with login accounts who can
|
10
|
+
# view contents of sections but not edit anything.
|
11
|
+
# 2. Editor -- users with the ability to add or edit
|
12
|
+
# content to the system. These are the users for
|
13
|
+
# whom an "edit" button displays on content details
|
14
|
+
# pages allowing them to make changes
|
15
|
+
# (or an "add" button as appropriate)
|
16
|
+
# 3. Administrator -- users who have administrative
|
17
|
+
# rights to the system to add users, etc.
|
18
|
+
# 4. Superuser -- Magee and Jake
|
19
|
+
#
|
20
|
+
# There may not be any system behaviors defined for
|
21
|
+
# Superusers. They may strictly be Conceptual Roles
|
22
|
+
# to describe users who may make backend or other
|
23
|
+
# changes outside of the scope of normal system
|
24
|
+
# operations. If necessary, a system role may be
|
25
|
+
# added in the future to address functions only
|
26
|
+
# for that group.
|
27
|
+
#
|
28
|
+
class Role < ActiveRecord::Base
|
29
|
+
acts_as_list
|
30
|
+
default_scope :order => :position
|
31
|
+
has_and_belongs_to_many :users, :uniq => true
|
32
|
+
validates_presence_of :name
|
33
|
+
validates_uniqueness_of :name
|
34
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
ActionController::Routing::Routes.draw do |map|
|
2
|
+
|
3
|
+
map.resources :users, :only => [:destroy,:show,:index],
|
4
|
+
:collection => { :menu => :get } do |user|
|
5
|
+
# map.resources :users, :only => [] do |user|
|
6
|
+
user.resources :roles, :only => [:update,:destroy]
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
File without changes
|
@@ -0,0 +1,84 @@
|
|
1
|
+
class SimplyAuthorizedGenerator < Rails::Generator::Base
|
2
|
+
|
3
|
+
def manifest
|
4
|
+
# See Rails::Generator::Commands::Create
|
5
|
+
# rails-2.3.10/lib/rails_generator/commands.rb
|
6
|
+
# for code methods for record (Manifest)
|
7
|
+
record do |m|
|
8
|
+
m.directory('config/autotest')
|
9
|
+
m.file('autotest_simply_authorized.rb', 'config/autotest/simply_authorized.rb')
|
10
|
+
m.directory('lib/tasks')
|
11
|
+
m.file('simply_authorized.rake', 'lib/tasks/simply_authorized.rake')
|
12
|
+
|
13
|
+
# File.open('Rakefile','a'){|f|
|
14
|
+
# f.puts <<-EOF
|
15
|
+
## From `script/generate simply_authorized` ...
|
16
|
+
#require 'simply_authorized/test_tasks'
|
17
|
+
# EOF
|
18
|
+
# }
|
19
|
+
#
|
20
|
+
# File.open('.autotest','a'){|f|
|
21
|
+
# f.puts <<-EOF
|
22
|
+
## From `script/generate simply_authorized` ...
|
23
|
+
#require 'simply_authorized/autotest'
|
24
|
+
# EOF
|
25
|
+
# }
|
26
|
+
|
27
|
+
%w( create_roles create_roles_users ).each do |migration|
|
28
|
+
m.migration_template "migrations/#{migration}.rb",
|
29
|
+
'db/migrate', :migration_file_name => migration
|
30
|
+
end
|
31
|
+
dot = File.dirname(__FILE__)
|
32
|
+
m.directory('public/javascripts')
|
33
|
+
Dir["#{dot}/templates/javascripts/*js"].each{|file|
|
34
|
+
f = file.split('/').slice(-2,2).join('/')
|
35
|
+
m.file(f, "public/javascripts/#{File.basename(file)}")
|
36
|
+
}
|
37
|
+
m.directory('public/stylesheets')
|
38
|
+
Dir["#{dot}/templates/stylesheets/*css"].each{|file|
|
39
|
+
f = file.split('/').slice(-2,2).join('/')
|
40
|
+
m.file(f, "public/stylesheets/#{File.basename(file)}")
|
41
|
+
}
|
42
|
+
# m.directory('test/functional/authorized')
|
43
|
+
# Dir["#{dot}/templates/functional/*rb"].each{|file|
|
44
|
+
# f = file.split('/').slice(-2,2).join('/')
|
45
|
+
# m.file(f, "test/functional/authorized/#{File.basename(file)}")
|
46
|
+
# }
|
47
|
+
# m.directory('test/unit/authorized')
|
48
|
+
# Dir["#{dot}/templates/unit/*rb"].each{|file|
|
49
|
+
# f = file.split('/').slice(-2,2).join('/')
|
50
|
+
# m.file(f, "test/unit/authorized/#{File.basename(file)}")
|
51
|
+
# }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
module Rails::Generator::Commands
|
57
|
+
class Create
|
58
|
+
def migration_template(relative_source,
|
59
|
+
relative_destination, template_options = {})
|
60
|
+
migration_directory relative_destination
|
61
|
+
migration_file_name = template_options[
|
62
|
+
:migration_file_name] || file_name
|
63
|
+
if migration_exists?(migration_file_name)
|
64
|
+
puts "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}: Skipping"
|
65
|
+
else
|
66
|
+
template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end # Create
|
70
|
+
class Base
|
71
|
+
protected
|
72
|
+
# the loop through migrations happens so fast
|
73
|
+
# that they all have the same timestamp which
|
74
|
+
# won't work when you actually try to migrate.
|
75
|
+
# All the timestamps MUST be unique.
|
76
|
+
def next_migration_string(padding = 3)
|
77
|
+
@s = (!@s.nil?)? @s.to_i + 1 : if ActiveRecord::Base.timestamped_migrations
|
78
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
79
|
+
else
|
80
|
+
"%.#{padding}d" % next_migration_number
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end # Base
|
84
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
#require File.dirname(__FILE__) + '/../../test_helper'
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class SimplyAuthorized::RolesControllerTest < ActionController::TestCase
|
5
|
+
tests RolesController
|
6
|
+
|
7
|
+
# no user_id
|
8
|
+
assert_no_route(:put, :update, :id => 'reader')
|
9
|
+
assert_no_route(:delete, :destroy, :id => 'reader')
|
10
|
+
|
11
|
+
%w( super_user admin ).each do |cu|
|
12
|
+
|
13
|
+
test "should update with #{cu} login" do
|
14
|
+
login_as send(cu)
|
15
|
+
u = active_user
|
16
|
+
assert !u.reload.role_names.include?('reader')
|
17
|
+
assert_difference("User.find(#{u.id}).roles.length",1){
|
18
|
+
put :update, :user_id => u.id, :id => 'reader'
|
19
|
+
}
|
20
|
+
assert u.reload.role_names.include?('reader')
|
21
|
+
assert_not_nil flash[:notice]
|
22
|
+
assert_redirected_to user_path(assigns(:user))
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should destroy with #{cu} login" do
|
26
|
+
login_as send(cu)
|
27
|
+
u = active_user
|
28
|
+
u.roles << Role.find_or_create_by_name('reader')
|
29
|
+
assert u.reload.role_names.include?('reader')
|
30
|
+
assert_difference("User.find(#{u.id}).roles.length",-1){
|
31
|
+
delete :destroy, :user_id => u.id, :id => 'reader'
|
32
|
+
}
|
33
|
+
assert !u.reload.role_names.include?('reader')
|
34
|
+
assert_not_nil flash[:notice]
|
35
|
+
assert_redirected_to user_path(assigns(:user))
|
36
|
+
end
|
37
|
+
|
38
|
+
test "should NOT update without valid user_id with #{cu} login" do
|
39
|
+
login_as send(cu)
|
40
|
+
put :update, :user_id => 0, :id => 'reader'
|
41
|
+
assert_not_nil flash[:error]
|
42
|
+
assert_redirected_to users_path
|
43
|
+
end
|
44
|
+
|
45
|
+
test "should NOT destroy without valid user_id with #{cu} login" do
|
46
|
+
login_as send(cu)
|
47
|
+
delete :destroy, :user_id => 0, :id => 'reader'
|
48
|
+
assert_not_nil flash[:error]
|
49
|
+
assert_redirected_to users_path
|
50
|
+
end
|
51
|
+
|
52
|
+
test "should NOT update self with #{cu} login" do
|
53
|
+
u = send(cu)
|
54
|
+
login_as u
|
55
|
+
assert_difference("User.find(#{u.id}).roles.length",0){
|
56
|
+
put :update, :user_id => u.id, :id => 'reader'
|
57
|
+
}
|
58
|
+
assert_not_nil flash[:error]
|
59
|
+
assert_equal u, assigns(:user)
|
60
|
+
assert_redirected_to user_path(assigns(:user))
|
61
|
+
# assert_redirected_to root_path
|
62
|
+
end
|
63
|
+
|
64
|
+
test "should NOT destroy self with #{cu} login" do
|
65
|
+
u = send(cu)
|
66
|
+
login_as u
|
67
|
+
assert_difference("User.find(#{u.id}).roles.length",0){
|
68
|
+
delete :destroy, :user_id => u.id, :id => 'reader'
|
69
|
+
}
|
70
|
+
assert_not_nil flash[:error]
|
71
|
+
assert_equal u, assigns(:user)
|
72
|
+
assert_redirected_to user_path(assigns(:user))
|
73
|
+
# assert_redirected_to root_path
|
74
|
+
end
|
75
|
+
|
76
|
+
test "should NOT update without valid role_name with #{cu} login" do
|
77
|
+
login_as send(cu)
|
78
|
+
u = active_user
|
79
|
+
assert_difference("User.find(#{u.id}).roles.length",0){
|
80
|
+
put :update, :user_id => u.id, :id => 'bogus_role_name'
|
81
|
+
}
|
82
|
+
assert_not_nil flash[:error]
|
83
|
+
assert_redirected_to user_path(assigns(:user))
|
84
|
+
end
|
85
|
+
|
86
|
+
test "should NOT destroy without valid role_name with #{cu} login" do
|
87
|
+
login_as send(cu)
|
88
|
+
u = active_user
|
89
|
+
assert_difference("User.find(#{u.id}).roles.length",0){
|
90
|
+
delete :destroy, :user_id => u.id, :id => 'bogus_role_name'
|
91
|
+
}
|
92
|
+
assert_not_nil flash[:error]
|
93
|
+
assert_redirected_to user_path(assigns(:user))
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
%w( interviewer reader editor active_user ).each do |cu|
|
99
|
+
|
100
|
+
test "should NOT update with #{cu} login" do
|
101
|
+
login_as send(cu)
|
102
|
+
u = active_user
|
103
|
+
assert !u.reload.role_names.include?('administrator')
|
104
|
+
assert_difference("User.find(#{u.id}).roles.length",0){
|
105
|
+
put :update, :user_id => u.id, :id => 'administrator'
|
106
|
+
}
|
107
|
+
assert !u.reload.role_names.include?('administrator')
|
108
|
+
assert_not_nil flash[:error]
|
109
|
+
assert_redirected_to root_path
|
110
|
+
end
|
111
|
+
|
112
|
+
test "should NOT destroy with #{cu} login" do
|
113
|
+
login_as send(cu)
|
114
|
+
u = active_user
|
115
|
+
u.roles << Role.find_or_create_by_name('administrator')
|
116
|
+
assert u.reload.role_names.include?('administrator')
|
117
|
+
assert_difference("User.find(#{u.id}).roles.length",0){
|
118
|
+
delete :destroy, :user_id => u.id, :id => 'administrator'
|
119
|
+
}
|
120
|
+
assert u.reload.role_names.include?('administrator')
|
121
|
+
assert_not_nil flash[:error]
|
122
|
+
assert_redirected_to root_path
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
test "should NOT update without login" do
|
128
|
+
u = active_user
|
129
|
+
assert_difference("User.find(#{u.id}).roles.length",0){
|
130
|
+
put :update, :user_id => u.id, :id => 'administrator'
|
131
|
+
}
|
132
|
+
assert_redirected_to_login
|
133
|
+
end
|
134
|
+
|
135
|
+
test "should NOT destroy without login" do
|
136
|
+
u = active_user
|
137
|
+
assert_difference("User.find(#{u.id}).roles.length",0){
|
138
|
+
delete :destroy, :user_id => u.id, :id => 'administrator'
|
139
|
+
}
|
140
|
+
assert_redirected_to_login
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateRolesUsers < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :roles_users, :id => false do |t|
|
4
|
+
t.references :role
|
5
|
+
t.references :user
|
6
|
+
end
|
7
|
+
add_index :roles_users, :role_id
|
8
|
+
add_index :roles_users, :user_id
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
drop_table :roles_users
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# From `script/generate simply_authorized` ...
|
2
|
+
if sa_gem = Gem.source_index.find_name('jakewendt-simply_authorized').last
|
3
|
+
gem 'jakewendt-simply_authorized'
|
4
|
+
require 'simply_authorized'
|
5
|
+
# it sucks, but this is needed for rake due to the configuration
|
6
|
+
require sa_gem.full_gem_path + '/app/models/role'
|
7
|
+
require 'simply_authorized/test_tasks'
|
8
|
+
end
|
File without changes
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#require File.dirname(__FILE__) + '/../../test_helper'
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class SimplyAuthorized::RoleTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
assert_should_act_as_list(:model => 'Role')
|
7
|
+
assert_should_require(:name,
|
8
|
+
:model => 'Role')
|
9
|
+
assert_should_require_unique(:name,
|
10
|
+
:model => 'Role')
|
11
|
+
assert_should_habtm(:users,
|
12
|
+
:model => 'Role')
|
13
|
+
|
14
|
+
test "should create role" do
|
15
|
+
assert_difference('Role.count',1) do
|
16
|
+
object = create_object
|
17
|
+
assert !object.new_record?,
|
18
|
+
"#{object.errors.full_messages.to_sentence}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def create_object(options = {})
|
25
|
+
record = Factory.build(:role,options)
|
26
|
+
record.save
|
27
|
+
record
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|