Skip to content

Commit

Permalink
✨ Add view mode to dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloalba committed Oct 15, 2024
1 parent c841ed6 commit cf15089
Show file tree
Hide file tree
Showing 36 changed files with 701 additions and 242 deletions.
2 changes: 1 addition & 1 deletion backend/src/app/rpc/commands/binfile.clj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
::sse/stream? true
::sm/params schema:import-binfile}
[{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id name project-id file] :as params}]
(projects/check-read-permissions! pool profile-id project-id)
(projects/check-edition-permissions! pool profile-id project-id)
(let [cfg (-> cfg
(assoc ::bf.v1/project-id project-id)
(assoc ::bf.v1/profile-id profile-id)
Expand Down
29 changes: 16 additions & 13 deletions backend/src/app/rpc/commands/teams.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
[app.common.features :as cfeat]
[app.common.logging :as l]
[app.common.schema :as sm]
[app.common.types.team :as tt]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.db :as db]
Expand All @@ -20,6 +21,7 @@
[app.loggers.audit :as audit]
[app.main :as-alias main]
[app.media :as media]
[app.msgbus :as mbus]
[app.rpc :as-alias rpc]
[app.rpc.commands.profile :as profile]
[app.rpc.doc :as-alias doc]
Expand Down Expand Up @@ -605,14 +607,8 @@
nil)))

;; --- Mutation: Team Update Role

;; Temporarily disabled viewer role
;; https://tree.taiga.io/project/penpot/issue/1083
(def valid-roles
#{:owner :admin :editor #_:viewer})

(def schema:role
[::sm/one-of valid-roles])
[::sm/one-of tt/valid-roles])

(defn role->params
[role]
Expand All @@ -623,15 +619,14 @@
:viewer {:is-owner false :is-admin false :can-edit false}))

(defn update-team-member-role
[conn {:keys [profile-id team-id member-id role] :as params}]
[{:keys [::db/conn ::mbus/msgbus]} {:keys [profile-id team-id member-id role] :as params}]
;; We retrieve all team members instead of query the
;; database for a single member. This is just for
;; convenience, if this becomes a bottleneck or problematic,
;; we will change it to more efficient fetch mechanisms.
(let [perms (get-permissions conn profile-id team-id)
members (get-team-members conn team-id)
member (d/seek #(= member-id (:id %)) members)

is-owner? (:is-owner perms)
is-admin? (:is-admin perms)]

Expand All @@ -655,6 +650,13 @@
(ex/raise :type :validation
:code :cant-promote-to-owner))

(mbus/pub! msgbus
:topic member-id
:message {:type :team-permissions-change
:subs-id member-id
:team-id team-id
:role role})

(let [params (role->params role)]
;; Only allow single owner on team
(when (= role :owner)
Expand All @@ -678,9 +680,8 @@
(sv/defmethod ::update-team-member-role
{::doc/added "1.17"
::sm/params schema:update-team-member-role}
[{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id] :as params}]
(db/with-atomic [conn pool]
(update-team-member-role conn (assoc params :profile-id profile-id))))
[cfg {:keys [::rpc/profile-id] :as params}]
(db/tx-run! cfg update-team-member-role (assoc params :profile-id profile-id)))

;; --- Mutation: Delete Team Member

Expand Down Expand Up @@ -724,6 +725,7 @@
::sm/params schema:update-team-photo}
[cfg {:keys [::rpc/profile-id file] :as params}]
;; Validate incoming mime type

(media/validate-media-type! file #{"image/jpeg" "image/png" "image/webp"})
(update-team-photo cfg (assoc params :profile-id profile-id)))

Expand Down Expand Up @@ -1115,7 +1117,7 @@
::sm/params schema:update-team-invitation-role}
[{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id email role] :as params}]
(db/with-atomic [conn pool]
(let [perms (get-permissions conn profile-id team-id)]
(let [perms (get-permissions conn profile-id team-id)]

(when-not (:is-admin perms)
(ex/raise :type :validation
Expand All @@ -1124,6 +1126,7 @@
(db/update! conn :team-invitation
{:role (name role) :updated-at (dt/now)}
{:team-id team-id :email-to (profile/clean-email email)})

nil)))

;; --- Mutation: Delete invitation
Expand Down
10 changes: 10 additions & 0 deletions common/src/app/common/types/team.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC

(ns app.common.types.team)

(def valid-roles
#{:owner :admin :editor :viewer})
3 changes: 3 additions & 0 deletions frontend/resources/images/assets/empty-placeholder-1-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions frontend/resources/images/assets/empty-placeholder-2-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions frontend/src/app/main/data/common.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,26 @@
(->> (rp/cmd! :create-team-access-request params)
(rx/tap on-success)
(rx/catch on-error))))))

