moduler 0.7.0 → 0.8.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: 48b04e1bddcad684cb26f044713db46a9b049c4f6a7ceaa0f0f5e60211dd085e
4
- data.tar.gz: c01f94740262115c801c293abeeacec41a53c4e6fa17bcba1a8c8bd5899d3439
3
+ metadata.gz: 38043a542a46b3fab8f574d2c177a80e2c79bbbd5a6303ace0e5e21d0d7e3ca5
4
+ data.tar.gz: 4d8f5c2a53e892702bbc5f9ccce4cfdfa8bc78a7c18d8b667199afb0d97f5550
5
5
  SHA512:
6
- metadata.gz: 4889b74712446a913cb1ec858ddb37a85cec01bd55769ba45d648250b01f0308e1f8a38e303d99a612f65d2cac105609fbc5db1d219686d80ba65376ec8f6eeb
7
- data.tar.gz: a09513f54e837d8b7cf0f70a32ee9171b55c05bc2428ab5a71422bdbfe41428364f5bb384205e321c73bb20340b097f4e74fa74b05f36bb74c36ca61c47c1304
6
+ metadata.gz: 8bb6e309397bf8ada31a4cfd5925164b2ef3dcf4d234434ce9dd41205aa210d45117e04782e8b7a8d945d5b9b4c98c3643dc1aa65f7e69f8bb6eebffa6b783a7
7
+ data.tar.gz: 90e433d24a4aa2f3a2b992a14e5909d77a065f160ab3f6c97b318849e10d9ba25e68660350d885a56ebc714496d7cc86353d72f9139022239dc016561f6c23e3
@@ -1,8 +1,10 @@
1
1
  module Moduler
2
2
  # Scopes a controller to the organization in the URL
3
3
  # (+/organizations/:organization_id/...+), available as
4
- # +Current.organization+ and the +current_organization+ helper. The
5
- # organization is looked up among the signed-in user's organizations; for
4
+ # +Current.organization+ and the +current_organization+ helper, and to the
5
+ # signed-in user's membership in it, available as +Current.membership+ and
6
+ # the +current_membership+ helper (its role's permissions are checked with
7
+ # +can?+). The organization is looked up among the signed-in user's organizations; for
6
8
  # any other organization (for example after the user was removed from it)
7
9
  # the user is redirected to the home page to choose another one. Keeping the
8
10
  # organization in the URL lets every browser tab work in a different
@@ -17,7 +19,8 @@ module Moduler
17
19
  private
18
20
 
19
21
  def set_current_organization
20
- Current.organization = Current.user.organizations.find_by(id: organization_id_param)
22
+ Current.membership = Current.user.memberships.includes(:organization, :role).find_by(moduler_organization_id: organization_id_param)
23
+ Current.organization = Current.membership&.organization
21
24
 
22
25
  redirect_to moduler.root_path, alert: t("moduler.flash.organization_not_found") unless Current.organization
23
26
  end
@@ -9,7 +9,7 @@ module Moduler
9
9
 
10
10
  rescue_from Pundit::NotAuthorizedError, with: :not_authorized
11
11
 
12
- helper_method :current_user, :current_organization
12
+ helper_method :current_user, :current_organization, :current_membership, :can?
13
13
 
14
14
  private
15
15
 
@@ -21,6 +21,17 @@ module Moduler
21
21
  Current.organization
22
22
  end
23
23
 
24
+ def current_membership
25
+ Current.membership
26
+ end
27
+
28
+ # Whether the signed-in user's role in the current organization grants the
29
+ # permission (one of `permissions` in config/moduler.yml). Always false
30
+ # outside an organization (see OrganizationScoped).
31
+ def can?(permission)
32
+ current_membership&.permission?(permission) || false
33
+ end
34
+
24
35
  def not_authorized
25
36
  redirect_to current_home_path, alert: t("moduler.flash.not_authorized")
26
37
  end
@@ -1,6 +1,6 @@
1
1
  module Moduler
2
2
  class Current < ActiveSupport::CurrentAttributes
3
- attribute :session, :organization
3
+ attribute :session, :organization, :membership
4
4
  delegate :user, to: :session, allow_nil: true
5
5
  end
6
6
  end
@@ -6,6 +6,8 @@ module Moduler
6
6
  belongs_to :organization, class_name: "Moduler::Organization", foreign_key: "moduler_organization_id"
