2018-06-17 07:28:18 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-06-17 07:28:18 +00:00
|
|
|
)
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
func Users(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "users", "users")
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2018-12-06 11:09:10 +00:00
|
|
|
perPage := 15
|
2019-04-19 08:20:10 +00:00
|
|
|
offset, page, lastPage := c.PageOffset(basePage.Stats.Users, page, perPage)
|
2018-06-17 07:28:18 +00:00
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
users, err := c.Users.GetOffset(offset, perPage)
|
2018-06-17 07:28:18 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 05:48:12 +00:00
|
|
|
pageList := c.Paginate(page, lastPage, 5)
|
2019-04-19 08:20:10 +00:00
|
|
|
pi := c.PanelUserPage{basePage, users, c.Paginator{pageList, page, lastPage}}
|
2019-04-29 08:17:12 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage,"","","panel_users",&pi})
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
func UsersEdit(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "edit_user", "users")
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.EditUser {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uid, err := strconv.Atoi(suid)
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
targetUser, err := c.Users.Get(uid)
|
2018-06-17 07:28:18 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The user you're trying to edit doesn't exist.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if targetUser.IsAdmin && !user.IsAdmin {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("Only administrators can edit the account of an administrator.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ? - Should we stop admins from deleting all the groups? Maybe, protect the group they're currently using?
|
2019-04-19 08:20:10 +00:00
|
|
|
groups, err := c.Groups.GetRange(1, 0) // ? - 0 = Go to the end
|
2018-06-17 07:28:18 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var groupList []interface{}
|
|
|
|
for _, group := range groups {
|
|
|
|
if !user.Perms.EditUserGroupAdmin && group.IsAdmin {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !user.Perms.EditUserGroupSuperMod && group.IsMod {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupList = append(groupList, group)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.FormValue("updated") == "1" {
|
|
|
|
basePage.AddNotice("panel_user_updated")
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
pi := c.PanelPage{basePage, groupList, targetUser}
|
2019-04-29 08:17:12 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage,"","","panel_user_edit",&pi})
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
func UsersEditSubmit(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, &user)
|
2018-06-17 07:28:18 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.EditUser {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uid, err := strconv.Atoi(suid)
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The provided UserID is not a valid number.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
targetUser, err := c.Users.Get(uid)
|
2018-06-17 07:28:18 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The user you're trying to edit doesn't exist.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
if targetUser.IsAdmin && !user.IsAdmin {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("Only administrators can edit the account of other administrators.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
newname := c.SanitiseSingleLine(r.PostFormValue("user-name"))
|
2018-06-17 07:28:18 +00:00
|
|
|
if newname == "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("You didn't put in a username.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: How should activation factor into admin set emails?
|
|
|
|
// TODO: How should we handle secondary emails? Do we even have secondary emails implemented?
|
2019-04-19 08:20:10 +00:00
|
|
|
newemail := c.SanitiseSingleLine(r.PostFormValue("user-email"))
|
2018-06-17 07:28:18 +00:00
|
|
|
if newemail == "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("You didn't put in an email address.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
if (newemail != targetUser.Email) && !user.Perms.EditUserEmail {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("You need the EditUserEmail permission to edit the email address of a user.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newpassword := r.PostFormValue("user-password")
|
|
|
|
if newpassword != "" && !user.Perms.EditUserPassword {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("You need the EditUserPassword permission to edit the password of a user.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newgroup, err := strconv.Atoi(r.PostFormValue("user-group"))
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("You need to provide a whole number for the group ID", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
group, err := c.Groups.Get(newgroup)
|
2018-06-17 07:28:18 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The group you're trying to place this user in doesn't exist.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !user.Perms.EditUserGroupAdmin && group.IsAdmin {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("You need the EditUserGroupAdmin permission to assign someone to an administrator group.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
if !user.Perms.EditUserGroupSuperMod && group.IsMod {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("You need the EditUserGroupSuperMod permission to assign someone to a super mod group.", w, r, user)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = targetUser.Update(newname, newemail, newgroup)
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if newpassword != "" {
|
2019-04-19 08:20:10 +00:00
|
|
|
c.SetPassword(targetUser.ID, newpassword)
|
2018-06-17 07:28:18 +00:00
|
|
|
// Log the user out as a safety precaution
|
2019-04-19 08:20:10 +00:00
|
|
|
c.Auth.ForceLogout(targetUser.ID)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
|
|
|
targetUser.CacheRemove()
|
|
|
|
|
|
|
|
// If we're changing our own password, redirect to the index rather than to a noperms error due to the force logout
|
|
|
|
if targetUser.ID == user.ID {
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/panel/users/edit/"+strconv.Itoa(targetUser.ID)+"?updated=1", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-10 22:00:57 +00:00
|
|
|
|
|
|
|
func UsersAvatarSubmit(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, &user)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-06-11 04:36:03 +00:00
|
|
|
// TODO: Check the UploadAvatars permission too?
|
2019-06-10 22:00:57 +00:00
|
|
|
if !user.Perms.EditUser {
|
|
|
|
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 edit doesn't exist.", w, r, user)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
if targetUser.IsAdmin && !user.IsAdmin {
|
|
|
|
return c.LocalError("Only administrators can edit the account of other administrators.", w, r, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, ferr := c.UploadAvatar(w,r,user,targetUser.ID)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
ferr = c.ChangeAvatar("." + ext, w, r, *targetUser)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Only schedule a resize if the avatar isn't tiny
|
|
|
|
err = targetUser.ScheduleAvatarResize()
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/users/edit/"+strconv.Itoa(targetUser.ID)+"?updated=1", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsersAvatarRemoveSubmit(w http.ResponseWriter, r *http.Request, user c.User, suid string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, &user)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.EditUser {
|
|
|
|
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 edit doesn't exist.", w, r, user)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
if targetUser.IsAdmin && !user.IsAdmin {
|
|
|
|
return c.LocalError("Only administrators can edit the account of other administrators.", w, r, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
ferr = c.ChangeAvatar("", w, r, *targetUser)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/users/edit/"+strconv.Itoa(targetUser.ID)+"?updated=1", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|