motion-authorization 1.0.0

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: 400658a54623abf6a66395abb59934aae5123f49
4
+ data.tar.gz: cddd3ec92a8340b14526c7e700374522289df96a
5
+ SHA512:
6
+ metadata.gz: 99ec888101700efb512d47c23b29455aee60012ba6237e321a6b822506fdacffb5ebad3383812f27a6c7aa5f68e0dc98f6f1f2d1d9b2a663e45d51590ee41e3d
7
+ data.tar.gz: 538fc59e76e941282a5ca461f8d5a0292deddb87f1a8d1aaf2ba4697aefe8081169ca1e613c048075b568b8e9a97221b1d0f4fa2563d351351e93b7a481779cf
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in motion-authorization.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Andrew Havens
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.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # Motion::Authorization
2
+
3
+ This gem provides simple authorization to your RubyMotion app. It was inspired by both [CanCan](https://github.com/ryanb/cancan) and [Pundit](https://github.com/elabs/pundit). There are no dependencies, so it should work in all kinds of RubyMotion apps. Permissions are defined as "policy" classes (like Pundit) and queried with a syntax similar to CanCan.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "motion-authorization"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Be sure to `require "motion-authorization"` in your Rakefile (unless you are using `Bundler.require`).
18
+
19
+ ## Usage
20
+
21
+ ### Step 1: Specify the current user.
22
+
23
+ This gem provides two different ways to specify the current user: directly or as a callback to be evaluated later. If setting directly, you will want to do that when the current user changes. Assuming you already have a method for fetching your current user, you can hook into your existing method by defining the `current_user_method` as a block.
24
+
25
+ ```ruby
26
+ Motion::Authorization.current_user = current_user_instance
27
+ # OR...
28
+ Motion::Authorization.current_user_method do
29
+ MyAuthClass.current_user
30
+ end
31
+ ```
32
+
33
+ ### Step 2: Include the DSL methods.
34
+
35
+ In any class where you want to check if the current user is authorized to do something, start by including the DSL methods:
36
+
37
+ ```ruby
38
+ class MyRubyMotionClass
39
+ include Motion::Authorization::Methods
40
+ # ...
41
+ end
42
+ ```
43
+
44
+ Now you can use one of Motion::Authorization's query methods to check if the current user can do something. Let's say that we have a `SecretMessage` object, and we only want to display it if the current user is able to view it.
45
+
46
+ ```ruby
47
+ display_secret_message if can? :view, secret_message
48
+ ```
49
+
50
+ Motion::Authorization provides several methods to choose from that may (or may not) make your code easier to read.
51
+
52
+ * `can?`
53
+ * `permitted_to?`
54
+ * `authorized_to?`
55
+ * `authorised_to?`
56
+
57
+ Choose the method which you like best.
58
+
59
+ ### Step 3: Define a Policy class.
60
+
61
+ Extending our previous example of `can? :view, secret_message`, Motion::Authorization will look for a class named `SecretMessagePolicy` with a method named `view?` which returns true or false.
62
+
63
+ Policy classes in Motion::Authorization are identical to those in [Pundit](https://github.com/elabs/pundit). Typically, you will create a class within a `policies` directory, such as `app/policies/secret_message_policy.rb`. Next, you would define a class that accepts the current user and object that it is related to. Something like this:
64
+
65
+ ```ruby
66
+ class SecretMessagePolicy
67
+ attr_accessor :user, :secret_message
68
+
69
+ def initialize(user, secret_message)
70
+ @user = user
71
+ @secret_message = secret_message
72
+ end
73
+
74
+ def view?
75
+ user.id == secret_message.owner.id
76
+ end
77
+ end
78
+ ```
79
+
80
+ Or even more simply by using `Struct`:
81
+
82
+
83
+ ```ruby
84
+ class SecretMessagePolicy < Struct.new(:user, :secret_message)
85
+ def view?
86
+ user.id == secret_message.owner.id
87
+ end
88
+ end
89
+ ```
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andrewhavens/motion-authorization.
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise 'This gem is intended to be used in a RubyMotion project.'
3
+ end
4
+
5
+ Motion::Project::App.setup do |app|
6
+ app.files += Dir.glob(File.join(File.dirname(__FILE__), 'motion', '**', '*.rb'))
7
+ end
@@ -0,0 +1,45 @@
1
+ module Motion
2
+ class Authorization
3
+
4
+ class << self
5
+ attr_accessor :current_user
6
+
7
+ def current_user_method(&block)
8
+ @current_user_block = block
9
+ end
10
+
11
+ def current_user
12
+ if @current_user_block
13
+ @current_user_block.call
14
+ else
15
+ @current_user
16
+ end
17
+ end
18
+
19
+ def can?(action, object)
20
+ policy_class_name = "#{object.class}Policy"
21
+
22
+ unless Object.const_defined?(policy_class_name)
23
+ puts "Undefined permissions policy class #{policy_class_name}"
24
+ return false
25
+ end
26
+
27
+ policy = policy_class_name.constantize.new(current_user, object)
28
+
29
+ unless policy.respond_to?("#{action}?")
30
+ puts "No #{action}? method found in #{policy_class_name}"
31
+ return false
32
+ end
33
+
34
+ policy.send "#{action}?"
35
+ end
36
+ end
37
+
38
+ module Methods
39
+ def can?(action, object) Motion::Authorization.can?(action, object) end
40
+ def permitted_to?(action, object) Motion::Authorization.can?(action, object) end
41
+ def authorized_to?(action, object) Motion::Authorization.can?(action, object) end
42
+ def authorised_to?(action, object) Motion::Authorization.can?(action, object) end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "motion-authorization"
5
+ spec.version = "1.0.0"
6
+ spec.authors = ["Andrew Havens"]
7
+ spec.email = ["email@andrewhavens.com"]
8
+
9
+ spec.summary = %q{Simple authorization for RubyMotion. Inspired by CanCan and Pundit.}
10
+ spec.homepage = "https://github.com/andrewhavens/motion-authorization"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+ spec.require_paths = ["lib"]
15
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-authorization
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Havens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - email@andrewhavens.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/motion-authorization.rb
26
+ - lib/motion/authorization.rb
27
+ - motion-authorization.gemspec
28
+ homepage: https://github.com/andrewhavens/motion-authorization
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.4.5
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Simple authorization for RubyMotion. Inspired by CanCan and Pundit.
52
+ test_files: []