64 Star 391 Fork 127

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
manager_login.go 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2020-02-21 10:57 . 表名称统一增加nging前缀
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.2.2

搜索帮助