Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/controllers/user/profile.go
kilo 5dcd30676f
Janitor (#1728)
* Fix "torrent is being generated" message showing up even when the torrent couldn't be generated

* Add janitor user status

* Remove usage of deprecated HasAdmin() function

* Give Janitors access to mod panel

* Stop using deprecated HasAdmin() function

* Stop using deprecated HasAdmin() function

* Update edit.go

* Update profile.go

* Rollback bad changes, remove redundant email check and stop using deprecated function

* Show every userstatus (member, janitor, banned, mod) in comments

* Return empty status if anonymous

* Show no userstatus for anonymous profile

* Show moderation link to janitors too

* Stop using deprecated HasAdmin() function

* Stop using deprecated HasAdmin() function

* Stop using deprecated HasAdmin() function

* Add Janitor to userstatus select in user edit

* "Janitor" translation string

* didnt mean to commit this change

* rollback wrongfully comitted changes

* rollback commit

* Update CHANGELOG.md

* Fix wrong id for translation

* remove deprecated HasAdmin() function again

* change name of variable used in comment loop for clarity purposes

* visual cue for locked torrents in torrent listing for admins

* add visual cues for hidden torrents in admin torrent listing

* Dont show delete buttons for janitors

* janitor cannot delete torrents

* show block/unblock button in torrent list for janitors instead of delete button

* fix function that didnt get executed

* add ban buttons on userlist & visual cue for banned users

* Fix "user successfully deleted" message showing even if user wasnt deleted

* Add "ban" button, no "delete" button for jantiors

* add "unban" and "ban" translation strings

* add "unban" and "ban" translation strings

* different <form> for ban button

* Update index.jet.html

* add userprofile ban route

* add toggleBan() function, janitors dont need captcha either

* fix panic error when deleting an anonymous comment

* add user_banned_by and user_unbanned_by

* add user_banned_by and user_unbanned_by

* Make ToggleBan() return whether or not the user is now banned

* Add handler for /ban route

* change log filter

* hide locked torrents from regular users

* hide locked torrents from regular users

* hide locked torrent from api search for regular users

* change function to CurrentOrJanitor

* change function to currentorjanitor

* change function to currentorjanitor

* add CurrentOrJanitor function

* fix extra (

* fix extra ) and wrong variable name

* Fix wrong value for janitor user status

* Fix user edit that did not work because of "unique constraint user.emails" error

* only immediately visually update user if user has been updated successfully

* use FindAllForAdminsOrderBy in order to preload users

* create FindAllForAdminsOrderBy that preloads users

* Show username instead of ID in Uploader column in admin panel

* Fix userprofile buttons overflowing at some specific resolutions

* Mods can set users as janitors

* Show ban/unban buttons on userprofile for janitors

* Identical styling for usermenu links and buttons

* dont show ban buttons on other staff

* add ban message through get parameter

* make toggleBan() trigger user update

* Add "user_banned" and "user_unbanned"

* add "user_banned" and "user_unbanned"

* Visual cue for banned user

* banned users can still log in

* visual cue for banned user in  badgemenu

* locked status if banned user on upload

* banned users cannot comment

* Put "banned" text between ()

* add GetCategoryName() to template_test

* add GetCategoryName() that returns full category name from full category string

* Show search content in page title if it exists, or search category if it exists, otherwise shows "Home"

* error message when user uploads an torrent & is banned

* add torrent_uploaded_locked

* add torrent_uploaded_locked

* fix delete definitely button that never appeared

* Show delete definitely button on admin panel index

* admins can undelete a torrent by editing it's status

* Trigger ViewHandler() directly instead of redirecting

* Render the template directly instead of triggering torrent view handler

* bigger usermenu buttons once responsive design kicks in

* make btn-* class non-bold

* Responsive notification page

* visual cue for locked torrents in torrent listing

* Update search.go

* Update search.go

* Update api.go

* Update helpers.go

* Update template.go

* Update torrentParam.go

* remove "hidden" class

* Update search.go

* fix an html error

* Add files via upload

* Update admin.jet.html

* Update index.go

* Update index.go

* Update router.go

* Update torrentParam_test.go

* Update torrentParam_test.go

* fix extra "

* fix bad copypaste

* Update CHANGELOG.md

* Update guidelines.jet.html

* Update CHANGELOG.md

* add Guidelines and Moderation Guidelines

* fix missing commas

* Update torrentlist.jet.html

* Update find.go

* Update stats.go

* Update view.jet.html

* Update index.jet.html
2017-11-14 09:39:39 +01:00

284 lignes
8,8 Kio
Go

package userController
import (
"strconv"
"time"
"fmt"
"net/http"
"github.com/NyaaPantsu/nyaa/controllers/router"
"github.com/NyaaPantsu/nyaa/models/notifications"
"github.com/NyaaPantsu/nyaa/models/activities"
"github.com/NyaaPantsu/nyaa/models/users"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/templates"
"github.com/NyaaPantsu/nyaa/utils/cookies"
"github.com/NyaaPantsu/nyaa/utils/crypto"
"github.com/NyaaPantsu/nyaa/utils/email"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
"github.com/NyaaPantsu/nyaa/utils/validator"
"github.com/NyaaPantsu/nyaa/utils/validator/user"
"github.com/gin-gonic/gin"
)
// UserProfileDelete : Deleting User Profile
func UserProfileDelete(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser == nil{
currentUser := router.GetUser(c)
if (currentUser.CurrentOrAdmin(userProfile.ID)) {
_, err := userProfile.Delete(currentUser)
if err == nil && currentUser.CurrentUserIdentical(userProfile.ID) {
cookies.Clear(c)
}
templates.Static(c, "site/static/delete_success.jet.html")
}
} else {
c.AbortWithStatus(http.StatusNotFound)
}
}
// UserProfileBan : Ban an User
func UserProfileBan(c *gin.Context) {
currentUser := router.GetUser(c)
if currentUser.IsJanitor() {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser == nil && !userProfile.IsModerator() {
action := "user_unbanned_by"
message := "?unbanned"
if userProfile.ToggleBan() {
action = "user_banned_by"
message = "?banned"
}
activities.Log(&models.User{}, fmt.Sprintf("user_%d", id), "edit", action, userProfile.Username, strconv.Itoa(int(id)), currentUser.Username)
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/user/%d/%s", id, c.Param("username") + message))
} else {
c.AbortWithStatus(http.StatusNotFound)
}
} else {
c.AbortWithStatus(http.StatusNotFound)
}
}
// UserProfileHandler : Getting User Profile
func UserProfileHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
Ts, _ := publicSettings.GetTfuncAndLanguageFromRequest(c)
messages := msg.GetMessages(c)
if c.Param("id") != "0" && id == 0 && ContainsNonNumbersChars(c.Param("id")) {
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/username/%s", c.Param("id")))
return
}
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser == nil {
currentUser := router.GetUser(c)
follow := c.Request.URL.Query()["followed"]
unfollow := c.Request.URL.Query()["unfollowed"]
deleteVar := c.Request.URL.Query()["delete"]
if !((deleteVar != nil) && (currentUser.CurrentOrAdmin(userProfile.ID))) {
if follow != nil {
messages.AddInfof("infos", Ts("user_followed_msg"), userProfile.Username)
}
if unfollow != nil {
messages.AddInfof("infos", Ts("user_unfollowed_msg"), userProfile.Username)
}
userProfile.ParseSettings()
templates.UserProfile(c, userProfile)
}
} else {
variables := templates.Commonvariables(c)
templates.Render(c, "errors/user_not_found.jet.html", variables)
}
}
func ContainsNonNumbersChars(source string) bool {
for char := range source {
if char < 30 || char > 39 {
return true
}
}
return false
}
func UserGetFromName(c *gin.Context) {
username := c.Param("username")
Ts, _ := publicSettings.GetTfuncAndLanguageFromRequest(c)
messages := msg.GetMessages(c)
userProfile, _, _, err := users.FindByUsername(username)
if err == nil {
currentUser := router.GetUser(c)
follow := c.Request.URL.Query()["followed"]
unfollow := c.Request.URL.Query()["unfollowed"]
deleteVar := c.Request.URL.Query()["delete"]
if (deleteVar != nil) && (currentUser.CurrentOrAdmin(userProfile.ID)) {
_, err := userProfile.Delete(currentUser)
if err == nil && currentUser.CurrentUserIdentical(userProfile.ID) {
cookies.Clear(c)
}
templates.Static(c, "site/static/delete_success.jet.html")
} else {
if follow != nil {
messages.AddInfof("infos", Ts("user_followed_msg"), userProfile.Username)
}
if unfollow != nil {
messages.AddInfof("infos", Ts("user_unfollowed_msg"), userProfile.Username)
}
userProfile.ParseSettings()
templates.UserProfile(c, userProfile)
}
} else {
variables := templates.Commonvariables(c)
searchForm := templates.NewSearchForm(c)
searchForm.User = username
variables.Set("Search", searchForm)
templates.Render(c, "errors/user_not_found.jet.html", variables)
}
}
func RedirectToUserSearch(c *gin.Context) {
username := c.Query("username")
if username == "" {
variables := templates.Commonvariables(c)
templates.Render(c, "errors/user_not_found.jet.html", variables)
} else {
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/username/%s", username))
}
}
// UserDetailsHandler : Getting User Profile Details View
func UserDetailsHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
currentUser := router.GetUser(c)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser == nil && currentUser.CurrentOrAdmin(userProfile.ID) {
b := userValidator.UserForm{}
c.Bind(&b)
availableLanguages := publicSettings.GetAvailableLanguages()
userProfile.ParseSettings()
templates.UserProfileEdit(c, userProfile, b, availableLanguages)
} else {
variables := templates.Commonvariables(c)
templates.Render(c, "errors/user_not_found.jet.html", variables)
}
}
// UserProfileFormHandler : Updating User Profile
func UserProfileFormHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
currentUser := router.GetUser(c)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser != nil || !currentUser.CurrentOrAdmin(userProfile.ID) || userProfile.ID == 0 {
c.Status(http.StatusNotFound)
return
}
userProfile.ParseSettings()
messages := msg.GetMessages(c)
userForm := userValidator.UserForm{}
userSettingsForm := userValidator.UserSettingsForm{}
if len(c.PostForm("email")) > 0 {
if !userValidator.EmailValidation(c.PostForm("email")) {
messages.AddErrorT("email", "email_not_valid")
}
}
if len(c.PostForm("username")) > 0 {
if !userValidator.ValidateUsername(c.PostForm("username")) {
messages.AddErrorT("username", "username_illegal")
}
}
if !messages.HasErrors() {
c.Bind(&userForm)
c.Bind(&userSettingsForm)
if !currentUser.IsModerator() {
userForm.Username = userProfile.Username
userForm.Status = userProfile.Status
} else {
if userProfile.Status != userForm.Status && (userForm.Status == 2){
messages.AddErrorT("errors", "elevating_user_error")
}
}
validator.ValidateForm(&userForm, messages)
if !messages.HasErrors() {
if userForm.Email != userProfile.Email {
if currentUser.IsModerator() {
userProfile.Email = userForm.Email // reset, it will be set when user clicks verification
} else {
email.SendVerificationToUser(userProfile, userForm.Email)
messages.AddInfoTf("infos", "email_changed", userForm.Email)
userForm.Email = userProfile.Email // reset, it will be set when user clicks verification
}
}
user, _, err := users.UpdateFromRequest(c, &userForm, &userSettingsForm, currentUser, uint(id))
if err != nil {
messages.Error(err)
}
if !messages.HasErrors() {
messages.AddInfoT("infos", "profile_updated")
userProfile = user
}
}
}
availableLanguages := publicSettings.GetAvailableLanguages()
templates.UserProfileEdit(c, userProfile, userForm, availableLanguages)
}
// UserNotificationsHandler : Controller to show user notifications
func UserNotificationsHandler(c *gin.Context) {
currentUser := router.GetUser(c)
if currentUser.ID > 0 {
if c.Request.URL.Query()["clear"] != nil {
notifications.DeleteNotifications(currentUser, false)
} else if c.Request.URL.Query()["clear_all"] != nil {
notifications.DeleteNotifications(currentUser, true)
} else if c.Request.URL.Query()["read_all"] != nil {
notifications.MarkAllNotificationsAsRead(currentUser)
}
templates.UserProfileNotifications(c, currentUser)
} else {
c.Status(http.StatusNotFound)
}
}
// UserAPIKeyResetHandler : Controller to reset user api key
func UserAPIKeyResetHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
currentUser := router.GetUser(c)
messages := msg.GetMessages(c)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser != nil || !currentUser.CurrentOrAdmin(userProfile.ID) || userProfile.ID == 0 {
c.Status(http.StatusNotFound)
return
}
userProfile.APIToken, _ = crypto.GenerateRandomToken32()
userProfile.APITokenExpiry = time.Unix(0, 0)
_, errorUser = userProfile.UpdateRaw()
if errorUser != nil {
messages.Error(errorUser)
} else {
messages.AddInfoT("infos", "profile_updated")
}
UserDetailsHandler(c)
}