lato_spaces 3.1.24 → 3.3.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: 36ee9aa758f48c9b1f32c5921701308a5ee3cc462728c2128e7029ec323e9454
4
- data.tar.gz: e466dfc7f67349bd8fb4c190d27f59a0c5ae5bdc51b0360aa2c0c4d2a6e3c892
3
+ metadata.gz: ad365379004f23a5ede907df73872a7a86c4623d9b55c1fe271819d6f56b4db3
4
+ data.tar.gz: a147c99bcc76a3b01136f36864148f95e8afe546b6e99d1a8784d4dd8ac47e83
5
5
  SHA512:
6
- metadata.gz: a293aa99cb50fbdd593eceaf096a74d1296bda6c222f4ff9411b51fb864ec11d9f65cd0917d082c4dcb2437c9238c51de15fb4f185b97470154e43c860708446
7
- data.tar.gz: 4a16c6c4e6b9cdce284b0791132d5645a36d2227949b34dac41c2c18aea22ed6ce569d5fbb7aca9183d1e683eab0d196fbff8e81753363bd0dd181be1cf85e60
6
+ metadata.gz: 64b75f312a9ce907c132707ddc2cc52c1616ff98a700063e52416c573834319662ef647aeeda6cf1d7d71f6e30d2ac9eb142e1d4658cbd9b6e11c5d6c49e146f
7
+ data.tar.gz: a1b18d19738eec3df017a1e47cc4b40f2608534f1610103ccc4e9b6326cdd558296d706b673b572a03adf418e63dd7d6325e9267c97c41e6fbb15c4087e9900d
data/README.md CHANGED
@@ -1,7 +1,11 @@
1
1
  # Lato Spaces
2
2
  Integrate different workspaces on Lato projects.
3
3
 
4
+ Users select an active group; application data is scoped to it. Group selection requires a current membership. Every request protected by `authenticate_group` rechecks access and clears revoked selections. Users with `lato_spaces_admin` can select and manage all groups.
5
+
4
6
  ## Installation
7
+
8
+ Requires Rails 8.1.3.1 or newer in the 8.1 series and Lato 3.25.0 or newer. Existing applications must install the new Lato session migration before deployment; previous sessions are invalidated.
5
9
  Add required dependencies to your application's Gemfile:
6
10
 
7
11
  ```ruby
@@ -43,25 +47,164 @@ import "lato_spaces/application";
43
47
  // ....
44
48
  ```
45
49
 
46
- ## Todo
50
+ Add a menu entry pointing to `lato_spaces.root_path` (the group selection page), e.g. in `app/views/layouts/lato/_navbar-nav_content.html.erb`:
47
51
 
48
- ### Manage models relations with spaces groups
52
+ ```erb
53
+ <%= lato_navbar_nav_item :lato_spaces, lato_spaces.root_path do %>
54
+ Spaces
55
+ <% end %>
56
+ ```
49
57
 
50
- - [x] Create a new model to manage the relations between spaces and groups
51
- - [x] Create a new concern for models that must be related to spaces groups
52
- - [x] Add hooks on concern to manage the relation on model creation/destroy
53
- - [x] Add method on concern to update the group of a model
54
- - [x] Add scopes on concern to get data filtered by group
58
+ ### Upgrading
55
59
 
56
- ### Other features
60
+ Upgrading from 3.2.0 or earlier: a new migration adds unique indexes on memberships (group + user, group + invitation) and associations (group + item). Run `rails lato_spaces:install:migrations` and `rails db:migrate`: the migration removes duplicate rows before adding the indexes.
57
61
 
