rabarber 1.0.3 → 1.0.5

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: f83c97e518621ea7749eab381d5e1e4f84785c8197036e923220fa0d29fcc43e
4
- data.tar.gz: 9fe75afecf6c3066c0cbf779ffdf79a2e67716a8acdc0f15c72e85008528f5ab
3
+ metadata.gz: e54ea7ddbb816cbc279edf4cb5d8863086fb452528f1403da9c944e4d23b862a
4
+ data.tar.gz: a6cc1c88ced2c76561c368669e6413a476b7590fa105a9876a6dfcbd139c2b41
5
5
  SHA512:
6
- metadata.gz: 407ed9c374cbe8dafd964aa939f342d47a307bd101e2932bf8ae12c526f48a75524df6eb6f6f35a6a3fb4a023f7db5e50a7c04b580f56fde06ccfcc09f431df8
7
- data.tar.gz: 4cfcde7d7b6ed23faff8d0e6f8f036c641ecf6dfb18e1b075dbdfd6aaa5f8391dfdf13ea7e25b70b7881489fc3b292791f663c6c70b1a32a634e7caf681a2355
6
+ metadata.gz: ae10eca6248824b4dab6a7038b813933ee9ecaab1fd1cce67ef07ed95fdb0a77f5f70b3138574e9b42ea5d570da2a56186845978950b1fda03240e836cb07f8f
7
+ data.tar.gz: f8f4e00a8b76a8eb7a23e7ad7088ab38325e23ebd2ccab43b744def1d977b531f02866025c56f982578d3af8894582756bd980057159095a1bfe9eb3f6d261d5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.0.5
2
+
3
+ - Add co-author: [trafium](https://github.com/trafium)
4
+
5
+ ## 1.0.4
6
+
7
+ - Allow to use strings as role names
8
+
1
9
  ## 1.0.3
2
10
 
3
11
  - Enhance clarity by improving error types and messages
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2023 enjaku4
3
+ Copyright (c) 2023 enjaku4, trafium
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -21,19 +21,21 @@ module Rabarber
21
21
  end
22
22
 
23
23
  def current_user_method=(method_name)
24
- raise ConfigurationError, "current_user_method must be a symbol" unless method_name.is_a?(Symbol)
24
+ unless (method_name.is_a?(Symbol) || method_name.is_a?(String)) && method_name.present?
25
+ raise ConfigurationError, "Configuration 'current_user_method' must be a Symbol or a String"
26
+ end
25
27
 
26
28
  @current_user_method = method_name.to_sym
27
29
  end
28
30
 
29
31
  def must_have_roles=(value)
30
- raise ConfigurationError, "must_have_roles must be a boolean" unless [true, false].include?(value)
32
+ raise ConfigurationError, "Configuration 'must_have_roles' must be a Boolean" unless [true, false].include?(value)
31
33
 
32
34
  @must_have_roles = value
33
35
  end
34
36
 
35
37
  def when_unauthorized=(callable)
36
- raise ConfigurationError, "when_unauthorized must be a proc" unless callable.is_a?(Proc)
38
+ raise ConfigurationError, "Configuration 'when_unauthorized' must be a Proc" unless callable.is_a?(Proc)
37
39
 
38
40
  @when_unauthorized = callable
39
41
  end
@@ -19,36 +19,23 @@ module Rabarber
19
19
  end
20
20
 
21
21
  def has_role?(*role_names)
22
- validate_role_names(role_names)
23
-
24
- (roles & role_names).any?
22
+ (roles & RoleNames.pre_process(role_names)).any?
25
23
  end
26
24
 
27
25
  def assign_roles(*role_names, create_new: true)
28
- validate_role_names(role_names)
26
+ roles_to_assign = RoleNames.pre_process(role_names)
29
27
 
30
- create_new_roles(role_names) if create_new
28
+ create_new_roles(roles_to_assign) if create_new
31
29
 
32
- rabarber_roles << Role.where(name: role_names) - rabarber_roles
30
+ rabarber_roles << Role.where(name: roles_to_assign) - rabarber_roles
33
31
  end
34
32
 
35
33
  def revoke_roles(*role_names)
36
- validate_role_names(role_names)
37
-
38
- self.rabarber_roles = rabarber_roles - Role.where(name: role_names)
34
+ self.rabarber_roles = rabarber_roles - Role.where(name: RoleNames.pre_process(role_names))
39
35
  end
40
36
 
41
37
  private
42
38
 
43
- def validate_role_names(role_names)
44
- return if role_names.all? { |role_name| role_name.is_a?(Symbol) && role_name.to_s.match?(Role::NAME_REGEX) }
45
-
46
- raise(
47
- InvalidArgumentError,
48
- "Role names must be symbols and may only contain lowercase letters, numbers and underscores"
49
- )
50
- end
51
-
52
39
  def create_new_roles(role_names)
53
40
  new_roles = role_names - Role.names
54
41
  new_roles.each { |role_name| Role.create!(name: role_name) }
@@ -4,9 +4,7 @@ module Rabarber
4
4
  class Role < ActiveRecord::Base
5
5
  self.table_name = "rabarber_roles"
6
6
 
7
- NAME_REGEX = /\A[a-z0-9_]+\z/
8
-
9
- validates :name, presence: true, uniqueness: true, format: { with: NAME_REGEX }
7
+ validates :name, presence: true, uniqueness: true, format: { with: RoleNames::REGEX }
10
8
 
11
9
  def self.names
12
10
  pluck(:name).map(&:to_sym)
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rabarber
4
+ module RoleNames
5
+ module_function
6
+
7
+ REGEX = /\A[a-z0-9_]+\z/
8
+
9
+ def pre_process(role_names)
10
+ return role_names.map(&:to_sym) if role_names.all? do |role_name|
11
+ (role_name.is_a?(Symbol) || role_name.is_a?(String)) && role_name.to_s.match?(REGEX)
12
+ end
13
+
14
+ raise(
15
+ InvalidArgumentError,
16
+ "Role names must be Symbols or Strings and may only contain lowercase letters, numbers and underscores"
17
+ )
18
+ end
19
+ end
20
+ end
data/lib/rabarber/rule.rb CHANGED
@@ -5,9 +5,9 @@ module Rabarber
5
5
  attr_reader :action, :roles, :custom
6
6
 
7
7
  def initialize(action, roles, custom)
8
- @action = validate_action(action)
9
- @roles = validate_roles(roles)
10
- @custom = validate_custom_rule(custom)
8
+ @action = pre_process_action(action)
9
+ @roles = RoleNames.pre_process(Array(roles))
10
+ @custom = pre_process_custom_rule(custom)
11
11
  end
12
12
 
13
13
  def verify_access(user_roles, custom_rule_receiver, action_name = nil)
@@ -38,29 +38,18 @@ module Rabarber
38
38
  end
39
39
  end
40
40
 
41
- def validate_action(action)
42
- return action if action.nil? || action.is_a?(Symbol)
41
+ def pre_process_action(action)
42
+ return action.to_sym if (action.is_a?(String) || action.is_a?(Symbol)) && action.present?
43
+ return action if action.nil?
43
44
 
44
- raise InvalidArgumentError, "Action name must be a symbol"
45
+ raise InvalidArgumentError, "Action name must be a Symbol or a String"
45
46
  end
46
47
 
47
- def validate_roles(roles)
48
- roles_array = Array(roles)
48
+ def pre_process_custom_rule(custom)
49
+ return custom.to_sym if (custom.is_a?(String) || custom.is_a?(Symbol)) && action.present?
50
+ return custom if custom.nil? || custom.is_a?(Proc)
49
51
 
50
- return roles_array if roles_array.empty? || roles_array.all? do |role|
51
- role.is_a?(Symbol) && role.match?(Role::NAME_REGEX)
52
- end
53
-
54
- raise(
55
- InvalidArgumentError,
56
- "Role names must be symbols and may only contain lowercase letters, numbers and underscores"
57
- )
58
- end
59
-
60
- def validate_custom_rule(custom)
61
- return custom if custom.nil? || custom.is_a?(Symbol) || custom.is_a?(Proc)
62
-
63
- raise InvalidArgumentError, "Custom rule must be a symbol or a proc"
52
+ raise InvalidArgumentError, "Custom rule must be a Symbol, a String, or a Proc"
64
53
  end
65
54
  end
66
55
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rabarber
4
- VERSION = "1.0.3"
4
+ VERSION = "1.0.5"
5
5
  end
data/lib/rabarber.rb CHANGED
@@ -6,13 +6,12 @@ require_relative "rabarber/configuration"
6
6
  require "active_record"
7
7
  require "active_support"
8
8
 
9
- require_relative "rabarber/controllers/concerns/authorization"
9
+ require_relative "rabarber/role_names"
10
10
 
11
+ require_relative "rabarber/controllers/concerns/authorization"
11
12
  require_relative "rabarber/helpers/helpers"
12
-
13
- require_relative "rabarber/models/role"
14
13
  require_relative "rabarber/models/concerns/has_roles"
15
-
14
+ require_relative "rabarber/models/role"
16
15
  require_relative "rabarber/permissions"
17
16
 
18
17
  module Rabarber
data/rabarber.gemspec CHANGED
@@ -5,7 +5,7 @@ require_relative "lib/rabarber/version"
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "rabarber"
7
7
  spec.version = Rabarber::VERSION
8
- spec.authors = ["enjaku4"]
8
+ spec.authors = ["enjaku4", "trafium"]
9
9
  spec.email = ["enjaku4@gmail.com"]
10
10
 
11
11
  spec.summary = "Simple authorization library for Ruby on Rails."
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabarber
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - enjaku4
8
+ - trafium
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2024-01-01 00:00:00.000000000 Z
12
+ date: 2024-01-10 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rails
@@ -44,6 +45,7 @@ files:
44
45
  - lib/rabarber/models/concerns/has_roles.rb
45
46
  - lib/rabarber/models/role.rb
46
47
  - lib/rabarber/permissions.rb
48
+ - lib/rabarber/role_names.rb
47
49
  - lib/rabarber/rule.rb
48
50
  - lib/rabarber/version.rb
49
51
  - rabarber.gemspec