Experimenting with reducing the amount of boilerplate in route files.
Added the action_end_ban_user plugin hook. Added the action_end_unban_user plugin hook. Added the action_end_activate_user plugin hook.
This commit is contained in:
parent
29d14a4a4a
commit
00e30460b5
@ -101,6 +101,10 @@ var hookTable = &HookTable{
|
|||||||
|
|
||||||
"action_end_like_reply":nil,
|
"action_end_like_reply":nil,
|
||||||
|
|
||||||
|
"action_end_ban_user":nil,
|
||||||
|
"action_end_unban_user":nil,
|
||||||
|
"action_end_activate_user":nil,
|
||||||
|
|
||||||
"router_after_filters": nil,
|
"router_after_filters": nil,
|
||||||
"router_pre_route": nil,
|
"router_pre_route": nil,
|
||||||
|
|
||||||
|
110
routes/user.go
110
routes/user.go
@ -6,53 +6,53 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Azareal/Gosora/common"
|
c "github.com/Azareal/Gosora/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BanUserSubmit(w http.ResponseWriter, r *http.Request, user common.User, suid string) common.RouteError {
|
func BanUserSubmit(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
||||||
if !user.Perms.BanUsers {
|
if !user.Perms.BanUsers {
|
||||||
return common.NoPermissions(w, r, user)
|
return c.NoPermissions(w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
uid, err := strconv.Atoi(suid)
|
uid, err := strconv.Atoi(suid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.LocalError("The provided UserID is not a valid number.", w, r, user)
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
||||||
}
|
}
|
||||||
if uid == -2 {
|
if uid == -2 {
|
||||||
return common.LocalError("Why don't you like Merlin?", w, r, user)
|
return c.LocalError("Why don't you like Merlin?", w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
targetUser, err := common.Users.Get(uid)
|
targetUser, err := c.Users.Get(uid)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return common.LocalError("The user you're trying to ban no longer exists.", w, r, user)
|
return c.LocalError("The user you're trying to ban no longer exists.", w, r, user)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Is there a difference between IsMod and IsSuperMod? Should we delete the redundant one?
|
// TODO: Is there a difference between IsMod and IsSuperMod? Should we delete the redundant one?
|
||||||
if targetUser.IsMod {
|
if targetUser.IsMod {
|
||||||
return common.LocalError("You may not ban another staff member.", w, r, user)
|
return c.LocalError("You may not ban another staff member.", w, r, user)
|
||||||
}
|
}
|
||||||
if uid == user.ID {
|
if uid == user.ID {
|
||||||
return common.LocalError("Why are you trying to ban yourself? Stop that.", w, r, user)
|
return c.LocalError("Why are you trying to ban yourself? Stop that.", w, r, user)
|
||||||
}
|
}
|
||||||
if targetUser.IsBanned {
|
if targetUser.IsBanned {
|
||||||
return common.LocalError("The user you're trying to unban is already banned.", w, r, user)
|
return c.LocalError("The user you're trying to unban is already banned.", w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
durationDays, err := strconv.Atoi(r.FormValue("ban-duration-days"))
|
durationDays, err := strconv.Atoi(r.FormValue("ban-duration-days"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.LocalError("You can only use whole numbers for the number of days", w, r, user)
|
return c.LocalError("You can only use whole numbers for the number of days", w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
durationWeeks, err := strconv.Atoi(r.FormValue("ban-duration-weeks"))
|
durationWeeks, err := strconv.Atoi(r.FormValue("ban-duration-weeks"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.LocalError("You can only use whole numbers for the number of weeks", w, r, user)
|
return c.LocalError("You can only use whole numbers for the number of weeks", w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
durationMonths, err := strconv.Atoi(r.FormValue("ban-duration-months"))
|
durationMonths, err := strconv.Atoi(r.FormValue("ban-duration-months"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.LocalError("You can only use whole numbers for the number of months", w, r, user)
|
return c.LocalError("You can only use whole numbers for the number of months", w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
var duration time.Duration
|
var duration time.Duration
|
||||||
@ -60,96 +60,118 @@ func BanUserSubmit(w http.ResponseWriter, r *http.Request, user common.User, sui
|
|||||||
duration, _ = time.ParseDuration("0")
|
duration, _ = time.ParseDuration("0")
|
||||||
} else {
|
} else {
|
||||||
var seconds int
|
var seconds int
|
||||||
seconds += durationDays * int(common.Day)
|
seconds += durationDays * int(c.Day)
|
||||||
seconds += durationWeeks * int(common.Week)
|
seconds += durationWeeks * int(c.Week)
|
||||||
seconds += durationMonths * int(common.Month)
|
seconds += durationMonths * int(c.Month)
|
||||||
duration, _ = time.ParseDuration(strconv.Itoa(seconds) + "s")
|
duration, _ = time.ParseDuration(strconv.Itoa(seconds) + "s")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = targetUser.Ban(duration, user.ID)
|
err = targetUser.Ban(duration, user.ID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return common.LocalError("The user you're trying to ban no longer exists.", w, r, user)
|
return c.LocalError("The user you're trying to ban no longer exists.", w, r, user)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = common.ModLogs.Create("ban", uid, "user", user.LastIP, user.ID)
|
err = c.ModLogs.Create("ban", uid, "user", user.LastIP, user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Trickle the hookTable down from the router
|
||||||
|
hTbl := c.GetHookTable()
|
||||||
|
skip, rerr := hTbl.VhookSkippable("action_end_ban_user", targetUser.ID, &user)
|
||||||
|
if skip || rerr != nil {
|
||||||
|
return rerr
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Redirect(w, r, "/user/"+strconv.Itoa(uid), http.StatusSeeOther)
|
http.Redirect(w, r, "/user/"+strconv.Itoa(uid), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnbanUser(w http.ResponseWriter, r *http.Request, user common.User, suid string) common.RouteError {
|
func UnbanUser(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
||||||
if !user.Perms.BanUsers {
|
if !user.Perms.BanUsers {
|
||||||
return common.NoPermissions(w, r, user)
|
return c.NoPermissions(w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
uid, err := strconv.Atoi(suid)
|
uid, err := strconv.Atoi(suid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.LocalError("The provided UserID is not a valid number.", w, r, user)
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
targetUser, err := common.Users.Get(uid)
|
targetUser, err := c.Users.Get(uid)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return common.LocalError("The user you're trying to unban no longer exists.", w, r, user)
|
return c.LocalError("The user you're trying to unban no longer exists.", w, r, user)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !targetUser.IsBanned {
|
if !targetUser.IsBanned {
|
||||||
return common.LocalError("The user you're trying to unban isn't banned.", w, r, user)
|
return c.LocalError("The user you're trying to unban isn't banned.", w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = targetUser.Unban()
|
err = targetUser.Unban()
|
||||||
if err == common.ErrNoTempGroup {
|
if err == c.ErrNoTempGroup {
|
||||||
return common.LocalError("The user you're trying to unban is not banned", w, r, user)
|
return c.LocalError("The user you're trying to unban is not banned", w, r, user)
|
||||||
} else if err == sql.ErrNoRows {
|
} else if err == sql.ErrNoRows {
|
||||||
return common.LocalError("The user you're trying to unban no longer exists.", w, r, user)
|
return c.LocalError("The user you're trying to unban no longer exists.", w, r, user)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = common.ModLogs.Create("unban", uid, "user", user.LastIP, user.ID)
|
err = c.ModLogs.Create("unban", uid, "user", user.LastIP, user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Trickle the hookTable down from the router
|
||||||
|
hTbl := c.GetHookTable()
|
||||||
|
skip, rerr := hTbl.VhookSkippable("action_end_unban_user", targetUser.ID, &user)
|
||||||
|
if skip || rerr != nil {
|
||||||
|
return rerr
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Redirect(w, r, "/user/"+strconv.Itoa(uid), http.StatusSeeOther)
|
http.Redirect(w, r, "/user/"+strconv.Itoa(uid), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ActivateUser(w http.ResponseWriter, r *http.Request, user common.User, suid string) common.RouteError {
|
func ActivateUser(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
||||||
if !user.Perms.ActivateUsers {
|
if !user.Perms.ActivateUsers {
|
||||||
return common.NoPermissions(w, r, user)
|
return c.NoPermissions(w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
uid, err := strconv.Atoi(suid)
|
uid, err := strconv.Atoi(suid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.LocalError("The provided UserID is not a valid number.", w, r, user)
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
targetUser, err := common.Users.Get(uid)
|
targetUser, err := c.Users.Get(uid)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return common.LocalError("The account you're trying to activate no longer exists.", w, r, user)
|
return c.LocalError("The account you're trying to activate no longer exists.", w, r, user)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
if targetUser.Active {
|
if targetUser.Active {
|
||||||
return common.LocalError("The account you're trying to activate has already been activated.", w, r, user)
|
return c.LocalError("The account you're trying to activate has already been activated.", w, r, user)
|
||||||
}
|
}
|
||||||
err = targetUser.Activate()
|
err = targetUser.Activate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = common.ModLogs.Create("activate", targetUser.ID, "user", user.LastIP, user.ID)
|
err = c.ModLogs.Create("activate", targetUser.ID, "user", user.LastIP, user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.InternalError(err, w, r)
|
return c.InternalError(err, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Trickle the hookTable down from the router
|
||||||
|
hTbl := c.GetHookTable()
|
||||||
|
skip, rerr := hTbl.VhookSkippable("action_end_activate_user", targetUser.ID, &user)
|
||||||
|
if skip || rerr != nil {
|
||||||
|
return rerr
|
||||||
|
}
|
||||||
|
|
||||||
http.Redirect(w, r, "/user/"+strconv.Itoa(targetUser.ID), http.StatusSeeOther)
|
http.Redirect(w, r, "/user/"+strconv.Itoa(targetUser.ID), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user