jakewendt-authorized 0.1.4

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.
@@ -0,0 +1,52 @@
1
+ = Authorized
2
+
3
+ This is a rails app built around a ruby gem for testing.
4
+
5
+
6
+ == ToDo
7
+
8
+ * merge authorized/controller.rb into authorized/permissive_controller.rb
9
+ * perhaps include authorized/resourceful_controller.rb as well
10
+ * remove hard coded :users from Role model
11
+ * build a full development testing app
12
+
13
+ == Required Gem Sources
14
+
15
+ == Required Gems
16
+
17
+ == Other Required
18
+
19
+ * current_user method
20
+
21
+ == Installation (as a plugin/engine)
22
+
23
+ config.gem "jakewendt-authorized",
24
+ :lib => "authorized"
25
+
26
+
27
+ class User
28
+ authorized
29
+ end
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
@@ -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
@@ -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,66 @@
1
+ class AuthorizedGenerator < 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
+
9
+ %w( create_roles create_roles_users ).each do |migration|
10
+ m.migration_template "migrations/#{migration}.rb",
11
+ 'db/migrate', :migration_file_name => migration
12
+ end
13
+ dot = File.dirname(__FILE__)
14
+ m.directory('public/javascripts')
15
+ Dir["#{dot}/templates/javascripts/*js"].each{|file|
16
+ f = file.split('/').slice(-2,2).join('/')
17
+ m.file(f, "public/javascripts/#{File.basename(file)}")
18
+ }
19
+ m.directory('public/stylesheets')
20
+ Dir["#{dot}/templates/stylesheets/*css"].each{|file|
21
+ f = file.split('/').slice(-2,2).join('/')
22
+ m.file(f, "public/stylesheets/#{File.basename(file)}")
23
+ }
24
+ m.directory('test/functional/authorized')
25
+ Dir["#{dot}/templates/functional/*rb"].each{|file|
26
+ f = file.split('/').slice(-2,2).join('/')
27
+ m.file(f, "test/functional/authorized/#{File.basename(file)}")
28
+ }
29
+ m.directory('test/unit/authorized')
30
+ Dir["#{dot}/templates/unit/*rb"].each{|file|
31
+ f = file.split('/').slice(-2,2).join('/')
32
+ m.file(f, "test/unit/authorized/#{File.basename(file)}")
33
+ }
34
+ end
35
+ end
36
+
37
+ end
38
+ module Rails::Generator::Commands
39
+ class Create
40
+ def migration_template(relative_source,
41
+ relative_destination, template_options = {})
42
+ migration_directory relative_destination
43
+ migration_file_name = template_options[
44
+ :migration_file_name] || file_name
45
+ if migration_exists?(migration_file_name)
46
+ puts "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}: Skipping"
47
+ else
48
+ template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
49
+ end
50
+ end
51
+ end # Create
52
+ class Base
53
+ protected
54
+ # the loop through migrations happens so fast
55
+ # that they all have the same timestamp which
56
+ # won't work when you actually try to migrate.
57
+ # All the timestamps MUST be unique.
58
+ def next_migration_string(padding = 3)
59
+ @s = (!@s.nil?)? @s.to_i + 1 : if ActiveRecord::Base.timestamped_migrations
60
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
61
+ else
62
+ "%.#{padding}d" % next_migration_number
63
+ end
64
+ end
65
+ end # Base
66
+ end
@@ -0,0 +1,142 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class Authorized::RolesControllerTest < ActionController::TestCase
4
+ tests RolesController
5
+
6
+ # no user_id
7
+ assert_no_route(:put, :update, :id => 'reader')
8
+ assert_no_route(:delete, :destroy, :id => 'reader')
9
+
10
+ %w( super_user admin ).each do |cu|
11
+
12
+ test "should update with #{cu} login" do
13
+ login_as send(cu)
14
+ u = active_user
15
+ assert !u.reload.role_names.include?('reader')
16
+ assert_difference("User.find(#{u.id}).roles.length",1){
17
+ put :update, :user_id => u.id, :id => 'reader'
18
+ }
19
+ assert u.reload.role_names.include?('reader')
20
+ assert_not_nil flash[:notice]
21
+ assert_redirected_to user_path(assigns(:user))
22
+ end
23
+
24
+ test "should destroy with #{cu} login" do
25
+ login_as send(cu)
26
+ u = active_user
27
+ u.roles << Role.find_or_create_by_name('reader')
28
+ assert u.reload.role_names.include?('reader')
29
+ assert_difference("User.find(#{u.id}).roles.length",-1){
30
+ delete :destroy, :user_id => u.id, :id => 'reader'
31
+ }
32
+ assert !u.reload.role_names.include?('reader')
33
+ assert_not_nil flash[:notice]
34
+ assert_redirected_to user_path(assigns(:user))
35
+ end
36
+
37
+ test "should NOT update without valid user_id with #{cu} login" do
38
+ login_as send(cu)
39
+ put :update, :user_id => 0, :id => 'reader'
40
+ assert_not_nil flash[:error]
41
+ assert_redirected_to users_path
42
+ end
43
+
44
+ test "should NOT destroy without valid user_id with #{cu} login" do
45
+ login_as send(cu)
46
+ delete :destroy, :user_id => 0, :id => 'reader'
47
+ assert_not_nil flash[:error]
48
+ assert_redirected_to users_path
49
+ end
50
+
51
+ test "should NOT update self with #{cu} login" do
52
+ u = send(cu)
53
+ login_as u
54
+ assert_difference("User.find(#{u.id}).roles.length",0){
55
+ put :update, :user_id => u.id, :id => 'reader'
56
+ }
57
+ assert_not_nil flash[:error]
58
+ assert_equal u, assigns(:user)
59
+ assert_redirected_to user_path(assigns(:user))
60
+ # assert_redirected_to root_path
61
+ end
62
+
63
+ test "should NOT destroy self with #{cu} login" do
64
+ u = send(cu)
65
+ login_as u
66
+ assert_difference("User.find(#{u.id}).roles.length",0){
67
+ delete :destroy, :user_id => u.id, :id => 'reader'
68
+ }
69
+ assert_not_nil flash[:error]
70
+ assert_equal u, assigns(:user)
71
+ assert_redirected_to user_path(assigns(:user))
72
+ # assert_redirected_to root_path
73
+ end
74
+
75
+ test "should NOT update without valid role_name with #{cu} login" do
76
+ login_as send(cu)
77
+ u = active_user
78
+ assert_difference("User.find(#{u.id}).roles.length",0){
79
+ put :update, :user_id => u.id, :id => 'bogus_role_name'
80
+ }
81
+ assert_not_nil flash[:error]
82
+ assert_redirected_to user_path(assigns(:user))
83
+ end
84
+
85
+ test "should NOT destroy without valid role_name with #{cu} login" do
86
+ login_as send(cu)
87
+ u = active_user
88
+ assert_difference("User.find(#{u.id}).roles.length",0){
89
+ delete :destroy, :user_id => u.id, :id => 'bogus_role_name'
90
+ }
91
+ assert_not_nil flash[:error]
92
+ assert_redirected_to user_path(assigns(:user))
93
+ end
94
+
95
+ end
96
+
97
+ %w( interviewer reader editor active_user ).each do |cu|
98
+
99
+ test "should NOT update with #{cu} login" do
100
+ login_as send(cu)
101
+ u = active_user
102
+ assert !u.reload.role_names.include?('administrator')
103
+ assert_difference("User.find(#{u.id}).roles.length",0){
104
+ put :update, :user_id => u.id, :id => 'administrator'
105
+ }
106
+ assert !u.reload.role_names.include?('administrator')
107
+ assert_not_nil flash[:error]
108
+ assert_redirected_to root_path
109
+ end
110
+
111
+ test "should NOT destroy with #{cu} login" do
112
+ login_as send(cu)
113
+ u = active_user
114
+ u.roles << Role.find_or_create_by_name('administrator')
115
+ assert u.reload.role_names.include?('administrator')
116
+ assert_difference("User.find(#{u.id}).roles.length",0){
117
+ delete :destroy, :user_id => u.id, :id => 'administrator'
118
+ }
119
+ assert u.reload.role_names.include?('administrator')
120
+ assert_not_nil flash[:error]
121
+ assert_redirected_to root_path
122
+ end
123
+
124
+ end
125
+
126
+ test "should NOT update without login" do
127
+ u = active_user
128
+ assert_difference("User.find(#{u.id}).roles.length",0){
129
+ put :update, :user_id => u.id, :id => 'administrator'
130
+ }
131
+ assert_redirected_to_login
132
+ end
133
+
134
+ test "should NOT destroy without login" do
135
+ u = active_user
136
+ assert_difference("User.find(#{u.id}).roles.length",0){
137
+ delete :destroy, :user_id => u.id, :id => 'administrator'
138
+ }
139
+ assert_redirected_to_login
140
+ end
141
+
142
+ end
@@ -0,0 +1,14 @@
1
+ class CreateRoles < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :roles do |t|
4
+ t.integer :position
5
+ t.string :name
6
+ t.timestamps
7
+ end
8
+ add_index :roles, :name, :unique => true
9
+ end
10
+
11
+ def self.down
12
+ drop_table :roles
13
+ end
14
+ 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,29 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class Authorized::RoleTest < ActiveSupport::TestCase
4
+
5
+ assert_should_act_as_list(:model => 'Role')
6
+ assert_should_require(:name,
7
+ :model => 'Role')
8
+ assert_should_require_unique(:name,
9
+ :model => 'Role')
10
+ assert_should_habtm(:users,
11
+ :model => 'Role')
12
+
13
+ test "should create role" do
14
+ assert_difference('Role.count',1) do
15
+ object = create_object
16
+ assert !object.new_record?,
17
+ "#{object.errors.full_messages.to_sentence}"
18
+ end
19
+ end
20
+
21
+ protected
22
+
23
+ def create_object(options = {})
24
+ record = Factory.build(:role,options)
25
+ record.save
26
+ record
27
+ end
28
+
29
+ end
@@ -0,0 +1,45 @@
1
+ module Authorized
2
+ # predefined namespace
3
+ end
4
+ require 'active_support'
5
+ require 'ruby_extension'
6
+ require 'rails_helpers'
7
+ require 'acts_as_list'
8
+ require 'calnet_authenticated'
9
+
10
+ HTML::WhiteListSanitizer.allowed_attributes.merge(%w(
11
+ id class style
12
+ ))
13
+
14
+ %w{models controllers}.each do |dir|
15
+ path = File.expand_path(File.join(File.dirname(__FILE__), '../app', dir))
16
+ ActiveSupport::Dependencies.autoload_paths << path
17
+ ActiveSupport::Dependencies.autoload_once_paths << path
18
+
19
+ # I don't know why I have to do this here
20
+ # and nowhere else. Photos can't find 'role'
21
+ # when needed?
22
+ # $: << path
23
+ end
24
+
25
+ require 'authorized/core_extension'
26
+ require 'authorized/user_model'
27
+ require 'authorized/authorization'
28
+ require 'authorized/helper'
29
+ require 'authorized/controller'
30
+ require 'authorized/resourceful_controller'
31
+ require 'authorized/permissive_controller'
32
+
33
+ if !defined?(RAILS_ENV) || RAILS_ENV == 'test'
34
+ require 'active_support/test_case'
35
+ require 'factory_girl'
36
+ require 'assert_this_and_that'
37
+ require 'authorized/factories'
38
+ require 'authorized/factory_test_helper'
39
+ require 'authorized/pending'
40
+ end
41
+
42
+ ActionController::Routing::Routes.add_configuration_file(
43
+ File.expand_path(
44
+ File.join(
45
+ File.dirname(__FILE__), '../config/routes.rb')))