Browse Source

Use boltdb to store lowry status

merge-requests/4/head
meskio 3 years ago
parent
commit
19c93d6648
Signed by: meskio GPG Key ID: 52B8F5AC97A2DA86
  1. 1
      .gitignore
  2. 17
      db/askSindominante.go
  3. 68
      db/db.go
  4. 10
      main.go
  5. 5
      server/server.go
  6. 14
      server/user.go

1
.gitignore

@ -1,4 +1,5 @@
lowry
bolt.db
node_modules
dist

17
db/askSindominante.go

@ -0,0 +1,17 @@
package db
var (
askSindominanteBucket = []byte("ask-sindominante")
)
// HasBeingAskedForSindominante checks if the user has being already asked for sindominante
func (db *DB) HasBeingAskedForSindominante(user string) bool {
var answer string
err := db.get(askSindominanteBucket, user, &answer)
return err == nil
}
// AnswerForSindominante register the answer for sindominante for user
func (db *DB) AnswerForSindominante(user string, answer string) error {
return db.put(askSindominanteBucket, user, answer)
}

68
db/db.go

@ -0,0 +1,68 @@
package db
import (
"encoding/json"
"go.etcd.io/bbolt"
)
// DB to store the status of lowry
type DB struct {
bolt *bbolt.DB
}
// Init database storing it in path
func Init(path string) (*DB, error) {
bolt, err := bbolt.Open(path, 0660, nil)
if err != nil {
return nil, err
}
err = bolt.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(askSindominanteBucket)
return err
})
if err != nil {
bolt.Close()
return nil, err
}
return &DB{bolt}, nil
}
// Close the database
func (db *DB) Close() {
db.bolt.Close()
}
func (db *DB) put(bucket []byte, key string, value interface{}) error {
encodedValue, err := json.Marshal(value)
if err != nil {
return err
}
return db.bolt.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(bucket)
return b.Put([]byte(key), encodedValue)
})
}
func (db *DB) get(bucket []byte, key string, value interface{}) error {
var encodedValue []byte
db.bolt.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(bucket)
encodedValue = b.Get([]byte(key))
return nil
})
if encodedValue == nil {
return notFoundError{}
}
return json.Unmarshal(encodedValue, value)
}
type notFoundError struct{}
func (e notFoundError) Error() string {
return "Not found"
}

10
main.go

@ -7,6 +7,7 @@ import (
"sort"
"strings"
"0xacab.org/sindominio/lowry/db"
"0xacab.org/sindominio/lowry/ldap"
"0xacab.org/sindominio/lowry/server"
"github.com/namsral/flag"
@ -19,6 +20,7 @@ func main() {
ldappass = flag.String("ldappass", "", "Password of the LDAP `admin' user")
homepath = flag.String("homepath", "/home/", "Path to the user homes")
httpaddr = flag.String("httpaddr", ":8080", "Web server address and port")
dbpath = flag.String("dbpath", "bolt.db", "The path to store the lowry status database")
ro = flag.Bool("ro", false, "Read-Only mode")
askRolePath = flag.String("ask-role-list", "", "List of usernames to ask if want to be sindominantes")
)
@ -37,12 +39,18 @@ func main() {
log.Fatal(err)
}
db, err := db.Init(*dbpath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
usersAskRole := []string{}
if *askRolePath != "" {
usersAskRole = readUserList(*askRolePath)
}
log.Fatal(server.Serve(*httpaddr, &l, usersAskRole))
log.Fatal(server.Serve(*httpaddr, &l, db, usersAskRole))
}
func readUserList(listPath string) []string {

5
server/server.go

@ -4,21 +4,24 @@ import (
"html/template"
"net/http"
"0xacab.org/sindominio/lowry/db"
"0xacab.org/sindominio/lowry/ldap"
"github.com/gorilla/mux"
)
type server struct {
ldap *ldap.Ldap
db *db.DB
sess *sessionStore
tmpl *template.Template
usersAskRole []string
}
// Serve lowry web site
func Serve(addr string, l *ldap.Ldap, usersAskRole []string) error {
func Serve(addr string, l *ldap.Ldap, db *db.DB, usersAskRole []string) error {
var s server
s.ldap = l
s.db = db
s.sess = initSessionStore()
s.tmpl = initTemplate()
s.usersAskRole = usersAskRole

14
server/user.go

@ -16,8 +16,7 @@ func (s *server) homeHandler(w http.ResponseWriter, r *http.Request) {
return
}
askSindominante := (response.Role != ldap.Sindominante &&
s.userInAskList(response.User))
askSindominante := s.needsToAskForSindominante(response.User)
needPasswordChange := !s.ldap.IsUserPassUptodate(response.User)
data := struct {
AskSindominante bool
@ -89,12 +88,19 @@ func (s *server) askSindominante(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("An error has ocurred setting the role: %v", err)
} else {
idx := sort.SearchStrings(s.usersAskRole, response.User)
s.usersAskRole = append(s.usersAskRole[:idx], s.usersAskRole[idx+1:]...)
s.db.AnswerForSindominante(response.User, role)
}
http.Redirect(w, r, "/", http.StatusFound)
}
func (s *server) needsToAskForSindominante(user string) bool {
if !s.userInAskList(user) {
return false
}
return !s.db.HasBeingAskedForSindominante(user)
}
func (s *server) userInAskList(user string) bool {
return sort.SearchStrings(s.usersAskRole, user) != len(s.usersAskRole)
}

Loading…
Cancel
Save