summaryrefslogtreecommitdiff
path: root/system/db/user.go
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-10-22 01:39:19 -0700
committerSteve Manuel <nilslice@gmail.com>2016-10-22 01:39:19 -0700
commit735f99addd98f232ae126cac11d93a9dbbcc3c79 (patch)
treef8d3804f326fda466ac9ac91782228909ef05fd6 /system/db/user.go
parent936b8aef13b164ce74f9ec11bf1385275d282df8 (diff)
adding initial support to edit and add admin users
Diffstat (limited to 'system/db/user.go')
-rw-r--r--system/db/user.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/system/db/user.go b/system/db/user.go
index a3a0be3..3b09dbe 100644
--- a/system/db/user.go
+++ b/system/db/user.go
@@ -4,10 +4,13 @@ import (
"bytes"
"encoding/json"
"errors"
+ "fmt"
+ "net/http"
"github.com/bosssauce/ponzu/system/admin/user"
"github.com/boltdb/bolt"
+ "github.com/nilslice/jwt"
)
// ErrUserExists is used for the db to report to admin user of existing user
@@ -72,3 +75,50 @@ func User(email string) ([]byte, error) {
return val.Bytes(), nil
}
+
+// UserAll returns all users from the db
+func UserAll() ([][]byte, error) {
+ var users [][]byte
+ err := store.View(func(tx *bolt.Tx) error {
+ b := tx.Bucket([]byte("_users"))
+ err := b.ForEach(func(k, v []byte) error {
+ users = append(users, v)
+ return nil
+ })
+ if err != nil {
+ return err
+ }
+
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return users, nil
+}
+
+// CurrentUser extracts the user from the request data and returns the current user from the db
+func CurrentUser(req *http.Request) ([]byte, error) {
+ if !user.IsValid(req) {
+ return nil, fmt.Errorf("Error. Invalid User.")
+ }
+
+ token, err := req.Cookie("_token")
+ if err != nil {
+ return nil, err
+ }
+
+ claims := jwt.GetClaims(token.Value)
+ email, ok := claims["user"]
+ if !ok {
+ return nil, fmt.Errorf("Error. No user data found in request token.")
+ }
+
+ usr, err := User(email.(string))
+ if err != nil {
+ return nil, err
+ }
+
+ return usr, nil
+}