Merge pull request #500 from penpot/niwinz/enhacements-5

Niwinz/enhacements 5
This commit is contained in:
Hirunatan 2021-01-29 18:03:50 +01:00 committed by GitHub
commit 676ce9b68d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 82 deletions

View file

@ -437,7 +437,7 @@
;; --- Mutation: Delete Profile ;; --- Mutation: Delete Profile
(declare check-teams-ownership!) (declare check-can-delete-profile!)
(declare mark-profile-as-deleted!) (declare mark-profile-as-deleted!)
(s/def ::delete-profile (s/def ::delete-profile
@ -446,7 +446,7 @@
(sv/defmethod ::delete-profile (sv/defmethod ::delete-profile
[{:keys [pool session] :as cfg} {:keys [profile-id] :as params}] [{:keys [pool session] :as cfg} {:keys [profile-id] :as params}]
(db/with-atomic [conn pool] (db/with-atomic [conn pool]
(check-teams-ownership! conn profile-id) (check-can-delete-profile! conn profile-id)
;; Schedule a complete deletion of profile ;; Schedule a complete deletion of profile
(tasks/submit! conn {:name "delete-profile" (tasks/submit! conn {:name "delete-profile"
@ -464,24 +464,26 @@
(assoc response (assoc response
:cookies (session/cookies session {:value "" :max-age -1})))}))) :cookies (session/cookies session {:value "" :max-age -1})))})))
(def ^:private sql:teams-ownership-check (def sql:owned-teams
"with teams as ( "with owner_teams as (
select tpr.team_id as id select tpr.team_id as id
from team_profile_rel as tpr from team_profile_rel as tpr
where tpr.profile_id = ? where tpr.is_owner is true
and tpr.is_owner is true and tpr.profile_id = ?
) )
select tpr.team_id, select tpr.team_id,
count(tpr.profile_id) as num_profiles count(tpr.profile_id) as num_profiles
from team_profile_rel as tpr from team_profile_rel as tpr
where tpr.team_id in (select id from teams) where tpr.team_id in (select id from owner_teams)
group by tpr.team_id group by 1")
having count(tpr.profile_id) > 1")
(defn- check-teams-ownership! (defn- check-can-delete-profile!
[conn profile-id] [conn profile-id]
(let [rows (db/exec! conn [sql:teams-ownership-check profile-id])] (let [rows (db/exec! conn [sql:owned-teams profile-id])]
(when-not (empty? rows) ;; If we found owned teams with more than one profile we don't
;; allow delete profile until the user properly transfer ownership
;; or explictly removes all participants from the team.
(when (some #(> (:num-profiles %) 1) rows)
(ex/raise :type :validation (ex/raise :type :validation
:code :owner-teams-with-people :code :owner-teams-with-people
:hint "The user need to transfer ownership of owned teams." :hint "The user need to transfer ownership of owned teams."

View file

@ -5,20 +5,24 @@
;; This Source Code Form is "Incompatible With Secondary Licenses", as ;; This Source Code Form is "Incompatible With Secondary Licenses", as
;; defined by the Mozilla Public License, v. 2.0. ;; defined by the Mozilla Public License, v. 2.0.
;; ;;
;; Copyright (c) 2020 UXBOX Labs SL ;; Copyright (c) 2020-2021 UXBOX Labs SL
(ns app.tasks.delete-profile (ns app.tasks.delete-profile
"Task for permanent deletion of profiles." "Task for permanent deletion of profiles."
(:require (:require
[app.common.spec :as us] [app.common.spec :as us]
[app.db :as db] [app.db :as db]
[app.db.sql :as sql]
[app.metrics :as mtx] [app.metrics :as mtx]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[integrant.core :as ig])) [integrant.core :as ig]))
(declare delete-profile-data)
(declare handler) (declare handler)
;; --- INIT
(defmethod ig/pre-init-spec ::handler [_] (defmethod ig/pre-init-spec ::handler [_]
(s/keys :req-un [::db/pool ::mtx/metrics])) (s/keys :req-un [::db/pool ::mtx/metrics]))
@ -31,74 +35,52 @@
:help "delete profile task timing"} :help "delete profile task timing"}
(mtx/instrument handler)))) (mtx/instrument handler))))
(declare delete-profile-data) ;; This task is responsible to permanently delete a profile with all
(declare delete-teams) ;; the dependent data. As step (1) we delete all owned teams of the
(declare delete-files) ;; profile (that will cause to delete all underlying projects, files,
(declare delete-profile) ;; file_media and mark to be deleted storage_object's used by team,
;; profile and files previously deleted. Then, finally as step (2) we
;; proceed to delete the profile row.
;;
;; The storage_objects marked as deleted will be deleted by the
;; corresponding garbage collector task.
(s/def ::profile-id ::us/uuid) (s/def ::profile-id ::us/uuid)
(s/def ::props (s/def ::props (s/keys :req-un [::profile-id]))
(s/keys :req-un [::profile-id]))
(defn handler (defn handler
[{:keys [pool]} {:keys [props] :as task}] [{:keys [pool]} {:keys [props] :as task}]
(us/verify ::props props) (us/verify ::props props)
(db/with-atomic [conn pool] (db/with-atomic [conn pool]
(let [id (:profile-id props) (let [id (:profile-id props)
profile (db/get-by-id conn :profile id {:for-update true})] profile (db/exec-one! conn (sql/select :profile {:id id} {:for-update true}))]
(if (or (:is-demo profile) (if (or (:is-demo profile)
(not (nil? (:deleted-at profile)))) (:deleted-at profile))
(delete-profile-data conn (:id profile)) (delete-profile-data conn id)
(log/warn "Profile " (:id profile) (log/warnf "Profile %s does not match constraints for deletion" id)))))
"does not match constraints for deletion")))))
(defn- delete-profile-data ;; --- IMPL
[conn profile-id]
(log/info "Proceding to delete all data related to profile" profile-id)
(delete-teams conn profile-id)
(delete-files conn profile-id)
(delete-profile conn profile-id))
(def ^:private sql:remove-owned-teams (def ^:private sql:remove-owned-teams
"with teams as ( "delete from team
select distinct where id in (
tpr.team_id as id select tpr.team_id
from team_profile_rel as tpr from team_profile_rel as tpr
where tpr.profile_id = ? where tpr.is_owner is true
and tpr.is_owner is true and tpr.profile_id = ?
), to_delete_teams as ( )")
select tpr.team_id as id
from team_profile_rel as tpr
where tpr.team_id in (select id from teams)
group by tpr.team_id
having count(tpr.profile_id) = 1
)
delete from team
where id in (select id from to_delete_teams)
returning id")
(defn- delete-teams (defn- delete-teams
[conn profile-id] [conn profile-id]
(db/exec-one! conn [sql:remove-owned-teams profile-id])) (db/exec-one! conn [sql:remove-owned-teams profile-id]))
(def ^:private sql:remove-owned-files
"with files_to_delete as (
select distinct
fpr.file_id as id
from file_profile_rel as fpr
inner join file as f on (fpr.file_id = f.id)
where fpr.profile_id = ?
and fpr.is_owner is true
and f.project_id is null
)
delete from file
where id in (select id from files_to_delete)
returning id")
(defn- delete-files
[conn profile-id]
(db/exec-one! conn [sql:remove-owned-files profile-id]))
(defn delete-profile (defn delete-profile
[conn profile-id] [conn profile-id]
(db/delete! conn :profile {:id profile-id})) (db/delete! conn :profile {:id profile-id}))
(defn- delete-profile-data
[conn profile-id]
(log/infof "Proceding to delete all data related to profile id = %s" profile-id)
(delete-teams conn profile-id)
(delete-profile conn profile-id))

View file

@ -11,7 +11,7 @@
danlentz/clj-uuid {:mvn/version "0.1.9"} danlentz/clj-uuid {:mvn/version "0.1.9"}
frankiesardo/linked {:mvn/version "1.3.0"} frankiesardo/linked {:mvn/version "1.3.0"}
funcool/beicon {:mvn/version "2020.12.20-1"} funcool/beicon {:mvn/version "2021.01.29-1"}
funcool/cuerdas {:mvn/version "2020.03.26-3"} funcool/cuerdas {:mvn/version "2020.03.26-3"}
funcool/okulary {:mvn/version "2020.04.14-0"} funcool/okulary {:mvn/version "2020.04.14-0"}
funcool/potok {:mvn/version "3.2.0"} funcool/potok {:mvn/version "3.2.0"}

View file

@ -57,29 +57,38 @@
[x] [x]
(C. x)) (C. x))
(defn empty-string?
[v]
(or (nil? v) (empty? v)))
(defn t (defn t
([locale code] ([locale code]
(let [code (name code) (let [code (name code)
value (gobj/getValueByKeys translations code locale) value (gobj/getValueByKeys translations code locale)]
value (if (nil? value) code value)] (if (empty-string? value)
(if (array? value) (if (= cfg/default-language locale)
(aget value 0) code
value))) (t cfg/default-language code))
(if (array? value)
(aget value 0)
value))))
([locale code & args] ([locale code & args]
(let [code (name code) (let [code (name code)
value (gobj/getValueByKeys translations code locale) value (gobj/getValueByKeys translations code locale)]
value (if (nil? value) code value) (if (empty-string? value)
plural (first (filter c? args)) (if (= cfg/default-language locale)
value (if (array? value) code
(if (= @plural 1) (aget value 0) (aget value 1)) (apply t cfg/default-language code args))
value)] (let [plural (first (filter c? args))
(apply str/format value (map #(if (c? %) @% %) args))))) value (if (array? value)
(if (= @plural 1) (aget value 0) (aget value 1))
value)]
(apply str/fmt value (map #(if (c? %) @% %) args)))))))
(defn tr (defn tr
([code] (t @locale code)) ([code] (t @locale code))
([code & args] (apply t @locale code args))) ([code & args] (apply t @locale code args)))
;; DEPRECATED ;; DEPRECATED
(defn use-locale (defn use-locale
[] []