Add /add and /delete endpoints, with dn in json body
This commit is contained in:
parent
a02b8b9359
commit
d557521a5c
61
main.go
61
main.go
@ -18,10 +18,11 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
//"github.com/gin-gonic/gin/render"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gVersion = "0.5.3"
|
gVersion = "0.5.4"
|
||||||
|
|
||||||
gRoLdap *MyLdap
|
gRoLdap *MyLdap
|
||||||
)
|
)
|
||||||
@ -344,10 +345,14 @@ func initRouter(r *gin.Engine) {
|
|||||||
return
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/* 2 call methods : Either DN in url, or DN in body using /add :
|
||||||
* curl -u "admin:admin" --header "Content-Type: application/json" -X POST
|
* * curl -u "admin:admin" -H "Content-Type: application/json" -X POST
|
||||||
* --data '{"objectClass":["person","top"],"cn":"newuser","sn":"New"}' \
|
* --data '{"objectClass":["person","top"],"cn":"newuser","sn":"New"}' \
|
||||||
* https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
|
* https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
|
||||||
|
*
|
||||||
|
* curl -u "admin:admin" -H "Content-Type: application/json" -X POST
|
||||||
|
* --data '{"dn":"cn=newuser,ou=users,dc=example,dc=org","objectClass":["person","top"],"cn":"newuser","sn":"New"}' \
|
||||||
|
* https://localhost:8443/add
|
||||||
*/
|
*/
|
||||||
r.POST("/:dn", ldapBasicAuth, func(c *gin.Context) {
|
r.POST("/:dn", ldapBasicAuth, func(c *gin.Context) {
|
||||||
dn := c.Param("dn")
|
dn := c.Param("dn")
|
||||||
@ -369,11 +374,30 @@ func initRouter(r *gin.Engine) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get dn in body if called with "http://1.2.3.4/add"
|
||||||
|
if strings.EqualFold(dn, "add") {
|
||||||
|
dn = attributes["dn"].(string)
|
||||||
|
}
|
||||||
|
if len(dn) == 0 {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = createEntry(ldapCon, dn, attributes)
|
err = createEntry(ldapCon, dn, attributes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Contains(err.Error(), "LDAP Result Code 50") {
|
if strings.Contains(err.Error(), "LDAP Result Code 50") {
|
||||||
c.AbortWithStatus(http.StatusUnauthorized)
|
c.AbortWithStatus(http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
// "Entry Already Exists"
|
||||||
|
} else if strings.Contains(err.Error(), "LDAP Result Code 68") {
|
||||||
|
c.JSON(http.StatusCreated, gin.H{"message": "Entry already exists"})
|
||||||
|
/* This returns 201/Created with Location header, although 303/SeeOther is specified
|
||||||
|
* c.Render(http.StatusSeeOther, render.Redirect{
|
||||||
|
Code: 303,
|
||||||
|
Location: fmt.Sprintf("http://1.2.3.4/%s", dn),
|
||||||
|
Request: c.Request,
|
||||||
|
})*/
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
c.AbortWithError(http.StatusBadRequest, err)
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
@ -426,8 +450,10 @@ func initRouter(r *gin.Engine) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/* 2 call methods : Either DN in url, or DN in body using /delete :
|
||||||
* curl -i -u "admin:admin" -X DELETE https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
|
* curl -i -u "admin:admin" -X DELETE https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
|
||||||
|
* or
|
||||||
|
* curl -i -u "admin:admin" -X DELETE -H "Content-Type: application/json" -d '{"dn":"cn=newuser,ou=users,dc=example,dc=org"}' https://localhost:8443/delete
|
||||||
*
|
*
|
||||||
* Each leaf have to be deleted (cannot delete if subordinates)
|
* Each leaf have to be deleted (cannot delete if subordinates)
|
||||||
*/
|
*/
|
||||||
@ -441,6 +467,24 @@ func initRouter(r *gin.Engine) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unmarshall json body to a map
|
||||||
|
if c.Request.Header.Get("Content-Type") == "application/json" {
|
||||||
|
var attributes map[string]interface{}
|
||||||
|
err := c.ShouldBindJSON(&attributes)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get dn in body if called with "http://1.2.3.4/delete"
|
||||||
|
if strings.EqualFold(dn, "delete") {
|
||||||
|
dn = attributes["dn"].(string)
|
||||||
|
}
|
||||||
|
if len(dn) == 0 {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = deleteEntry(ldapCon, dn)
|
err = deleteEntry(ldapCon, dn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//log.Errorf("Error creating %s: %v", dn, err)
|
//log.Errorf("Error creating %s: %v", dn, err)
|
||||||
@ -448,7 +492,14 @@ func initRouter(r *gin.Engine) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusCreated, gin.H{"message": "Successfully deleted"})
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully deleted"})
|
||||||
|
} else {
|
||||||
|
err = deleteEntry(ldapCon, dn)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user