2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func domainModeratorNew(domain string, email string) error {
|
|
|
|
if domain == "" || email == "" {
|
|
|
|
return errorMissingField
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:23:44 +08:00
|
|
|
if err := emailNew(email); err != nil {
|
|
|
|
logger.Errorf("cannot create email when creating moderator: %v", err)
|
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
statement := `
|
|
|
|
INSERT INTO
|
|
|
|
moderators (domain, email, addDate)
|
|
|
|
VALUES ($1, $2, $3 );
|
|
|
|
`
|
|
|
|
_, err := db.Exec(statement, domain, email, time.Now().UTC())
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot insert new moderator: %v", err)
|
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func domainModeratorNewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
type request struct {
|
2018-06-20 11:29:55 +08:00
|
|
|
OwnerToken *string `json:"ownerToken"`
|
2018-06-20 11:50:11 +08:00
|
|
|
Domain *string `json:"domain"`
|
|
|
|
Email *string `json:"email"`
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var x request
|
2018-07-24 14:58:43 +08:00
|
|
|
if err := bodyUnmarshal(r, &x); err != nil {
|
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:00:45 +08:00
|
|
|
domain := domainStrip(*x.Domain)
|
2018-05-27 22:40:42 +08:00
|
|
|
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
|
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isOwner {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = domainModeratorNew(domain, *x.Email); err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": true})
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|