-
Notifications
You must be signed in to change notification settings - Fork 0
/
Post.go
69 lines (65 loc) · 2.08 KB
/
Post.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
package pembimbing
import (
"crypto/rand"
"encoding/hex"
"fmt"
"strings"
"github.com/badoux/checkmail"
intermoni "github.com/intern-monitoring/backend-intermoni"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"golang.org/x/crypto/argon2"
)
func AddPembimbingByAdmin(db *mongo.Database, insertedDoc intermoni.Pembimbing) error {
objectId := primitive.NewObjectID()
if insertedDoc.NamaLengkap == "" || insertedDoc.NIK == "" || insertedDoc.Prodi == "" || insertedDoc.Akun.Email == "" || insertedDoc.Akun.Password == "" {
return fmt.Errorf("mohon untuk melengkapi data")
}
if err := checkmail.ValidateFormat(insertedDoc.Akun.Email); err != nil {
return fmt.Errorf("email tidak valid")
}
userExists, _ := intermoni.GetUserFromEmail(insertedDoc.Akun.Email, db)
if insertedDoc.Akun.Email == userExists.Email {
return fmt.Errorf("email sudah terdaftar")
}
if insertedDoc.Akun.Confirmpassword != insertedDoc.Akun.Password {
return fmt.Errorf("konfirmasi password salah")
}
if strings.Contains(insertedDoc.Akun.Password, " ") {
return fmt.Errorf("password tidak boleh mengandung spasi")
}
if len(insertedDoc.Akun.Password) < 8 {
return fmt.Errorf("password terlalu pendek")
}
salt := make([]byte, 16)
_, err := rand.Read(salt)
if err != nil {
return fmt.Errorf("kesalahan server : salt")
}
hashedPassword := argon2.IDKey([]byte(insertedDoc.Akun.Password), salt, 1, 64*1024, 4, 32)
user := bson.M{
"_id": objectId,
"email": insertedDoc.Akun.Email,
"password": hex.EncodeToString(hashedPassword),
"salt": hex.EncodeToString(salt),
"role": "pembimbing",
}
pembimbing := bson.M{
"namalengkap": insertedDoc.NamaLengkap,
"nik": insertedDoc.NIK,
"akun": intermoni.User{
ID: objectId,
},
"prodi": insertedDoc.Prodi,
}
_, err = intermoni.InsertOneDoc(db, "user", user)
if err != nil {
return fmt.Errorf("kesalahan server")
}
_, err = intermoni.InsertOneDoc(db, "pembimbing", pembimbing)
if err != nil {
return fmt.Errorf("kesalahan server")
}
return nil
}