Added start of Muldap web service
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
15
lib/web/routes.go
Normal file
15
lib/web/routes.go
Normal file
@ -0,0 +1,15 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/flamego/flamego"
|
||||
|
||||
"git.metaunix.net/metaunix.net/muldap/lib/web/routes"
|
||||
)
|
||||
|
||||
func RegisterRoutes(f *flamego.Flame) {
|
||||
// top-level routes
|
||||
f.Get("/", routes.GetTopIndex)
|
||||
|
||||
// user routes
|
||||
f.Get("/user/{uid}", routes.GetUserInfo)
|
||||
}
|
44
lib/web/routes/top.go
Normal file
44
lib/web/routes/top.go
Normal file
@ -0,0 +1,44 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/flamego/template"
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/ldap.v2"
|
||||
)
|
||||
|
||||
func GetTopIndex(t template.Template, data template.Data) {
|
||||
// create new LDAP connection
|
||||
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
// bind as the admin user
|
||||
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
searchRequest := ldap.NewSearchRequest(
|
||||
viper.GetString("user.base_ou"), // The base dn to search
|
||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
||||
[]string{"dn", "uid", "displayName"}, // A list attributes to retrieve
|
||||
nil,
|
||||
)
|
||||
|
||||
sr, err := l.Search(searchRequest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
data["results"] = sr
|
||||
|
||||
data["title"] = "Dashboard"
|
||||
t.HTML(http.StatusOK, "index")
|
||||
}
|
48
lib/web/routes/user.go
Normal file
48
lib/web/routes/user.go
Normal file
@ -0,0 +1,48 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/flamego/flamego"
|
||||
"github.com/flamego/template"
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/ldap.v2"
|
||||
)
|
||||
|
||||
func GetUserInfo(c flamego.Context, t template.Template, data template.Data) {
|
||||
// create new LDAP connection
|
||||
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
// bind as the admin user
|
||||
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// set up user DN
|
||||
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), c.Param("uid"), viper.GetString("user.base_ou"))
|
||||
|
||||
searchRequest := ldap.NewSearchRequest(
|
||||
userDn, // The base dn to search
|
||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
||||
nil, // A list attributes to retrieve
|
||||
nil,
|
||||
)
|
||||
|
||||
sr, err := l.Search(searchRequest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
data["user"] = sr.Entries[0]
|
||||
|
||||
data["title"] = "User Info"
|
||||
t.HTML(http.StatusOK, "user/info")
|
||||
}
|
Reference in New Issue
Block a user