lato_spaces 3.2.0 → 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: 3444a3028e399641c312a023bf6ac4ac02815747ed0bb6185a07120800a6d4ee
4
- data.tar.gz: e29d329206491723e51c31ce8a97a73b9c7d5410fa2653bbf12a576f605f6327
3
+ metadata.gz: ad365379004f23a5ede907df73872a7a86c4623d9b55c1fe271819d6f56b4db3
4
+ data.tar.gz: a147c99bcc76a3b01136f36864148f95e8afe546b6e99d1a8784d4dd8ac47e83
5
5
  SHA512:
6
- metadata.gz: 9f51a50f75322d535d2bae852ad86554069ca4c2f292cc6a750b69eb93082bfd07c6af2a9f4c72466372827338c79ab3803cdd119961ff35763c1e96291b2ad8
7
- data.tar.gz: def89bea0b5460d7109c9e07fdc362fc220ae2b2b54e1ca43bfa56c81b4c6e31e7428754995913453b3bdbaf2b7981ae2594faf09bfbd955f98b2f35ee3eaea2
6
+ metadata.gz: 64b75f312a9ce907c132707ddc2cc52c1616ff98a700063e52416c573834319662ef647aeeda6cf1d7d71f6e30d2ac9eb142e1d4658cbd9b6e11c5d6c49e146f
7
+ data.tar.gz: a1b18d19738eec3df017a1e47cc4b40f2608534f1610103ccc4e9b6326cdd558296d706b673b572a03adf418e63dd7d6325e9267c97c41e6fbb15c4087e9900d
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Lato Spaces
2
2
  Integrate different workspaces on Lato projects.
3
3
 
4
- 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. When non-admin management is enabled, membership deletion and invitation resends are restricted to their own groups.
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
5
 
6
6
  ## Installation
7
7
 
@@ -47,25 +47,164 @@ import "lato_spaces/application";
47
47
  // ....
48
48
  ```
49
49
 
50
- ## 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`:
51
51
 
52
- ### 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
+ ```
53
57
 
54
- - [x] Create a new model to manage the relations between spaces and groups
55
- - [x] Create a new concern for models that must be related to spaces groups
56
- - [x] Add hooks on concern to manage the relation on model creation/destroy
57
- - [x] Add method on concern to update the group of a model
58
- - [x] Add scopes on concern to get data filtered by group
58
+ ### Upgrading
59
59
 
60
- ### 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.
61
61
 
62
- - [ ] Show stats about entities on spaces show page
63
- - [ ] Show stats about entities attachments on spaces show page
64
- - [ ] 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
134
+
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`):
65
173
 
