Skip to content

Commit

Permalink
[Dash] Add clear app to admin (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
nezaj authored Sep 27, 2024
1 parent 2122600 commit c87a7f1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
62 changes: 50 additions & 12 deletions client/www/pages/dash/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,9 @@ function Admin({
}) {
const token = useContext(TokenContext);
const [deleteAppOk, updateDeleteAppOk] = useState(false);
const [clearAppOk, updateClearAppOk] = useState(false);
const [editMember, setEditMember] = useState<InstantMember | null>();
const clearDialog = useDialog();
const deleteDialog = useDialog();
const inviteDialog = useDialog();

Expand Down Expand Up @@ -1288,21 +1290,57 @@ function Admin({
</Content>
<Copyable label="Secret" value={app.admin_token} />
{isMinRole('owner', app.user_app_role) ? (
<>
<div className="space-y-2">
<SectionHeading>Danger zone</SectionHeading>
<Content>
These are destructive actions and will irreversibly delete associated data.
</Content>
<div>
<div className="flex flex-col md:flex-row justify-between gap-2 md:gap-8">
<div>
<SubsectionHeading>Delete app</SubsectionHeading>
<Content>Once you delete this app, you can’t undo it.</Content>
</div>
<div>
<Button variant="destructive" onClick={deleteDialog.onOpen}>
<TrashIcon height={'1rem'} /> Delete app
</Button>
</div>
<div className="flex flex-col space-y-6">
<Button variant="destructive" onClick={clearDialog.onOpen}>
<TrashIcon height={'1rem'} /> Clear app
</Button>
<Button variant="destructive" onClick={deleteDialog.onOpen}>
<TrashIcon height={'1rem'} /> Delete app
</Button>
</div>
</div>
<Dialog {...clearDialog}>
<div className="flex flex-col gap-2">
<SubsectionHeading className="text-red-600">
Clear app
</SubsectionHeading>
<Content className="space-y-2">
<p>Clearing an app will irreversibly delete all namespaces, triples, and permissions.</p>
<p>All other data like app id, admin token, users, billing, team members, etc. will remain.</p>
<p>This is equivalent to deleting all your namespaces in the explorer and clearing your permissions.</p>
</Content>
<Checkbox
checked={clearAppOk}
onChange={(c) => updateClearAppOk(c)}
label="I understand and want to clear this app."
/>
<Button
disabled={!clearAppOk}
variant="destructive"
onClick={async () => {
await jsonFetch(`${config.apiURI}/dash/apps/${app.id}/clear`, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
});

clearDialog.onClose();
dashResponse.mutate();
successToast('App cleared!');
}}
>
Clear data
</Button>
</div>
</Dialog>
<Dialog {...deleteDialog}>
<div className="flex flex-col gap-2">
<SubsectionHeading className="text-red-600">
Expand Down Expand Up @@ -1335,7 +1373,7 @@ function Admin({
</Button>
</div>
</Dialog>
</>
</div>
) : null}
</TabContent>
);
Expand Down
6 changes: 6 additions & 0 deletions server/src/instant/dash/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@
(app-model/delete-by-id! {:id app-id})
(response/ok {:ok true})))

(defn apps-clear [req]
(let [{{app-id :id} :app} (req->app-and-user! req)]
(app-model/clear-by-id! {:id app-id})
(response/ok {:ok true})))

(defn admin-tokens-regenerate [req]
(let [{{app-id :id} :app} (req->app-and-user! req)
admin-token (ex/get-param! req [:body :admin-token] uuid-util/coerce)]
Expand Down Expand Up @@ -1109,6 +1114,7 @@
(POST "/dash/apps" [] apps-post)
(POST "/dash/profiles" [] profiles-post)
(DELETE "/dash/apps/:app_id" [] apps-delete)
(POST "/dash/apps/:app_id/clear" [] apps-clear)
(POST "/dash/apps/:app_id/rules" [] rules-post)
(POST "/dash/apps/:app_id/tokens" [] admin-tokens-regenerate)

Expand Down
2 changes: 1 addition & 1 deletion server/src/instant/db/model/attr.clj
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
[conn app-id]
(sql/do-execute!
conn
["DELETE FROM attrs WHERE attrs.app_id = ?" app-id]))
["DELETE FROM attrs WHERE attrs.app_id = ?::uuid" app-id]))

;; ------
;; insert-multi!
Expand Down
17 changes: 16 additions & 1 deletion server/src/instant/model/app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
[instant.model.app-admin-token :as app-admin-token-model]
[instant.util.crypt :as crypt-util]
[instant.util.exception :as ex]
[instant.util.uuid :as uuid-util])
[instant.util.uuid :as uuid-util]
[instant.db.model.attr :as attr-model]
[instant.model.rule :as rule-model]
[instant.db.model.transaction :as transaction-model])
(:import
(java.util UUID)))

Expand Down Expand Up @@ -273,6 +276,18 @@
WHERE a.id = ?::uuid"
new-creator-id id])))

(defn clear-by-id!
"Deletes attrs, rules, and triples for the specified app_id"
([params] (clear-by-id! aurora/conn-pool params))
([conn {:keys [id]}]
(next-jdbc/with-transaction [tx-conn conn]
(attr-model/delete-by-app-id! tx-conn id)
(rule-model/delete-by-app-id! tx-conn {:app-id id})
(transaction-model/create! tx-conn {:app-id id}))))

(comment
(clear-by-id! {:id "9a6d8f38-991d-4264-9801-4a05d8b1eab1"}))

(defn delete-by-ids!
([params] (delete-by-ids! aurora/conn-pool params))
([conn {:keys [creator-id ids]}]
Expand Down
7 changes: 7 additions & 0 deletions server/src/instant/model/rule.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
([conn {:keys [app-id]}]
(sql/select-one conn ["SELECT * FROM rules WHERE app_id = ?::uuid" app-id])))

(defn delete-by-app-id!
([params] (delete-by-app-id! aurora/conn-pool params))
([conn {:keys [app-id]}]
(sql/do-execute!
conn
["DELETE FROM rules WHERE app_id = ?::uuid" app-id])))

(defn with-binds [rule etype expr]
(->> (get-in rule [etype "bind"])
(partition-all 2)
Expand Down

0 comments on commit c87a7f1

Please sign in to comment.