-
Notifications
You must be signed in to change notification settings - Fork 21
/
clientgroup.go
79 lines (68 loc) · 2.31 KB
/
clientgroup.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
package core
import (
"errors"
"fmt"
"github.com/MG-RAST/AWE/lib/clientGroupAcl"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core/uuid"
"github.com/MG-RAST/AWE/lib/user"
"github.com/MG-RAST/golib/mgo/bson"
"github.com/MG-RAST/golib/uniuri"
"os"
"regexp"
"strconv"
"time"
)
type ClientGroup struct {
Id string `bson:"id" json:"id"`
IP_CIDR string `bson:"ip_cidr" json:"ip_cidr"`
Name string `bson:"name" json:"name"`
Token string `bson:"token" json:"token"`
Acl clientGroupAcl.ClientGroupAcl `bson:"acl" json:"-"`
CreatedOn time.Time `bson:"created_on" json:"created_on"`
Expiration time.Time `bson:"expiration" json:"expiration"`
LastModified time.Time `bson:"last_modified" json:"last_modified"`
}
var (
CGNameRegex = regexp.MustCompile(`^[A-Za-z0-9\_\-\.]+$`)
)
func CreateClientGroup(name string, u *user.User) (cg *ClientGroup, err error) {
q := bson.M{"name": name}
clientgroups := new(ClientGroups)
if count, err := dbFindClientGroups(q, clientgroups); err != nil {
return nil, err
} else if count > 0 {
return nil, errors.New("A clientgroup already exists with this name.")
}
if !CGNameRegex.Match([]byte(name)) {
return nil, errors.New(fmt.Sprintf("Clientgroup name (%s) must contain only alphanumeric characters, underscore, dash or dot.", name))
}
t := time.Now()
cg = new(ClientGroup)
cg.Id = uuid.New()
cg.IP_CIDR = "0.0.0.0/0"
cg.Name = name
cg.Expiration = t.AddDate(10, 0, 0)
cg.Acl.SetOwner(u.Uuid)
cg.Acl.Set(u.Uuid, clientGroupAcl.Rights{"read": true, "write": true, "delete": true, "execute": true})
cg.CreatedOn = t
cg.SetToken()
if err = cg.Save(); err != nil {
err = errors.New("error in cg.Save(), error=" + err.Error())
return
}
return
}
func (cg *ClientGroup) SetToken() {
var host string
if hostname, err := os.Hostname(); err == nil {
host = fmt.Sprintf("%s:%d", hostname, conf.API_PORT)
}
cg.Token = "name=" + cg.Name + "|id=" + cg.Id + "|exp=" + strconv.FormatInt(cg.Expiration.Unix(), 10) + "|server=" + host + "|sig=" + uniuri.NewLen(256)
return
}
func (cg *ClientGroup) Save() (err error) {
cg.LastModified = time.Now()
err = dbUpsert(cg)
return
}