cannie 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4767806af908d3fbb42922daeb46aa9793c43f09
4
+ data.tar.gz: d1e4a3df2a8055cfa9e80645b63ceb62a66ea88c
5
+ SHA512:
6
+ metadata.gz: 28cae6c32b9616c3d9af5983b0044cd1b34044b552f55856a680adc3560548d6b82e064ca6a9356ef09bbc37cf4672e81293a35ffd4152786435f77111700b3c
7
+ data.tar.gz: a03394d05eaea44c85b0e7d2572f911b24f10700c0d5fd3e78c578919af0ebffaa6114cb234100fb936ce67718f4193f6e78261a44f9979e65e310324861bc2c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 4.0'
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ gem 'simplecov'
8
+ end
9
+
10
+ # Specify your gem's dependencies in cannie.gemspec
11
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 hck
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,94 @@
1
+ # Cannie
2
+
3
+ Cannie is a gem for authorization/permissions checking.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cannie'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cannie
18
+
19
+ ## Usage
20
+
21
+ ### Define permissions
22
+
23
+ Permissions are defined in Permissions class, which could be generated by Rails generator:
24
+
25
+ rails g cannie:permissions
26
+
27
+ Than you can define all the permissions you want inside ::initialize method of Permissions class:
28
+
29
+ class Permissions
30
+ include Cannie::Permissions
31
+
32
+ def initialize(user)
33
+ if user.admin?
34
+ allow :manage, on: :all
35
+ else
36
+ allow :read, on: Post
37
+ allow :read, on: Comment
38
+ allow :create, on: Comment
39
+
40
+ # allow delete comments, that were created only if user has posted those comments
41
+ allow :delete, on: Comment do |*comments|
42
+ comments.all?{|c| c.user_id == user.id}
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ ### Checking permissions
49
+
50
+ To be sure that permissions checking is handled in each action of your controller, add `check_permissions` method call to your controllers:
51
+
52
+ class PostsController < ApplicationController
53
+ check_permissions
54
+
55
+ #...
56
+ end
57
+
58
+ To skip checking permissions for controller, add `skip_check_permissions` method call:
59
+
60
+ class PagesController < ApplicationController
61
+ skip_check_permissions
62
+
63
+ #...
64
+ end
65
+
66
+ Checking of permissions on per-action basis is done by calling `permit!` method inside of controller's actions:
67
+
68
+ class PostsController < ApplicationController
69
+ check_permissions
70
+
71
+ def index
72
+ @posts = Posts.all
73
+ permit! :read, on: posts # checks whether user able to read fetched posts
74
+ end
75
+ end
76
+
77
+ ### Handling of unpermitted access
78
+
79
+ If user is not permitted for appropriate action, `Cannie::ActionForbidden` exception will be raised.
80
+ It can be handled globally by using `rescue_from` inside ApplicationController:
81
+
82
+ class ApplicationController < ActionController::Base
83
+ rescue_from Cannie::ActionForbidden do |exception|
84
+ redirect_to root_path, alert: exception.message
85
+ end
86
+ end
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/cannie.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cannie/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cannie"
8
+ spec.version = Cannie::VERSION
9
+ spec.authors = ["hck"]
10
+ spec.description = %q{Cannie is a gem for authorization/permissions checking on per-controller/per-action basis.}
11
+ spec.summary = %q{Simple gem for checking permissions on per-action basis}
12
+ spec.homepage = "http://guthub.com/hck/cannie"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ end
@@ -0,0 +1,3 @@
1
+ module Cannie
2
+ VERSION = "0.0.1"
3
+ end
data/lib/cannie.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'cannie/exceptions'
2
+ require 'cannie/rule'
3
+ require 'cannie/permissions'
4
+ require 'cannie/controller_extensions'
5
+ require 'cannie/version'
6
+
7
+ module Cannie
8
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cannie::Rule do
4
+ describe '#initialize' do
5
+ it 'stores passed actions' do
6
+ actions = %i(read create update delete)
7
+ rule = described_class.new *actions, Array
8
+ expect(rule.actions).to eq(actions)
9
+ end
10
+
11
+ it 'stores passed subject' do
12
+ rule = described_class.new :read, Array
13
+ expect(rule.subject).to eq(Array)
14
+ end
15
+
16
+ it 'scores passed block' do
17
+ rule = described_class.new(:read, Array){ |*attrs| attrs.all?{ |v| v % 2 == 0 } }
18
+ expect(rule.condition.call(2,4,8)).to be_true
19
+ end
20
+ end
21
+
22
+ describe '#permits?' do
23
+ let(:rule) do
24
+ described_class.new(:read, Array) do |*attrs|
25
+ attrs.all?{ |v| v % 2 == 0 }
26
+ end
27
+ end
28
+
29
+ it 'returns true if result of executing condition is true' do
30
+ expect(rule.permits?(2,4,8)).to be_true
31
+ end
32
+
33
+ it 'returns false if result of executing condition is false' do
34
+ expect(rule.permits?(1,4,8)).to be_false
35
+ end
36
+
37
+ it 'returns true for any subject if rule subject is :all' do
38
+ rule = described_class.new(:read, :all)
39
+ expect(rule.permits?(1,2,3)).to be_true
40
+ end
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cannie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hck
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-07 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Cannie is a gem for authorization/permissions checking on per-controller/per-action
42
+ basis.
43
+ email:
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - cannie.gemspec
54
+ - lib/cannie.rb
55
+ - lib/cannie/version.rb
56
+ - spec/cannie/rule_spec.rb
57
+ homepage: http://guthub.com/hck/cannie
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.0.3
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Simple gem for checking permissions on per-action basis
81
+ test_files:
82
+ - spec/cannie/rule_spec.rb