Luminus Database Access

308 views Asked by At

I have followed most of the tutorials on the luminusweb.net website, setting up a database system using the +h2 new app. What I have currently mirrors the guestbook setup like the tutorial shows. I am now wondering how I can access specific entries into the migrations up table. More specifically, I am trying to have restricted access to webpages (a login system) based on the entries inside of the table.

1

There are 1 answers

0
laurentmeyer On

The migration file purpose is to create the tables in your database. To access those tables you will have to write queries in the file located here: your_project > resources > sql > queries.sql

Here you should write queries, there are a few examples on the Luminus website. When you see parameters with semicolumns, it means that you have to pass a map with those parameters when you call these queries in your program. Ex: if you have this query:

-- name: accounts_for_user
-- retrieve all accounts a user has access to and the associated rights
SELECT account_name, admin
FROM accounts_users
WHERE email = :email;

The call:

(db/accounts_for_user {:email "[email protected]"})

will return a lazy sequence like this:

[{"account_name":"account1","admin":false},
{"account_name":"account2","admin":true},
{"account_name":"account2","admin":true}]

Then if you want to restrict the access to a specific page based on what's in your database, there are a few options. The Buddy auth library offers a few options, the easiest to use is the session one. First, when a user enters a correct password, you inject their identifier in :session :identity in any request. For instance

(-> (redirect "/accounts-list")
    (assoc :session {:identity "[email protected]"}))

The identity parameter will be in every request until the session dies (30 minutes by default) or you overwrite it. In your pages, you can test buddy.auth/authenticated? on the requests, and redirect to an error page or whatever you like if it returns false. I am currently writing a tutorial for webapps using Luminus, I'll update this answer when it's available.