/
user_management.go
107 lines (88 loc) · 3.23 KB
/
user_management.go
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
package auth
import (
"context"
"net/http"
"os"
"time"
"github.com/aevanra/mictakepic/pkg/filesharing"
"github.com/aevanra/mictakepic/pkg/objects"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/crypto/bcrypt"
)
func CreateNewUser(c *gin.Context) {
newUsername := c.PostForm("username")
newPassword := c.PostForm("password")
newDatashare := c.PostForm("datashare")
isAdmin := c.PostForm("isAdmin")
availableShares := filesharing.ListDataShareNames()
accessShares := make([]string, 0)
for _, share := range availableShares {
access := c.PostForm("access" + share)
if access == "on" {
accessShares = append(accessShares, share)
}
}
if !filesharing.ValidateUserDatashare(obj.User{DefaultDataShare: newDatashare}) {
c.HTML(http.StatusBadRequest, "createUser.html",
gin.H{"message": "Provided DataShare does not exist.", "Shares": availableShares})
return
}
if newUsername == "" || newPassword == "" || newDatashare == "" {
c.HTML(http.StatusBadRequest, "createUser.html",
gin.H{"message": "New Users must have a username, a password, and a default datashare", "Shares": availableShares})
return
}
err := addUser(newUsername, newPassword, newDatashare, accessShares, isAdmin)
if err != nil {
c.HTML(http.StatusNotFound, "createUser.html",
gin.H{"message": "Something went wrong -- user was not created or updated", "Shares": availableShares})
return
}
c.HTML(http.StatusOK, "createUser.html", gin.H{"message": "New user " + newUsername + " has been created or updated", "Shares": availableShares})
}
func addUser(username string, password string, datashare string, accessShares []string, isAdmin string) (err error) {
//Load env file
err = godotenv.Load()
if err != nil {
return err
}
admin := false
if isAdmin == "on" {
admin = true
}
// Creating Mongo Connection
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI")))
if err != nil {
return err
}
userCollection := client.Database(os.Getenv("DB_NAME")).Collection(os.Getenv("USERS_COLLECTION"))
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
filter := bson.D{{Key: "Username", Value: username}}
update := bson.D{{Key: "$set",
Value: bson.D{
{Key: "ID", Value: uuid.New()},
{Key: "Username", Value: username},
{Key: "PassHash", Value: hash},
{Key: "DefaultDataShare", Value: datashare},
{Key: "AllDataShares", Value: accessShares},
{Key: "Admin", Value: admin},
}}}
opts := options.Update().SetUpsert(true)
_, err = userCollection.UpdateOne(ctx, filter, update, opts)
if err != nil {
return err
}
return nil
}