sharing_policy 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: de27986501ee5d4754088513b1271acae1882d91
4
+ data.tar.gz: 1818d8421a6be74ea124cd06e5c2351b1fb6bb04
5
+ SHA512:
6
+ metadata.gz: b94c7cd7edcf2522941fbc7df6a51ebf69e68f9fff9178e1ffc87f793a422e19b085b1a5811cadcc2ce0ddd11279149a6829d080d7ec6e5e8185035dfeb89ad0
7
+ data.tar.gz: 9fed42302c061499ec43173e4053ea49d0ba559b4335a220c5c8b22eb7dd678433e1bef124526ece5c96f9b5b7ce073da88b60f19a8614dd649c11abb03672c6
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sharing_policy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # SharingPolicy
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sharing_policy'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sharing_policy
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/sharing_policy/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module SharingPolicy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,98 @@
1
+ require "sharing_policy/version"
2
+ require "active_support/concern"
3
+ require "active_support/dependencies/autoload"
4
+
5
+ module SharingPolicy
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ end
11
+
12
+ class << self
13
+ def policy(policy_holder)
14
+ require 'json'
15
+ JSON.parse(policy_holder.policy)
16
+ end
17
+ end
18
+
19
+ def init_policy(policy_text)
20
+ require 'json'
21
+ @policy = JSON.parse(policy_text)
22
+ end
23
+ end
24
+
25
+ module ViewingPolicy
26
+ extend ActiveSupport::Concern
27
+ include SharingPolicy
28
+
29
+ included do
30
+ end
31
+
32
+ def init_policy(policy_text)
33
+ require 'json'
34
+ @policy = JSON.parse(policy_text)
35
+ end
36
+
37
+ #tries to authorize user against policy, test against each user group
38
+ def authorize(user, membership_assert, action_assert)
39
+ auth_responses = []
40
+ @policy["cases"].each do |user_group, required_actions|
41
+ response_of_group = authorize_case(user, user_group, membership_assert, action_assert)
42
+ auth_responses << response_of_group
43
+ end
44
+
45
+ #responde with highest authorization can give
46
+ status_codes = auth_responses.map {|response| response[0]}
47
+
48
+ auth_responses.reject {|response| response[0] > status_codes.min}
49
+
50
+ end
51
+
52
+ #authorize user for each group specified in policy
53
+ #return [STATUS_CODE, MESSAGE, BODY]
54
+ def authorize_case(user, user_group, membership_assert, action_assert)
55
+ status_code, message, body = 500, "internal error", []
56
+
57
+ if membership_assert.call(user, user_group, self)
58
+ @group_policy = @policy["cases"][user_group]
59
+ required_actions = @group_policy["actions"]
60
+
61
+ if required_actions.size >= 1
62
+ required_actions.each { |action|
63
+ body << action if !action_assert.call(user, action, self)
64
+ }
65
+ end
66
+
67
+ if body.empty?
68
+ status_code, message = 200, "authorized as member of #{user_group}"
69
+ else
70
+ status_code = 300
71
+ message = "actions required"
72
+ end
73
+
74
+ else
75
+
76
+ status_code = 400
77
+ message = "no membership found"
78
+ end
79
+
80
+ [status_code, message, body]
81
+
82
+ end
83
+
84
+ end
85
+
86
+ module PullingPolicy
87
+ extend ActiveSupport::Concern
88
+ include SharingPolicy
89
+
90
+
91
+ def init_policy(policy_text)
92
+ require 'json'
93
+ @policy = JSON.parse(policy_text)
94
+ end
95
+
96
+ included do
97
+ end
98
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sharing_policy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sharing_policy"
8
+ spec.version = SharingPolicy::VERSION
9
+ spec.authors = ["Miushock"]
10
+ spec.email = ["miushock@gmail.com"]
11
+ spec.summary = %q{freelog sharing policy}
12
+ spec.description = %q{describe later}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 2.6"
24
+
25
+ spec.add_dependency "activesupport", "~> 4.0.0"
26
+ end
@@ -0,0 +1,116 @@
1
+ require "spec_helper"
2
+ require "sharing_policy"
3
+
4
+ describe ViewingPolicy do
5
+
6
+ let(:litters_digest) {Node.new(unhappy)}
7
+
8
+ #3 kittens
9
+ let(:dummy) {User.new("dummy@litter.com")}
10
+ let(:unhappy) {User.new("unhappy@litter.com")}
11
+ let(:tom) {User.new("tom@wb.com")}
12
+
13
+ #a rabbit and a bird
14
+ let(:bugs_bunny) {User.new("bbunny@wb.com")}
15
+ let(:tweety) {User.new("tweety@wb.com")}
16
+
17
+ #kitten user group
18
+ let(:cats) {UserGroup.new(litters_digest, [dummy, unhappy, tom])}
19
+ let(:litter_executives) {UserGroup.new([dummy, unhappy])}
20
+
21
+ let(:node) {Node.new(unhappy)}
22
+
23
+ let(:pulling_policy_copyleft) {File.read('./spec/testcase_policy/pulling_policy_copyleft.json')}
24
+ let(:viewing_policy_ffa) {File.read('./spec/testcase_policy/viewing_policy_ffa.json')}
25
+ let(:pay_once_policy) {File.read('./spec/testcase_policy/pay_once.json')}
26
+ let(:cats_only) {File.read('./spec/testcase_policy/cats_only.json')}
27
+
28
+ let(:resource_1) {Resource.new(dummy, "1", pulling_policy_copyleft)}
29
+ let(:presentable_1) {Presentable.new(node, resource_1, viewing_policy_ffa)}
30
+
31
+ let(:resource_2) {Resource.new(dummy, "2", pulling_policy_copyleft)}
32
+ let(:presentable_2) {Presentable.new(node, resource_2, cats_only)}
33
+
34
+ let(:pay_once_resource) {Resource.new(dummy, "3" , pulling_policy_copyleft)}
35
+ let(:pay_once_presentable) {Presentable.new(node, pay_once_resource, pay_once_policy)}
36
+
37
+ #create actions
38
+ let(:pay_for_resource_3) {Action.new("payment", "3", nil)}
39
+
40
+ describe ".viewing_policy" do
41
+ it "returns an viewing policy instance of given json spec" do
42
+ #puts SharingPolicy.policy(presentable_1)
43
+ end
44
+
45
+ it "raise parsing exception when failed to read or validate givine json" do
46
+ end
47
+ end
48
+
49
+ describe "#authorize" do
50
+ it "return OK status on public viewable resource" do
51
+ presentable_1.init_policy(viewing_policy_ffa)
52
+ result = presentable_1.authorize(tom, DummyPredicates.method(:member_of_group?), DummyPredicates.method(:action_conducted?))
53
+
54
+ result[0][0].should eql(200)
55
+ end
56
+
57
+ it "return OK status on self visit" do
58
+ presentable_1.init_policy(viewing_policy_ffa)
59
+ result = presentable_1.authorize(dummy, DummyPredicates.method(:member_of_group?), DummyPredicates.method(:action_conducted?))
60
+
61
+ result[0][0].should eql(200)
62
+ end
63
+
64
+ it "let cats access cats only resource" do
65
+ presentable_2.init_policy(cats_only)
66
+ result = presentable_2.authorize(unhappy, DummyPredicates.method(:member_of_group?), DummyPredicates.method(:action_conducted?))
67
+
68
+ result[0][0].should eql(200)
69
+ end
70
+
71
+ it "reject public from accessing cats resource" do
72
+ presentable_2.init_policy(cats_only)
73
+ result = presentable_2.authorize(bugs_bunny, DummyPredicates.method(:member_of_group?), DummyPredicates.method(:action_conducted?))
74
+
75
+ result[0][0].should eql(400)
76
+ end
77
+
78
+ it "accept/reject payed/unpayed user from accessing pay_once_resource" do
79
+ unhappy.take_action(pay_for_resource_3)
80
+
81
+ pay_once_presentable.init_policy(pay_once_policy)
82
+ paid_result = pay_once_presentable.authorize(unhappy, DummyPredicates.method(:member_of_group?), DummyPredicates.method(:action_conducted?))
83
+ unpaid_result = pay_once_presentable.authorize(bugs_bunny, DummyPredicates.method(:member_of_group?), DummyPredicates.method(:action_conducted?))
84
+
85
+
86
+
87
+ paid_result[0][0].should eql(200)
88
+ unpaid_result[0][0].should eql(300)
89
+ end
90
+
91
+ it "reject strangers from cats only resources" do
92
+ end
93
+ end
94
+
95
+ end
96
+
97
+ describe PullingPolicy do
98
+ let(:user) {double}
99
+ let(:node) {Node.new(user)}
100
+
101
+ let(:pulling_policy_copyleft) {File.read('./spec/testcase_policy/pulling_policy_copyleft.json')}
102
+ let(:viewing_policy_ffa) {File.read('./spec/testcase_policy/viewing_policy_ffa.json')}
103
+
104
+
105
+ let(:resource_1) {Resource.new(user, "123", pulling_policy_copyleft)}
106
+ let(:presentable_1) {Presentable.new(node, resource_1, viewing_policy_ffa)}
107
+
108
+ describe ".pulling_policy" do
109
+ it "returns an pulling policy instance when given json spec" do
110
+ # puts SharingPolicy.policy(resource_1)
111
+ end
112
+
113
+ it "raise parsing exception when failed to read or validate givine json" do
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,92 @@
1
+ require "active_support/core_ext/kernel"
2
+
3
+ warning = capture(:stderr) do
4
+ require "sharing_policy"
5
+ end
6
+
7
+ class User
8
+
9
+ attr_accessor :id
10
+
11
+ def initialize (id)
12
+ @id = id
13
+ @action_list = []
14
+ end
15
+
16
+ def actions
17
+ @action_list
18
+ end
19
+
20
+ def take_action(action)
21
+ @action_list << action
22
+ end
23
+ end
24
+
25
+ class Node < Struct.new(:owner)
26
+ end
27
+
28
+ class Presentable < Struct.new(:node, :resource, :policy)
29
+ include ViewingPolicy
30
+
31
+ def owner
32
+ resource.owner
33
+ end
34
+
35
+ def resource_id
36
+ resource.resource_id
37
+ end
38
+ end
39
+
40
+ class Resource < Struct.new(:owner, :resource_id, :policy)
41
+ include PullingPolicy
42
+ end
43
+
44
+ class UserGroup < Struct.new(:owner, :roster)
45
+ end
46
+
47
+ class Action
48
+ attr_accessor :type, :resource_id, :content
49
+ def initialize(type, resource_id, content)
50
+ @type, @resource_id, @content = type, resource_id, content
51
+ end
52
+
53
+ def eql?(other_action)
54
+ (@type.eql? other_action.type) && (@resource_id.eql? other_action.resource_id)
55
+ end
56
+ attr_accessor :type
57
+ end
58
+
59
+ module DummyPredicates
60
+ def self.member_of_group? (user, user_group, resource=nil)
61
+
62
+ case user_group
63
+ when "public"
64
+ return true
65
+ when "self"
66
+ return (resource.nil? ? false : resource.owner == user)
67
+ when "cats"
68
+ case user.id
69
+ when "dummy@litter.com"
70
+ return true
71
+ when "unhappy@litter.com"
72
+ return true
73
+ when "tom@wb.com"
74
+ return true
75
+ else
76
+ return false
77
+ end
78
+ else
79
+ return false
80
+ end
81
+ end
82
+
83
+ def self.action_conducted? (user, requirement, resource)
84
+ action = Action.new(requirement[0], resource.resource_id, nil)
85
+ action_record_list = user.actions
86
+ result = action_record_list.select do |record|
87
+ record.eql? action
88
+ end
89
+
90
+ return result.size > 0
91
+ end
92
+ end
@@ -0,0 +1,7 @@
1
+ {
2
+ "type": "viewing",
3
+ "cases": {
4
+ "self" : {"actions":{}},
5
+ "cats": {"actions":{}}
6
+ }
7
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "type": "viewing",
3
+ "cases": {
4
+ "self" : {"actions":{}},
5
+ "public": {"actions":
6
+ {
7
+ "payment":50
8
+ }
9
+ }
10
+ }
11
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "type": "pulling",
3
+ "cases": {
4
+ "self": {},
5
+ "public": {
6
+ "actions":{
7
+ "agreement": "GPL"
8
+ }
9
+ }
10
+ }
11
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "type": "viewing",
3
+ "cases": {
4
+ "self": {"actions":{}},
5
+ "public": {"actions":{}}
6
+ }
7
+ }
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sharing_policy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Miushock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ description: describe later
70
+ email:
71
+ - miushock@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/sharing_policy.rb
82
+ - lib/sharing_policy/version.rb
83
+ - sharing_policy.gemspec
84
+ - spec/sharing_policy_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/testcase_policy/cats_only.json
87
+ - spec/testcase_policy/pay_once.json
88
+ - spec/testcase_policy/pulling_policy_copyleft.json
89
+ - spec/testcase_policy/viewing_policy_ffa.json
90
+ homepage: ''
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: freelog sharing policy
114
+ test_files:
115
+ - spec/sharing_policy_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/testcase_policy/cats_only.json
118
+ - spec/testcase_policy/pay_once.json
119
+ - spec/testcase_policy/pulling_policy_copyleft.json
120
+ - spec/testcase_policy/viewing_policy_ffa.json