|
|
|
|
|
|
|
|
|
package adapter |
|
|
|
import ( |
|
"bytes" |
|
"fmt" |
|
"net/http" |
|
"net/url" |
|
|
|
"github.com/GoAdminGroup/go-admin/context" |
|
"github.com/GoAdminGroup/go-admin/modules/auth" |
|
"github.com/GoAdminGroup/go-admin/modules/config" |
|
"github.com/GoAdminGroup/go-admin/modules/constant" |
|
"github.com/GoAdminGroup/go-admin/modules/db" |
|
"github.com/GoAdminGroup/go-admin/modules/errors" |
|
"github.com/GoAdminGroup/go-admin/modules/logger" |
|
"github.com/GoAdminGroup/go-admin/modules/menu" |
|
"github.com/GoAdminGroup/go-admin/plugins" |
|
"github.com/GoAdminGroup/go-admin/plugins/admin/models" |
|
"github.com/GoAdminGroup/go-admin/template" |
|
"github.com/GoAdminGroup/go-admin/template/types" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
type WebFrameWork interface { |
|
|
|
Name() string |
|
|
|
|
|
|
|
Use(app interface{}, plugins []plugins.Plugin) error |
|
|
|
|
|
|
|
Content(ctx interface{}, fn types.GetPanelFn, fn2 context.NodeProcessor, navButtons ...types.Button) |
|
|
|
|
|
User(ctx interface{}) (models.UserModel, bool) |
|
|
|
|
|
AddHandler(method, path string, handlers context.Handlers) |
|
|
|
DisableLog() |
|
|
|
Static(prefix, path string) |
|
|
|
Run() error |
|
|
|
|
|
|
|
|
|
SetApp(app interface{}) error |
|
SetConnection(db.Connection) |
|
GetConnection() db.Connection |
|
SetContext(ctx interface{}) WebFrameWork |
|
GetCookie() (string, error) |
|
Lang() string |
|
Path() string |
|
Method() string |
|
Request() *http.Request |
|
FormParam() url.Values |
|
Query() url.Values |
|
IsPjax() bool |
|
Redirect() |
|
SetContentType() |
|
Write(body []byte) |
|
CookieKey() string |
|
HTMLContentType() string |
|
} |
|
|
|
|
|
type BaseAdapter struct { |
|
db db.Connection |
|
} |
|
|
|
|
|
func (base *BaseAdapter) SetConnection(conn db.Connection) { |
|
base.db = conn |
|
} |
|
|
|
|
|
func (base *BaseAdapter) GetConnection() db.Connection { |
|
return base.db |
|
} |
|
|
|
|
|
func (*BaseAdapter) HTMLContentType() string { |
|
return "text/html; charset=utf-8" |
|
} |
|
|
|
|
|
func (*BaseAdapter) CookieKey() string { |
|
return auth.DefaultCookieKey |
|
} |
|
|
|
|
|
func (*BaseAdapter) GetUser(ctx interface{}, wf WebFrameWork) (models.UserModel, bool) { |
|
cookie, err := wf.SetContext(ctx).GetCookie() |
|
|
|
if err != nil { |
|
return models.UserModel{}, false |
|
} |
|
|
|
user, exist := auth.GetCurUser(cookie, wf.GetConnection()) |
|
return user.ReleaseConn(), exist |
|
} |
|
|
|
|
|
func (*BaseAdapter) GetUse(app interface{}, plugin []plugins.Plugin, wf WebFrameWork) error { |
|
if err := wf.SetApp(app); err != nil { |
|
return err |
|
} |
|
|
|
for _, plug := range plugin { |
|
for path, handlers := range plug.GetHandler() { |
|
if plug.Prefix() == "" { |
|
wf.AddHandler(path.Method, path.URL, handlers) |
|
} else { |
|
wf.AddHandler(path.Method, config.Url("/"+plug.Prefix()+path.URL), handlers) |
|
} |
|
} |
|
} |
|
|
|
return nil |
|
} |
|
|
|
func (*BaseAdapter) Run() error { panic("not implement") } |
|
func (*BaseAdapter) DisableLog() { panic("not implement") } |
|
func (*BaseAdapter) Static(_, _ string) { panic("not implement") } |
|
|
|
|
|
func (base *BaseAdapter) GetContent(ctx interface{}, getPanelFn types.GetPanelFn, wf WebFrameWork, |
|
navButtons types.Buttons, fn context.NodeProcessor) { |
|
|
|
var ( |
|
newBase = wf.SetContext(ctx) |
|
cookie, hasError = newBase.GetCookie() |
|
) |
|
|
|
if hasError != nil || cookie == "" { |
|
newBase.Redirect() |
|
return |
|
} |
|
|
|
user, authSuccess := auth.GetCurUser(cookie, wf.GetConnection()) |
|
|
|
if !authSuccess { |
|
newBase.Redirect() |
|
return |
|
} |
|
|
|
var ( |
|
panel types.Panel |
|
err error |
|
) |
|
|
|
gctx := context.NewContext(newBase.Request()) |
|
|
|
if !auth.CheckPermissions(user, newBase.Path(), newBase.Method(), newBase.FormParam()) { |
|
panel = template.WarningPanel(gctx, errors.NoPermission, template.NoPermission403Page) |
|
} else { |
|
panel, err = getPanelFn(ctx) |
|
if err != nil { |
|
panel = template.WarningPanel(gctx, err.Error()) |
|
} |
|
} |
|
|
|
fn(panel.Callbacks...) |
|
|
|
tmpl, tmplName := template.Default(gctx).GetTemplate(newBase.IsPjax()) |
|
|
|
buf := new(bytes.Buffer) |
|
hasError = tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(gctx, &types.NewPageParam{ |
|
User: user, |
|
Menu: menu.GetGlobalMenu(user, wf.GetConnection(), newBase.Lang()).SetActiveClass(config.URLRemovePrefix(newBase.Path())), |
|
Panel: panel.GetContent(config.IsProductionEnvironment()), |
|
Assets: template.GetComponentAssetImportHTML(gctx), |
|
Buttons: navButtons.CheckPermission(user), |
|
TmplHeadHTML: template.Default(gctx).GetHeadHTML(), |
|
TmplFootJS: template.Default(gctx).GetFootJS(), |
|
Iframe: newBase.Query().Get(constant.IframeKey) == "true", |
|
})) |
|
|
|
if hasError != nil { |
|
logger.Error(fmt.Sprintf("error: %s adapter content, ", newBase.Name()), hasError) |
|
} |
|
|
|
newBase.SetContentType() |
|
newBase.Write(buf.Bytes()) |
|
} |
|
|