forked from admpub/nging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_login.go
82 lines (75 loc) · 1.98 KB
/
manager_login.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
package database
import (
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/library/dbmanager"
"github.com/admpub/nging/application/library/dbmanager/driver"
"github.com/admpub/nging/application/model"
)
func addAuth(ctx echo.Context, auth *driver.DbAuth) {
accounts := getAccounts(ctx)
accounts.Add(auth)
ctx.Session().Set(`dbAccounts`, accounts)
}
func getAccounts(ctx echo.Context) driver.AuthAccounts {
if accounts, ok := ctx.Internal().Get(`dbAccounts`).(*driver.AuthAccounts); ok {
return *accounts
}
accounts, ok := ctx.Session().Get(`dbAccounts`).(driver.AuthAccounts)
if !ok {
accounts = driver.AuthAccounts{}
} else {
ctx.Internal().Set(`dbAccounts`, &accounts)
}
return accounts
}
func deleteAuth(ctx echo.Context, auth *driver.DbAuth) {
accounts := getAccounts(ctx)
accounts.Delete(auth)
ctx.Session().Set(`dbAccounts`, accounts)
}
func clearAuth(ctx echo.Context) {
accounts := getAccounts(ctx)
for key := range accounts {
accounts.DeleteByKey(key)
}
ctx.Session().Delete(`dbAccounts`)
}
func getLoginInfo(mgr dbmanager.Manager, accountID uint, m *model.DbAccount, user *dbschema.NgingUser) (err error) {
ctx := mgr.Context()
if !ctx.IsPost() {
return nil
}
auth := mgr.Account()
data := &driver.DbAuth{AccountID: accountID, AccountTitle: m.Title}
ctx.Bind(data)
if len(data.Username) == 0 {
data.Username = `root`
}
if len(data.Host) == 0 {
data.Host = `127.0.0.1`
}
auth.CopyFrom(data)
if ctx.Form(`remember`) == `1` {
if len(m.Title) == 0 {
m.Title = auth.Driver + `://` + auth.Username + `@` + auth.Host + `/` + auth.Db
}
m.Engine = auth.Driver
m.Host = auth.Host
m.User = auth.Username
m.Password = auth.Password
m.Name = auth.Db
err = m.SetOptions()
if err != nil {
return err
}
if accountID < 1 || m.Id < 1 {
m.Uid = user.Id
_, err = m.Add()
} else {
err = m.Edit(accountID, nil, db.Cond{`id`: accountID})
}
}
return
}