mirror of
https://github.com/kataras/iris.git
synced 2026-01-04 18:57:03 +00:00
Fix mail send, complete jade support, fix iriscontrol index , replace iriscontrol session to basicauth
This commit is contained in:
@@ -26,13 +26,15 @@ func (i *irisControlPlugin) startControlPanel() {
|
||||
}
|
||||
|
||||
i.server = iris.New()
|
||||
i.server.Config().DisableBanner = true
|
||||
i.server.Config().Render.Template.Directory = installationPath + "templates"
|
||||
//i.server.SetRenderConfig(i.server.Config.Render)
|
||||
i.setPluginsInfo()
|
||||
i.setPanelRoutes()
|
||||
|
||||
go i.server.Listen(strconv.Itoa(i.options.Port))
|
||||
i.pluginContainer.Printf("[%s] %s is running at port %d with %d authenticated users", time.Now().UTC().String(), Name, i.options.Port, len(i.auth.authenticatedUsers))
|
||||
|
||||
i.pluginContainer.Printf("[%s] %s is running at port %d", time.Now().UTC().String(), Name, i.options.Port)
|
||||
|
||||
}
|
||||
|
||||
@@ -67,21 +69,10 @@ func (i *irisControlPlugin) installAssets() (err error) {
|
||||
func (i *irisControlPlugin) setPanelRoutes() {
|
||||
|
||||
i.server.Static("/public", installationPath+"static", 1)
|
||||
i.server.Get("/login", func(ctx *iris.Context) {
|
||||
ctx.Render("login", nil)
|
||||
})
|
||||
|
||||
i.server.Post("/login", func(ctx *iris.Context) {
|
||||
i.auth.login(ctx)
|
||||
})
|
||||
|
||||
i.server.Use(i.auth)
|
||||
i.server.Use(i.authFunc)
|
||||
i.server.Get("/", func(ctx *iris.Context) {
|
||||
ctx.Render("index", DashboardPage{ServerIsRunning: i.station.Server().IsListening(), Routes: i.routes.All(), Plugins: i.plugins})
|
||||
})
|
||||
|
||||
i.server.Post("/logout", func(ctx *iris.Context) {
|
||||
i.auth.logout(ctx)
|
||||
ctx.Render("index.html", DashboardPage{ServerIsRunning: i.station.Server().IsListening(), Routes: i.routes.All(), Plugins: i.plugins})
|
||||
})
|
||||
|
||||
//the controls
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/config"
|
||||
"github.com/kataras/iris/middleware/basicauth"
|
||||
"github.com/kataras/iris/plugin/routesinfo"
|
||||
"github.com/kataras/iris/server"
|
||||
)
|
||||
@@ -29,7 +30,7 @@ type irisControlPlugin struct {
|
||||
plugins []PluginInfo
|
||||
//
|
||||
|
||||
auth *userAuth
|
||||
authFunc iris.HandlerFunc
|
||||
}
|
||||
|
||||
// New returns the plugin which is ready-to-use inside iris.Plugin method
|
||||
@@ -39,12 +40,13 @@ func New(cfg ...config.IrisControl) iris.IPlugin {
|
||||
if len(cfg) > 0 {
|
||||
c = cfg[0]
|
||||
}
|
||||
auth := newUserAuth(c.Users)
|
||||
if auth == nil {
|
||||
if c.Users == nil || len(c.Users) == 0 {
|
||||
panic(Name + " Error: you should pass authenticated users map to the options, refer to the docs!")
|
||||
}
|
||||
|
||||
return &irisControlPlugin{options: c, auth: auth, routes: routesinfo.RoutesInfo()}
|
||||
auth := basicauth.Default(c.Users)
|
||||
|
||||
return &irisControlPlugin{options: c, authFunc: auth, routes: routesinfo.RoutesInfo()}
|
||||
}
|
||||
|
||||
// Web set the options for the plugin and return the plugin which is ready-to-use inside iris.Plugin method
|
||||
@@ -106,7 +108,6 @@ func (i *irisControlPlugin) Destroy() {
|
||||
i.station = nil
|
||||
i.server.Close()
|
||||
i.pluginContainer = nil
|
||||
i.auth.Destroy()
|
||||
i.auth = nil
|
||||
i.authFunc = nil
|
||||
i.pluginContainer.Printf("[%s] %s is turned off", time.Now().UTC().String(), Name)
|
||||
}
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
package iriscontrol
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/sessions"
|
||||
// _ empty because it auto-registers
|
||||
_ "github.com/kataras/iris/sessions/providers/memory"
|
||||
)
|
||||
|
||||
var panelSessions *sessions.Manager
|
||||
|
||||
func init() {
|
||||
//using the default
|
||||
panelSessions = sessions.New()
|
||||
}
|
||||
|
||||
type user struct {
|
||||
username string
|
||||
password string
|
||||
}
|
||||
type userAuth struct {
|
||||
authenticatedUsers []user
|
||||
}
|
||||
|
||||
// newUserAuth returns a new userAuth object, parameter is the authenticated users as map
|
||||
func newUserAuth(usersMap map[string]string) *userAuth {
|
||||
if usersMap != nil {
|
||||
obj := &userAuth{make([]user, 0)}
|
||||
for key, val := range usersMap {
|
||||
obj.authenticatedUsers = append(obj.authenticatedUsers, user{key, val})
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userAuth) login(ctx *iris.Context) {
|
||||
session := panelSessions.Start(ctx)
|
||||
|
||||
username := ctx.PostFormValue("username")
|
||||
password := ctx.PostFormValue("password")
|
||||
|
||||
for _, authenticatedUser := range u.authenticatedUsers {
|
||||
if authenticatedUser.username == username && authenticatedUser.password == password {
|
||||
session.Set("username", username)
|
||||
session.Set("password", password)
|
||||
ctx.Write("success")
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.Write("fail")
|
||||
|
||||
}
|
||||
|
||||
func (u *userAuth) logout(ctx *iris.Context) {
|
||||
session := panelSessions.Start(ctx)
|
||||
session.Set("user", nil)
|
||||
|
||||
ctx.Redirect("/login")
|
||||
}
|
||||
|
||||
// check if session stored, then check if this user is the correct, each time, then continue, else not
|
||||
func (u *userAuth) Serve(ctx *iris.Context) {
|
||||
if ctx.PathString() == "/login" || strings.HasPrefix(ctx.PathString(), "/public") {
|
||||
ctx.Next()
|
||||
return
|
||||
}
|
||||
session := panelSessions.Start(ctx)
|
||||
|
||||
if sessionVal := session.Get("username"); sessionVal != nil {
|
||||
username := sessionVal.(string)
|
||||
password := session.GetString("password")
|
||||
if username != "" && password != "" {
|
||||
|
||||
for _, authenticatedUser := range u.authenticatedUsers {
|
||||
if authenticatedUser.username == username && authenticatedUser.password == password {
|
||||
ctx.Next()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//if not logged in the redirect to the /login
|
||||
ctx.Redirect("/login")
|
||||
|
||||
}
|
||||
|
||||
// Destroy this is called on PreClose by the iriscontrol.go
|
||||
func (u *userAuth) Destroy() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user