 
 (defn- extract-permissions
   [params]
-  (throw (RuntimeException. "Not implemented")))
+  (->> grants/resource-types
+       (mapcat (fn [resource-type]
+                 (map #(hash-map :resource-type resource-type
+                                 :action %) grants/actions)))
+       (map #(assoc % :key (keyword (permission-key (:resource-type %)
+                                                    (:action %)))))
+       (map #(assoc % :value ((:key %) params)))
+       (filter #(= "1" (:value %)))
+       (reduce (fn [m {:keys [resource-type action]}]
+                 (update-in m
+                           [resource-type]
+                           #((fnil conj []) % action)))
+               {})))
+
+(defn- find-or-create-user
+  [email]
+  (or (users/find-by-email (env :db) email)
+      (throw (RuntimeException. "create user not implemented"))))
 
 (defn create
   [{params :params}]
   (let [grant (-> params
-                  (select-keys [:entity-id :user-id])
-                  (assoc extract-permissions params)
+                  (select-keys [:entity-id])
+                  (assoc :permissions (extract-permissions params)
+                         :user-id (:id (find-or-create-user (:email params))))
                   (tag-resource :grant)
                   (authorize :create))
         result (grants/create (env :db) grant)]

            [clj-money.validation :as validation]
            [clj-money.inflection :refer [humanize]]
            [clj-money.models.entities :as entities]
            [clj-money.models.grants :as grants])
  (:use [clj-money.web.shared :refer :all]))

(defn- grant-row
  [grant]
  [:tr
   [:td (:email grant)]
   [:td (:email grant)]])

(defn index
  [{{entity :entity} :params}]
  (with-layout (format "User grants for entity %s" (:name entity)) {}
    ; TODO apply scope
    (let [grants (grants/search (env :db) {:entity-id (:id entity)})]
      [:div.row
       [:div.col-md-6
        [:table.table.table-striped

            [:label
             [:input {:type "checkbox"
                      :name (permission-key resource-type action)
                      :checked (grants/has-permission grant resource-type action)
                      :value 1}]
             (humanize action)]])
         grants/actions)]])

