aboutsummaryrefslogtreecommitdiffstats
path: root/scrumli.lisp
blob: ef2b66ab1b91f17f531534d69284ea1544e3fa42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(in-package #:scrumli)

(defvar *scrumli-host* "http://localhost:8080"
  "The host currently running Scrumli. Used by Mozilla Persona.")

(defvar *scrumelo-bootstrap-css-location*
  "http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css"
  "The location of the twitter bootstrap CSS file.")

(defvar *scrumelo-bootstrap-js-location*
  "http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"
  "The location of the twitter bootstrap JS file.")

(defvar *scrumelo-font-awesome-css-location*
  "http://netdna.bootstrapcdn.com/font-awesome/3.1.1/css/font-awesome.min.css"
  "The location of the font awesome CSS file.")

(defvar *scrumelo-jquery-js-location*
  "http://code.jquery.com/jquery-2.0.0.min.js"
  "The location of the jQuery JS file.")

(defvar *scrumelo-react-js-location*
  "http://cdnjs.cloudflare.com/ajax/libs/react/0.3.2/react.min.js"
  "The location of the React JS file.")

(defvar *scrumelo-jsxtransformer-js-location*
  "http://cdnjs.cloudflare.com/ajax/libs/react/0.3.2/JSXTransformer.js"
  "The location of the JSX Transformer JS file.")

(defun logged-in-p ()
  (hunchentoot:session-value :username))

(define-route main ("")
  (if (logged-in-p)
      (<:html
       (<:head (<:title "Hello!")
               (<:link :href *scrumelo-bootstrap-css-location*
                       :rel "stylesheet" :type "text/css")
               (<:link :href *scrumelo-font-awesome-css-location*
                       :rel "stylesheet" :type "text/css")
               (<:script :type "text/javascript"
                         :src *scrumelo-bootstrap-js-location*)
               (<:script :type "text/javascript"
                         :src *scrumelo-jquery-js-location*)
               (<:script :type "text/javascript"
                         :src *scrumelo-react-js-location*)
               (<:script :type "text/javascript"
                         :src *scrumelo-jsxtransformer-js-location*))
       (<:body
        (<:a :href "/logout" "Logout") " "
        (hunchentoot:session-value :username)
        (<:div :class "container"
               (<:h1 "Backlog")
               (<:div :id "content")
               (<:script :type "text/jsx" :src "js/main.js"))))
      (redirect 'login-page)))

(define-route react-ui ("js/main.js"
                        :content-type "application/ecmascript")
  (merge-pathnames "js/main.js" *static-directory*))

(define-route login-js ("js/login.js"
                        :content-type "application/ecmascript")
  (merge-pathnames "js/login.js" *static-directory*))

(define-route stories-json ("stories" :content-type "text/json")
  (if (logged-in-p)
      (with-output-to-string (out)
        (encode-json (get-all-stories) out))
      403))

(define-route stories-new ("stories/new" :method :post)
  (if (logged-in-p)
      (let ((role (hunchentoot:post-parameter "role"))
            (necessity (hunchentoot:post-parameter "necessity"))
            (title (hunchentoot:post-parameter "headline"))
            (content (hunchentoot:post-parameter "content")))
        (post-story role necessity title content
                    (hunchentoot:session-value :username))
        200)
      403))

(define-route stories-state ("stories/state" :method :post)
  (if (logged-in-p)
      (let* ((id (hunchentoot:post-parameter "id"))
             (current-state (story-get-state id)))
        (story-set-state id (ecase (intern current-state :scrumli)
                              (todo "DOING")
                              (doing "DONE")
                              (done "TODO")))
        200)
      403))

(define-route stories-priority ("stories/:dir" :method :post)
  (if (logged-in-p)
      (let* ((id (hunchentoot:post-parameter "id")))
        (story-change-priority id (intern (string-upcase dir) :keyword))
        200)
      403))

(define-route login-page ("login")
  (if (not (logged-in-p))
      (<:html :lang "en"
              (<:head (<:meta :charset "utf-8")
                      (<:title "Login")
                      (<:script :src "https://login.persona.org/include.js")
                      (<:script :src "/js/login.js"))
              (<:body
               (<:form :id "login-form" :method "POST" :action ""
                       (<:input :id "assertion-field" :type "hidden"
                                :name "assertion" :value ""))
               (<:p (<:a :href "javascript:login()" "Login"))))
      (redirect 'main)))

(define-route logout-page ("logout")
  (if (logged-in-p)
      (progn
        (setf (hunchentoot:session-value :username) nil)))
  (redirect 'login-page))

(defun json-to-string (obj)
  (with-output-to-string (out)
    (encode-json obj out)))

(defun verify-credentials (audience assertion)
  (let ((response
         (http-request "https://verifier.login.persona.org/verify"
                       :method :post :content-type "application/json"
                       :content (json-to-string
                                 `(("assertion" . ,assertion)
                                   ("audience" . ,audience)))
                       :want-stream t)))
    (decode-json response)))

(define-route login-page/post ("login" :method :post)
  (let ((result (verify-credentials
                 *scrumli-host*
                 (hunchentoot:post-parameter "assertion"))))
    (if (equal (cdr (assoc :status result)) "okay")
        (progn
          (hunchentoot:start-session)
          (setf (hunchentoot:session-value :username)
                (cdr (assoc :email result)))
          (redirect 'main))
        403)))

(define-route scrumelo-story ("stories/:id")
  (if (logged-in-p)
      (with-output-to-string (out)
        (encode-json (get-story id) out))
      403))