From b1d7c31a839e006cc8a83939b067bb5cdbc02be7 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 26 Aug 2019 14:57:19 +0200 Subject: [PATCH] some logic into schema/domain --- src/wanijo/schema/db.clj | 109 +++++++++++++++-------------------- src/wanijo/schema/domain.clj | 20 +++++++ 2 files changed, 68 insertions(+), 61 deletions(-) create mode 100644 src/wanijo/schema/domain.clj diff --git a/src/wanijo/schema/db.clj b/src/wanijo/schema/db.clj index 9bd2cad..e97a847 100644 --- a/src/wanijo/schema/db.clj +++ b/src/wanijo/schema/db.clj @@ -1,18 +1,6 @@ (ns wanijo.schema.db - (:require [clojure.spec.alpha :as spec] - [wanijo.specs :as specs] - [wanijo.infrastructure.neo4j :as neo4j])) - -(spec/def ::name - (spec/and ::specs/name (complement empty?))) -(spec/def ::assigned-to - (spec/or :public empty? - :assigned (spec/coll-of ::neo4j/uuid) - :assigned-single ::neo4j/uuid)) -(spec/def ::schema - (spec/keys ::req-un [::name - ::specs/created-at - ::neo4j/uuid])) + (:require [wanijo.infrastructure.neo4j :as neo4j] + [wanijo.schema.domain :as domain])) (neo4j/defquery all-created-by "MATCH (s:schema)-[:created_by]->(u:user) @@ -22,8 +10,8 @@ (defn all-created-by! [user-uuid] (map :s (neo4j/exec-query! - all-created-by - {:uuid user-uuid}))) + all-created-by + {:uuid user-uuid}))) (neo4j/defquery all "MATCH (s:schema) @@ -43,14 +31,14 @@ RETURN s") (defn create-new! [schema-name user-uuid] (->> - (neo4j/exec-query! - create-new - {:u_uuid user-uuid - :name schema-name - :s_uuid (neo4j/uuid) - :created_at (neo4j/now-str)}) - first - :uuid)) + (neo4j/exec-query! + create-new + {:u_uuid user-uuid} + :name schema-name + :s_uuid (neo4j/uuid) + :created_at (neo4j/now-str)) + first + :uuid)) (neo4j/defquery find-by-uuid "MATCH (s:schema) @@ -58,11 +46,11 @@ RETURN s") (defn find-by-uuid! [uuid] (->> - (neo4j/exec-query! - find-by-uuid - {:uuid uuid}) - first - :s)) + (neo4j/exec-query! + find-by-uuid + {:uuid uuid}) + first + :s)) (neo4j/defquery schema-creator "MATCH (s:schema)-->(u:user) @@ -70,11 +58,11 @@ RETURN u.uuid as uuid") (defn find-schema-creator! [uuid] (->> - (neo4j/exec-query! - schema-creator - {:uuid uuid}) - first - :uuid)) + (neo4j/exec-query! + schema-creator + {:uuid uuid}) + first + :uuid)) (neo4j/defquery schema-permissions "MATCH (s:schema {uuid:{schema_uuid}}) @@ -86,13 +74,12 @@ -[:permission {type:{type}}]- (s)) AS is_public") (defn has-user-permission? [perm-type schema-uuid user-uuid] - (let [permissions (first (neo4j/exec-query! schema-permissions - {:schema_uuid schema-uuid - :user_uuid user-uuid - :type perm-type})) - public? (:is_public permissions) - user? (:user_has_permission permissions)] - (or public? user?))) + (let [perms (first + (neo4j/exec-query! schema-permissions + {:schema_uuid schema-uuid + :user_uuid user-uuid + :type perm-type}))] + (domain/has-user-permission? perms))) (defn has-user-write-permissions? [schema-uuid user-uuid] (has-user-permission? "write" schema-uuid user-uuid)) (defn has-user-read-permissions? [schema-uuid user-uuid] @@ -120,8 +107,8 @@ DELETE c, s") (defn delete! [uuid] (neo4j/exec-query! - delete - {:uuid uuid})) + delete + {:uuid uuid})) (neo4j/defquery edit "MATCH (s:schema) @@ -129,8 +116,8 @@ SET s.name = {name}") (defn edit! [schema] (neo4j/exec-query! - edit - schema)) + edit + schema)) (neo4j/defquery assigned-users "MATCH (s:schema)-[p:permission]-(u:user) @@ -139,8 +126,8 @@ ORDER BY u.ident") (defn assigned-users! [uuid] (neo4j/exec-query! - assigned-users - {:uuid uuid})) + assigned-users + {:uuid uuid})) (neo4j/defquery assigned-schemas "MATCH (s1:schema)-[p:permission]-(s2:schema) @@ -149,8 +136,8 @@ ORDER BY s2.name") (defn assigned-schemas! [uuid] (neo4j/exec-query! - assigned-schemas - {:uuid uuid})) + assigned-schemas + {:uuid uuid})) (defn find-with-assigned-entities! [uuid] (let [users (reduce #(case (-> %2 :p :type) @@ -160,7 +147,7 @@ {:write [] :read []} (assigned-users! uuid)) - schemas (map #(-> % :s2 :uuid) (assigned-schemas! uuid))] + schemas (map #(-> % :s2 :uuid) (assigned-schemas! uuid))] (assoc (find-by-uuid! uuid) :assigned-read-users (:read users) :assigned-write-users (:write users) @@ -178,13 +165,13 @@ CREATE (s)<-[:permission{type:{permtype}}]-(u)") (defn assign-users! [uuid users permission] (neo4j/exec-queries! - [remove-assignments - {:uuid uuid - :permtype permission}] - [create-assignments - {:uuid uuid - :users users - :permtype permission}])) + [remove-assignments + {:uuid uuid + :permtype permission}] + [create-assignments + {:uuid uuid + :users users + :permtype permission}])) (neo4j/defquery remove-schema-assignments "MATCH (s1:schema)-[p:permission]-(s2:schema) @@ -197,10 +184,10 @@ CREATE (s1)-[:permission]->(s2)") (defn assign-schemas! [uuid schemas] (neo4j/exec-queries! - [remove-schema-assignments - {:uuid uuid}] - [create-schema-assignments - {:uuid uuid :schemas schemas}])) + [remove-schema-assignments + {:uuid uuid}] + [create-schema-assignments + {:uuid uuid :schemas schemas}])) (neo4j/defquery find-by-instance "MATCH (i:instance {uuid:{uuid}})-[:of]->(s:schema) diff --git a/src/wanijo/schema/domain.clj b/src/wanijo/schema/domain.clj new file mode 100644 index 0000000..b7dee76 --- /dev/null +++ b/src/wanijo/schema/domain.clj @@ -0,0 +1,20 @@ +(ns wanijo.schema.domain + (:require [clojure.spec.alpha :as spec] + [wanijo.specs :as specs] + [wanijo.infrastructure.neo4j :as neo4j])) + +(spec/def ::name + (spec/and ::specs/name (complement empty?))) +(spec/def ::assigned-to + (spec/or :public empty? + :assigned (spec/coll-of ::neo4j/uuid) + :assigned-single ::neo4j/uuid)) +(spec/def ::schema + (spec/keys ::req-un [::name + ::specs/created-at + ::neo4j/uuid])) + +(defn has-user-permission? + [{public? :is_public + user-permission :user_has_permission}] + (or public? user-permission))