2017-05-06 21:21:39 +02:00
package router
2017-05-07 01:20:13 +02:00
import (
2017-05-06 21:21:39 +02:00
"net/http"
2017-05-10 03:15:29 +02:00
"strconv"
2017-05-25 04:18:31 +02:00
"time"
2017-05-10 03:15:29 +02:00
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service/captcha"
2017-05-21 01:06:40 +02:00
"github.com/NyaaPantsu/nyaa/service/notifier"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/user"
"github.com/NyaaPantsu/nyaa/service/user/form"
"github.com/NyaaPantsu/nyaa/service/user/permission"
2017-05-25 04:18:31 +02:00
"github.com/NyaaPantsu/nyaa/util/crypto"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util/languages"
2017-05-21 01:06:40 +02:00
msg "github.com/NyaaPantsu/nyaa/util/messages"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util/modelHelper"
2017-05-07 00:10:40 +02:00
"github.com/gorilla/mux"
2017-05-06 21:21:39 +02:00
)
2017-05-25 21:54:58 +02:00
// UserRegisterFormHandler : Getting View User Registration
2017-05-06 21:21:39 +02:00
func UserRegisterFormHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-07 22:00:45 +02:00
_ , errorUser := userService . CurrentUser ( r )
2017-05-19 04:55:59 +02:00
// User is already connected, redirect to home
if errorUser == nil {
2017-05-07 22:00:45 +02:00
HomeHandler ( w , r )
2017-05-19 04:55:59 +02:00
return
}
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-19 04:55:59 +02:00
registrationForm := form . RegistrationForm { }
modelHelper . BindValueForm ( & registrationForm , r )
registrationForm . CaptchaID = captcha . GetID ( )
2017-05-25 21:54:58 +02:00
urtv := formTemplateVariables {
commonTemplateVariables : newCommonVariables ( r ) ,
Form : registrationForm ,
FormErrors : messages . GetAllErrors ( ) ,
2017-05-19 04:55:59 +02:00
}
err := viewRegisterTemplate . ExecuteTemplate ( w , "index.html" , urtv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
2017-05-07 00:10:40 +02:00
}
2017-05-06 21:21:39 +02:00
}
2017-05-25 21:54:58 +02:00
// UserLoginFormHandler : Getting View User Login
2017-05-06 21:21:39 +02:00
func UserLoginFormHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-21 19:38:39 +02:00
_ , errorUser := userService . CurrentUser ( r )
// User is already connected, redirect to home
if errorUser == nil {
HomeHandler ( w , r )
return
}
2017-05-19 04:55:59 +02:00
loginForm := form . LoginForm { }
modelHelper . BindValueForm ( & loginForm , r )
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-25 21:54:58 +02:00
ulfv := formTemplateVariables {
commonTemplateVariables : newCommonVariables ( r ) ,
Form : loginForm ,
FormErrors : messages . GetAllErrors ( ) ,
2017-05-19 04:55:59 +02:00
}
2017-05-08 00:22:57 +02:00
2017-05-19 04:55:59 +02:00
err := viewLoginTemplate . ExecuteTemplate ( w , "index.html" , ulfv )
2017-05-07 04:02:57 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
2017-05-06 21:21:39 +02:00
}
2017-05-25 21:54:58 +02:00
// UserProfileHandler : Getting User Profile
2017-05-06 21:21:39 +02:00
func UserProfileHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-09 03:36:48 +02:00
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-22 10:15:18 +02:00
Ts , _ := languages . GetTfuncAndLanguageFromRequest ( r )
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-09 03:36:48 +02:00
userProfile , _ , errorUser := userService . RetrieveUserForAdmin ( id )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if errorUser == nil {
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
follow := r . URL . Query ( ) [ "followed" ]
unfollow := r . URL . Query ( ) [ "unfollowed" ]
deleteVar := r . URL . Query ( ) [ "delete" ]
2017-05-10 03:15:29 +02:00
2017-05-10 14:05:29 +02:00
if ( deleteVar != nil ) && ( userPermission . CurrentOrAdmin ( currentUser , userProfile . ID ) ) {
2017-05-09 17:47:06 +02:00
_ , errUser := userService . DeleteUser ( w , currentUser , id )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if errUser != nil {
2017-05-21 19:38:39 +02:00
messages . ImportFromError ( "errors" , errUser )
2017-05-09 17:47:06 +02:00
}
2017-05-25 21:54:58 +02:00
htv := userVerifyTemplateVariables { newCommonVariables ( r ) , messages . GetAllErrors ( ) }
2017-05-09 17:47:06 +02:00
errorTmpl := viewUserDeleteTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
} else {
if follow != nil {
2017-05-22 10:15:18 +02:00
messages . AddInfof ( "infos" , Ts ( "user_followed_msg" ) , userProfile . Username )
2017-05-10 03:15:29 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if unfollow != nil {
2017-05-22 10:15:18 +02:00
messages . AddInfof ( "infos" , Ts ( "user_unfollowed_msg" ) , userProfile . Username )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
}
2017-05-22 00:22:42 +02:00
userProfile . ParseSettings ( )
2017-05-25 21:54:58 +02:00
htv := userProfileVariables { newCommonVariables ( r ) , & userProfile , messages . GetAllInfos ( ) }
2017-05-06 21:21:39 +02:00
2017-05-09 03:36:48 +02:00
err := viewProfileTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
}
} else {
2017-05-21 19:38:39 +02:00
NotFoundHandler ( w , r )
2017-05-09 03:36:48 +02:00
}
2017-05-06 21:21:39 +02:00
}
2017-05-25 21:54:58 +02:00
// UserDetailsHandler : Getting User Profile Details View
2017-05-10 14:05:29 +02:00
func UserDetailsHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-10 14:05:29 +02:00
userProfile , _ , errorUser := userService . RetrieveUserForAdmin ( id )
2017-05-11 02:18:19 +02:00
if errorUser == nil && userPermission . CurrentOrAdmin ( currentUser , userProfile . ID ) {
2017-05-11 02:17:01 +02:00
if userPermission . CurrentOrAdmin ( currentUser , userProfile . ID ) {
b := form . UserForm { }
modelHelper . BindValueForm ( & b , r )
availableLanguages := languages . GetAvailableLanguages ( )
2017-05-22 00:22:42 +02:00
userProfile . ParseSettings ( )
2017-05-25 21:54:58 +02:00
htv := userProfileEditVariables { newCommonVariables ( r ) , & userProfile , b , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) , availableLanguages }
2017-05-11 02:17:01 +02:00
err := viewProfileEditTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
2017-05-10 14:05:29 +02:00
}
2017-05-11 02:29:29 +02:00
} else {
2017-05-21 19:38:39 +02:00
NotFoundHandler ( w , r )
2017-05-14 21:45:50 +02:00
}
2017-05-10 14:05:29 +02:00
}
2017-05-11 02:17:01 +02:00
2017-05-25 21:54:58 +02:00
// UserProfileFormHandler : Getting View User Profile Update
2017-05-06 21:21:39 +02:00
func UserProfileFormHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-09 17:47:06 +02:00
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-09 17:47:06 +02:00
userProfile , _ , errorUser := userService . RetrieveUserForAdmin ( id )
2017-05-21 00:02:57 +02:00
if errorUser != nil || ! userPermission . CurrentOrAdmin ( currentUser , userProfile . ID ) || userProfile . ID == 0 {
2017-05-19 04:55:59 +02:00
NotFoundHandler ( w , r )
return
}
2017-05-22 00:22:42 +02:00
userProfile . ParseSettings ( )
2017-05-21 18:13:28 +02:00
messages := msg . GetMessages ( r )
2017-05-19 04:55:59 +02:00
userForm := form . UserForm { }
2017-05-21 18:13:28 +02:00
userSettingsForm := form . UserSettingsForm { }
2017-05-22 10:15:18 +02:00
Ts , _ := languages . GetTfuncAndLanguageFromRequest ( r )
2017-05-19 04:55:59 +02:00
if len ( r . PostFormValue ( "email" ) ) > 0 {
2017-05-22 10:15:18 +02:00
form . EmailValidation ( r . PostFormValue ( "email" ) , messages )
2017-05-19 04:55:59 +02:00
}
if len ( r . PostFormValue ( "username" ) ) > 0 {
2017-05-22 10:15:18 +02:00
form . ValidateUsername ( r . PostFormValue ( "username" ) , messages )
2017-05-19 04:55:59 +02:00
}
2017-05-21 18:13:28 +02:00
if ! messages . HasErrors ( ) {
2017-05-19 04:55:59 +02:00
modelHelper . BindValueForm ( & userForm , r )
2017-05-21 18:13:28 +02:00
modelHelper . BindValueForm ( & userSettingsForm , r )
2017-05-19 04:55:59 +02:00
if ! userPermission . HasAdmin ( currentUser ) {
userForm . Username = userProfile . Username
userForm . Status = userProfile . Status
2017-05-09 17:47:06 +02:00
} else {
2017-05-19 04:55:59 +02:00
if userProfile . Status != userForm . Status && userForm . Status == 2 {
2017-05-21 18:13:28 +02:00
messages . AddError ( "errors" , "Elevating status to moderator is prohibited" )
2017-05-09 17:47:06 +02:00
}
}
2017-05-22 10:15:18 +02:00
modelHelper . ValidateForm ( & userForm , messages )
2017-05-21 18:13:28 +02:00
if ! messages . HasErrors ( ) {
2017-05-19 04:55:59 +02:00
if userForm . Email != userProfile . Email {
userService . SendVerificationToUser ( * currentUser , userForm . Email )
2017-05-22 10:15:18 +02:00
messages . AddInfof ( "infos" , Ts ( "email_changed" ) , userForm . Email )
2017-05-19 04:55:59 +02:00
userForm . Email = userProfile . Email // reset, it will be set when user clicks verification
}
2017-05-22 00:22:42 +02:00
userProfile , _ , errorUser = userService . UpdateUser ( w , & userForm , & userSettingsForm , currentUser , id )
2017-05-19 04:55:59 +02:00
if errorUser != nil {
2017-05-21 18:13:28 +02:00
messages . ImportFromError ( "errors" , errorUser )
2017-05-19 04:55:59 +02:00
} else {
2017-05-22 10:15:18 +02:00
messages . AddInfo ( "infos" , Ts ( "profile_updated" ) )
2017-05-19 04:55:59 +02:00
}
2017-05-09 17:47:06 +02:00
}
}
2017-05-19 04:55:59 +02:00
availableLanguages := languages . GetAvailableLanguages ( )
2017-05-25 21:54:58 +02:00
upev := userProfileEditVariables {
commonTemplateVariables : newCommonVariables ( r ) ,
2017-05-21 09:10:19 +02:00
UserProfile : & userProfile ,
UserForm : userForm ,
2017-05-21 18:13:28 +02:00
FormErrors : messages . GetAllErrors ( ) ,
FormInfos : messages . GetAllInfos ( ) ,
2017-05-21 09:10:19 +02:00
Languages : availableLanguages ,
2017-05-19 04:55:59 +02:00
}
errorTmpl := viewProfileEditTemplate . ExecuteTemplate ( w , "index.html" , upev )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
2017-05-06 21:21:39 +02:00
}
2017-05-25 21:54:58 +02:00
// UserRegisterPostHandler : Post Registration controller, we do some check on the form here, the rest on user service
2017-05-06 21:21:39 +02:00
func UserRegisterPostHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-07 19:59:38 +02:00
b := form . RegistrationForm { }
2017-05-21 20:20:40 +02:00
messages := msg . GetMessages ( r )
2017-05-21 19:38:39 +02:00
2017-05-07 14:53:01 +02:00
if ! captcha . Authenticate ( captcha . Extract ( r ) ) {
2017-05-21 19:38:39 +02:00
messages . AddError ( "errors" , "Wrong captcha!" )
2017-05-07 19:59:38 +02:00
}
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-05-08 22:12:57 +02:00
if len ( r . PostFormValue ( "email" ) ) > 0 {
2017-05-22 10:15:18 +02:00
form . EmailValidation ( r . PostFormValue ( "email" ) , messages )
2017-05-08 22:12:57 +02:00
}
2017-05-22 10:15:18 +02:00
form . ValidateUsername ( r . PostFormValue ( "username" ) , messages )
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-05-07 19:59:38 +02:00
modelHelper . BindValueForm ( & b , r )
2017-05-22 10:15:18 +02:00
modelHelper . ValidateForm ( & b , messages )
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-05-07 19:59:38 +02:00
_ , errorUser := userService . CreateUser ( w , r )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if errorUser != nil {
2017-05-21 19:38:39 +02:00
messages . ImportFromError ( "errors" , errorUser )
2017-05-07 20:47:29 +02:00
}
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-05-25 21:54:58 +02:00
common := newCommonVariables ( r )
2017-05-21 09:10:19 +02:00
common . User = & model . User {
2017-05-10 00:04:07 +02:00
Email : r . PostFormValue ( "email" ) , // indicate whether user had email set
}
2017-05-25 21:54:58 +02:00
htv := formTemplateVariables { common , b , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-07 19:59:38 +02:00
errorTmpl := viewRegisterSuccessTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
}
}
}
2017-05-07 14:53:01 +02:00
}
2017-05-21 19:38:39 +02:00
if messages . HasErrors ( ) {
UserRegisterFormHandler ( w , r )
2017-05-07 05:04:55 +02:00
}
2017-05-06 21:21:39 +02:00
}
2017-05-25 21:54:58 +02:00
// UserVerifyEmailHandler : Controller when verifying email, needs a token
2017-05-07 22:00:45 +02:00
func UserVerifyEmailHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
token := vars [ "token" ]
2017-05-21 19:38:39 +02:00
messages := msg . GetMessages ( r )
2017-05-07 22:00:45 +02:00
_ , errEmail := userService . EmailVerification ( token , w )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if errEmail != nil {
2017-05-21 19:38:39 +02:00
messages . ImportFromError ( "errors" , errEmail )
2017-05-07 22:00:45 +02:00
}
2017-05-25 21:54:58 +02:00
htv := userVerifyTemplateVariables { newCommonVariables ( r ) , messages . GetAllErrors ( ) }
2017-05-07 22:00:45 +02:00
errorTmpl := viewVerifySuccessTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
}
2017-05-25 21:54:58 +02:00
// UserLoginPostHandler : Post Login controller
2017-05-06 21:21:39 +02:00
func UserLoginPostHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-08 00:21:31 +02:00
b := form . LoginForm { }
modelHelper . BindValueForm ( & b , r )
2017-05-21 20:20:40 +02:00
messages := msg . GetMessages ( r )
2017-05-21 19:38:39 +02:00
2017-05-22 10:15:18 +02:00
modelHelper . ValidateForm ( & b , messages )
2017-05-21 19:38:39 +02:00
if ! messages . HasErrors ( ) {
2017-05-08 00:21:31 +02:00
_ , errorUser := userService . CreateUserAuthentication ( w , r )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if errorUser != nil {
2017-05-21 19:38:39 +02:00
messages . ImportFromError ( "errors" , errorUser )
2017-05-25 21:54:58 +02:00
htv := formTemplateVariables { newCommonVariables ( r ) , b , messages . GetAllErrors ( ) , messages . GetAllInfos ( ) }
2017-05-08 00:21:31 +02:00
errorTmpl := viewLoginTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
2017-05-20 12:45:27 +02:00
return
2017-05-08 00:21:31 +02:00
}
2017-05-25 21:54:58 +02:00
url , _ := Router . Get ( "home" ) . URL ( )
http . Redirect ( w , r , url . String ( ) , http . StatusSeeOther )
2017-05-08 00:21:31 +02:00
}
2017-05-21 19:38:39 +02:00
if messages . HasErrors ( ) {
2017-05-24 09:11:13 +02:00
UserLoginFormHandler ( w , r )
2017-05-20 01:10:16 +02:00
}
2017-05-06 21:21:39 +02:00
}
2017-05-25 21:54:58 +02:00
// UserLogoutHandler : Controller to logout users
2017-05-09 03:36:48 +02:00
func UserLogoutHandler ( w http . ResponseWriter , r * http . Request ) {
_ , _ = userService . ClearCookie ( w )
url , _ := Router . Get ( "home" ) . URL ( )
http . Redirect ( w , r , url . String ( ) , http . StatusSeeOther )
}
2017-05-10 03:15:29 +02:00
2017-05-25 21:54:58 +02:00
// UserFollowHandler : Controller to follow/unfollow users, need user id to follow
2017-05-10 03:15:29 +02:00
func UserFollowHandler ( w http . ResponseWriter , r * http . Request ) {
var followAction string
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-10 03:15:29 +02:00
user , _ , errorUser := userService . RetrieveUserForAdmin ( id )
2017-05-21 00:02:57 +02:00
if errorUser == nil && user . ID > 0 {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
if ! userPermission . IsFollower ( & user , currentUser ) {
2017-05-10 03:15:29 +02:00
followAction = "followed"
userService . SetFollow ( & user , currentUser )
} else {
followAction = "unfollowed"
userService . RemoveFollow ( & user , currentUser )
}
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
url , _ := Router . Get ( "user_profile" ) . URL ( "id" , strconv . Itoa ( int ( user . ID ) ) , "username" , user . Username )
2017-05-10 03:15:29 +02:00
http . Redirect ( w , r , url . String ( ) + "?" + followAction , http . StatusSeeOther )
}
2017-05-21 00:02:57 +02:00
2017-05-25 21:54:58 +02:00
// UserNotificationsHandler : Controller to show user notifications
2017-05-21 00:02:57 +02:00
func UserNotificationsHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-21 00:02:57 +02:00
if currentUser . ID > 0 {
2017-05-21 01:06:40 +02:00
messages := msg . GetMessages ( r )
2017-05-21 01:22:07 +02:00
Ts , _ := languages . GetTfuncAndLanguageFromRequest ( r )
2017-05-21 01:06:40 +02:00
if r . URL . Query ( ) [ "clear" ] != nil {
notifierService . DeleteAllNotifications ( currentUser . ID )
2017-05-21 01:22:07 +02:00
messages . AddInfo ( "infos" , Ts ( "notifications_cleared" ) )
2017-05-21 01:06:40 +02:00
currentUser . Notifications = [ ] model . Notification { }
}
2017-05-26 14:33:55 +02:00
htv := userProfileVariables { newCommonVariables ( r ) , currentUser , messages . GetAllInfos ( ) }
2017-05-21 00:02:57 +02:00
err := viewProfileNotifTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
} else {
NotFoundHandler ( w , r )
}
2017-05-21 09:10:19 +02:00
}
2017-05-25 04:18:31 +02:00
2017-05-25 21:54:58 +02:00
// UserAPIKeyResetHandler : Controller to reset user api key
func UserAPIKeyResetHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-25 04:18:31 +02:00
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-25 21:54:58 +02:00
currentUser := getUser ( r )
2017-05-25 04:18:31 +02:00
Ts , _ := languages . GetTfuncAndLanguageFromRequest ( r )
messages := msg . GetMessages ( r )
userProfile , _ , errorUser := userService . RetrieveUserForAdmin ( id )
if errorUser != nil || ! userPermission . CurrentOrAdmin ( currentUser , userProfile . ID ) || userProfile . ID == 0 {
NotFoundHandler ( w , r )
return
}
2017-05-26 12:12:52 +02:00
userProfile . APIToken , _ = crypto . GenerateRandomToken32 ( )
userProfile . APITokenExpiry = time . Unix ( 0 , 0 )
2017-05-25 04:18:31 +02:00
_ , errorUser = userService . UpdateUserCore ( & userProfile )
if errorUser != nil {
messages . ImportFromError ( "errors" , errorUser )
} else {
messages . AddInfo ( "infos" , Ts ( "profile_updated" ) )
}
}