some logic into schema/domain

neo4j-4
Josha von Gizycki 5 years ago
parent cc30b94d24
commit b1d7c31a83

@ -1,18 +1,6 @@
(ns wanijo.schema.db (ns wanijo.schema.db
(:require [clojure.spec.alpha :as spec] (:require [wanijo.infrastructure.neo4j :as neo4j]
[wanijo.specs :as specs] [wanijo.schema.domain :as domain]))
[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]))
(neo4j/defquery all-created-by (neo4j/defquery all-created-by
"MATCH (s:schema)-[:created_by]->(u:user) "MATCH (s:schema)-[:created_by]->(u:user)
@ -22,8 +10,8 @@
(defn all-created-by! [user-uuid] (defn all-created-by! [user-uuid]
(map :s (map :s
(neo4j/exec-query! (neo4j/exec-query!
all-created-by all-created-by
{:uuid user-uuid}))) {:uuid user-uuid})))
(neo4j/defquery all (neo4j/defquery all
"MATCH (s:schema) "MATCH (s:schema)
@ -43,14 +31,14 @@
RETURN s") RETURN s")
(defn create-new! [schema-name user-uuid] (defn create-new! [schema-name user-uuid]
(->> (->>
(neo4j/exec-query! (neo4j/exec-query!
create-new create-new
{:u_uuid user-uuid {:u_uuid user-uuid}
:name schema-name :name schema-name
:s_uuid (neo4j/uuid) :s_uuid (neo4j/uuid)
:created_at (neo4j/now-str)}) :created_at (neo4j/now-str))
first first
:uuid)) :uuid))
(neo4j/defquery find-by-uuid (neo4j/defquery find-by-uuid
"MATCH (s:schema) "MATCH (s:schema)
@ -58,11 +46,11 @@
RETURN s") RETURN s")
(defn find-by-uuid! [uuid] (defn find-by-uuid! [uuid]
(->> (->>
(neo4j/exec-query! (neo4j/exec-query!
find-by-uuid find-by-uuid
{:uuid uuid}) {:uuid uuid})
first first
:s)) :s))
(neo4j/defquery schema-creator (neo4j/defquery schema-creator
"MATCH (s:schema)-->(u:user) "MATCH (s:schema)-->(u:user)
@ -70,11 +58,11 @@
RETURN u.uuid as uuid") RETURN u.uuid as uuid")
(defn find-schema-creator! [uuid] (defn find-schema-creator! [uuid]
(->> (->>
(neo4j/exec-query! (neo4j/exec-query!
schema-creator schema-creator
{:uuid uuid}) {:uuid uuid})
first first
:uuid)) :uuid))
(neo4j/defquery schema-permissions (neo4j/defquery schema-permissions
"MATCH (s:schema {uuid:{schema_uuid}}) "MATCH (s:schema {uuid:{schema_uuid}})
@ -86,13 +74,12 @@
-[:permission {type:{type}}]- -[:permission {type:{type}}]-
(s)) AS is_public") (s)) AS is_public")
(defn has-user-permission? [perm-type schema-uuid user-uuid] (defn has-user-permission? [perm-type schema-uuid user-uuid]
(let [permissions (first (neo4j/exec-query! schema-permissions (let [perms (first
{:schema_uuid schema-uuid (neo4j/exec-query! schema-permissions
:user_uuid user-uuid {:schema_uuid schema-uuid
:type perm-type})) :user_uuid user-uuid
public? (:is_public permissions) :type perm-type}))]
user? (:user_has_permission permissions)] (domain/has-user-permission? perms)))
(or public? user?)))
(defn has-user-write-permissions? [schema-uuid user-uuid] (defn has-user-write-permissions? [schema-uuid user-uuid]
(has-user-permission? "write" schema-uuid user-uuid)) (has-user-permission? "write" schema-uuid user-uuid))
(defn has-user-read-permissions? [schema-uuid user-uuid] (defn has-user-read-permissions? [schema-uuid user-uuid]
@ -120,8 +107,8 @@
DELETE c, s") DELETE c, s")
(defn delete! [uuid] (defn delete! [uuid]
(neo4j/exec-query! (neo4j/exec-query!
delete delete
{:uuid uuid})) {:uuid uuid}))
(neo4j/defquery edit (neo4j/defquery edit
"MATCH (s:schema) "MATCH (s:schema)
@ -129,8 +116,8 @@
SET s.name = {name}") SET s.name = {name}")
(defn edit! [schema] (defn edit! [schema]
(neo4j/exec-query! (neo4j/exec-query!
edit edit
schema)) schema))
(neo4j/defquery assigned-users (neo4j/defquery assigned-users
"MATCH (s:schema)-[p:permission]-(u:user) "MATCH (s:schema)-[p:permission]-(u:user)
@ -139,8 +126,8 @@
ORDER BY u.ident") ORDER BY u.ident")
(defn assigned-users! [uuid] (defn assigned-users! [uuid]
(neo4j/exec-query! (neo4j/exec-query!
assigned-users assigned-users
{:uuid uuid})) {:uuid uuid}))
(neo4j/defquery assigned-schemas (neo4j/defquery assigned-schemas
"MATCH (s1:schema)-[p:permission]-(s2:schema) "MATCH (s1:schema)-[p:permission]-(s2:schema)
@ -149,8 +136,8 @@
ORDER BY s2.name") ORDER BY s2.name")
(defn assigned-schemas! [uuid] (defn assigned-schemas! [uuid]
(neo4j/exec-query! (neo4j/exec-query!
assigned-schemas assigned-schemas
{:uuid uuid})) {:uuid uuid}))
(defn find-with-assigned-entities! [uuid] (defn find-with-assigned-entities! [uuid]
(let [users (reduce #(case (-> %2 :p :type) (let [users (reduce #(case (-> %2 :p :type)
@ -160,7 +147,7 @@
{:write [] {:write []
:read []} :read []}
(assigned-users! uuid)) (assigned-users! uuid))
schemas (map #(-> % :s2 :uuid) (assigned-schemas! uuid))] schemas (map #(-> % :s2 :uuid) (assigned-schemas! uuid))]
(assoc (find-by-uuid! uuid) (assoc (find-by-uuid! uuid)
:assigned-read-users (:read users) :assigned-read-users (:read users)
:assigned-write-users (:write users) :assigned-write-users (:write users)
@ -178,13 +165,13 @@
CREATE (s)<-[:permission{type:{permtype}}]-(u)") CREATE (s)<-[:permission{type:{permtype}}]-(u)")
(defn assign-users! [uuid users permission] (defn assign-users! [uuid users permission]
(neo4j/exec-queries! (neo4j/exec-queries!
[remove-assignments [remove-assignments
{:uuid uuid {:uuid uuid
:permtype permission}] :permtype permission}]
[create-assignments [create-assignments
{:uuid uuid {:uuid uuid
:users users :users users
:permtype permission}])) :permtype permission}]))
(neo4j/defquery remove-schema-assignments (neo4j/defquery remove-schema-assignments
"MATCH (s1:schema)-[p:permission]-(s2:schema) "MATCH (s1:schema)-[p:permission]-(s2:schema)
@ -197,10 +184,10 @@
CREATE (s1)-[:permission]->(s2)") CREATE (s1)-[:permission]->(s2)")
(defn assign-schemas! [uuid schemas] (defn assign-schemas! [uuid schemas]
(neo4j/exec-queries! (neo4j/exec-queries!
[remove-schema-assignments [remove-schema-assignments
{:uuid uuid}] {:uuid uuid}]
[create-schema-assignments [create-schema-assignments
{:uuid uuid :schemas schemas}])) {:uuid uuid :schemas schemas}]))
(neo4j/defquery find-by-instance (neo4j/defquery find-by-instance
"MATCH (i:instance {uuid:{uuid}})-[:of]->(s:schema) "MATCH (i:instance {uuid:{uuid}})-[:of]->(s:schema)

@ -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))
Loading…
Cancel
Save