7
7
  belongs_to :role, class_name: "Moduler::Role", foreign_key: "moduler_role_id"
8
8
 
9
+ delegate :permission?, to: :role
10
+
9
11
  validates :user, uniqueness: { scope: :moduler_organization_id }
10
12
  validate :user_must_be_member
11
13
  validate :role_must_belong_to_organization
@@ -3,8 +3,9 @@
3
3
 
4
4
  <nav class="flex gap-1 overflow-x-auto md:flex-col text-sm p-2">
5
5
  <%= render(Moduler::SidebarActionComponent.new(t(".action.dashboard"), moduler.organization_path(current_organization), "moduler/organizations")) %>
6
- <% Array(Moduler.configuration.dig(:resources)).each do |resource| %>
7
- <%= render(Moduler::SidebarActionComponent.new(t(".action.#{resource}"), moduler.url_for([current_organization, resource.to_sym]), "moduler/#{resource}")) %>
6
+ <% Moduler.configuration.resources.each do |resource| %>
7
+ <% next if resource.permission && !can?(resource.permission) %>
8
+ <%= render(Moduler::SidebarActionComponent.new(t(".action.#{resource.name}"), moduler.url_for([current_organization, resource.name.to_sym]), "moduler/#{resource.name}")) %>
8
9
  <% end %>
9
10
  </nav>
10
11
  </aside>
@@ -15,7 +15,7 @@
15
15
 
16
16
  <div role="separator" class="mx-1 w-px shrink-0 self-stretch bg-gray-200 md:mx-0 md:my-2 md:h-px md:w-auto"></div>
17
17
 
18
- <% Array(Moduler.configuration.dig(:administration, :resources)).each do |resource| %>
18
+ <% Moduler.configuration.administration_resources.each do |resource| %>
19
19
  <%= render(Moduler::SidebarActionComponent.new(t(".action.#{resource}"), moduler.url_for([:administration, resource.to_sym]), "moduler/administration/#{resource}")) %>
20
20
  <% end %>
21
21
  </nav>
@@ -2,6 +2,11 @@ module Moduler
2
2
  class Configuration
3
3
  extend Forwardable
4
4
 
5
+ # A sidebar resource from config/moduler.yml: its +name+ (for the route, the
6
+ # translation and the active state) and the +permission+ a member needs to
7
+ # see it, or +nil+ when every member of the organization sees it.
8
+ Resource = Data.define(:name, :permission)
9
+
5
10
  def_delegator :@configuration, :[]
6
11
  def_delegator :@configuration, :dig
7
12
 
@@ -14,5 +19,40 @@ module Moduler
14
19
  def permissions
15
20
  Array(dig(:permissions)).map(&:to_s)
16
21
  end
22
+
23
+ # Resources linked from the application sidebar, defined under `resources`
24
+ # in config/moduler.yml either by name or with the permission needed to see
25
+ # them:
26
+ #
27
+ # resources:
28
+ # - reports
29
+ # - name: tasks
30
+ # permission: tasks.read
31
+ def resources
32
+ Array(dig(:resources)).map { |resource| build_resource(resource) }
33
+ end
34
+
35
+ # Resources linked from the administration sidebar, defined by name under
36
+ # `administration.resources` in config/moduler.yml. Administrators see all of them.
37
+ def administration_resources
38
+ Array(dig(:administration, :resources)).map { |resource| build_resource(resource).name }
39
+ end
40
+
41
+ private
42
+
43
+ def build_resource(resource)
44
+ return Resource.new(name: resource.to_s, permission: nil) unless resource.is_a?(Hash)
45
+
46
+ resource = resource.to_h.symbolize_keys
47
+ name = resource[:name].to_s
48
+ permission = resource[:permission]&.to_s
49
+
50
+ raise ArgumentError, "Resource in config/moduler.yml needs a name: #{resource.inspect}" if name.blank?
51
+ if permission && !permissions.include?(permission)
52
+ raise ArgumentError, "Permission #{permission.inspect} of the #{name} resource is not defined under permissions in config/moduler.yml"
53
+ end
54
+
55
+ Resource.new(name:, permission:)
56
+ end
17
57
  end
18
58
  end
@@ -1,3 +1,3 @@
1
1
  module Moduler
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Kowalewski