66
- ### Tests
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`.
67
176
 
68
- - [ ] Add tests for model concerns
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
69
208
 
70
209
  ## Development
71
210
 
@@ -1,15 +1,20 @@
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
8
  group_id = @session.get(:spaces_group_id)
6
- unless group_id.present? && accessible_spaces_groups.exists?(id: group_id)
7
- session_group_destroy if group_id.present?
8
- redirect_to lato_spaces.root_path
9
- return false
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 }
10
15
  end
11
16
 
12
- true
17
+ false
13
18
  end
14
19
 
15
20
  def session_group_create(group_id)
@@ -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,6 +95,19 @@ 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
112
  @group = accessible_spaces_groups.find(params[:id])
100
113
  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
@@ -62,5 +70,13 @@ module LatoSpaces
62
70
  def find_membership
63
71
  @membership = LatoSpaces::Membership.where(lato_spaces_group_id: accessible_spaces_groups.select(:id)).find(params[:id])
64
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?
80
+ end
65
81
  end
66
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
@@ -1,6 +1,9 @@
1
1
  module LatoSpaces::AssociableRequired
2
2
  extend ActiveSupport::Concern
3
3
 
4
+ # NOTE: LatoSpaces::Association destroys items including this module when
5
+ # their association is destroyed (e.g. on group destruction).
6
+
4
7
  attr_writer :lato_spaces_group_id
5
8
 
6
9
  included do
@@ -8,26 +11,18 @@ module LatoSpaces::AssociableRequired
8
11
  ##
9
12
 
10
13
  validates :lato_spaces_group_id, presence: true, on: :create
11
-
12
- # Hooks
13
- ##
14
-
15
- after_create_commit do
16
- unless lato_spaces_associations.create(lato_spaces_group_id: lato_spaces_group_id)
17
- Rails.logger.error "Failed to create LatoSpaces association for #{self.class.name} with ID #{id} and group ID #{lato_spaces_group_id}"
14
+ validate on: :create do
15
+ if lato_spaces_group_id.present? && !LatoSpaces::Group.exists?(id: lato_spaces_group_id)
16
+ errors.add(:lato_spaces_group_id, :invalid)
18
17
  end
19
18
  end
20
19
 
21
- # External hooks
20
+ # Hooks
22
21
  ##
23
22
 
24
- # listen LatoSpaces::Association destroy event to destroy item
25
- LatoSpaces::Association.after_destroy_commit do |association|
26
- if association.item
27
- unless association.item.destroy
28
- Rails.logger.error "Failed to destroy #{association.item.class.name} with ID #{association.item_id} after LatoSpaces association destruction"
29
- end
30
- end
23
+ # Runs inside the create transaction: a failure rolls back the item.
24
+ after_create do
25
+ lato_spaces_associations.create!(lato_spaces_group_id: lato_spaces_group_id)
31
26
  end
32
27
  end
33
28
  end
@@ -1,16 +1,6 @@
1
1
  module LatoSpaces::AssociableUnique
2
2
  extend ActiveSupport::Concern
3
3
 
4
- included do
5
- # External hooks
6
- ##
7
-
8
- # listen LatoSpaces::Association create event to avoid duplicate associations
9
- LatoSpaces::Association.before_create do |association|
10
- if association.item&.lato_spaces_associations&.length&.positive?
11
- throw(:abort)
12
- end
13
- end
14
- end
4
+ # Marker concern: LatoSpaces::Association rejects a second association
5
+ # for items including this module.
15
6
  end
16
-
@@ -10,5 +10,27 @@ module LatoSpaces
10
10
  ##
11
11
 
12
12
  validates :lato_spaces_group_id, uniqueness: { scope: [:item_id, :item_type] }
13
+
14
+ # Items using LatoSpaces::AssociableUnique can belong to a single group.
15
+ validate on: :create do
16
+ next unless item.is_a?(LatoSpaces::AssociableUnique)
17
+ next unless LatoSpaces::Association.where(item_type: item_type, item_id: item_id).exists?
18
+
19
+ errors.add(:item, :already_associated)
20
+ end
21
+
22
+ # Hooks
23
+ ##
24
+
25
+ # Items using LatoSpaces::AssociableRequired can not live without a group:
26
+ # destroying their association destroys them. The check is done per item,
27
+ # so models using only LatoSpaces::Associable are never affected.
28
+ after_destroy_commit do
29
+ next unless item.is_a?(LatoSpaces::AssociableRequired)
30
+
31
+ unless item.destroy
32
+ Rails.logger.error "Failed to destroy #{item_type} with ID #{item_id} after LatoSpaces association destruction"
33
+ end
34
+ end
13
35
  end
14
36
  end
@@ -3,6 +3,7 @@ module LatoSpaces
3
3
  include LatoSpacesMembershipApplication
4
4
 
5
5
  attr_accessor :email
6
+ attr_accessor :invitations_disabled # when true, unregistered emails are rejected instead of invited
6
7
  attr_accessor :user_infos, :actions # lato index
7
8
 
8
9
  # Relations
@@ -16,34 +17,58 @@ module LatoSpaces
16
17
  ##
17
18
 
18
19
  scope :lato_index_order, ->(column, order) do
19
- return left_joins(:lato_user, :lato_invitation).order("lato_users.last_name #{order}, lato_users.first_name #{order}, lato_users.email #{order}, lato_invitations.email #{order}") if column == :user_infos
20
+ direction = order.to_s.casecmp?('desc') ? 'DESC' : 'ASC'
21
+ return left_joins(:lato_user, :lato_invitation).order(Arel.sql("lato_users.last_name #{direction}, lato_users.first_name #{direction}, lato_users.email #{direction}, lato_invitations.email #{direction}")) if column == :user_infos
20
22
 
21
- order("#{column} #{order}")
23
+ order(column => direction)
22
24
  end
23
25
 
24
26
  scope :lato_index_search, ->(search) do
25
27
  left_joins(:lato_user, :lato_invitation).where("lower(lato_users.first_name) LIKE :search OR lower(lato_users.last_name) LIKE :search OR lower(lato_users.email) LIKE :search OR lower(lato_invitations.email) LIKE :search", search: "%#{search.downcase.strip}%")
26
28
  end
27
29
 
30
+ # Validations
31
+ ##
32
+
33
+ validate on: :create do
34
+ next if lato_user_id.present? || lato_invitation_id.present?
35
+
36
+ if email.blank?
37
+ errors.add(:email, :blank)
38
+ elsif invitations_disabled
39
+ errors.add(:email, :invitation_not_permitted)
40
+ end
41
+ end
42
+
43
+ validate on: :create do
44
+ group_memberships = LatoSpaces::Membership.where(lato_spaces_group_id: lato_spaces_group_id)
45
+ taken = (lato_user_id.present? && group_memberships.exists?(lato_user_id: lato_user_id)) ||
46
+ (lato_invitation_id.present? && group_memberships.exists?(lato_invitation_id: lato_invitation_id))
47
+ errors.add(:email, :already_member) if taken
48
+ end
49
+
28
50
  # Hooks
29
51
  ##
30
52
 
31
- # find user or create invitation when create membership
53
+ # resolve email to an existing user or invitation
54
+ before_validation on: :create do
55
+ self.email = email.to_s.strip.downcase.presence
56
+ next if lato_user_id.present? || lato_invitation_id.present? || email.blank?
57
+
58
+ self.lato_user = Lato::User.find_by(email: email)
59
+ self.lato_invitation = Lato::Invitation.find_by(email: email) unless lato_user
60
+ end
61
+
62
+ # create (and send) the invitation only once validations passed
32
63
  before_create do
33
- if new_record? && email.present? && lato_user_id.blank? && lato_invitation_id.blank?
34
- user = Lato::User.find_by_email(email)
35
- if user
36
- self.lato_user_id = user.id
37
- else
38
- invitation = Lato::Invitation.find_by_email(email)
39
- invitation ||= Lato::Invitation.create(email: email)
40
- unless invitation
41
- errors.add(:email, 'There was an error with the invitation.')
42
- throw(:abort)
43
- end
44
- self.lato_invitation_id = invitation.id
45
- end
64
+ next if lato_user_id.present? || lato_invitation_id.present?
65
+
66
+ invitation = Lato::Invitation.create(email: email)
67
+ unless invitation.persisted?
68
+ errors.add(:base, invitation.errors.full_messages.to_sentence.presence || :invitation_error)
69
+ throw(:abort)
46
70
  end
71
+ self.lato_invitation = invitation
47
72
  end
48
73
 
49
74
  # External hooks
@@ -51,8 +76,12 @@ module LatoSpaces
51
76
 
52
77
  # listen Lato::Invitation save event to update memberships
53
78
  Lato::Invitation.after_save do |invitation|
54
- if invitation.lato_user_id.present?
55
- LatoSpaces::Membership.where(lato_invitation_id: invitation.id).each do |space_member|
79
+ next if invitation.lato_user_id.blank?
80
+
81
+ LatoSpaces::Membership.where(lato_invitation_id: invitation.id).each do |space_member|
82
+ if LatoSpaces::Membership.exists?(lato_spaces_group_id: space_member.lato_spaces_group_id, lato_user_id: invitation.lato_user_id)
83
+ space_member.destroy!
84
+ else
56
85
  space_member.update!(lato_user_id: invitation.lato_user_id, lato_invitation_id: nil)
57
86
  end
58
87
  end
@@ -71,10 +100,12 @@ module LatoSpaces
71
100
  # Helpers
72
101
  ##
73
102
 
74
- def user_infos_label
75
- return lato_user.full_name if lato_user.present?
103
+ # show_name: false hides the registered user's full name (only the email is shown)
104
+ def user_infos_label(show_name: true)
105
+ return (show_name ? lato_user.full_name : lato_user.email) if lato_user.present?
76
106
  return lato_invitation.email if lato_invitation.present?
77
- 'Unknown'
107
+
108
+ I18n.t('lato_spaces.unknown_member')
78
109
  end
79
110
 
80
111
  # Operations
@@ -91,7 +122,7 @@ module LatoSpaces
91
122
 
92
123
  def send_invite
93
124
  if lato_invitation.blank? || lato_user.present?
94
- errors.add(:base, 'This user is already a member of this space.')
125
+ errors.add(:base, :already_joined)
95
126
  return false
96
127
  end
97
128
 
@@ -1,5 +1,5 @@
1
1
  <%= lato_page_head I18n.t('lato_spaces.spaces') do %>
2
- <p class="lead"><%= raw I18n.t('lato_spaces.welcome', user: @session.user.first_name) %></p>
2
+ <p class="lead"><%= t('lato_spaces.welcome_html', user: @session.user.first_name) %></p>
3
3
  <% end %>
4
4
 
5
5
  <%= turbo_frame_tag 'index' do %>
@@ -18,34 +18,44 @@
18
18
  end
19
19
 
20
20
  groups = groups.sort_by { |g| [g[:is_preferred] ? 0 : 1, g[:name].downcase] }
21
+ can_create = @session.user.lato_spaces_admin || LatoSpaces.config.permit_group_creation
21
22
  %>
22
23
 
24
+ <% if groups.empty? && !can_create %>
25
+ <div class="alert alert-info" role="status"><%= I18n.t('lato_spaces.no_groups') %></div>
26
+ <% end %>
27
+
23
28
  <div class="row">
24
29
  <% groups.each do |group| %>
25
- <div class="col col-12 col-lg-6 col-lg-4 col-xl-3 mb-3">
26
- <div class="card h-100 border border-4 position-relative <%= group[:is_selected] ? 'border-success' : 'border-light' %>">
27
- <%= link_to lato_spaces.setgroup_path(group[:id]), class: "position-absolute top-0 left-0 w-100 h-100", data: { turbo_method: :post } do %><% end %>
30
+ <div class="col-12 col-md-6 col-lg-4 col-xl-3 mb-3">
31
+ <div class="card h-100 border border-4 <%= group[:is_selected] ? 'border-success' : 'border-light' %>" <%= 'aria-current="true"'.html_safe if group[:is_selected] %>>
28
32
  <div class="card-body">
29
33
  <div class="d-flex align-items-center justify-content-between d-lg-block">
30
34
  <div class="d-flex align-items-center d-lg-block text-center">
31
- <i class="<%= LatoSpaces.config.group_icon %> fs-1 me-2 me-lg-0"></i><br>
35
+ <i class="<%= LatoSpaces.config.group_icon %> fs-1 me-2 me-lg-0" aria-hidden="true"></i><br>
32
36
  <span class="fw-bold"><%= group[:name] %></span>
37
+ <% if group[:is_selected] %>
38
+ <span class="badge text-bg-success ms-1"><%= I18n.t('lato_spaces.selected') %></span>
39
+ <% end %>
40
+ <% if group[:is_preferred] && LatoSpaces.config.permit_group_preferred %>
41
+ <span class="badge text-bg-warning ms-1"><%= I18n.t('lato_spaces.preferred') %></span>
42
+ <% end %>
33
43
  </div>
34
- <div class="d-flex justify-content-center align-items-center mt-lg-2 position-relative">
44
+ <div class="d-flex justify-content-center align-items-center mt-lg-2">
35
45
  <% if @session.user.lato_spaces_admin || LatoSpaces.config.permit_group_management %>
36
- <%= link_to lato_spaces.groups_show_path(group[:id]), class: "btn btn-light me-1", data: { turbo_frame: '_top' } do %>
37
- <i class="bi bi-gear"></i>
46
+ <%= link_to lato_spaces.groups_show_path(group[:id]), class: "btn btn-light me-1", title: I18n.t('lato_spaces.aria.manage_group', name: group[:name]), aria: { label: I18n.t('lato_spaces.aria.manage_group', name: group[:name]) }, data: { turbo_frame: '_top' } do %>
47
+ <i class="bi bi-gear" aria-hidden="true"></i>
38
48
  <% end %>
39
49
  <% end %>
40
50
 
41
51
  <% if LatoSpaces.config.permit_group_preferred %>
42
- <%= link_to lato_spaces.setpreferred_path(group[:id]), class: "btn btn-#{group[:is_preferred] ? 'warning' : 'light'} ms-1 me-1", data: { turbo_method: :post } do %>
43
- <i class="bi bi-star<%= group[:is_preferred] ? '-fill' : '' %>"></i>
52
+ <%= button_to lato_spaces.setpreferred_path(group[:id]), class: "btn btn-#{group[:is_preferred] ? 'warning' : 'light'} ms-1 me-1", title: I18n.t('lato_spaces.aria.prefer_group', name: group[:name]), aria: { label: I18n.t('lato_spaces.aria.prefer_group', name: group[:name]), pressed: group[:is_preferred] ? 'true' : 'false' } do %>
53
+ <i class="bi bi-star<%= group[:is_preferred] ? '-fill' : '' %>" aria-hidden="true"></i>
44
54
  <% end %>
45
55
  <% end %>
46
56
 
47
- <%= link_to lato_spaces.setgroup_path(group[:id]), class: "btn btn-#{group[:is_selected] ? 'success' : 'primary'} ms-1", data: { turbo_method: :post } do %>
48
- <i class="bi bi-check-circle<%= group[:is_selected] ? '-fill' : '' %>"></i>
57
+ <%= button_to lato_spaces.setgroup_path(group[:id]), class: "btn btn-#{group[:is_selected] ? 'success' : 'primary'} ms-1", form: { data: { turbo_frame: '_top' } }, title: I18n.t('lato_spaces.aria.select_group', name: group[:name]), aria: { label: I18n.t('lato_spaces.aria.select_group', name: group[:name]) } do %>
58
+ <i class="bi bi-check-circle<%= group[:is_selected] ? '-fill' : '' %>" aria-hidden="true"></i>
49
59
  <% end %>
50
60
  </div>
51
61
  </div>
@@ -55,22 +65,22 @@
55
65
  <% end %>
56
66
 
57
67
  <% if @session.user.lato_spaces_admin %>
58
- <div class="col col-12 col-lg-6 col-lg-4 col-xl-3 mb-3">
59
- <%= link_to lato_spaces.groups_path, class: 'card border border-4 border-primary bg-primary text-white text-decoration-none h-100' do %>
68
+ <div class="col-12 col-md-6 col-lg-4 col-xl-3 mb-3">
69
+ <%= link_to lato_spaces.groups_path, class: 'card border border-4 border-primary bg-primary text-white text-decoration-none h-100', data: { turbo_frame: '_top' } do %>
60
70
  <div class="card-body d-flex flex-column justify-content-center align-items-center">
61
71
  <div class="text-center">
62
- <i class="bi bi-sliders fs-1"></i><br>
72
+ <i class="bi bi-sliders fs-1" aria-hidden="true"></i><br>
63
73
  <span class="fw-bold"><%= I18n.t('lato_spaces.cta_manage_groups') %></span>
64
74
  </div>
65
75
  </div>
66
76
  <% end %>
67
77
  </div>
68
78
  <% elsif LatoSpaces.config.permit_group_creation %>
69
- <div class="col col-12 col-lg-6 col-lg-4 col-xl-3 mb-3">
79
+ <div class="col-12 col-md-6 col-lg-4 col-xl-3 mb-3">
70
80
  <%= link_to lato_spaces.groups_create_path, class: 'card border border-4 border-primary bg-primary text-white text-decoration-none h-100', data: { lato_action_target: 'trigger', turbo_frame: dom_id(LatoSpaces::Group.new, 'form'), action_title: I18n.t('lato_spaces.new_group') } do %>
71
81
  <div class="card-body d-flex flex-column justify-content-center align-items-center">
72
82
  <div class="text-center">
73
- <i class="bi bi-plus-circle fs-1"></i><br>
83
+ <i class="bi bi-plus-circle fs-1" aria-hidden="true"></i><br>
74
84
  <span class="fw-bold"><%= I18n.t('lato_spaces.cta_new_group') %></span>
75
85
  </div>
76
86
  </div>
@@ -78,4 +88,4 @@
78
88
  </div>
79
89
  <% end %>
80
90
  </div>
81
- <% end %>
91
+ <% end %>
@@ -1,10 +1,7 @@
1
- <%= lato_page_head I18n.t('lato_spaces.new_group'), [
2
- { label: I18n.t('lato_spaces.spaces'), path: lato_spaces.root_path },
3
- { label: I18n.t('lato_spaces.groups'), path: lato_spaces.groups_path }
4
- ] %>
1
+ <%= lato_page_head I18n.t('lato_spaces.new_group'), lato_spaces_breadcrumbs %>
5
2
 
6
3
  <div class="card">
7
4
  <div class="card-body">
8
5
  <%= render "form", group: @group %>
9
6
  </div>
10
- </div>
7
+ </div>
@@ -1,14 +1,15 @@
1
- <%= lato_page_head @group.name, [
2
- { label: I18n.t('lato_spaces.spaces'), path: lato_spaces.root_path },
3
- { label: I18n.t('lato_spaces.groups'), path: lato_spaces.groups_path }
4
- ] %>
1
+ <%# h: lato_page_head renders the title as raw HTML %>
2
+ <%= lato_page_head h(@group.name), lato_spaces_breadcrumbs %>
5
3
 
6
4
  <div class="card mb-4">
7
5
  <div class="card-header d-flex justify-content-between align-items-center">
8
6
  <h2 class="fs-4 mb-0"><%= I18n.t('lato_spaces.group_informations') %></h2>
9
- <div class="btn-group btn-group-sm" role="group">
10
- <%= link_to I18n.t('lato_spaces.cta_edit'), lato_spaces.groups_update_path(@group), class: 'btn btn-primary', data: { lato_action_target: 'trigger', turbo_frame: dom_id(@group, 'form'), action_title: I18n.t('lato_spaces.edit_group') } %>
11
- <%= link_to I18n.t('lato_spaces.cta_delete'), lato_spaces.groups_destroy_action_path(@group), class: 'btn btn-danger', data: { turbo_confirm: 'Are you sure?', turbo_method: :delete } %>
7
+ <div class="d-flex gap-1">
8
+ <%= button_to I18n.t('lato_spaces.cta_select'), lato_spaces.setgroup_path(@group), class: 'btn btn-sm btn-success', form: { data: { turbo_frame: '_top' } }, aria: { label: I18n.t('lato_spaces.aria.select_group', name: @group.name) } %>
9
+ <div class="btn-group btn-group-sm" role="group">
10
+ <%= link_to I18n.t('lato_spaces.cta_edit'), lato_spaces.groups_update_path(@group), class: 'btn btn-primary', data: { lato_action_target: 'trigger', turbo_frame: dom_id(@group, 'form'), action_title: I18n.t('lato_spaces.edit_group') } %>
11
+ <%= link_to I18n.t('lato_spaces.cta_delete'), lato_spaces.groups_destroy_action_path(@group), class: 'btn btn-danger', data: { turbo_confirm: I18n.t('lato_spaces.confirm.group_destroy', name: @group.name, members: @group.lato_spaces_memberships.count, items: @group.lato_spaces_associations.count), turbo_method: :delete } %>
12
+ </div>
12
13
  </div>
13
14
  </div>
14
15
  <div class="card-body">
@@ -31,4 +32,4 @@
31
32
  }
32
33
  %>
33
34
  </div>
34
- </div>
35
+ </div>
@@ -1,11 +1,9 @@
1
- <%= lato_page_head I18n.t('lato_spaces.edit_group'), [
2
- { label: I18n.t('lato_spaces.spaces'), path: lato_spaces.root_path },
3
- { label: I18n.t('lato_spaces.groups'), path: lato_spaces.groups_path },
4
- { label: @group.name, path: lato_spaces.groups_show_path(@group) }
5
- ] %>
1
+ <%= lato_page_head I18n.t('lato_spaces.edit_group'), lato_spaces_breadcrumbs(
2
+ { label: @group.name_was, path: lato_spaces.groups_show_path(@group) }
3
+ ) %>
6
4
 
7
5
  <div class="card">
8
6
  <div class="card-body">
9
7
  <%= render "form", group: @group %>
10
8
  </div>
11
- </div>
9
+ </div>
@@ -5,7 +5,10 @@
5
5
 
6
6
  <div class="mb-3">
7
7
  <%= lato_form_item_label form, :email %>
8
- <%= lato_form_item_input_email form, :email, required: true %>
8
+ <%= lato_form_item_input_email form, :email, required: true, aria: { describedby: 'membership-email-help' } %>
9
+ <div id="membership-email-help" class="form-text">
10
+ <%= Lato.config.auth_disable_signup && !@session.user.lato_spaces_admin ? I18n.t('lato_spaces.membership_email_help_registered_only') : I18n.t('lato_spaces.membership_email_help') %>
11
+ </div>
9
12
  </div>
10
13
 
11
14
  <%= render 'layouts/lato_spaces/membership-form-extra_content', form: form %>
@@ -1,11 +1,9 @@
1
- <%= lato_page_head I18n.t('lato_spaces.new_membership'), [
2
- { label: I18n.t('lato_spaces.spaces'), path: lato_spaces.root_path },
3
- { label: I18n.t('lato_spaces.groups'), path: lato_spaces.groups_path },
1
+ <%= lato_page_head I18n.t('lato_spaces.new_membership'), lato_spaces_breadcrumbs(
4
2
  { label: @group.name, path: lato_spaces.groups_show_path(@group) }
5
- ] %>
3
+ ) %>
6
4
 
7
5
  <div class="card">
8
6
  <div class="card-body">
9
7
  <%= render "form", membership: @membership %>
10
8
  </div>
11
- </div>
9
+ </div>
@@ -7,12 +7,12 @@ group ||= LatoSpaces::Group.new
7
7
  <table class="table">
8
8
  <tbody>
9
9
  <tr>
10
- <th>Total members</th>
10
+ <th scope="row"><%= I18n.t('lato_spaces.total_members') %></th>
11
11
  <td><%= group.lato_spaces_memberships.count %></td>
12
12
  </tr>
13
13
  <tr>
14
- <th>Total items</th>
14
+ <th scope="row"><%= I18n.t('lato_spaces.total_items') %></th>
15
15
  <td><%= group.lato_spaces_associations.count %></td>
16
16
  </tr>
17
17
  </tbody>
18
- </table>
18
+ </table>
@@ -4,7 +4,7 @@ en:
4
4
  spaces: Spaces
5
5
  group: Group
6
6
  groups: Groups
7
- welcome: Hi <b>%{user}</b>! Please <b>select the group</b> you want to work on.
7
+ welcome_html: Hi <b>%{user}</b>! Please <b>select the group</b> you want to work on.
8
8
  cta_manage_groups: Manage groups
9
9
  new_group: New group
10
10
  edit_group: Edit group
@@ -17,8 +17,60 @@ en:
17
17
  cta_edit: Edit
18
18
  cta_delete: Delete
19
19
  cta_resend: Resend
20
+ cta_select: Select group
20
21
  new_membership: New membership
22
+ no_groups: You are not a member of any group yet. Ask an administrator to add you to a group.
23
+ selected: Selected
24
+ preferred: Preferred
25
+ unknown_member: Unknown
26
+ invitation_pending: Invitation pending (sent %{time} ago)
27
+ total_members: Total members
28
+ total_items: Total items
29
+ membership_email_help: If the email does not belong to a registered user, an invitation email is sent.
30
+ membership_email_help_registered_only: Only registered users can be added. Ask an administrator to invite new users.
31
+ aria:
32
+ view_group: View group %{name}
33
+ manage_group: Manage group %{name}
34
+ prefer_group: Set %{name} as preferred group
35
+ select_group: Select group %{name}
36
+ resend_invitation: Resend invitation to %{name}
37
+ remove_member: Remove %{name} from the group
38
+ confirm:
39
+ group_destroy: Delete group "%{name}"? Its %{members} memberships and %{items} data associations will be removed, and records that require a group will be destroyed. This cannot be undone.
40
+ membership_destroy: Remove %{name} from this group?
41
+ membership_leave: You are about to leave this group and lose access to its data. Continue?
42
+ invite_resend: Resend the invitation email to %{name}?
43
+ flash:
44
+ group_selected: Group selected.
45
+ group_select_error: Error on changing group.
46
+ preferred_error: Error on changing preferred group.
47
+ access_denied: You have not access to this section.
48
+ group_created: Group was successfully created.
49
+ group_updated: Group was successfully updated.
50
+ group_destroyed: Group was successfully destroyed.
51
+ group_destroy_error: Group was not destroyed.
52
+ membership_created: User was successfully added to the group.
53
+ membership_invited: User was invited to the group. They join it after accepting the invitation.
54
+ membership_last_member: The last member can not be removed from the group.
55
+ membership_destroyed: Membership was successfully destroyed.
56
+ membership_destroy_error: Membership was not destroyed.
57
+ invite_sent: Invite was successfully sent.
58
+ invite_error: Invite was not sent.
21
59
  activerecord:
22
60
  attributes:
61
+ lato_spaces/group:
62
+ name: Name
63
+ actions: Actions
23
64
  lato_spaces/membership:
24
- user_infos: User
65
+ email: Email
66
+ user_infos: User
67
+ actions: Actions
68
+ errors:
69
+ models:
70
+ lato_spaces/membership:
71
+ invitation_not_permitted: does not belong to a registered user and invitations are disabled
72
+ already_member: already belongs to a member of this group
73
+ invitation_error: There was an error with the invitation.
74
+ already_joined: This user is already a member of this space.
75
+ lato_spaces/association:
76
+ already_associated: already belongs to a group
@@ -1,10 +1,10 @@
1
1
  it:
2
2
  lato_spaces:
3
- space: Space
4
- spaces: Spaces
3
+ space: Spazio
4
+ spaces: Spazi
5
5
  group: Gruppo
6
6
  groups: Gruppi
7
- welcome: Ciao <b>%{user}</b>!<b> Seleziona il gruppo</b> con cui vuoi lavorare.
7
+ welcome_html: Ciao <b>%{user}</b>!<b> Seleziona il gruppo</b> con cui vuoi lavorare.
8
8
  cta_manage_groups: Gestisci gruppi
9
9
  new_group: Nuovo gruppo
10
10
  edit_group: Modifica gruppo
@@ -17,12 +17,60 @@ it:
17
17
  cta_edit: Modifica
18
18
  cta_delete: Cancella
19
19
  cta_resend: Reinvia
20
+ cta_select: Seleziona gruppo
20
21
  new_membership: Nuova iscrizione
22
+ no_groups: Non sei ancora membro di nessun gruppo. Chiedi a un amministratore di aggiungerti a un gruppo.
23
+ selected: Selezionato
24
+ preferred: Preferito
25
+ unknown_member: Sconosciuto
26
+ invitation_pending: Invito in attesa (inviato %{time} fa)
27
+ total_members: Membri totali
28
+ total_items: Elementi totali
29
+ membership_email_help: Se l'email non appartiene a un utente registrato, viene inviata un'email di invito.
30
+ membership_email_help_registered_only: Puoi aggiungere solo utenti registrati. Chiedi a un amministratore di invitare nuovi utenti.
31
+ aria:
32
+ view_group: Visualizza il gruppo %{name}
33
+ manage_group: Gestisci il gruppo %{name}
34
+ prefer_group: Imposta %{name} come gruppo preferito
35
+ select_group: Seleziona il gruppo %{name}
36
+ resend_invitation: Reinvia l'invito a %{name}
37
+ remove_member: Rimuovi %{name} dal gruppo
38
+ confirm:
39
+ group_destroy: Cancellare il gruppo "%{name}"? Verranno rimossi %{members} membri e %{items} associazioni di dati, e i record che richiedono un gruppo verranno cancellati. L'operazione è irreversibile.
40
+ membership_destroy: Rimuovere %{name} da questo gruppo?
41
+ membership_leave: Stai per uscire da questo gruppo e perderai l'accesso ai suoi dati. Continuare?
42
+ invite_resend: Reinviare l'email di invito a %{name}?
43
+ flash:
44
+ group_selected: Gruppo selezionato.
45
+ group_select_error: Errore durante il cambio di gruppo.
46
+ preferred_error: Errore durante il cambio del gruppo preferito.
47
+ access_denied: Non hai accesso a questa sezione.
48
+ group_created: Gruppo creato con successo.
49
+ group_updated: Gruppo aggiornato con successo.
50
+ group_destroyed: Gruppo cancellato con successo.
51
+ group_destroy_error: Il gruppo non è stato cancellato.
52
+ membership_created: Utente aggiunto al gruppo con successo.
53
+ membership_invited: Utente invitato nel gruppo. Entrerà nel gruppo dopo aver accettato l'invito.
54
+ membership_last_member: L'ultimo membro non può essere rimosso dal gruppo.
55
+ membership_destroyed: Iscrizione cancellata con successo.
56
+ membership_destroy_error: L'iscrizione non è stata cancellata.
57
+ invite_sent: Invito inviato con successo.
58
+ invite_error: L'invito non è stato inviato.
21
59
  activerecord:
22
60
  attributes:
23
61
  lato_spaces/group:
24
62
  name: Nome
25
63
  actions: Azioni
26
64
  lato_spaces/membership:
65
+ email: Email
27
66
  user_infos: Utente
28
- actions: Azioni
67
+ actions: Azioni
68
+ errors:
69
+ models:
70
+ lato_spaces/membership:
71
+ invitation_not_permitted: non appartiene a un utente registrato e gli inviti sono disabilitati
72
+ already_member: appartiene già a un membro di questo gruppo
73
+ invitation_error: Si è verificato un errore con l'invito.
74
+ already_joined: Questo utente è già membro di questo spazio.
75
+ lato_spaces/association:
76
+ already_associated: appartiene già a un gruppo
@@ -0,0 +1,33 @@
1
+ class AddUniqueIndexesToLatoSpaces < ActiveRecord::Migration[7.1]
2
+ # Existing duplicates are removed before adding the unique indexes.
3
+ # Duplicates are redundant rows: the kept row is the preferred one (memberships) or the oldest.
4
+ def up
5
+ deduplicate(:lato_spaces_memberships, %w[lato_spaces_group_id lato_user_id], 'preferred DESC, id ASC')
6
+ deduplicate(:lato_spaces_memberships, %w[lato_spaces_group_id lato_invitation_id], 'id ASC')
7
+ deduplicate(:lato_spaces_associations, %w[lato_spaces_group_id item_type item_id], 'id ASC')
8
+
9
+ add_index :lato_spaces_memberships, %i[lato_spaces_group_id lato_user_id], unique: true, name: 'index_lato_spaces_memberships_on_group_and_user'
10
+ add_index :lato_spaces_memberships, %i[lato_spaces_group_id lato_invitation_id], unique: true, name: 'index_lato_spaces_memberships_on_group_and_invitation'
11
+ add_index :lato_spaces_associations, %i[lato_spaces_group_id item_type item_id], unique: true, name: 'index_lato_spaces_associations_on_group_and_item'
12
+ end
13
+
14
+ def down
15
+ remove_index :lato_spaces_memberships, name: 'index_lato_spaces_memberships_on_group_and_user'
16
+ remove_index :lato_spaces_memberships, name: 'index_lato_spaces_memberships_on_group_and_invitation'
17
+ remove_index :lato_spaces_associations, name: 'index_lato_spaces_associations_on_group_and_item'
18
+ end
19
+
20
+ private
21
+
22
+ # Raw SQL on purpose: model callbacks (e.g. AssociableRequired item destruction) must not run.
23
+ def deduplicate(table, columns, keep_order)
24
+ not_null = columns.map { |column| "#{column} IS NOT NULL" }.join(' AND ')
25
+ duplicates = select_rows("SELECT #{columns.join(', ')} FROM #{table} WHERE #{not_null} GROUP BY #{columns.join(', ')} HAVING COUNT(*) > 1")
26
+
27
+ duplicates.each do |values|
28
+ conditions = columns.zip(values).map { |column, value| "#{column} = #{connection.quote(value)}" }.join(' AND ')
29
+ ids = select_values("SELECT id FROM #{table} WHERE #{conditions} ORDER BY #{keep_order}")
30
+ execute("DELETE FROM #{table} WHERE id IN (#{ids.drop(1).map { |id| connection.quote(id) }.join(', ')})")
31
+ end
32
+ end
33
+ end
@@ -4,14 +4,14 @@ module LatoSpaces
4
4
  ##
5
5
  class Config
6
6
 
7
- # Path of the redirect after set group.
7
+ # Name of a host route helper (no arguments) used as redirect after set group, e.g. :dashboard_path.
8
8
  attr_accessor :setgroup_redirect_path
9
9
 
10
- # Auto-select first group after login.
10
+ # Auto-select preferred group (or first group by name) when the user opens the Spaces root page without a selected group.
11
11
  # NOTE: Requires to set `setgroup_redirect_path` to work properly.
12
12
  attr_accessor :setgroup_auto_after_login
13
13
 
14
- # Auto-select first group after login only if user has a single group.
14
+ # Auto-select preferred group (or first group by name) when the user opens the Spaces root page without a selected group.only if user has a single group.
15
15
  # NOTE: Requires to set `setgroup_redirect_path` to work properly.
16
16
  attr_accessor :setgroup_auto_after_login_if_single
17
17
 
@@ -1,3 +1,3 @@
1
1
  module LatoSpaces
2
- VERSION = "3.2.0"
2
+ VERSION = "3.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lato_spaces
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregorio Galante
@@ -92,6 +92,7 @@ files:
92
92
  - db/migrate/20231102082236_create_lato_spaces_memberships.rb
93
93
  - db/migrate/20231129183145_create_lato_spaces_associations.rb
94
94
  - db/migrate/20231215220127_add_preferred_to_lato_spaces_membership.rb
95
+ - db/migrate/20261110120000_add_unique_indexes_to_lato_spaces.rb
95
96
  - lib/lato_spaces.rb
96
97
  - lib/lato_spaces/config.rb
97
98
  - lib/lato_spaces/engine.rb