From ef252de79d706583baa7563502cccd558c9f5383 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Wed, 20 Dec 2017 09:00:45 -0800 Subject: [PATCH] implementing permissions to companies --- resources/public/index.html | 1 - src/clj/auto_ap/handler.clj | 19 ++++++++++++------- src/cljs/auto_ap/db.cljs | 7 +------ src/cljs/auto_ap/subs.cljs | 4 +++- test/auto_ap/handler_test.clj | 14 -------------- 5 files changed, 16 insertions(+), 29 deletions(-) delete mode 100644 test/auto_ap/handler_test.clj diff --git a/resources/public/index.html b/resources/public/index.html index ace4a0d0..722c716e 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -7,7 +7,6 @@ Auto AP - diff --git a/src/clj/auto_ap/handler.clj b/src/clj/auto_ap/handler.clj index b0dc3fa7..a92af5c2 100644 --- a/src/clj/auto_ap/handler.clj +++ b/src/clj/auto_ap/handler.clj @@ -4,6 +4,7 @@ [clojure.java.io :as io] [clojure.string :as str] [auto-ap.db.invoices :as invoices] + [auto-ap.db.users :as users] [auto-ap.parse :as parse] [ring.middleware.multipart-params :as mp] [ring.util.response :as response] @@ -42,7 +43,7 @@ (response/resource-response "index.html" {:root "public"})) (GET "/api/oauth" {{:strs [code]} :query-params} (try - (let [token (-> "https://accounts.google.com/o/oauth2/token" + (let [auth (-> "https://accounts.google.com/o/oauth2/token" (http/post {:form-params {"client_id" google-client-id "client_secret" google-client-secret @@ -50,18 +51,22 @@ "redirect_uri" "http://localhost:3449/api/oauth" "grant_type" "authorization_code"} :as :json}) - :body - :access_token) + :body) + token (:access_token auth) profile (-> (http/get "https://www.googleapis.com/oauth2/v1/userinfo" {:headers {"Authorization" (str "Bearer " token)} :as :json}) :body - :name) - ] - (if token + (doto println) + ) + user (users/find-or-insert! {:provider "google" + :provider_id (:id profile)})] + + (if (and token user) {:status 301 :headers {"Location" (str "/?jwt=" (jwt/sign {:user "test" :exp (time/plus (time/now) (time/days 7)) - :name profile} + :companies (:companies user) + :name (:name profile)} jwt-secret {:alg :hs512}))}} {:status 401 diff --git a/src/cljs/auto_ap/db.cljs b/src/cljs/auto_ap/db.cljs index 98afc34f..1c76bd9d 100644 --- a/src/cljs/auto_ap/db.cljs +++ b/src/cljs/auto_ap/db.cljs @@ -3,12 +3,7 @@ (def default-db {:user (.getItem js/localStorage "jwt") :company {:name "Campbell Brewing Company"} - :companies [{:name "Campbell Brewing Company" - :matches ["campbell brewing company" "campbell brewery company" "campbell brewing"]} - {:name "Brown Chicken Brown Cow" - :matches ["brown chicken brown cow"]} - {:name "Naschmarkt Restaurant" - :matches ["naschmarkt" "naschmarkt restaurant"]}] + :companies [] :invoices {:pending #{} :unpaid #{}} :status {:loading false} diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index 6c672144..be127340 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -11,7 +11,9 @@ (re-frame/reg-sub ::companies (fn [db] - (:companies db))) + (when (:user db) + (let [{:strs [companies] :as x} (js->clj (.parse js/JSON (base64/decodeString (second (str/split (:user db) #"\.")))))] + (map (fn [c] {:name c}) companies))))) (re-frame/reg-sub ::menu diff --git a/test/auto_ap/handler_test.clj b/test/auto_ap/handler_test.clj deleted file mode 100644 index 906bf302..00000000 --- a/test/auto_ap/handler_test.clj +++ /dev/null @@ -1,14 +0,0 @@ -(ns auto-ap.handler-test - (:require [clojure.test :refer :all] - [ring.mock.request :as mock] - [auto-ap.handler :refer :all])) - -(deftest test-app - (testing "main route" - (let [response (app (mock/request :get "/"))] - (is (= (:status response) 200)) - (is (= (:body response) "Hello World")))) - - (testing "not-found route" - (let [response (app (mock/request :get "/invalid"))] - (is (= (:status response) 404)))))