2018-01-21 11:17:43 +00:00
|
|
|
package routes
|
2016-12-07 09:34:09 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
import (
|
2018-01-21 11:17:43 +00:00
|
|
|
"database/sql"
|
2017-05-11 13:04:43 +00:00
|
|
|
"net/http"
|
2017-09-03 04:50:31 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2017-11-11 04:06:16 +00:00
|
|
|
|
2019-04-19 01:20:43 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2017-05-11 13:04:43 +00:00
|
|
|
)
|
2016-12-07 09:34:09 +00:00
|
|
|
|
2019-04-19 01:20:43 +00:00
|
|
|
func BanUserSubmit(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
2016-12-21 02:30:32 +00:00
|
|
|
if !user.Perms.BanUsers {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-12-25 02:41:10 +00:00
|
|
|
uid, err := strconv.Atoi(suid)
|
2016-12-08 14:11:18 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-11-23 05:37:08 +00:00
|
|
|
if uid == -2 {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("Why don't you like Merlin?", w, r, user)
|
2017-11-23 05:37:08 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2019-04-19 01:20:43 +00:00
|
|
|
targetUser, err := c.Users.Get(uid)
|
2018-01-21 11:17:43 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The user you're trying to ban no longer exists.", w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-10-31 07:26:44 +00:00
|
|
|
// TODO: Is there a difference between IsMod and IsSuperMod? Should we delete the redundant one?
|
2017-11-23 05:37:08 +00:00
|
|
|
if targetUser.IsMod {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("You may not ban another staff member.", w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
|
|
|
if uid == user.ID {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("Why are you trying to ban yourself? Stop that.", w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
if targetUser.IsBanned {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The user you're trying to unban is already banned.", w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2020-01-14 05:07:00 +00:00
|
|
|
durDays, err := strconv.Atoi(r.FormValue("dur-days"))
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("You can only use whole numbers for the number of days", w, r, user)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2020-01-14 05:07:00 +00:00
|
|
|
durWeeks, err := strconv.Atoi(r.FormValue("dur-weeks"))
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("You can only use whole numbers for the number of weeks", w, r, user)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2020-01-14 05:07:00 +00:00
|
|
|
durMonths, err := strconv.Atoi(r.FormValue("dur-months"))
|
2016-12-08 14:11:18 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("You can only use whole numbers for the number of months", w, r, user)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2020-01-14 05:07:00 +00:00
|
|
|
deletePosts := false
|
|
|
|
switch r.FormValue("delete-posts") {
|
|
|
|
case "1":
|
|
|
|
deletePosts = true
|
|
|
|
}
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2020-01-14 05:07:00 +00:00
|
|
|
var dur time.Duration
|
|
|
|
if durDays > 1 && durWeeks > 1 && durMonths > 1 {
|
|
|
|
dur, _ = time.ParseDuration("0")
|
2017-08-27 09:33:45 +00:00
|
|
|
} else {
|
2020-01-14 05:07:00 +00:00
|
|
|
var secs int
|
|
|
|
secs += durDays * int(c.Day)
|
|
|
|
secs += durWeeks * int(c.Week)
|
|
|
|
secs += durMonths * int(c.Month)
|
|
|
|
dur, _ = time.ParseDuration(strconv.Itoa(secs) + "s")
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 05:07:00 +00:00
|
|
|
err = targetUser.Ban(dur, user.ID)
|
2018-01-21 11:17:43 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The user you're trying to ban no longer exists.", w, r, user)
|
2017-08-27 09:33:45 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
err = c.ModLogs.Create("ban", uid, "user", user.GetIP(), user.ID)
|
2017-04-06 17:37:32 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
2020-01-14 05:07:00 +00:00
|
|
|
if deletePosts {
|
|
|
|
err = targetUser.DeletePosts()
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.LocalError("The user you're trying to ban no longer exists.", w, r, user)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
err = c.ModLogs.Create("delete-posts", uid, "user", user.GetIP(), user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 01:20:43 +00:00
|
|
|
// 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
|
2017-04-06 17:37:32 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
http.Redirect(w, r, "/user/"+strconv.Itoa(uid), http.StatusSeeOther)
|
2017-10-30 09:57:08 +00:00
|
|
|
return nil
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:20:43 +00:00
|
|
|
func UnbanUser(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
2016-12-21 02:30:32 +00:00
|
|
|
if !user.Perms.BanUsers {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-12-25 02:41:10 +00:00
|
|
|
uid, err := strconv.Atoi(suid)
|
2016-12-08 14:11:18 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2019-04-19 01:20:43 +00:00
|
|
|
targetUser, err := c.Users.Get(uid)
|
2018-01-21 11:17:43 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The user you're trying to unban no longer exists.", w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
if !targetUser.IsBanned {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The user you're trying to unban isn't banned.", w, r, user)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2017-08-27 09:33:45 +00:00
|
|
|
err = targetUser.Unban()
|
2019-04-19 01:20:43 +00:00
|
|
|
if err == c.ErrNoTempGroup {
|
|
|
|
return c.LocalError("The user you're trying to unban is not banned", w, r, user)
|
2018-01-21 11:17:43 +00:00
|
|
|
} else if err == sql.ErrNoRows {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The user you're trying to unban no longer exists.", w, r, user)
|
2017-08-27 09:33:45 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
err = c.ModLogs.Create("unban", uid, "user", user.GetIP(), user.ID)
|
2017-04-06 17:37:32 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
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
|
2017-04-06 17:37:32 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
http.Redirect(w, r, "/user/"+strconv.Itoa(uid), http.StatusSeeOther)
|
2017-10-30 09:57:08 +00:00
|
|
|
return nil
|
2016-12-08 14:11:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:20:43 +00:00
|
|
|
func ActivateUser(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
2016-12-21 02:30:32 +00:00
|
|
|
if !user.Perms.ActivateUsers {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2016-12-18 12:56:06 +00:00
|
|
|
}
|
2017-12-25 02:41:10 +00:00
|
|
|
uid, err := strconv.Atoi(suid)
|
2016-12-18 12:56:06 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
2016-12-18 12:56:06 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2019-04-19 01:20:43 +00:00
|
|
|
targetUser, err := c.Users.Get(uid)
|
2018-01-21 11:17:43 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The account you're trying to activate no longer exists.", w, r, user)
|
2016-12-18 12:56:06 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2016-12-18 12:56:06 +00:00
|
|
|
}
|
2017-09-22 02:21:17 +00:00
|
|
|
if targetUser.Active {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.LocalError("The account you're trying to activate has already been activated.", w, r, user)
|
2016-12-18 12:56:06 +00:00
|
|
|
}
|
2017-09-22 02:21:17 +00:00
|
|
|
err = targetUser.Activate()
|
2016-12-21 02:30:32 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2016-12-21 02:30:32 +00:00
|
|
|
}
|
2017-06-06 14:41:06 +00:00
|
|
|
|
2020-02-09 10:00:08 +00:00
|
|
|
targetUser, err = c.Users.Get(uid)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.LocalError("The account you're trying to activate no longer exists.", w, r, user)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
err = c.GroupPromotions.PromoteIfEligible(targetUser, targetUser.Level, targetUser.Posts, targetUser.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
targetUser.CacheRemove()
|
|
|
|
|
2019-12-31 21:57:54 +00:00
|
|
|
err = c.ModLogs.Create("activate", targetUser.ID, "user", user.GetIP(), user.ID)
|
2017-04-06 17:37:32 +00:00
|
|
|
if err != nil {
|
2019-04-19 01:20:43 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2017-04-06 17:37:32 +00:00
|
|
|
}
|
2019-04-19 01:20:43 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-09-22 02:21:17 +00:00
|
|
|
http.Redirect(w, r, "/user/"+strconv.Itoa(targetUser.ID), http.StatusSeeOther)
|
2017-10-30 09:57:08 +00:00
|
|
|
return nil
|
2016-12-18 12:56:06 +00:00
|
|
|
}
|
2020-01-14 05:07:00 +00:00
|
|
|
|
|
|
|
func DeletePostsSubmit(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
|
|
|
if !user.Perms.BanUsers {
|
|
|
|
return c.NoPermissions(w, r, user)
|
|
|
|
}
|
|
|
|
uid, err := strconv.Atoi(suid)
|
|
|
|
if err != nil {
|
|
|
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
targetUser, err := c.Users.Get(uid)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.LocalError("The user you're trying to purge posts of no longer exists.", w, r, user)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
// TODO: Is there a difference between IsMod and IsSuperMod? Should we delete the redundant one?
|
|
|
|
if targetUser.IsMod {
|
|
|
|
return c.LocalError("You may not purge the posts of another staff member.", w, r, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = targetUser.DeletePosts()
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.LocalError("The user you're trying to purge posts of no longer exists.", w, r, user)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
err = c.ModLogs.Create("delete-posts", uid, "user", user.GetIP(), user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Trickle the hookTable down from the router
|
|
|
|
hTbl := c.GetHookTable()
|
|
|
|
skip, rerr := hTbl.VhookSkippable("action_end_delete_posts", targetUser.ID, &user)
|
|
|
|
if skip || rerr != nil {
|
|
|
|
return rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/user/"+strconv.Itoa(uid), http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|