guarda 0.0.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37fb9b774cc00795bf96edc8655287433b68c4f9f1b95f146cfd28936a887343
4
- data.tar.gz: 4a94650d4778d42c8104294f0193b3621f0f7b07c11d0d3c5cf12c156153928b
3
+ metadata.gz: 6589ad1f554cc85e4de7d7741dbae1f3dadca5ce9470ff8193608e3780f3f9f8
4
+ data.tar.gz: f58af82e04958ae3224d63655ea0c0557600f66dc30007a8c52bb4968720d281
5
5
  SHA512:
6
- metadata.gz: a0d29b1ae0d34a9bfa5f2c2bbc5cc076a55d8b394495ef886cb6348e4887740287fae4ba862d4b5536095ac37f4ff04e8309940113f1aea2836224843dc26c3c
7
- data.tar.gz: 46843ee2edf9e91337c0828d2a1451f324160f857542cbad424a9a4a290730ff8ecb14deb07b6d231a14bf77f86bfdc5e3e9554d405879b6683fcb75eb4bd76c
6
+ metadata.gz: e3fdf34785e8357041d476f2425856427b49951dcf371a85f2fefbb68e66e4b24fed6f4f9c8a422bee288fe1da19ed6aa4a3faa9538ffeacaa7323de27d83013
7
+ data.tar.gz: f9320de4e0061852cc22c39fb377cef0e8a20eaa499b6aa491d5e22d20cf8db739321c8ee79570d1dcc58127e0579c543ada8a478c1248c53aeafceccc581a06
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Abdullah Hashim
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,71 @@
1
+ # Guarda
2
+
3
+ Is another authorization gem that was heavily inspired by [Pundit](https://rubygems.org/gems/pundit).
4
+
5
+ It assumes you are using [Current Attributes](https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html) to get your currently authenticated user.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```bash
12
+ bundle add guarda
13
+ ```
14
+
15
+ Include `Guarda::Authorization` in your application controller:
16
+
17
+ ```ruby
18
+ class ApplicationController < ActionController::Base
19
+ include Guarda::Authorization
20
+ end
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ In the controller:
26
+
27
+ ```ruby
28
+ class PostsController < ApplicationController
29
+ def index
30
+ authorize
31
+ end
32
+
33
+ def update
34
+ authorize @post
35
+ end
36
+ end
37
+ ```
38
+
39
+ In the view:
40
+
41
+ ```erb
42
+ <% if policy("posts").index? %>
43
+ <%= link_to "Posts", "#" %>
44
+ <% end %>
45
+
46
+ <% if policy("posts", @post).update? %>
47
+ <%= link_to "Edit Post", "#" %>
48
+ <% end %>
49
+ ```
50
+
51
+ With this policy class `app/policies/posts_policy.rb`:
52
+
53
+ ```ruby
54
+ class PostsPolicy
55
+ def initialize(post = nil)
56
+ @post = post
57
+ end
58
+
59
+ def index?
60
+ Current.person.admin?
61
+ end
62
+
63
+ def update?
64
+ @post.author == Current.person
65
+ end
66
+ end
67
+ ```
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
data/guarda.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/guarda/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "guarda"
7
+ spec.version = Guarda::VERSION
8
+ spec.authors = ["Abdullah Hashim"]
9
+ spec.email = ["abdul@hey.com"]
10
+
11
+ spec.summary = "Another authorization gem"
12
+ spec.homepage = "https://wwww.guidedrails.com"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.1.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/Guided-Rails/guarda"
18
+
19
+ spec.files = Dir.chdir(__dir__) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ (File.expand_path(f) == __FILE__) ||
22
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
23
+ end
24
+ end
25
+
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "activesupport", "~> 7.1", ">= 7.1.3"
31
+ spec.add_development_dependency "minitest-reporters", "~> 1.6", ">= 1.6.1"
32
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Guarda
4
+ module Authorization
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ if respond_to?(:helper_method)
9
+ helper_method :policy
10
+ end
11
+ end
12
+
13
+ def authorize(record = nil, controller: nil, query: nil)
14
+ @_authorization_performed = true
15
+ controller ||= controller_path
16
+ query ||= "#{action_name}?"
17
+
18
+ policy(controller, record).public_send(query) ||
19
+ raise(NotAuthorizedError, self.class)
20
+ end
21
+
22
+ def policy(controller, record)
23
+ PolicyFinder.find(controller).new(record)
24
+ end
25
+
26
+ def verify_authorization_performed
27
+ authorization_performed? ||
28
+ raise(AuthorizationNotPerformedError, self.class)
29
+ end
30
+
31
+ private
32
+
33
+ def authorization_performed?
34
+ !!@_authorization_performed
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Guarda
4
+ class PolicyFinder
5
+ def initialize(controller_name)
6
+ @controller_name = controller_name
7
+ end
8
+
9
+ def self.find(controller_name)
10
+ new(controller_name).find
11
+ end
12
+
13
+ def find
14
+ policy_class.presence || raise(NotFoundError, policy_class_name)
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :controller_name
20
+
21
+ def policy_class
22
+ policy_class_name.safe_constantize
23
+ end
24
+
25
+ def policy_class_name
26
+ controller_name.to_s.camelize.concat("Policy")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Guarda
4
+ VERSION = "0.2.0"
5
+ end
data/lib/guarda.rb CHANGED
@@ -1,5 +1,13 @@
1
- class Guarda
2
- def self.hi
3
- puts "Hello World"
4
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+ require_relative "guarda/version"
5
+ require_relative "guarda/policy_finder"
6
+ require_relative "guarda/authorization"
7
+
8
+ module Guarda
9
+ class Error < StandardError; end
10
+ class Authorization::NotAuthorizedError < Error; end
11
+ class Authorization::AuthorizationNotPerformedError < Error; end
12
+ class PolicyFinder::NotFoundError < Error; end
5
13
  end
data/sig/guarda.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Guarda
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata CHANGED
@@ -1,26 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guarda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdullah Hashim
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-21 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Another Authorization Gem
14
- email: abdul@hey.com
11
+ date: 2024-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 7.1.3
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '7.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 7.1.3
33
+ - !ruby/object:Gem::Dependency
34
+ name: minitest-reporters
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.6'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.6.1
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.6'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.6.1
53
+ description:
54
+ email:
55
+ - abdul@hey.com
15
56
  executables: []
16
57
  extensions: []
17
58
  extra_rdoc_files: []
18
59
  files:
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - guarda.gemspec
19
64
  - lib/guarda.rb
20
- homepage: https://rubygems.org/gems/guarda
65
+ - lib/guarda/authorization.rb
66
+ - lib/guarda/policy_finder.rb
67
+ - lib/guarda/version.rb
68
+ - sig/guarda.rbs
69
+ homepage: https://wwww.guidedrails.com
21
70
  licenses:
22
71
  - MIT
23
- metadata: {}
72
+ metadata:
73
+ homepage_uri: https://wwww.guidedrails.com
74
+ source_code_uri: https://github.com/Guided-Rails/guarda
24
75
  post_install_message:
25
76
  rdoc_options: []
26
77
  require_paths:
@@ -29,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
29
80
  requirements:
30
81
  - - ">="
31
82
  - !ruby/object:Gem::Version
32
- version: '0'
83
+ version: 3.1.0
33
84
  required_rubygems_version: !ruby/object:Gem::Requirement
34
85
  requirements:
35
86
  - - ">="
@@ -39,5 +90,5 @@ requirements: []
39
90
  rubygems_version: 3.3.7
40
91
  signing_key:
41
92
  specification_version: 4
42
- summary: Authorization
93
+ summary: Another authorization gem
43
94
  test_files: []