-
Notifications
You must be signed in to change notification settings - Fork 0
/
connexion.go
109 lines (98 loc) · 2.39 KB
/
connexion.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
108
109
package commands
import (
"encoding/json"
"errors"
"os"
"github.com/lefuturiste/jobatator/pkg/store"
)
// Ping -
func Ping(cmd CmdInterface) {
ReturnString(cmd, "PONG")
}
// Quit -
func Quit(cmd CmdInterface) {
NewLine(cmd)
cmd.Conn.Close()
}
// StopServer - Will exit the server process, only work if TEST_MODE env var or config key 'test_mode' is set to true
// Warning: this feature is really dangerous and open serious security risks. Use it with cares.
func StopServer(cmd CmdInterface) {
if store.Options.TestMode {
NewLine(cmd)
os.Exit(0)
} else {
ReturnError(cmd, "forbidden")
}
}
// UseGroupUniversal -
func UseGroupUniversal(groupRaw string, user store.User) (store.Group, error) {
var group store.Group
for _, value := range store.Options.Groups {
if value.Slug == groupRaw {
group = value
}
}
if group.Slug == "" {
return group, errors.New("unknown-group")
}
var isAllowed bool = false
for _, value := range user.Groups {
if value == group.Slug || value == "*" {
isAllowed = true
}
}
if !isAllowed {
return group, errors.New("forbidden-group")
}
user.CurrentGroup = group
store.UpdateSession(user)
return group, nil
}
// UseGroup - will switch the session on a specific group
func UseGroup(cmd CmdInterface) {
_, err := UseGroupUniversal(cmd.Parts[1], cmd.User)
if err == nil {
ReturnString(cmd, "OK")
} else {
ReturnError(cmd, err.Error())
}
}
// Auth -
func Auth(cmd CmdInterface) {
var user store.User
for _, val := range store.Options.Users {
if val.Username == cmd.Parts[1] && val.Password == cmd.Parts[2] {
user = val
}
}
if user.Username == "" {
ReturnError(cmd, "invalid-creds")
return
}
// we update the session
cmd.User.Username = user.Username
cmd.User.Password = user.Password
cmd.User.Groups = user.Groups
store.UpdateSession(cmd.User)
ReturnString(cmd, "Welcome!")
}
// SessionOutput - Represent the current session
type SessionOutput struct {
Username string
Addr string
CurrentGroup string
IsWorker bool
Groups []string
}
// CurrentSession - Get current session meta data
func CurrentSession(cmd CmdInterface) {
user := store.FindSession(cmd.Conn)
jsonStr, _ := json.Marshal(SessionOutput{
Username: user.Username,
Addr: user.Addr,
CurrentGroup: user.CurrentGroup.Slug,
IsWorker: user.Status != "",
Groups: user.Groups,
})
ReturnString(cmd, string(jsonStr))
}