Skip to content

Commit

Permalink
OY-3070 allow for permalocking synthetic applications with special us…
Browse files Browse the repository at this point in the history
…er id
  • Loading branch information
vaeinoe committed Nov 20, 2023
1 parent 1a45cd7 commit ce35db2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions resources/sql/form-queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ SELECT
f.deleted,
f.organization_oid,
f.locked,
f.locked_by as locked_by_oid,
f.properties,
(CASE WHEN f.locked_by IS NULL THEN NULL ELSE CONCAT(first_name, ' ', last_name) END) as locked_by,
(SELECT count(*)
Expand Down
36 changes: 35 additions & 1 deletion spec/ataru/forms/form_access_control_spec.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns ataru.forms.form-access-control-spec
(:require
[speclj.core :refer [describe it should= tags]]
[speclj.core :refer [describe it should= should-not-be-nil tags]]
[ataru.organization-service.organization-service :as os]
[ataru.forms.form-access-control :as fac]
[ataru.forms.form-store :as form-store]
Expand Down Expand Up @@ -35,6 +35,40 @@
:locked nil
:locked-by nil})

(def locked-form
{:key "form-access-control-test-basic-form"
:organization-oid "1.2.246.562.10.1234334543"
:locked nil
:locked-by nil})

(def locked-synthetic-form
{:key "form-access-control-test-basic-form"
:organization-oid "1.2.246.562.10.1234334543"
:locked nil
:locked-by nil
:locked-by-oid "synteettinen_hakemus"})

(describe "update-form-lock"
(tags :unit)

(it "changes form lock"
(with-redefs [form-store/fetch-form (fn [_] locked-form)
fac/post-form (fn [form _ _ _ _] form)]
(let [tarjonta-service (mts/->MockTarjontaService)
organization-service (os/->FakeOrganizationService)
result (fac/update-form-lock 0 "close" session tarjonta-service organization-service nil)]
(should-not-be-nil (:locked result)))))

(it "doesn't allow changing form lock with synthetic forms"
(with-redefs [form-store/fetch-form (fn [_] locked-synthetic-form)
fac/post-form (fn [form _ _ _ _] form)]
(let [tarjonta-service (mts/->MockTarjontaService)
organization-service (os/->FakeOrganizationService)
result (try (fac/update-form-lock 0 "close" session tarjonta-service organization-service nil)
(catch Throwable e
(.getMessage e)))]
(should= "Synteettistä hakemuslomaketta ei voi avata muokattavaksi" result)))))

(describe
"get-forms-for-editor"
(tags :unit)
Expand Down
9 changes: 7 additions & 2 deletions src/clj/ataru/forms/form_access_control.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
[ataru.tarjonta.haku :as haku]
[taoensso.timbre :as log]))

(def synthetic-application-permalock-user "synteettinen_hakemus")

(defn- form-allowed-by-id?
[authorized-organization-oids form-id]
(contains? authorized-organization-oids
Expand Down Expand Up @@ -46,7 +48,10 @@
:user-right-organizations
:form-edit)))

(defn- check-lock-authorization [{:keys [key]} session tarjonta-service]
(defn- check-lock-authorization [{:keys [key]} session tarjonta-service form]
(when (= (:locked-by-oid form) synthetic-application-permalock-user)
(throw (user-feedback-exception
(format "Synteettistä hakemuslomaketta ei voi avata muokattavaksi"))))
(when-not (-> session :identity :superuser)
(let [haut (tarjonta-protocol/hakus-by-form-key tarjonta-service key)]
(when (some :yhteishaku haut)
Expand Down Expand Up @@ -203,7 +208,7 @@
(if lock?
{:locked "now()" :locked-by (-> session :identity :oid)}
{:locked nil :locked-by nil}))]
(check-lock-authorization latest-version session tarjonta-service)
(check-lock-authorization latest-version session tarjonta-service latest-version)
(if (or (and lock? (some? previous-locked))
(and (not lock?) (nil? previous-locked)))
(throw (user-feedback-exception "Lomakkeen sisältö on muuttunut. Lataa sivu uudelleen."))
Expand Down
1 change: 1 addition & 0 deletions src/cljc/ataru/schema/form_element_schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
(s/optional-key :locked) #?(:clj (s/maybe DateTime)
:cljs (s/maybe s/Str))
(s/optional-key :locked-by) (s/maybe s/Str)
(s/optional-key :locked-by-oid) (s/maybe s/Str)
(s/optional-key :languages) [s/Str]
(s/optional-key :key) s/Str
(s/optional-key :created-by) s/Str
Expand Down

0 comments on commit ce35db2

Please sign in to comment.