Skip to content

Commit f41c5e6

Browse files
Validate user limit when trying to activate user
1 parent 0c68614 commit f41c5e6

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

app/contracts/users/update_contract.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Users
3232
class UpdateContract < BaseContract
3333
validate :user_allowed_to_update
3434
validate :at_least_one_admin_is_active
35+
validate :user_limit_not_exceeded
3536

3637
##
3738
# Users can only be updated when
@@ -60,6 +61,12 @@ def at_least_one_admin_is_active
6061
end
6162
end
6263

64+
def user_limit_not_exceeded
65+
if activating_user? && OpenProject::Enterprise.user_limit_reached?
66+
errors.add :base, :user_limit_reached
67+
end
68+
end
69+
6370
def editing_themself?
6471
user == model
6572
end
@@ -69,5 +76,9 @@ def editing_themself?
6976
def can_manage_user?
7077
user.allowed_globally?(:manage_user) && (user.admin? || !model.admin?)
7178
end
79+
80+
def activating_user?
81+
model.status_changed? && model.active?
82+
end
7283
end
7384
end

spec/contracts/users/update_contract_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@
126126
it_behaves_like "contract is valid"
127127
end
128128

129+
context "when user limit is reached" do
130+
before do
131+
allow(OpenProject::Enterprise).to receive(:user_limit_reached?).and_return(true)
132+
end
133+
134+
context "when activating a previously inactive user" do
135+
let(:attributes) { super().merge(status: Principal.statuses[:locked]) }
136+
137+
before do
138+
user.status = Principal.statuses[:active]
139+
end
140+
141+
it_behaves_like "contract is invalid", base: :user_limit_reached
142+
end
143+
144+
context "when updating an already active user" do
145+
before do
146+
user.mail = "a.new@email.address"
147+
end
148+
149+
it_behaves_like "contract is valid"
150+
end
151+
end
152+
129153
context "when updated user authenticates through LDAP and basic attributes are changed" do
130154
let(:attributes) { super().merge(ldap_auth_source_id: create(:ldap_auth_source).id) }
131155

spec/requests/api/v3/user/update_user_resource_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,22 @@ def send_request
276276

277277
it_behaves_like "update flow"
278278

279+
describe "activation when the user limit is reached" do
280+
let(:parameters) { { status: "active" } }
281+
282+
before do
283+
user.locked!
284+
allow(OpenProject::Enterprise).to receive(:user_limit_reached?).and_return(true)
285+
end
286+
287+
it "returns an error and does not activate the user" do
288+
send_request
289+
290+
expect(last_response).to have_http_status(:unprocessable_entity)
291+
expect(user.reload).to be_locked
292+
end
293+
end
294+
279295
describe "password update" do
280296
let(:password) { "my!new!password123" }
281297
let(:parameters) { { password: } }

0 commit comments

Comments
 (0)