logical_authz 0.1.6
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/app/controllers/groups_controller.rb +77 -0
- data/app/controllers/groups_users_controller.rb +27 -0
- data/app/controllers/permissions_controller.rb +63 -0
- data/app/helpers/logical_authz_helper.rb +158 -0
- data/app/views/groups/_controls.html.haml +18 -0
- data/app/views/groups/_form.html.haml +4 -0
- data/app/views/groups/create.rjs +1 -0
- data/app/views/groups/edit.html.haml +1 -0
- data/app/views/groups/index.html.haml +14 -0
- data/app/views/groups/new.html.haml +2 -0
- data/app/views/groups/show.html.haml +6 -0
- data/app/views/permissions/_controls.html.haml +18 -0
- data/app/views/permissions/_form.html.haml +8 -0
- data/app/views/permissions/create.rjs +1 -0
- data/app/views/permissions/edit.html.haml +1 -0
- data/app/views/permissions/index.html.haml +20 -0
- data/app/views/permissions/new.html.haml +2 -0
- data/config/initializers/activate.rb +1 -0
- data/generators/logical_authz/logical_authz_generator.rb +13 -0
- data/generators/logical_authz/templates/README +11 -0
- data/generators/logical_authz/templates/app/controllers/authz_controller.rb.erb +4 -0
- data/generators/logical_authz/templates/app/views/layouts/_explain_authz.html.haml.erb +21 -0
- data/generators/logical_authz_models/logical_authz_models_generator.rb +22 -0
- data/generators/logical_authz_routes/logical_authz_routes_generator.rb +12 -0
- data/generators/logical_authz_specs/logical_authz_specs_generator.rb +26 -0
- data/lib/logical_authz/access_control.rb +343 -0
- data/lib/logical_authz/application.rb +350 -0
- data/lib/logical_authz/authn_facade/authlogic.rb +13 -0
- data/lib/logical_authz/configuration.rb +64 -0
- data/lib/logical_authz/engine.rb +18 -0
- data/lib/logical_authz/generator.rb +22 -0
- data/lib/logical_authz/generators/controllers/generator.rb +15 -0
- data/lib/logical_authz/generators/controllers/templates/app/controllers/authz_controller.rb +6 -0
- data/lib/logical_authz/generators/models/generator.rb +109 -0
- data/lib/logical_authz/generators/models/templates/app/models/group.rb +33 -0
- data/lib/logical_authz/generators/models/templates/app/models/permission.rb +3 -0
- data/lib/logical_authz/generators/models/templates/config/initializers/logical_authz.rb +20 -0
- data/lib/logical_authz/generators/models/templates/db/seeds_logical_authz.rb +21 -0
- data/lib/logical_authz/generators/models/templates/migrations/create_groups.rb +12 -0
- data/lib/logical_authz/generators/models/templates/migrations/create_permissions.rb +15 -0
- data/lib/logical_authz/generators/models/templates/migrations/create_users_groups.rb +13 -0
- data/lib/logical_authz/generators/routes/generator.rb +21 -0
- data/lib/logical_authz/generators/specs/generator.rb +57 -0
- data/lib/logical_authz/generators/specs/templates/spec/controllers/groups_controller_spec.rb +102 -0
- data/lib/logical_authz/generators/specs/templates/spec/controllers/groups_users_controller_spec.rb +47 -0
- data/lib/logical_authz/generators/specs/templates/spec/controllers/permissions_controller_spec.rb +24 -0
- data/lib/logical_authz/generators/specs/templates/spec/factories/az_accounts.rb +7 -0
- data/lib/logical_authz/generators/specs/templates/spec/factories/az_groups.rb +7 -0
- data/lib/logical_authz/generators/specs/templates/spec/factories/permissions.rb +2 -0
- data/lib/logical_authz/generators/specs/templates/spec/helpers/logical_authz_helper_spec.rb +90 -0
- data/lib/logical_authz/generators/specs/templates/spec/support/logical_authz.rb +1 -0
- data/lib/logical_authz/generators/specs/templates/spec/support/mock_auth.rb +30 -0
- data/lib/logical_authz/spec_helper.rb +75 -0
- data/lib/logical_authz.rb +110 -0
- data/lib/tasks/rspec.rake +15 -0
- data/spec/gem_test_suite.rb +17 -0
- data/spec/spec_helper.rb +43 -0
- metadata +127 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module LogicalAuthz
|
|
2
|
+
#These settings are all available in your configuration as:
|
|
3
|
+
#config.logical_authz.{setting}
|
|
4
|
+
class Configuration
|
|
5
|
+
class << self
|
|
6
|
+
#XXX is this redundant and confusing now?
|
|
7
|
+
def policy_helper(name, &block)
|
|
8
|
+
require 'logical_authz/access_control'
|
|
9
|
+
AccessControl::Builder.register_policy_helper(name, &block)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def unauthorized_groups
|
|
13
|
+
return @unauthorized_groups unless @unauthorized_groups.nil?
|
|
14
|
+
groups = unauthorized_group_names.map do |name|
|
|
15
|
+
Group.find_by_name(name)
|
|
16
|
+
end
|
|
17
|
+
if Rails.configuration.cache_classes
|
|
18
|
+
@unauthorized_groups = groups
|
|
19
|
+
end
|
|
20
|
+
return groups
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def clear_unauthorized_groups
|
|
24
|
+
@unauthorized_groups = nil
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def unauthorized_group_names=(array)
|
|
28
|
+
@unauthorized_group_names = array
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def unauthorized_group_names
|
|
32
|
+
@unauthorized_group_names ||= []
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def permission_model=(klass)
|
|
36
|
+
@perm_model = klass
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def group_model=(klass)
|
|
40
|
+
@group_model = klass
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def permission_model
|
|
44
|
+
@perm_model || ::Permission rescue nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def group_model
|
|
48
|
+
@group_model || ::Group rescue nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def debug!
|
|
52
|
+
@debug = true
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def no_debug
|
|
56
|
+
@debug = false
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def debugging?
|
|
60
|
+
defined? @debug and @debug
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'rails'
|
|
2
|
+
require 'logical_authz/configuration'
|
|
3
|
+
|
|
4
|
+
module LogicalAuthz
|
|
5
|
+
class Engine < Rails::Engine
|
|
6
|
+
generators do
|
|
7
|
+
require 'logical_authz/generator'
|
|
8
|
+
require 'logical_authz/generators/models/generator'
|
|
9
|
+
require 'logical_authz/generators/routes/generator'
|
|
10
|
+
require 'logical_authz/generators/specs/generator'
|
|
11
|
+
require 'logical_authz/generators/controllers/generator'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
config.eager_load_paths.unshift 'app/helpers'
|
|
15
|
+
|
|
16
|
+
config.logical_authz = Configuration
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require 'rails/generators'
|
|
2
|
+
require 'rails/generators/base'
|
|
3
|
+
module LogicalAuthz
|
|
4
|
+
class LogicalAuthzGenerator < Rails::Generators::Base
|
|
5
|
+
def models
|
|
6
|
+
invoke("logical_authz:model")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def routes
|
|
10
|
+
invoke("logical_authz:routes")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def specs
|
|
14
|
+
invoke("logical_authz:specs")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def controllers
|
|
18
|
+
invoke("logical_authz:controller")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'logical_authz/generator'
|
|
2
|
+
|
|
3
|
+
module LogicalAuthz
|
|
4
|
+
class ControllerGenerator < LogicalAuthzGenerator
|
|
5
|
+
source_paths << File::expand_path("../templates", __FILE__)
|
|
6
|
+
|
|
7
|
+
def create_authz_controller
|
|
8
|
+
template "app/controllers/authz_controller.rb"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def insert_authz_application
|
|
12
|
+
inject_into_class "app/controllers/application_controller.rb", "ApplicationController", " include LogicalAuthz::Application\n"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
require 'logical_authz/generator'
|
|
2
|
+
|
|
3
|
+
module LogicalAuthz
|
|
4
|
+
class ModelGenerator < LogicalAuthzGenerator
|
|
5
|
+
include Rails::Generators::Migration
|
|
6
|
+
|
|
7
|
+
class_option :user_class, :required => true
|
|
8
|
+
class_option :permission_class, :default => "Permission"
|
|
9
|
+
class_option :group_class, :default => "Group"
|
|
10
|
+
class_option :admin_group, :default => "Administrators"
|
|
11
|
+
|
|
12
|
+
no_tasks do
|
|
13
|
+
def template_data
|
|
14
|
+
@template_data ||= {
|
|
15
|
+
:user_table => options[:user_class].tableize,
|
|
16
|
+
:permission_table => options[:permission_class].tableize,
|
|
17
|
+
:group_table => options[:group_class].tableize,
|
|
18
|
+
:user_field => options[:user_class].underscore,
|
|
19
|
+
:permission_field => options[:permission_class].underscore,
|
|
20
|
+
:group_field => options[:group_class].underscore,
|
|
21
|
+
}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def user_class; options[:user_class]; end
|
|
25
|
+
def permission_class; options[:permission_class]; end
|
|
26
|
+
def group_class; options[:group_class]; end
|
|
27
|
+
def admin_group; options[:admin_group]; end
|
|
28
|
+
|
|
29
|
+
def user_table; template_data[:user_table]; end
|
|
30
|
+
def permission_table; template_data[:permission_table]; end
|
|
31
|
+
def group_table; template_data[:group_table]; end
|
|
32
|
+
|
|
33
|
+
def user_field; template_data[:user_field]; end
|
|
34
|
+
def permission_field; template_data[:permission_field]; end
|
|
35
|
+
def group_field; template_data[:group_field]; end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
#Tragically, this is locked to AR right now
|
|
39
|
+
def self.next_migration_number(dirname) #:nodoc:
|
|
40
|
+
next_migration_number = current_migration_number(dirname) + 1
|
|
41
|
+
if ActiveRecord::Base.timestamped_migrations
|
|
42
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
|
43
|
+
else
|
|
44
|
+
"%.3d" % next_migration_number
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
source_paths << File::expand_path("../templates", __FILE__)
|
|
49
|
+
|
|
50
|
+
def generate_group_model
|
|
51
|
+
invoke "logical_authz:group_model"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def generate_permissions_model
|
|
55
|
+
invoke "logical_authz:permission_model"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def create_seeds
|
|
59
|
+
template "db/seeds_logical_authz.rb"
|
|
60
|
+
append_file "db/seeds.rb", "require 'db/seeds_logical_authz'"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def create_initializer
|
|
64
|
+
template "config/initializers/logical_authz.rb"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class GroupModelGenerator < ModelGenerator
|
|
69
|
+
def create_model
|
|
70
|
+
template "app/models/group.rb", "app/models/#{group_field}.rb"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def inject_habtm_groups
|
|
74
|
+
inject_into_class "app/models/#{user_field}.rb", user_class, " has_and_belongs_to_many :#{group_table}\n"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def create_migration
|
|
78
|
+
dest_file = "db/migrate/create_#{group_field}.rb"
|
|
79
|
+
begin
|
|
80
|
+
migration_template "migrations/create_groups.rb", dest_file
|
|
81
|
+
rescue Rails::Generators::Error
|
|
82
|
+
say_status :exist, dest_file, :blue
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
dest_file = "db/migrate/create_#{user_table}_#{group_table}.rb"
|
|
86
|
+
begin
|
|
87
|
+
migration_template "migrations/create_users_groups.rb", dest_file
|
|
88
|
+
rescue Rails::Generators::Error
|
|
89
|
+
say_status :exist, dest_file, :blue
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
class PermissionModelGenerator < ModelGenerator
|
|
95
|
+
def create_model
|
|
96
|
+
template "app/models/permission.rb", "app/models/#{permission_field}.rb"
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def create_migration
|
|
100
|
+
dest_file = "db/migrate/create_#{permission_field}.rb"
|
|
101
|
+
migration_template "migrations/create_permissions.rb", dest_file
|
|
102
|
+
rescue Rails::Generators::Error
|
|
103
|
+
say_status :exist, dest_file, :blue
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
#manifest.class_collisions options[:group_class],
|
|
108
|
+
#options[:permission_class]
|
|
109
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
class <%=group_class%> < ActiveRecord::Base
|
|
2
|
+
validates_presence_of :name
|
|
3
|
+
validates_uniqueness_of :name
|
|
4
|
+
attr_accessible :name
|
|
5
|
+
|
|
6
|
+
has_many :<%=permission_table%>
|
|
7
|
+
|
|
8
|
+
has_and_belongs_to_many :<%=user_table%>, :class_name => <%= user_class.inspect%>
|
|
9
|
+
alias members <%=user_table%>
|
|
10
|
+
|
|
11
|
+
# returns true if this group can do *action* on *controller* optional object
|
|
12
|
+
def can?(action, controller, object = nil)
|
|
13
|
+
conditions = {
|
|
14
|
+
:group => self,
|
|
15
|
+
:controller => controller,
|
|
16
|
+
:action => action,
|
|
17
|
+
:id => object.id
|
|
18
|
+
}
|
|
19
|
+
return LogicalAuthz::is_authorized?(conditions)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
def admin_group
|
|
25
|
+
self.find_by_name(<%=admin_group.inspect%>)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def member_class
|
|
29
|
+
<%= user_class%>
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#if you aren't using authlogic, you'll need to roll your own
|
|
2
|
+
#(otherwise you'll need to emulate the contents therein somewhere)
|
|
3
|
+
require 'logical_authz/authn_facade/authlogic'
|
|
4
|
+
|
|
5
|
+
#You only need this if you want guest users to be able to do some things that
|
|
6
|
+
#logged in users can't:
|
|
7
|
+
#LogicalAuthz.unauthorized_group_names = %w{Guest}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
#You only need these lines if, for whatever reason, you have a models already
|
|
11
|
+
#defined that colide with LogicalAuthz model names and need to rename the
|
|
12
|
+
#LogicalAuthz model
|
|
13
|
+
#(You can set that up by passing a switch to the generator)
|
|
14
|
+
#
|
|
15
|
+
<%if permission_class == "Permission"
|
|
16
|
+
%>#<%end%>LogicalAuthz::set_permission_model(<%=permission_class%>)
|
|
17
|
+
|
|
18
|
+
<%if group_class == "Group"
|
|
19
|
+
%>#<%end%>LogicalAuthz::set_group_model(<%=group_class%>)
|
|
20
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#Add:
|
|
2
|
+
#require 'db/logical_authz_seeds.rb'
|
|
3
|
+
#to your db/seeds
|
|
4
|
+
|
|
5
|
+
admin_group = <%=group_class%>.find_or_create_by_name(<%=admin_group.inspect%>).save!
|
|
6
|
+
|
|
7
|
+
module LogicalAuthz
|
|
8
|
+
module PermissionSeeds
|
|
9
|
+
def self.create_permission(user, controller, action = nil, subject_id = nil)
|
|
10
|
+
<%=permission_class%>.create!(
|
|
11
|
+
:group_id => user.id,
|
|
12
|
+
:controller => controller,
|
|
13
|
+
:action => action,
|
|
14
|
+
:subject_id => subject_id
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
#Create permissions like this:
|
|
19
|
+
#create_permission(admin_group, "admin/permissions")
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class Create<%= permission_class %> < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
create_table :<%= permission_table%> do |t|
|
|
4
|
+
t.references :<%= group_field%>
|
|
5
|
+
t.string :controller
|
|
6
|
+
t.string :action
|
|
7
|
+
t.integer :subject_id
|
|
8
|
+
t.timestamps
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.down
|
|
13
|
+
drop_table :<%=permission_table%>
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class CreateUsersGroups < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
create_table :<%= group_table%>_<%= user_table%>, :id => false do |t|
|
|
4
|
+
t.references :<%= user_field%>
|
|
5
|
+
t.references :<%= group_field%>
|
|
6
|
+
t.timestamps
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.down
|
|
11
|
+
drop_table :<%=group_table%>_<%=user_table%>
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module LogicalAuthz
|
|
2
|
+
class RoutesGenerator < LogicalAuthzGenerator
|
|
3
|
+
def add_group_user
|
|
4
|
+
route "post '/group_user' => 'groups_users#create'"
|
|
5
|
+
route "delete '/ungroup_user' => 'groups_users#destroy'"
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def add_permissions
|
|
9
|
+
route "post '/permit' => 'permissions#create'"
|
|
10
|
+
route "delete '/permit' => 'permissions#destroy'"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add_groups
|
|
14
|
+
route "resources :groups"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def default_unauthorized
|
|
18
|
+
route "match '/' => 'home#index', :as => :default_unauthorized"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
module LogicalAuthz
|
|
2
|
+
class SpecsGenerator < LogicalAuthzGenerator
|
|
3
|
+
source_paths << File::expand_path("../templates", __FILE__)
|
|
4
|
+
|
|
5
|
+
class_option :user_class, :required => true
|
|
6
|
+
class_option :permission_class, :default => "Permission"
|
|
7
|
+
class_option :group_class, :default => "Group"
|
|
8
|
+
|
|
9
|
+
no_tasks do
|
|
10
|
+
def template_data
|
|
11
|
+
@template_data ||= {
|
|
12
|
+
:user_table => options[:user_class].tableize,
|
|
13
|
+
:permission_table => options[:permission_class].tableize,
|
|
14
|
+
:group_table => options[:group_class].tableize,
|
|
15
|
+
}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def user_class; options[:user_class]; end
|
|
19
|
+
def permission_class; options[:permission_class]; end
|
|
20
|
+
def group_class; options[:group_class]; end
|
|
21
|
+
def admin_group; options[:admin_group]; end
|
|
22
|
+
|
|
23
|
+
def user_table; template_data[:user_table]; end
|
|
24
|
+
def permissions_table; template_data[:permissions_table]; end
|
|
25
|
+
def group_table; template_data[:group_table]; end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def create_factories
|
|
29
|
+
empty_directory "spec/factories"
|
|
30
|
+
|
|
31
|
+
template "spec/factories/az_accounts.rb", "spec/factories/logical_authz_#{template_data[:user_table]}.rb"
|
|
32
|
+
template "spec/factories/az_groups.rb", "spec/factories/logical_authz_#{template_data[:group_table]}.rb"
|
|
33
|
+
template "spec/factories/permissions.rb", "spec/factories/logical_authz_#{template_data[:permission_table]}.rb"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def create_helper_spec
|
|
37
|
+
empty_directory "spec/helpers"
|
|
38
|
+
|
|
39
|
+
template "spec/helpers/logical_authz_helper_spec.rb", "spec/helpers/logical_authz_helper_spec.rb"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def create_controller_specs
|
|
43
|
+
empty_directory "spec/controllers"
|
|
44
|
+
|
|
45
|
+
template "spec/controllers/permissions_controller_spec.rb", "spec/controllers/permissions_controller_spec.rb"
|
|
46
|
+
template "spec/controllers/groups_controller_spec.rb", "spec/controllers/groups_controller_spec.rb"
|
|
47
|
+
template "spec/controllers/groups_users_controller_spec.rb", "spec/controllers/groups_users_controller_spec.rb"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def create_support
|
|
51
|
+
empty_directory "spec/support"
|
|
52
|
+
|
|
53
|
+
template "spec/support/logical_authz.rb"
|
|
54
|
+
template "spec/support/mock_auth.rb"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
require 'spec/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe GroupsController do
|
|
4
|
+
include LogicalAuthz::MockAuth
|
|
5
|
+
|
|
6
|
+
def mock_group(stubs={})
|
|
7
|
+
@mock_group ||= mock_model(Group, stubs)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
describe "logging in as non-admin" do
|
|
11
|
+
before(:each) do
|
|
12
|
+
@person = Factory.create(:authz_account)
|
|
13
|
+
login_as(@person)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "should redirect away from index with an error message" do
|
|
17
|
+
pending "relocation to host app"
|
|
18
|
+
get :index
|
|
19
|
+
response.should be_redirect
|
|
20
|
+
flash[:error].should_not be_nil
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe "logged in as admin" do
|
|
25
|
+
before(:each) do
|
|
26
|
+
@person = login_as(Factory.create(:authz_admin))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe "GET index" do
|
|
30
|
+
it "assigns all groups as @groups" do
|
|
31
|
+
Group.stub!(:all).and_return([mock_group])
|
|
32
|
+
get :index
|
|
33
|
+
controller.should be_authorized
|
|
34
|
+
assigns[:groups].should == [mock_group]
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
describe "GET show" do
|
|
39
|
+
before(:each) do
|
|
40
|
+
@group = Factory.create(:group, :name => 'foo')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "should find and expose the requested group as @group" do
|
|
44
|
+
get :show, :id => @group.id
|
|
45
|
+
assigns[:group].should == @group
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe "POST create" do
|
|
50
|
+
describe "with valid params" do
|
|
51
|
+
it "creates a new group and assigns it as @group" do
|
|
52
|
+
lambda do
|
|
53
|
+
post :create, :group => { :name => "foo group" }
|
|
54
|
+
end.should change(Group, :count).by(1)
|
|
55
|
+
assigns[:group].name.should == "foo group"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "redirects back" do
|
|
59
|
+
post :create, :group => { :name => "foo group" }
|
|
60
|
+
response.should redirect_to(groups_url)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe "with invalid params" do
|
|
65
|
+
it "assigns a newly created but unsaved group as @group" do
|
|
66
|
+
pending
|
|
67
|
+
Group.stub!(:new).and_return(mock_group(:save => false))
|
|
68
|
+
lambda do
|
|
69
|
+
post :create, :group => {}
|
|
70
|
+
end.should_not change(Group).by(1)
|
|
71
|
+
assigns[:group].should_not be_nil
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "re-renders the 'new' template" do
|
|
75
|
+
Group.stub!(:new).and_return(mock_group(:save => false))
|
|
76
|
+
post :create, :group => {}
|
|
77
|
+
response.should render_template('new')
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
describe "DELETE destroy" do
|
|
85
|
+
# create a group for tests to operate on
|
|
86
|
+
before(:each) do
|
|
87
|
+
@group = Factory.create(:group, :name => "foo group")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it "deletes exactly one group" do
|
|
91
|
+
lambda do
|
|
92
|
+
delete :destroy, :id => @group.id
|
|
93
|
+
end.should change(Group, :count).by(-1)
|
|
94
|
+
end
|
|
95
|
+
it "removes the correct group" do
|
|
96
|
+
Group.find(@group.id).should_not be_nil
|
|
97
|
+
delete :destroy, :id => @group.id
|
|
98
|
+
lambda { Group.find(@group.id) }.should raise_error(ActiveRecord::RecordNotFound)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
data/lib/logical_authz/generators/specs/templates/spec/controllers/groups_users_controller_spec.rb
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require 'spec/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe GroupsUsersController do
|
|
4
|
+
include LogicalAuthz::MockAuth
|
|
5
|
+
|
|
6
|
+
before(:each) do
|
|
7
|
+
@admin = login_as( Factory.create(:authz_admin) )
|
|
8
|
+
request.env['HTTP_REFERER'] = "http://test.host/previous/page"
|
|
9
|
+
@user = Factory.create(:authz_account)
|
|
10
|
+
@user.groups.clear
|
|
11
|
+
@group = Factory.create(:group, :name => "registered")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "group should exist" do
|
|
15
|
+
@group.should_not be_nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe "POST 'create'" do
|
|
19
|
+
it "should succeed" do
|
|
20
|
+
post 'create', :user_id => @user.id, :group_id => @group.id
|
|
21
|
+
response.should be_redirect
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "should create the association" do
|
|
25
|
+
post 'create', :user_id => @user.id, :group_id => @group.id
|
|
26
|
+
@user.reload.groups.should include(@group)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
describe "GET 'destroy'" do
|
|
31
|
+
before(:each) do
|
|
32
|
+
@user.groups << @group
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "should be successful" do
|
|
36
|
+
delete 'destroy', :user_id => @user.id, :group_id => @group.id
|
|
37
|
+
response.should be_redirect
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "should delete the association" do
|
|
41
|
+
delete 'destroy', :user_id => @user.id, :group_id => @group.id
|
|
42
|
+
@user.reload.groups.should_not include(@group)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "should not allow removing a user from all_users"
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/logical_authz/generators/specs/templates/spec/controllers/permissions_controller_spec.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'spec/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe PermissionsController do
|
|
4
|
+
include LogicalAuthz::MockAuth
|
|
5
|
+
|
|
6
|
+
before(:each) do
|
|
7
|
+
@person = login_as(Factory.create(:authz_admin))
|
|
8
|
+
request.env["HTTP_ACCEPT"] = "application/javascript"
|
|
9
|
+
@group = Factory.create(:group)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe "POST Created" do
|
|
13
|
+
it "should respond with javascript" do
|
|
14
|
+
post :create, {
|
|
15
|
+
"permission"=> "true",
|
|
16
|
+
"group"=> @group.id,
|
|
17
|
+
"p_action"=>"show",
|
|
18
|
+
"p_controller" => "blah",
|
|
19
|
+
"object"=> 123
|
|
20
|
+
}
|
|
21
|
+
response.headers["Content-Type"].should =~ %r{/javascript}
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|