1
- package main
1
+ package staticbackend
2
2
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
7
"net/http"
8
8
"net/url"
9
- "regexp"
10
9
"staticbackend/internal"
11
10
"staticbackend/middleware"
12
11
"strconv"
13
12
"strings"
14
13
"time"
15
14
15
+ "staticbackend/cache"
16
+
16
17
"go.mongodb.org/mongo-driver/bson"
17
18
"go.mongodb.org/mongo-driver/bson/primitive"
18
19
"go.mongodb.org/mongo-driver/mongo"
@@ -21,7 +22,7 @@ import (
21
22
22
23
type Database struct {
23
24
client * mongo.Client
24
- cache * Cache
25
+ cache * cache. Cache
25
26
}
26
27
27
28
func (database * Database ) dbreq (w http.ResponseWriter , r * http.Request ) {
@@ -91,7 +92,7 @@ func (database *Database) add(w http.ResponseWriter, r *http.Request) {
91
92
doc ["id" ] = doc [internal .FieldID ]
92
93
delete (doc , internal .FieldID )
93
94
94
- database .cache .publishDocument ("db-" + col , MsgTypeDBCreated , doc )
95
+ database .cache .PublishDocument ("db-" + col , internal . MsgTypeDBCreated , doc )
95
96
96
97
respond (w , http .StatusCreated , doc )
97
98
}
@@ -131,10 +132,10 @@ func (database *Database) list(w http.ResponseWriter, r *http.Request) {
131
132
132
133
// if they're not root
133
134
if ! strings .HasPrefix (col , "pub_" ) && auth .Role < 100 {
134
- switch readPermission (col ) {
135
- case permGroup :
135
+ switch internal . ReadPermission (col ) {
136
+ case internal . PermGroup :
136
137
filter = bson.M {"accountId" : auth .AccountID }
137
- case permOwner :
138
+ case internal . PermOwner :
138
139
filter = bson.M {internal .FieldAccountID : auth .AccountID , internal .FieldOwnerID : auth .UserID }
139
140
}
140
141
}
@@ -217,10 +218,10 @@ func (database *Database) get(w http.ResponseWriter, r *http.Request) {
217
218
218
219
// if they're not root and repo is not public
219
220
if ! strings .HasPrefix (col , "pub_" ) && auth .Role < 100 {
220
- switch readPermission (col ) {
221
- case permGroup :
221
+ switch internal . ReadPermission (col ) {
222
+ case internal . PermGroup :
222
223
filter [internal .FieldAccountID ] = auth .AccountID
223
- case permOwner :
224
+ case internal . PermOwner :
224
225
filter [internal .FieldAccountID ] = auth .AccountID
225
226
filter [internal .FieldOwnerID ] = auth .UserID
226
227
}
@@ -321,10 +322,10 @@ func (database *Database) query(w http.ResponseWriter, r *http.Request) {
321
322
322
323
// either not a public repo or not root
323
324
if strings .HasPrefix (col , "pub_" ) == false && auth .Role < 100 {
324
- switch readPermission (col ) {
325
- case permGroup :
325
+ switch internal . ReadPermission (col ) {
326
+ case internal . PermGroup :
326
327
filter [internal .FieldAccountID ] = auth .AccountID
327
- case permOwner :
328
+ case internal . PermOwner :
328
329
filter [internal .FieldAccountID ] = auth .AccountID
329
330
filter [internal .FieldOwnerID ] = auth .UserID
330
331
}
@@ -439,10 +440,10 @@ func (database *Database) update(w http.ResponseWriter, r *http.Request) {
439
440
440
441
// if they are not "root", we use permission
441
442
if auth .Role < 100 {
442
- switch writePermission (col ) {
443
- case permGroup :
443
+ switch internal . WritePermission (col ) {
444
+ case internal . PermGroup :
444
445
filter [internal .FieldAccountID ] = auth .AccountID
445
- case permOwner :
446
+ case internal . PermOwner :
446
447
filter [internal .FieldAccountID ] = auth .AccountID
447
448
filter [internal .FieldOwnerID ] = auth .UserID
448
449
}
@@ -475,7 +476,7 @@ func (database *Database) update(w http.ResponseWriter, r *http.Request) {
475
476
result ["id" ] = result ["_id" ]
476
477
delete (result , internal .FieldID )
477
478
478
- database .cache .publishDocument ("db-" + col , MsgTypeDBUpdated , result )
479
+ database .cache .PublishDocument ("db-" + col , internal . MsgTypeDBUpdated , result )
479
480
480
481
respond (w , http .StatusOK , result )
481
482
}
@@ -505,10 +506,10 @@ func (database *Database) del(w http.ResponseWriter, r *http.Request) {
505
506
506
507
// if they're not root
507
508
if auth .Role < 100 {
508
- switch writePermission (col ) {
509
- case permGroup :
509
+ switch internal . WritePermission (col ) {
510
+ case internal . PermGroup :
510
511
filter [internal .FieldAccountID ] = auth .AccountID
511
- case permOwner :
512
+ case internal . PermOwner :
512
513
filter [internal .FieldAccountID ] = auth .AccountID
513
514
filter [internal .FieldOwnerID ] = auth .UserID
514
515
@@ -521,7 +522,7 @@ func (database *Database) del(w http.ResponseWriter, r *http.Request) {
521
522
return
522
523
}
523
524
524
- database .cache .publishDocument ("db-" + col , MsgTypeDBDeleted , id )
525
+ database .cache .PublishDocument ("db-" + col , internal . MsgTypeDBDeleted , id )
525
526
respond (w , http .StatusOK , res .DeletedCount )
526
527
}
527
528
@@ -578,77 +579,3 @@ func getPagination(u *url.URL) (page int64, size int64) {
578
579
579
580
return
580
581
}
581
-
582
- type permissionLevel int
583
-
584
- const (
585
- permOwner permissionLevel = iota
586
- permGroup
587
- permEveryone
588
- )
589
-
590
- func getPermission (col string ) (owner string , group string , everyone string ) {
591
- // default permission
592
- owner , group , everyone = "7" , "4" , "0"
593
-
594
- re := regexp .MustCompile (`_\d\d\d_$` )
595
- if re .MatchString (col ) == false {
596
- return
597
- }
598
-
599
- results := re .FindAllString (col , - 1 )
600
- if len (results ) != 1 {
601
- return
602
- }
603
-
604
- perm := strings .Replace (results [0 ], "_" , "" , - 1 )
605
-
606
- if len (perm ) != 3 {
607
- return
608
- }
609
-
610
- owner = string (perm [0 ])
611
- group = string (perm [1 ])
612
- everyone = string (perm [2 ])
613
- return
614
- }
615
-
616
- func writePermission (col string ) permissionLevel {
617
- _ , g , e := getPermission (col )
618
-
619
- if canWrite (e ) {
620
- return permEveryone
621
- }
622
- if canWrite (g ) {
623
- return permGroup
624
- }
625
- return permOwner
626
- }
627
-
628
- func readPermission (col string ) permissionLevel {
629
- _ , g , e := getPermission (col )
630
-
631
- if canRead (e ) {
632
- return permEveryone
633
- }
634
- if canRead (g ) {
635
- return permGroup
636
- }
637
- return permOwner
638
- }
639
-
640
- func canWrite (s string ) bool {
641
- i , err := strconv .Atoi (s )
642
- if err != nil {
643
- return false
644
- }
645
- return uint8 (i )& uint8 (2 ) != 0
646
- }
647
-
648
- func canRead (s string ) bool {
649
- i , err := strconv .Atoi (s )
650
- if err != nil {
651
- fmt .Println (err )
652
- }
653
- return uint8 (i )& uint8 (4 ) != 0
654
- }
0 commit comments