58
- - [ ] Show stats about entities on spaces show page
59
- - [ ] Show stats about entities attachments on spaces show page
60
- - [ ] Add roles management on spaces memberships
62
+ ## Configuration
63
+
64
+ ```ruby
65
+ # config/initializers/lato_spaces.rb
66
+ LatoSpaces.configure do |config|
67
+ config.setgroup_redirect_path = :dashboard_path
68
+ end
69
+ ```
70
+
71
+ | Option | Default | Description |
72
+ |--------|---------|-------------|
73
+ | `setgroup_redirect_path` | `nil` | Name of a host application route helper (no arguments), e.g. `:dashboard_path`. Users are redirected there after selecting a group. `nil` redirects back to the Spaces page. |
74
+ | `setgroup_auto_after_login` | `false` | Select a group automatically when the user opens the Spaces page without a selected group: the preferred group first, otherwise the first group by name. Requires `setgroup_redirect_path`. |
75
+ | `setgroup_auto_after_login_if_single` | `false` | Same as above, but only when the user belongs to exactly one group. Requires `setgroup_redirect_path`. |
76
+ | `group_icon` | `"bi bi-people-fill"` | Bootstrap Icons class shown on group cards. |
77
+ | `group_params` | `%i[name]` | Permitted group form params. |
78
+ | `membership_params` | `%i[email]` | Permitted membership form params. |
79
+ | `permit_group_creation` | `false` | Allow non-admin users to create groups. The creator becomes the first member. |
80
+ | `permit_group_management` | `false` | Allow non-admin members to manage their own groups (see [Permissions](#permissions)). |
81
+ | `permit_group_preferred` | `false` | Allow users to mark a preferred group. It is listed first and used by automatic selection. |
82
+
83
+ Automatic selection runs only when the user visits the Spaces root page (`lato_spaces.root_path`), so point Lato `session_root_path` or the post-login flow there.
84
+
85
+ Never add `lato_user_id`, `lato_invitation_id` or `lato_spaces_group_id` to `group_params` / `membership_params`: they would let members bypass invitations or move memberships between groups.
86
+
87
+ ## Permissions
88
+
89
+ | Action | `lato_spaces_admin` | Member, `permit_group_management = true` | Member, default |
90
+ |--------|---------------------|------------------------------------------|-----------------|
91
+ | Select a group | Any group (also from the group page) | Own groups | Own groups |
92
+ | List all groups | Yes | No | No |
93
+ | Create groups | Yes | Only with `permit_group_creation` | Only with `permit_group_creation` |
94
+ | Rename / delete group | Any group | Own groups | No |
95
+ | Add registered users | Any group | Own groups | No |
96
+ | Invite unregistered emails | Any group | Own groups, only when `Lato.config.auth_disable_signup` is `false` | No |
97
+ | Remove members / resend invitations | Any group | Own groups, except the last registered member | No |
98
+
99
+ There are no roles inside a group yet: with `permit_group_management` every member has the same powers, including deleting the group. Enable it only when members are trusted.
100
+
101
+ Membership creation and invitation resend are rate limited (20 requests per 10 minutes per IP).
102
+
103
+ Grant admin permission from the console (with `lato_users` it is also shown as a checkbox):
104
+
105
+ ```ruby
106
+ Lato::User.find_by(email: "admin@example.com").update!(lato_spaces_admin: true)
107
+ ```
108
+
109
+ ## Invitations
110
+
111
+ Adding a member by email:
112
+
113
+ - The email is normalized (stripped, downcased).
114
+ - Registered user: the user is added immediately. Non-admins see members by email; full names are shown to admins only.
115
+ - Existing pending `Lato::Invitation`: it is reused.
116
+ - Otherwise a `Lato::Invitation` is created and the invitation email is sent. When the invitation is accepted the membership is moved to the new user.
117
+ - A user or invitation can belong to a group only once.
118
+ - Destroying the invitation or the user destroys its memberships. Resending is limited by Lato to one email every 2 minutes.
119
+
120
+ Lato invitations bypass `auth_disable_signup`, so when signup is disabled only Spaces admins can invite unregistered emails.
121
+
122
+ ## Usage
123
+
124
+ ### Protecting controllers
125
+
126
+ ```ruby
127
+ class ApplicationController < Lato::ApplicationController
128
+ include LatoSpaces::Groupable
129
+ end
130
+
131
+ class ProjectsController < ApplicationController
132
+ before_action :authenticate_session
133
+ before_action :authenticate_group
61
134
 
62
- ### Tests
135
+ def index
136
+ @projects = Project.for_lato_spaces_group(@session.get(:spaces_group_id))
137
+ end
138
+ end
139
+ ```
140
+
141
+ `LatoSpaces::Groupable` adds protected methods:
142
+
143
+ | Method | Description |
144
+ |--------|-------------|
145
+ | `authenticate_group` | Checks that the selected group (`@session.get(:spaces_group_id)`) is still accessible. Otherwise it clears the selection and redirects to `lato_spaces.root_path` (HTML) or responds `403 Forbidden` (JSON). |
146
+ | `session_group_create(group_id)` | Selects a group the user can access. Returns `false` otherwise. |
147
+ | `session_group_destroy` | Clears the selected group. |
148
+
149
+ Read `:spaces_group_id` only in controllers protected by `authenticate_group`: elsewhere it may point to a group the user no longer belongs to.
150
+
151
+ ### Associating records with groups
152
+
153
+ ```ruby
154
+ class Project < ApplicationRecord
155
+ include LatoSpaces::Associable
156
+ end
157
+ ```
158
+
159
+ Scopes:
160
+
161
+ - `for_lato_spaces_group(group_id)`: records of a group (`nil` returns nothing; with an array of ids a record in many groups is returned once per group, add `.distinct` if needed).
162
+ - `with_lato_spaces_groups`: records with at least one group.
163
+ - `without_lato_spaces_groups`: records without groups.
164
+
165
+ Instance methods (operations return `true`/`false` and add errors to the record on failure):
166
+
167
+ - `lato_spaces_group`, `lato_spaces_group_id`: first group of the record.
168
+ - `add_to_lato_spaces_group(group_id)`
169
+ - `remove_from_lato_spaces_group(group_id)`
170
+ - `switch_lato_spaces_group(group_id)`: moves the first association to another group.
171
+
172
+ Stricter concerns (include them after `LatoSpaces::Associable`):
63
173
 
64
- - [ ] Add tests for model concerns
174
+ - `LatoSpaces::AssociableRequired`: records need an existing `lato_spaces_group_id` on create and are associated to it. **Destroying the association destroys the record**: removing it from its group or deleting the group deletes the record.
175
+ - `LatoSpaces::AssociableUnique`: records can belong to one group only; a second `add_to_lato_spaces_group` returns `false`.
176
+
177
+ Both only affect the models that include them.
178
+
179
+ ```ruby
180
+ class Project < ApplicationRecord
181
+ include LatoSpaces::Associable
182
+ include LatoSpaces::AssociableRequired
183
+ include LatoSpaces::AssociableUnique
184
+ end
185
+
186
+ # Take the group from the session, never from request params.
187
+ Project.create!(name: "Project 1", lato_spaces_group_id: @session.get(:spaces_group_id))
188
+ ```
189
+
190
+ Deleting a group destroys its memberships and associations, and every `AssociableRequired` record of the group.
191
+
192
+ ### Customization
193
+
194
+ `rails lato_spaces:install:application` copies into the host application:
195
+
196
+ - `app/views/layouts/lato_spaces/_group-form-extra_content.html.erb`: extra group form fields.
197
+ - `app/views/layouts/lato_spaces/_membership-form-extra_content.html.erb`: extra membership form fields.
198
+ - `app/views/layouts/lato_spaces/_group-informations_content.html.erb`: group details on the group page.
199
+ - `app/models/concerns/lato_spaces_membership_application.rb`: custom relations and methods for `LatoSpaces::Membership`.
200
+
201
+ Custom form fields must also be added to `group_params` / `membership_params`. Translations are provided for `en` and `it`.
202
+
203
+ ## Roadmap
204
+
205
+ - [ ] Roles inside groups (owner/member) to limit deletion and member management
206
+ - [ ] Show stats about entities on spaces show page
207
+ - [ ] Show stats about entities attachments on spaces show page
65
208
 
66
209
  ## Development
67
210
 
@@ -1,26 +1,39 @@
1
1
  module LatoSpaces::Groupable
2
2
  extend ActiveSupport::Concern
3
3
 
4
+ # Protected: callable from host controllers and before_action, never routable as actions.
5
+ protected
6
+
4
7
  def authenticate_group
5
- if @session.get(:spaces_group_id).blank?
6
- redirect_to lato_spaces.root_path
7
- return false
8
+ group_id = @session.get(:spaces_group_id)
9
+ return true if group_id.present? && accessible_spaces_groups.exists?(id: group_id)
10
+
11
+ session_group_destroy if group_id.present?
12
+ respond_to do |format|
13
+ format.html { redirect_to lato_spaces.root_path }
14
+ format.json { head :forbidden }
8
15
  end
9
16
 
10
- true
17
+ false
11
18
  end
12
19
 
13
20
  def session_group_create(group_id)
14
- cookies.encrypted[:lato_session] = { value: Lato::Session.generate_session_per_user(@session.user_id, spaces_group_id: group_id), expires: Lato.config.session_lifetime.from_now }
15
- @session = Lato::Session.new(cookies.encrypted[:lato_session])
21
+ group = accessible_spaces_groups.find_by(id: group_id)
22
+ return false unless group
16
23
 
17
- true
24
+ session_update(spaces_group_id: group.id)
18
25
  end
19
26
 
20
27
  def session_group_destroy
21
- cookies.encrypted[:lato_session] = { value: Lato::Session.generate_session_per_user(@session.user_id), expires: Lato.config.session_lifetime.from_now }
22
- @session = Lato::Session.new(cookies.encrypted[:lato_session])
28
+ session_update(spaces_group_id: nil)
29
+ end
30
+
31
+ private
32
+
33
+ def accessible_spaces_groups
34
+ return LatoSpaces::Group.none unless @session.really_valid?
35
+ return LatoSpaces::Group.all if @session.user.lato_spaces_admin
23
36
 
24
- true
37
+ LatoSpaces::Group.where(id: LatoSpaces::Membership.where(lato_user_id: @session.user_id).select(:lato_spaces_group_id))
25
38
  end
26
39
  end
@@ -10,33 +10,17 @@ module LatoSpaces
10
10
  @memberships = LatoSpaces::Membership.where(lato_user_id: @session.user_id).order(created_at: :desc)
11
11
  @groups = LatoSpaces::Group.joins(:lato_spaces_memberships).where(lato_spaces_memberships: { lato_user_id: @session.user_id }).order(name: :asc)
12
12
 
13
- if LatoSpaces.config.setgroup_auto_after_login_if_single && LatoSpaces.config.setgroup_redirect_path && @session.get(:spaces_group_id).blank? && @groups.size == 1
14
- group_id = @groups.first&.id
15
- if group_id && session_group_create(group_id)
16
- redirect_to Rails.application.routes.url_helpers.send(LatoSpaces.config.setgroup_redirect_path)
17
- return
18
- end
19
- end
20
-
21
- if LatoSpaces.config.setgroup_auto_after_login && LatoSpaces.config.setgroup_redirect_path && @session.get(:spaces_group_id).blank?
22
- group_id = @groups.first&.id
23
- if group_id && session_group_create(group_id)
24
- redirect_to Rails.application.routes.url_helpers.send(LatoSpaces.config.setgroup_redirect_path)
25
- return
26
- end
27
- end
13
+ auto_select_group if auto_select_group?
28
14
  end
29
15
 
30
16
  def setgroup
31
- success_redirect_path = LatoSpaces.config.setgroup_redirect_path ? Rails.application.routes.url_helpers.send(LatoSpaces.config.setgroup_redirect_path) : lato_spaces.root_path
32
-
33
17
  respond_to do |format|
34
18
  if session_group_create(params[:id])
35
- format.html { redirect_to success_redirect_path }
19
+ format.html { redirect_to setgroup_success_path, notice: I18n.t('lato_spaces.flash.group_selected') }
36
20
  format.json { render json: {} }
37
21
  else
38
- format.html { redirect_to lato_spaces.root_path, alert: 'Error on changing group.' }
39
- format.json { render json: { error: 'Error on changing group.' }, status: :unprocessable_entity }
22
+ format.html { redirect_to lato_spaces.root_path, alert: I18n.t('lato_spaces.flash.group_select_error') }
23
+ format.json { render json: { error: I18n.t('lato_spaces.flash.group_select_error') }, status: :unprocessable_entity }
40
24
  end
41
25
  end
42
26
  end
@@ -49,8 +33,8 @@ module LatoSpaces
49
33
  format.html { redirect_to lato_spaces.root_path }
50
34
  format.json { render json: {} }
51
35
  else
52
- format.html { redirect_to lato_spaces.root_path, alert: 'Error on changing preferred group.' }
53
- format.json { render json: { error: 'Error on changing preferred group.' }, status: :unprocessable_entity }
36
+ format.html { redirect_to lato_spaces.root_path, alert: I18n.t('lato_spaces.flash.preferred_error') }
37
+ format.json { render json: { error: I18n.t('lato_spaces.flash.preferred_error') }, status: :unprocessable_entity }
54
38
  end
55
39
  end
56
40
  end
@@ -60,21 +44,43 @@ module LatoSpaces
60
44
  def authenticate_lato_spaces_admin
61
45
  return true if @session.user.lato_spaces_admin
62
46
 
63
- redirect_to lato.root_path, alert: 'You have not access to this section.'
47
+ redirect_to lato.root_path, alert: I18n.t('lato_spaces.flash.access_denied')
64
48
  end
65
49
 
66
50
  def authenticate_lato_spaces_creation
67
51
  return true if LatoSpaces.config.permit_group_creation
68
52
  return true if @session.user.lato_spaces_admin
69
53
 
70
- redirect_to lato_spaces.root_path, alert: 'You have not access to this section.'
54
+ redirect_to lato_spaces.root_path, alert: I18n.t('lato_spaces.flash.access_denied')
71
55
  end
72
56
 
73
57
  def authenticate_lato_spaces_management
74
58
  return true if LatoSpaces.config.permit_group_management
75
59
  return true if @session.user.lato_spaces_admin
76
60
 
77
- redirect_to lato_spaces.root_path, alert: 'You have not access to this section.'
61
+ redirect_to lato_spaces.root_path, alert: I18n.t('lato_spaces.flash.access_denied')
62
+ end
63
+
64
+ private
65
+
66
+ def auto_select_group?
67
+ return false unless LatoSpaces.config.setgroup_redirect_path
68
+ return false if @session.get(:spaces_group_id).present?
69
+
70
+ LatoSpaces.config.setgroup_auto_after_login ||
71
+ (LatoSpaces.config.setgroup_auto_after_login_if_single && @groups.size == 1)
72
+ end
73
+
74
+ # Preferred group first, then the first group by name.
75
+ def auto_select_group
76
+ group_id = @memberships.find(&:preferred)&.lato_spaces_group_id || @groups.first&.id
77
+ redirect_to setgroup_success_path if group_id && session_group_create(group_id)
78
+ end
79
+
80
+ def setgroup_success_path
81
+ return lato_spaces.root_path unless LatoSpaces.config.setgroup_redirect_path
82
+
83
+ Rails.application.routes.url_helpers.public_send(LatoSpaces.config.setgroup_redirect_path)
78
84
  end
79
85
  end
80
- end
86
+ end
@@ -28,11 +28,11 @@ module LatoSpaces
28
28
  @group = LatoSpaces::Group.new(group_params)
29
29
 
30
30
  respond_to do |format|
31
- if @group.save && @group.lato_spaces_memberships.create(lato_user_id: @session.user_id)
31
+ if create_group_with_membership
32
32
  redirect = lato_spaces.groups_show_path(@group)
33
33
  redirect = lato_spaces.root_path unless @session.user.lato_spaces_admin
34
34
 
35
- format.html { redirect_to redirect, notice: 'Group was successfully created.' }
35
+ format.html { redirect_to redirect, notice: I18n.t('lato_spaces.flash.group_created') }
36
36
  format.json { render json: @group }
37
37
  else
38
38
  format.html { render :create, status: :unprocessable_entity }
@@ -62,10 +62,10 @@ module LatoSpaces
62
62
  def update_action
63
63
  respond_to do |format|
64
64
  if @group.update(group_params)
65
- format.html { redirect_to lato_spaces.groups_show_path(@group), notice: 'Group was successfully updated.' }
65
+ format.html { redirect_to lato_spaces.groups_show_path(@group), notice: I18n.t('lato_spaces.flash.group_updated') }
66
66
  format.json { render json: @group }
67
67
  else
68
- format.html { render :show, status: :unprocessable_entity }
68
+ format.html { render :update, status: :unprocessable_entity }
69
69
  format.json { render json: @group.errors, status: :unprocessable_entity }
70
70
  end
71
71
  end
@@ -80,10 +80,10 @@ module LatoSpaces
80
80
  redirect = lato_spaces.groups_path
81
81
  redirect = lato_spaces.root_path unless @session.user.lato_spaces_admin
82
82
 
83
- format.html { redirect_to redirect, notice: 'Group was successfully destroyed.' }
83
+ format.html { redirect_to redirect, notice: I18n.t('lato_spaces.flash.group_destroyed') }
84
84
  format.json { render json: @group }
85
85
  else
86
- format.html { redirect_to lato_spaces.groups_show_path(@group), notice: 'Group was not destroyed.' }
86
+ format.html { redirect_to lato_spaces.groups_show_path(@group), alert: I18n.t('lato_spaces.flash.group_destroy_error') }
87
87
  format.json { render json: @group.errors, status: :unprocessable_entity }
88
88
  end
89
89
  end
@@ -95,10 +95,21 @@ module LatoSpaces
95
95
  params.require(:group).permit(LatoSpaces.config.group_params)
96
96
  end
97
97
 
98
+ # The creator becomes the first member: group and membership are saved together or not at all.
99
+ def create_group_with_membership
100
+ ActiveRecord::Base.transaction do
101
+ raise ActiveRecord::Rollback unless @group.save
102
+
103
+ @group.lato_spaces_memberships.create!(lato_user_id: @session.user_id)
104
+ true
105
+ end || false
106
+ rescue ActiveRecord::RecordInvalid => e
107
+ @group.errors.add(:base, e.record.errors.full_messages.to_sentence)
108
+ false
109
+ end
110
+
98
111
  def find_group
99
- query = LatoSpaces::Group.all
100
- query = query.joins(:lato_spaces_memberships).where(lato_spaces_memberships: { lato_user_id: @session.user_id }) unless @session.user.lato_spaces_admin
101
- @group = query.find(params[:id])
112
+ @group = accessible_spaces_groups.find(params[:id])
102
113
  end
103
114
  end
104
- end
115
+ end
@@ -1,6 +1,8 @@
1
1
  module LatoSpaces
2
2
  class MembershipsController < ApplicationController
3
3
  before_action :authenticate_lato_spaces_management
4
+ # Each invitation sends an email: throttle per IP to limit abuse.
5
+ before_action -> { limit_requests(20, 10.minutes) }, only: %i[create_action send_invite_action]
4
6
  before_action :find_group, only: %i[create create_action]
5
7
  before_action :find_membership, only: %i[destroy_action send_invite_action]
6
8
 
@@ -10,10 +12,13 @@ module LatoSpaces
10
12
 
11
13
  def create_action
12
14
  @membership = @group.lato_spaces_memberships.new(membership_params)
15
+ # Lato invitations bypass auth_disable_signup: only admins can invite unregistered emails then.
16
+ @membership.invitations_disabled = Lato.config.auth_disable_signup && !@session.user.lato_spaces_admin
13
17
 
14
18
  respond_to do |format|
15
19
  if @membership.save
16
- format.html { redirect_to lato_spaces.groups_show_path(@group), notice: 'Membership was successfully created.' }
20
+ notice = @membership.lato_invitation_id ? I18n.t('lato_spaces.flash.membership_invited') : I18n.t('lato_spaces.flash.membership_created')
21
+ format.html { redirect_to lato_spaces.groups_show_path(@group), notice: notice }
17
22
  format.json { render json: @membership }
18
23
  else
19
24
  format.html { render :create, status: :unprocessable_entity }
@@ -24,14 +29,17 @@ module LatoSpaces
24
29
 
25
30
  def destroy_action
26
31
  respond_to do |format|
27
- if @membership.destroy
32
+ if last_member_removal?
33
+ format.html { redirect_to lato_spaces.groups_show_path(@membership.lato_spaces_group), alert: I18n.t('lato_spaces.flash.membership_last_member') }
34
+ format.json { render json: { error: I18n.t('lato_spaces.flash.membership_last_member') }, status: :unprocessable_entity }
35
+ elsif @membership.destroy
28
36
  redirect = lato_spaces.groups_show_path(@membership.lato_spaces_group)
29
37
  redirect = lato_spaces.root_path if !@session.user.lato_spaces_admin && @membership.lato_user_id == @session.user_id
30
38
 
31
- format.html { redirect_to redirect, notice: 'Membership was successfully destroyed.' }
39
+ format.html { redirect_to redirect, notice: I18n.t('lato_spaces.flash.membership_destroyed') }
32
40
  format.json { head :no_content }
33
41
  else
34
- format.html { redirect_to lato_spaces.groups_show_path(@membership.lato_spaces_group), notice: 'Membership was not destroyed.' }
42
+ format.html { redirect_to lato_spaces.groups_show_path(@membership.lato_spaces_group), alert: I18n.t('lato_spaces.flash.membership_destroy_error') }
35
43
  format.json { render json: @membership.errors, status: :unprocessable_entity }
36
44
  end
37
45
  end
@@ -40,10 +48,10 @@ module LatoSpaces
40
48
  def send_invite_action
41
49
  respond_to do |format|
42
50
  if @membership.send_invite
43
- format.html { redirect_to lato_spaces.groups_show_path(@membership.lato_spaces_group), notice: 'Invite was successfully sent.' }
51
+ format.html { redirect_to lato_spaces.groups_show_path(@membership.lato_spaces_group), notice: I18n.t('lato_spaces.flash.invite_sent') }
44
52
  format.json { render json: @membership }
45
53
  else
46
- format.html { redirect_to lato_spaces.groups_show_path(@membership.lato_spaces_group), notice: 'Invite was not sent.' }
54
+ format.html { redirect_to lato_spaces.groups_show_path(@membership.lato_spaces_group), alert: I18n.t('lato_spaces.flash.invite_error') }
47
55
  format.json { render json: @membership.errors, status: :unprocessable_entity }
48
56
  end
49
57
  end
@@ -56,13 +64,19 @@ module LatoSpaces
56
64
  end
57
65
 
58
66
  def find_group
59
- query = LatoSpaces::Group.all
60
- query = query.joins(:lato_spaces_memberships).where(lato_spaces_memberships: { lato_user_id: @session.user_id }) unless @session.user.lato_spaces_admin
61
- @group = query.find(params[:group_id])
67
+ @group = accessible_spaces_groups.find(params[:group_id])
62
68
  end
63
69
 
64
70
  def find_membership
65
- @membership = LatoSpaces::Membership.find(params[:id])
71
+ @membership = LatoSpaces::Membership.where(lato_spaces_group_id: accessible_spaces_groups.select(:id)).find(params[:id])
72
+ end
73
+
74
+ # Non-admins can not leave a group without registered members: only admins could reach it.
75
+ def last_member_removal?
76
+ return false if @session.user.lato_spaces_admin
77
+ return false if @membership.lato_user_id.blank?
78
+
79
+ !LatoSpaces::Membership.where(lato_spaces_group_id: @membership.lato_spaces_group_id).where.not(id: @membership.id).where.not(lato_user_id: nil).exists?
66
80
  end
67
81
  end
68
- end
82
+ end
@@ -1,4 +1,10 @@
1
1
  module LatoSpaces
2
2
  module ApplicationHelper
3
+ # The groups index is admin only: non-admins get breadcrumbs without it.
4
+ def lato_spaces_breadcrumbs(*items)
5
+ breadcrumbs = [{ label: I18n.t('lato_spaces.spaces'), path: lato_spaces.root_path }]
6
+ breadcrumbs << { label: I18n.t('lato_spaces.groups'), path: lato_spaces.groups_path } if @session.user.lato_spaces_admin
7
+ breadcrumbs + items
8
+ end
3
9
  end
4
10
  end
@@ -4,7 +4,7 @@ module LatoSpaces
4
4
 
5
5
  def lato_spaces_group_actions(group)
6
6
  content_tag(:div, class: 'btn-group btn-group-sm') do
7
- concat link_to(I18n.t('lato_spaces.cta_view'), lato_spaces.groups_show_path(group), class: 'btn btn-primary')
7
+ concat link_to(I18n.t('lato_spaces.cta_view'), lato_spaces.groups_show_path(group), class: 'btn btn-primary', aria: { label: I18n.t('lato_spaces.aria.view_group', name: group.name) })
8
8
  end
9
9
  end
10
10
  end
@@ -3,24 +3,38 @@ module LatoSpaces
3
3
  def lato_spaces_membership_user_infos(membership)
4
4
  infos = ''
5
5
  if membership.lato_invitation
6
- infos = content_tag(:small, class: 'text-muted d-inline-block ms-1') do
7
- "(invited #{time_ago_in_words(membership.lato_invitation.created_at)} ago)"
6
+ infos = content_tag(:span, class: 'badge text-bg-secondary ms-2') do
7
+ I18n.t('lato_spaces.invitation_pending', time: time_ago_in_words(membership.lato_invitation.created_at))
8
8
  end
9
9
  end
10
10
 
11
11
  content_tag(:div, class: 'd-flex align-items-center') do
12
- concat lato_data_user(membership.user_infos_label)
12
+ concat lato_data_user(lato_spaces_membership_label(membership))
13
13
  concat infos
14
14
  end
15
15
  end
16
16
 
17
17
  def lato_spaces_membership_actions(membership)
18
+ label = lato_spaces_membership_label(membership)
19
+ destroy_confirm = if membership.lato_user_id.present? && membership.lato_user_id == @session.user_id
20
+ I18n.t('lato_spaces.confirm.membership_leave')
21
+ else
22
+ I18n.t('lato_spaces.confirm.membership_destroy', name: label)
23
+ end
24
+
18
25
  content_tag(:div, class: 'btn-group btn-group-sm') do
19
26
  if membership.lato_invitation
20
- concat link_to(I18n.t('lato_spaces.cta_resend'), lato_spaces.memberships_send_invite_action_path(membership), class: 'btn btn-primary', data: { turbo_confirm: 'Are you sure?', turbo_method: :post })
27
+ concat link_to(I18n.t('lato_spaces.cta_resend'), lato_spaces.memberships_send_invite_action_path(membership), class: 'btn btn-primary', aria: { label: I18n.t('lato_spaces.aria.resend_invitation', name: label) }, data: { turbo_confirm: I18n.t('lato_spaces.confirm.invite_resend', name: label), turbo_method: :post })
21
28
  end
22
- concat link_to(I18n.t('lato_spaces.cta_delete'), lato_spaces.memberships_destroy_action_path(membership), class: 'btn btn-danger', data: { turbo_confirm: 'Are you sure?', turbo_method: :delete })
29
+ concat link_to(I18n.t('lato_spaces.cta_delete'), lato_spaces.memberships_destroy_action_path(membership), class: 'btn btn-danger', aria: { label: I18n.t('lato_spaces.aria.remove_member', name: label) }, data: { turbo_confirm: destroy_confirm, turbo_method: :delete })
23
30
  end
24
31
  end
32
+
33
+ private
34
+
35
+ # Full names of registered users are shown to admins only, so adding an email does not reveal who owns it.
36
+ def lato_spaces_membership_label(membership)
37
+ membership.user_infos_label(show_name: @session.user.lato_spaces_admin)
38
+ end
25
39
  end
26
40
  end
@@ -5,7 +5,7 @@ module LatoSpaces::Associable
5
5
  # Relations
6
6
  ##
7
7
 
8
- has_many :lato_spaces_associations, class_name: 'LatoSpaces::Association', as: :item, dependent: :delete_all # HACK: delete_all instead of destroy to avoid infinite loop caused by AssociableRequired concern
8
+ has_many :lato_spaces_associations, class_name: 'LatoSpaces::Association', as: :item, dependent: :delete_all # delete_all skips association callbacks, so destroying an AssociableRequired item does not loop
9
9
  has_many :lato_spaces_groups, through: :lato_spaces_associations, class_name: 'LatoSpaces::Group'
10
10
 
11
11
  # Scopes
@@ -32,18 +32,18 @@ module LatoSpaces::Associable
32
32
 
33
33
  def add_to_lato_spaces_group(group_id)
34
34
  association = lato_spaces_associations.create(lato_spaces_group_id: group_id)
35
- unless association.valid?
36
- errors.add(:base, association.errors.full_messages.join(', '))
37
- return false
38
- end
35
+ reset_lato_spaces_group_cache
36
+ return true if association.persisted?
39
37
 
40
- true
38
+ errors.add(:base, association.errors.full_messages.join(', '))
39
+ false
41
40
  end
42
41
 
43
42
  def remove_from_lato_spaces_group(group_id)
44
43
  association = lato_spaces_associations.find_by(lato_spaces_group_id: group_id)
45
44
  return true unless association
46
45
 
46
+ reset_lato_spaces_group_cache
47
47
  unless association.destroy
48
48
  errors.add(:base, association.errors.full_messages.join(', '))
49
49
  return false
@@ -56,6 +56,7 @@ module LatoSpaces::Associable
56
56
  association = lato_spaces_associations.first
57
57
  return add_to_lato_spaces_group(group_id) unless association
58
58
 
59
+ reset_lato_spaces_group_cache
59
60
  unless association.update(lato_spaces_group_id: group_id)
60
61
  errors.add(:base, association.errors.full_messages.join(', '))
61
62
  return false
@@ -63,4 +64,12 @@ module LatoSpaces::Associable
63
64
 
64
65
  true
65
66
  end
67
+
68
+ private
69
+
70
+ def reset_lato_spaces_group_cache
71
+ @lato_spaces_group = nil
72
+ @lato_spaces_group_id = nil
73
+ lato_spaces_groups.reset
74
+ end
66
75
  end