Skip to content

Commit a72abd7

Browse files
authored
Inline Allowy gem (#4926)
1 parent ba4cf9a commit a72abd7

File tree

14 files changed

+479
-7
lines changed

14 files changed

+479
-7
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
source 'https://rubygems.org'
22

33
gem 'addressable'
4-
gem 'allowy', '>= 2.1.0'
54
gem 'bootsnap', require: false
65
gem 'clockwork', require: false
76
gem 'cloudfront-signer'

Gemfile.lock

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ GEM
7171
aliyun-sdk (0.8.0)
7272
nokogiri (~> 1.6)
7373
rest-client (~> 2.0)
74-
allowy (2.1.0)
75-
activesupport (>= 3.2)
76-
i18n
7774
ast (2.4.3)
7875
azure-core (0.1.15)
7976
faraday (~> 0.9)
@@ -619,7 +616,6 @@ DEPENDENCIES
619616
actionview (~> 8.1.1)
620617
activemodel (~> 8.1.2)
621618
addressable
622-
allowy (>= 2.1.0)
623619
azure-storage-blob!
624620
bootsnap
625621
byebug

lib/allowy/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Dmytrii Nagirniak
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

lib/allowy/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Allowy (Internalized Copy)
2+
3+
This directory contains an internalized copy of the archived allowy authorization library:
4+
https://github.com/dnagir/allowy
5+
6+
**License:** MIT License
7+
**Copyright:** (c) 2014 Dmytrii Nagirniak
8+
**Inlined version:** 2.1.0
9+
**Source commit:** `5d2c6f09a9617a2ad097a3b11ecabb32d48ff80b` (2015-01-06)
10+
**Upstream status:** Archived (last commit: 2015-01-06)
11+
12+
The upstream LICENSE file is included in this directory.
13+
14+
## Why Inlined
15+
16+
- The upstream repository was archived with no updates since 2015
17+
- Removes external gem dependency
18+
- CCNG only uses a subset of allowy functionality (AccessControl, Context, Registry)
19+
20+
## Changes from Upstream
21+
22+
**Files included:** `access_control.rb`, `context.rb`, `registry.rb` (with RuboCop fixes applied)
23+
24+
**Files skipped (not used by CCNG):**
25+
- `controller_extensions.rb` - Rails helper_method integration
26+
- `matchers.rb` and `rspec.rb` - RSpec `be_able_to` matcher (CCNG uses its own `allow_op_on_object`)
27+
- `version.rb` - version constant
28+
29+
## Usage in CCNG
30+
31+
Allowy is used **only by the V2 API** for authorization. This code can be removed together with the V2 API removal.
32+
33+
Note: If `/v2/info` endpoint is kept after V2 removal, `InfoController` should be refactored to not extend `RestController::BaseController` first.
34+
35+
The V3 API uses a different authorization system (`VCAP::CloudController::Permissions`).
36+
37+
## Tests
38+
39+
```bash
40+
bundle exec rspec spec/unit/lib/allowy/
41+
```

lib/allowy/access_control.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
# Inlined from https://github.com/dnagir/allowy
4+
# See lib/allowy/README.md for details
5+
6+
module Allowy
7+
# This module provides the interface for implementing the access control actions.
8+
# In order to use it, mix it into a plain Ruby class and define methods ending with `?`.
9+
#
10+
# @example
11+
# class PageAccess
12+
# include Allowy::AccessControl
13+
#
14+
# def view?(page)
15+
# page and page.wiki? and context.user_signed_in?
16+
# end
17+
# end
18+
#
19+
# And then you can check the permissions from a controller:
20+
#
21+
# @example
22+
# def show
23+
# @page = Page.find params[:id]
24+
# authorize! :view, @page
25+
# end
26+
#
27+
module AccessControl
28+
extend ActiveSupport::Concern
29+
30+
included do
31+
attr_reader :context
32+
end
33+
34+
def initialize(ctx)
35+
@context = ctx
36+
end
37+
38+
def can?(action, subject, *params)
39+
allowing, _payload = check_permission(action, subject, *params)
40+
allowing
41+
end
42+
43+
def cannot?(*)
44+
!can?(*)
45+
end
46+
47+
def authorize!(action, subject, *params)
48+
allowing, payload = check_permission(action, subject, *params)
49+
raise AccessDenied.new('Not authorized', action, subject, payload) unless allowing
50+
end
51+
52+
def deny!(payload)
53+
throw(:deny, payload)
54+
end
55+
56+
private
57+
58+
def check_permission(action, subject, *params)
59+
m = "#{action}?"
60+
raise UndefinedAction.new("The #{self.class.name} needs to have #{m} method. Please define it.") unless respond_to?(m)
61+
62+
allowing = false
63+
payload = catch(:deny) { allowing = send(m, subject, *params) }
64+
[allowing, payload]
65+
end
66+
end
67+
end

lib/allowy/allowy.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
# Inlined from https://github.com/dnagir/allowy
4+
# See lib/allowy/README.md for details
5+
6+
require 'active_support'
7+
require 'active_support/core_ext'
8+
require 'active_support/concern'
9+
require 'active_support/inflector'
10+
11+
require 'allowy/access_control'
12+
require 'allowy/registry'
13+
require 'allowy/context'
14+
15+
module Allowy
16+
class UndefinedAccessControl < StandardError; end
17+
class UndefinedAction < StandardError; end
18+
19+
class AccessDenied < StandardError
20+
attr_reader :action, :subject, :payload
21+
22+
def initialize(message, action, subject, payload=nil)
23+
super(message)
24+
@action = action
25+
@subject = subject
26+
@payload = payload
27+
end
28+
end
29+
end

lib/allowy/context.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
# Inlined from https://github.com/dnagir/allowy
4+
# See lib/allowy/README.md for details
5+
6+
module Allowy
7+
# This module provides the default and common context for checking the permissions.
8+
# It is mixed into controllers and provides an easy way to reuse it
9+
# in other parts of the application (RSpec, Cucumber or standalone).
10+
#
11+
# @example
12+
# class MyContext
13+
# include Allowy::Context
14+
# attr_accessor :current_user
15+
#
16+
# def initialize(user)
17+
# @current_user = user
18+
# end
19+
# end
20+
#
21+
# And then you can easily check the permissions like so:
22+
#
23+
# @example
24+
# MyContext.new(that_user).can?(:create, Blog)
25+
#
26+
module Context
27+
extend ActiveSupport::Concern
28+
29+
def allowy_context
30+
self
31+
end
32+
33+
def current_allowy
34+
@current_allowy ||= ::Allowy::Registry.new(allowy_context)
35+
end
36+
37+
def can?(action, subject, *)
38+
current_allowy.access_control_for!(subject).can?(action, subject, *)
39+
end
40+
41+
def cannot?(action, subject, *)
42+
current_allowy.access_control_for!(subject).cannot?(action, subject, *)
43+
end
44+
45+
def authorize!(action, subject, *)
46+
current_allowy.access_control_for!(subject).authorize!(action, subject, *)
47+
end
48+
end
49+
end

lib/allowy/registry.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
# Inlined from https://github.com/dnagir/allowy
4+
# See lib/allowy/README.md for details
5+
6+
module Allowy
7+
# Registry maps objects to their corresponding Access classes.
8+
# Given a Space object, it finds SpaceAccess class automatically.
9+
class Registry
10+
def initialize(ctx, options={})
11+
options.assert_valid_keys(:access_suffix)
12+
@context = ctx
13+
@registry = {}
14+
@options = options
15+
end
16+
17+
def access_control_for!(subject)
18+
ac = access_control_for(subject)
19+
raise UndefinedAccessControl.new("Please define Access Control class for #{subject.inspect}") unless ac
20+
21+
ac
22+
end
23+
24+
def access_control_for(subject)
25+
# Try subject as decorated object
26+
clazz = class_for(subject.class.source_class.name) if subject.class.respond_to?(:source_class)
27+
28+
# Try subject as an object
29+
clazz ||= class_for(subject.class.name)
30+
31+
# Try subject as a class
32+
clazz = class_for(subject.name) if !clazz && subject.is_a?(Class)
33+
34+
return unless clazz
35+
36+
# create a new instance or return existing
37+
@registry[clazz] ||= clazz.new(@context)
38+
end
39+
40+
private
41+
42+
def class_for(name)
43+
"#{name}#{access_suffix}".safe_constantize
44+
end
45+
46+
def access_suffix
47+
@options.fetch(:access_suffix, 'Access')
48+
end
49+
end
50+
end

lib/cloud_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'oj'
44
require 'delayed_job'
55

6-
require 'allowy'
6+
require 'allowy/allowy'
77

88
require 'uaa/token_coder'
99

spec/spec_helper_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def self.init
3030
require 'pry'
3131

3232
require 'cloud_controller'
33-
require 'allowy/rspec'
3433

3534
require 'rspec_api_documentation'
3635
require 'services'

0 commit comments

Comments
 (0)