(defn change-team-permissions
[team-id role]
(ptk/reify ::change-team-permissions
ptk/UpdateEvent
(update [_ state]
(update-in state [:teams team-id :permissions]
(fn [permissions]
(cond
(= role :viewer)
(assoc permissions :can-edit false :is-admin false :is-owner false)

(= role :editor)
(assoc permissions :can-edit true :is-admin false :is-owner false)

(= role :admin)
(assoc permissions :can-edit true :is-admin true :is-owner false)

(= role :owner)
(assoc permissions :can-edit true :is-admin true :is-owner true)

:else
permissions))))))
52 changes: 44 additions & 8 deletions frontend/src/app/main/data/dashboard.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
[app.common.files.helpers :as cfh]
[app.common.logging :as log]
[app.common.schema :as sm]
[app.common.types.team :as tt]
[app.common.uri :as u]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.main.data.common :refer [handle-notification]]
[app.main.data.common :as dc]
[app.main.data.events :as ev]
[app.main.data.fonts :as df]
[app.main.data.media :as di]
[app.main.data.notifications :as ntf]
[app.main.data.users :as du]
[app.main.data.websocket :as dws]
[app.main.features :as features]
[app.main.repo :as rp]
[app.main.store :as st]
[app.util.dom :as dom]
[app.util.i18n :as i18n :refer [tr]]
[app.util.router :as rt]
Expand All @@ -42,6 +45,7 @@

(declare fetch-projects)
(declare fetch-team-members)
(declare process-message)

(defn initialize
[{:keys [id]}]
Expand Down Expand Up @@ -77,11 +81,10 @@
(->> stream
(rx/filter (ptk/type? ::dws/message))
(rx/map deref)
(rx/filter (fn [{:keys [subs-id type] :as msg}]
(and (or (= subs-id uuid/zero)
(= subs-id profile-id))
(= :notification type))))
(rx/map handle-notification))
(rx/filter (fn [{:keys [subs-id] :as msg}]
(or (= subs-id uuid/zero)
(= subs-id profile-id))))
(rx/map process-message))

;; Once the teams are fecthed, initialize features related
;; to currently active team
Expand Down Expand Up @@ -477,10 +480,32 @@
:team-id team-id}))))
(rx/catch on-error))))))

(defn handle-team-permissions-change
[{:keys [role team-id]}]
(dm/assert! (uuid? team-id))
(dm/assert! (contains? tt/valid-roles role))

(let [msg (case role
:viewer
(tr "dashboard.permissions-change.viewer")

:editor
(tr "dashboard.permissions-change.editor")

:admin
(tr "dashboard.permissions-change.admin")

:owner
(tr "dashboard.permissions-change.owner"))]

(st/emit! (ntf/info msg)
(dc/change-team-permissions team-id role))))

(defn update-team-member-role
[{:keys [role member-id] :as params}]
(dm/assert! (uuid? member-id))
(dm/assert! (keyword? role)) ; FIXME: validate proper role?
(dm/assert! (contains? tt/valid-roles role))

(ptk/reify ::update-team-member-role
ptk/WatchEvent
(watch [_ state _]
Expand Down Expand Up @@ -602,7 +627,7 @@
(sm/check-email! email))

(dm/assert! (uuid? team-id))
(dm/assert! (keyword? role)) ;; FIXME validate role
(dm/assert! (contains? tt/valid-roles role))

(ptk/reify ::update-team-invitation-role
IDeref
Expand Down Expand Up @@ -1203,3 +1228,14 @@
(let [file (get-in state [:dashboard-files (first files)])]
(rx/of (go-to-workspace file)))
(rx/empty))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Notifications
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn- process-message
[{:keys [type] :as msg}]
(case type
:notification (dc/handle-notification msg)
:team-permissions-change (handle-team-permissions-change msg)
nil))
16 changes: 10 additions & 6 deletions frontend/src/app/main/ui/dashboard.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
content-width (mf/use-state 0)
project-id (:id project)
team-id (:id team)
you-viewer? (not (get-in team [:permissions :can-edit]))

dashboard-local (mf/deref refs/dashboard-local)
file-menu-open? (:menu-open dashboard-local)
Expand All @@ -84,7 +85,10 @@

clear-selected-fn
(mf/use-fn
#(st/emit! (dd/clear-selected-files)))]
#(st/emit! (dd/clear-selected-files)))

show-templates (and (contains? cf/flags :dashboard-templates-section)
(not you-viewer?))]

(mf/with-effect []
(let [key1 (events/listen js/window "resize" on-resize)]
Expand All @@ -105,24 +109,24 @@
:profile profile
:default-project-id default-project-id}]

(when (contains? cf/flags :dashboard-templates-section)
(when show-templates
[:& templates-section {:profile profile
:project-id project-id
:team-id team-id
:default-project-id default-project-id
:content-width @content-width}])]

:dashboard-fonts
[:& fonts-page {:team team}]
[:& fonts-page {:team team :you-viewer? you-viewer?}]

:dashboard-font-providers
[:& font-providers-page {:team team}]

:dashboard-files
(when project
[:*
[:& files-section {:team team :project project}]
(when (contains? cf/flags :dashboard-templates-section)
[:& files-section {:team team :project project :you-viewer? you-viewer?}]
(when show-templates
[:& templates-section {:profile profile
:team-id team-id
:project-id project-id
Expand All @@ -134,7 +138,7 @@
:search-term search-term}]

:dashboard-libraries
[:& libraries-page {:team team}]
[:& libraries-page {:team team :you-viewer? you-viewer?}]

:dashboard-team-members
[:& team-members-page {:team team :profile profile :invite-email invite-email}]
Expand Down
Loading

0 comments on commit cf15089

Please sign in to comment.