add zeusid column, link zeus oauth to local accounts

This commit is contained in:
flynn 2019-01-14 23:41:10 +01:00
parent f636248dc2
commit 4082fd0a36
8 changed files with 149 additions and 16 deletions

View file

@ -92,10 +92,72 @@
<section class="section"> <section class="section">
<div class="container"> <div class="container">
<div id="view"> <div id="view">
<div style="width: 400px; height: 300px"></div> <div class="loader-container">
<div class="loader"></div>
</div>
</div> </div>
</div> </div>
</section> </section>
{% if user %}
<section class="section">
<div class="columns">
<div class="column">
<h3 class="title is-3">Your cuddles</h3>
<table class="table">
<thead>
<tr>
<th>Person 1</th>
<th>Person 2</th>
</tr>
</thead>
<tbody>
{% for relation in user-relations %}
<tr>
<td>{{relation.name}}</td>
<td>{{relation.name_2}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="column">
<h3 class="title is-3">Request cuddles!</h3>
<form action="/request_relation" method="post">
{% csrf-field %}
<div class="field">
<label for="to_id">Person to</label>
<div class="control">
<div class="select">
<select name="to_id" id="to_id">
{% for user in other_users %}
<option value="{{user.id}}">{{user.name}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
<div class="field">
<div class="control">
<input class="button is-link" type="submit" value="Submit">
</div>
</div>
</form>
<table class="table">
<thead>
<tr>
<th>Outgoing requests</th>
</tr>
</thead>
{% for rel_req in user-relation-requests %}
<tr>
<td>{{rel_req.name}}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</section>
{% endif %}
{% if user.admin.enabled %} {% if user.admin.enabled %}
<section class="section"> <section class="section">
<div class="columns"> <div class="columns">

View file

@ -0,0 +1,2 @@
ALTER TABLE users
DROP COLUMN zeusid;

View file

@ -0,0 +1,2 @@
ALTER TABLE users
ADD zeusid INTEGER;

View file

@ -6,4 +6,27 @@ body {
table { table {
margin-top: 2em; margin-top: 2em;
}
.loader-container {
width: 700px;
height: 425px;
display: flex;
justify-content: center;
align-items: center;
}
.loader {
border: 4px solid #f3f3f3; /* Light grey */
border-top: 4px solid #3498db; /* Blue */
border-radius: 50%; /*to make it a circle*/
width: 60px;
height: 60px;
animation: spin 2s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
} }

View file

@ -39,7 +39,7 @@
}, },
{ {
"name": "nodeRadius", "name": "nodeRadius",
"value": 8, "value": 10,
"bind": { "bind": {
"input": "range", "input": "range",
"min": 1, "min": 1,

View file

@ -1,8 +1,8 @@
-- :name create-user! :! :n -- :name create-user! :insert
-- :doc creates a new user record -- :doc creates a new user record
INSERT INTO users INSERT INTO users
(name, gender) (name, gender, zeusid)
VALUES (:name, :gender) VALUES (:name, :gender, :zeusid)
-- :name update-user! :! :n -- :name update-user! :! :n
-- :doc updates an existing user record -- :doc updates an existing user record
@ -10,6 +10,11 @@ VALUES (:name, :gender)
--SET first_name = :first_name, last_name = :last_name, email = :email --SET first_name = :first_name, last_name = :last_name, email = :email
--WHERE id = :id --WHERE id = :id
-- :name get-zeus-user :? :1
-- :doc retrieve a user on their zeuswpi id
SELECT * FROM users
WHERE zeusid = :zeusid
-- :name get-users :? :* -- :name get-users :? :*
-- :doc retrieves a user record given the id -- :doc retrieves a user record given the id
SELECT * FROM users SELECT * FROM users

View file

@ -3,12 +3,8 @@
[cat.db.core :refer [*db*] :as db] [cat.db.core :refer [*db*] :as db]
[compojure.core :refer [defroutes GET POST]] [compojure.core :refer [defroutes GET POST]]
[ring.util.http-response :as response] [ring.util.http-response :as response]
[clojure.java.io :as io]
[struct.core :as st] [struct.core :as st]
[clojure.edn :as edn] [clojure.tools.logging :as log]))
[clojure.tools.logging :as log]
[clojure.data.json :as json]
[oauth.client :as oauth]))
(def user-schema (def user-schema
[[:name st/required st/string] [[:name st/required st/string]
@ -18,6 +14,9 @@
[[:from_id st/required st/integer-str] [[:from_id st/required st/integer-str]
[:to_id st/required st/integer-str]]) [:to_id st/required st/integer-str]])
(def request_relation-schema
[[:to_id st/required st/integer-str]])
(defn home-page [params] (defn home-page [params]
(layout/render "home.html" params)) (layout/render "home.html" params))
@ -32,9 +31,25 @@
(defroutes home-routes (defroutes home-routes
(GET "/" req (GET "/" req
(let [users (get-users) (let [users (get-users)
relations (get-relations)] relations (get-relations)
user (-> (get-in req [:session :user]))
user-relations (when user
(seq (filter (fn [rel]
(or
(= (:name rel) (:username user))
(= (:name_2 rel) (:username user))))
relations)))
other_users (when user
(seq (filter (fn [usr] (not (= (:id usr) (:id user))))
users)))]
(log/info (str "Session: " (:session req))) (log/info (str "Session: " (:session req)))
(home-page {:relations relations :users users :user (get-in req [:session :user])}))) ;(log/info (str "User relations: " user-relations))
;(log/info (str "Other Users: " other_users))
(home-page {:relations relations
:users users
:user user
:user-relations user-relations
:other_users other_users})))
;(GET "/docs" [] ;(GET "/docs" []
; (-> (response/ok (-> "docs/docs.md" io/resource slurp)) ; (-> (response/ok (-> "docs/docs.md" io/resource slurp))
; (response/header "Content-Type" "text/plain; charset=utf-8"))) ; (response/header "Content-Type" "text/plain; charset=utf-8")))
@ -64,6 +79,19 @@
(assoc :group (rand-int 5))))))] (assoc :group (rand-int 5))))))]
(response/ok {:nodes nodes-indexed (response/ok {:nodes nodes-indexed
:links rels-indexed}))) :links rels-indexed})))
(POST "/request_relation" req
(let [data (:params req) [err result] (st/validate data request_relation-schema)]
(log/info "Post to " (:uri req) "\n with data " result)
(if (nil? err)
(do
()
(response/no-content)
;TODO add a request to the db
)
(do
(response/bad-request "Incorrect input")))))
; TODO make bottom 2 protected
(POST "/relations" req (POST "/relations" req
(let [data (:params req) [err result] (st/validate data relation-schema)] (let [data (:params req) [err result] (st/validate data relation-schema)]
(log/info "Post to " (:uri req)) (log/info "Post to " (:uri req))

View file

@ -4,9 +4,10 @@
[clojure.java.io :as io] [clojure.java.io :as io]
[cat.oauth :as oauth] [cat.oauth :as oauth]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[cat.moauth :as mo])) [cat.moauth :as mo]
[cat.db.core :refer [*db*] :as db]))
(def admins #{117 ;flynn (def admins #{31 ;flynn
}) })
(defn set-user! [user session redirect-url] (defn set-user! [user session redirect-url]
@ -34,7 +35,9 @@
found)) found))
(defn oauth-callback (defn oauth-callback
"Handles the callback from adams." "Handles the callback from adams with the access_token
Fetches the user from the database, creating a new one if not found
Sets the user in the session and redirects back to origin \"/\" "
[req_token {:keys [params session]}] [req_token {:keys [params session]}]
; oauth request was denied by user ; oauth request was denied by user
(if (:denied params) (if (:denied params)
@ -46,7 +49,15 @@
(log/info "Fetching user info") (log/info "Fetching user info")
(let [user (mo/get-user-info access_token)] (let [user (mo/get-user-info access_token)]
(log/info "User info: " user) (log/info "User info: " user)
(set-user! user session "/"))))) (let [zeususer (db/get-zeus-user {:zeusid (:id user)})]
(println "Zeus user from db: " zeususer)
(if zeususer
(set-user! zeususer session "/")
(-> {:name (:username user)
:gender nil
:zeusid (:id user)}
(db/create-user!,,,)
(set-user!,,, session "/"))))))))
;(catch [:status 401] _ ;(catch [:status 401] _
; (error-page {:status 401 ; (error-page {:status 401