2017-05-06 21:21:39 +02:00
package router
2017-05-07 01:20:13 +02:00
import (
2017-05-10 03:15:29 +02:00
"fmt"
2017-05-06 21:21:39 +02:00
"net/http"
2017-05-10 03:15:29 +02:00
"strconv"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/service/captcha"
"github.com/NyaaPantsu/nyaa/service/user"
"github.com/NyaaPantsu/nyaa/service/user/form"
"github.com/NyaaPantsu/nyaa/service/user/permission"
"github.com/NyaaPantsu/nyaa/util/languages"
"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
)
// Getting View User Registration
func UserRegisterFormHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-07 22:00:45 +02:00
_ , errorUser := userService . CurrentUser ( 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-07 22:00:45 +02:00
b := form . RegistrationForm { }
modelHelper . BindValueForm ( & b , r )
b . CaptchaID = captcha . GetID ( )
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( viewRegisterTemplate , r )
2017-05-15 13:55:16 +02:00
htv := UserRegisterTemplateVariables { b , form . NewErrors ( ) , NewSearchForm ( ) , NewNavigation ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-07 22:00:45 +02:00
err := viewRegisterTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
} else {
HomeHandler ( w , r )
2017-05-07 00:10:40 +02:00
}
2017-05-06 21:21:39 +02:00
}
// Getting View User Login
func UserLoginFormHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-07 04:02:57 +02:00
b := form . LoginForm { }
modelHelper . BindValueForm ( & b , r )
2017-05-08 00:22:57 +02:00
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( viewLoginTemplate , r )
2017-05-15 13:55:16 +02:00
htv := UserLoginFormVariables { b , form . NewErrors ( ) , NewSearchForm ( ) , NewNavigation ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-08 00:22:57 +02:00
2017-05-07 04:02:57 +02:00
err := viewLoginTemplate . ExecuteTemplate ( w , "index.html" , htv )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
2017-05-06 21:21:39 +02:00
}
// Getting User Profile
func UserProfileHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-09 03:36:48 +02:00
vars := mux . Vars ( r )
id := vars [ "id" ]
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 {
currentUser := GetUser ( r )
follow := r . URL . Query ( ) [ "followed" ]
unfollow := r . URL . Query ( ) [ "unfollowed" ]
infosForm := form . NewInfos ( )
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
err := form . NewErrors ( )
_ , 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-09 17:47:06 +02:00
err [ "errors" ] = append ( err [ "errors" ] , errUser . Error ( ) )
}
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( viewUserDeleteTemplate , r )
2017-05-15 13:55:16 +02:00
htv := UserVerifyTemplateVariables { err , NewSearchForm ( ) , NewNavigation ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
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 {
2017-05-14 21:45:50 +02:00
T := languages . SetTranslationFromRequest ( viewProfileTemplate , 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 follow != nil {
2017-05-10 03:15:29 +02:00
infosForm [ "infos" ] = append ( infosForm [ "infos" ] , fmt . Sprintf ( T ( "user_followed_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
if unfollow != nil {
2017-05-10 03:15:29 +02:00
infosForm [ "infos" ] = append ( infosForm [ "infos" ] , fmt . Sprintf ( T ( "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-15 13:55:16 +02:00
htv := UserProfileVariables { & userProfile , infosForm , NewSearchForm ( ) , NewNavigation ( ) , currentUser , r . URL , mux . CurrentRoute ( r ) }
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-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( notFoundTemplate , r )
2017-05-15 13:55:16 +02:00
err := notFoundTemplate . ExecuteTemplate ( w , "index.html" , NotFoundTemplateVariables { NewNavigation ( ) , NewSearchForm ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) } )
2017-05-09 04:23:45 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
2017-05-09 03:36:48 +02:00
}
2017-05-06 21:21:39 +02:00
}
2017-05-10 14:05:29 +02:00
//Getting User Profile Details View
func UserDetailsHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
id := vars [ "id" ]
2017-05-11 02:17:01 +02:00
currentUser := GetUser ( 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 )
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( viewProfileEditTemplate , r )
2017-05-11 02:17:01 +02:00
availableLanguages := languages . GetAvailableLanguages ( )
2017-05-15 13:55:16 +02:00
htv := UserProfileEditVariables { & userProfile , b , form . NewErrors ( ) , form . NewInfos ( ) , availableLanguages , NewSearchForm ( ) , NewNavigation ( ) , currentUser , r . URL , mux . CurrentRoute ( r ) }
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-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( notFoundTemplate , r )
2017-05-15 13:55:16 +02:00
err := notFoundTemplate . ExecuteTemplate ( w , "index.html" , NotFoundTemplateVariables { NewNavigation ( ) , NewSearchForm ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) } )
2017-05-11 02:29:29 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
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-07 01:20:13 +02:00
// 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" ]
currentUser := GetUser ( r )
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 {
if userPermission . CurrentOrAdmin ( currentUser , userProfile . ID ) {
2017-05-09 17:47:06 +02:00
b := form . UserForm { }
err := form . NewErrors ( )
infos := form . NewInfos ( )
2017-05-14 21:45:50 +02:00
T := languages . SetTranslationFromRequest ( viewProfileEditTemplate , r )
2017-05-09 17:47:06 +02:00
if len ( r . PostFormValue ( "email" ) ) > 0 {
_ , err = form . EmailValidation ( r . PostFormValue ( "email" ) , err )
}
if len ( r . PostFormValue ( "username" ) ) > 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
_ , err = form . ValidateUsername ( r . PostFormValue ( "username" ) , err )
2017-05-09 17:47:06 +02:00
}
2017-05-10 23:43:58 +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 len ( err ) == 0 {
2017-05-09 17:47:06 +02:00
modelHelper . BindValueForm ( & b , r )
2017-05-11 02:17:01 +02:00
if ! userPermission . HasAdmin ( currentUser ) {
2017-05-11 23:31:34 +02:00
b . Username = userProfile . Username
b . Status = userProfile . Status
2017-05-11 11:58:44 +02:00
} else {
2017-05-11 23:31:34 +02:00
if userProfile . Status != b . Status && b . Status == 2 {
2017-05-11 11:58:44 +02:00
err [ "errors" ] = append ( err [ "errors" ] , "Elevating status to moderator is prohibited" )
}
2017-05-10 20:24:37 +02:00
}
2017-05-09 17:47:06 +02:00
err = modelHelper . ValidateForm ( & b , err )
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 len ( err ) == 0 {
2017-05-11 23:31:34 +02:00
if b . Email != userProfile . Email {
2017-05-10 23:43:58 +02:00
userService . SendVerificationToUser ( * currentUser , b . Email )
infos [ "infos" ] = append ( infos [ "infos" ] , fmt . Sprintf ( T ( "email_changed" ) , b . Email ) )
2017-05-11 23:31:34 +02:00
b . Email = userProfile . Email // reset, it will be set when user clicks verification
2017-05-10 23:43:58 +02:00
}
2017-05-09 17:47:06 +02:00
userProfile , _ , errorUser = userService . UpdateUser ( w , & b , 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 errorUser != nil {
2017-05-09 17:47:06 +02:00
err [ "errors" ] = append ( err [ "errors" ] , errorUser . Error ( ) )
2017-05-10 23:43:58 +02:00
} else {
2017-05-09 17:47:06 +02:00
infos [ "infos" ] = append ( infos [ "infos" ] , T ( "profile_updated" ) )
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-09 17:47:06 +02:00
}
2017-05-10 21:45:39 +02:00
availableLanguages := languages . GetAvailableLanguages ( )
2017-05-15 13:55:16 +02:00
htv := UserProfileEditVariables { & userProfile , b , err , infos , availableLanguages , NewSearchForm ( ) , NewNavigation ( ) , currentUser , r . URL , mux . CurrentRoute ( r ) }
2017-05-09 17:47:06 +02:00
errorTmpl := viewProfileEditTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
}
} else {
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( notFoundTemplate , r )
2017-05-15 13:55:16 +02:00
err := notFoundTemplate . ExecuteTemplate ( w , "index.html" , NotFoundTemplateVariables { NewNavigation ( ) , NewSearchForm ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) } )
2017-05-09 17:47:06 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
}
} else {
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( notFoundTemplate , r )
2017-05-15 13:55:16 +02:00
err := notFoundTemplate . ExecuteTemplate ( w , "index.html" , NotFoundTemplateVariables { NewNavigation ( ) , NewSearchForm ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) } )
2017-05-09 17:47:06 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
}
2017-05-06 21:21:39 +02:00
}
2017-05-07 14:53:01 +02:00
// 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 { }
err := form . NewErrors ( )
2017-05-07 14:53:01 +02:00
if ! captcha . Authenticate ( captcha . Extract ( r ) ) {
2017-05-07 19:59:38 +02:00
err [ "errors" ] = append ( err [ "errors" ] , "Wrong captcha!" )
}
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 len ( err ) == 0 {
2017-05-08 22:12:57 +02:00
if len ( r . PostFormValue ( "email" ) ) > 0 {
_ , err = form . EmailValidation ( r . PostFormValue ( "email" ) , err )
}
2017-05-07 19:59:38 +02:00
_ , err = form . ValidateUsername ( r . PostFormValue ( "username" ) , err )
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 len ( err ) == 0 {
2017-05-07 19:59:38 +02:00
modelHelper . BindValueForm ( & b , r )
err = modelHelper . ValidateForm ( & b , err )
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 len ( err ) == 0 {
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-07 20:47:29 +02:00
err [ "errors" ] = append ( err [ "errors" ] , errorUser . Error ( ) )
}
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 len ( err ) == 0 {
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( viewRegisterSuccessTemplate , r )
2017-05-10 00:04:07 +02:00
u := model . User {
Email : r . PostFormValue ( "email" ) , // indicate whether user had email set
}
2017-05-15 13:55:16 +02:00
htv := UserRegisterTemplateVariables { b , err , NewSearchForm ( ) , NewNavigation ( ) , & u , r . URL , mux . CurrentRoute ( r ) }
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
}
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 len ( err ) > 0 {
2017-05-07 19:59:38 +02:00
b . CaptchaID = captcha . GetID ( )
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( viewRegisterTemplate , r )
2017-05-15 13:55:16 +02:00
htv := UserRegisterTemplateVariables { b , err , NewSearchForm ( ) , NewNavigation ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
2017-05-07 19:59:38 +02:00
errorTmpl := viewRegisterTemplate . ExecuteTemplate ( w , "index.html" , htv )
if errorTmpl != nil {
http . Error ( w , errorTmpl . Error ( ) , http . StatusInternalServerError )
2017-05-07 05:04:55 +02:00
}
}
2017-05-06 21:21:39 +02:00
}
2017-05-07 22:00:45 +02:00
func UserVerifyEmailHandler ( w http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
token := vars [ "token" ]
err := form . NewErrors ( )
_ , 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-07 22:00:45 +02:00
err [ "errors" ] = append ( err [ "errors" ] , errEmail . Error ( ) )
}
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( viewVerifySuccessTemplate , r )
2017-05-15 13:55:16 +02:00
htv := UserVerifyTemplateVariables { err , NewSearchForm ( ) , NewNavigation ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
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-06 21:21:39 +02:00
// Post Login controller
func UserLoginPostHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-08 00:21:31 +02:00
b := form . LoginForm { }
modelHelper . BindValueForm ( & b , r )
err := form . NewErrors ( )
err = modelHelper . ValidateForm ( & b , err )
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 len ( err ) == 0 {
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-08 00:21:31 +02:00
err [ "errors" ] = append ( err [ "errors" ] , errorUser . Error ( ) )
2017-05-14 21:45:50 +02:00
languages . SetTranslationFromRequest ( viewLoginTemplate , r )
2017-05-15 13:55:16 +02:00
htv := UserLoginFormVariables { b , err , NewSearchForm ( ) , NewNavigation ( ) , GetUser ( r ) , r . URL , mux . CurrentRoute ( r ) }
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 )
}
} else {
url , _ := Router . Get ( "home" ) . URL ( )
http . Redirect ( w , r , url . String ( ) , http . StatusSeeOther )
}
}
2017-05-06 21:21:39 +02:00
}
2017-05-09 03:36:48 +02:00
// Logout
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
func UserFollowHandler ( w http . ResponseWriter , r * http . Request ) {
var followAction string
vars := mux . Vars ( r )
id := vars [ "id" ]
currentUser := GetUser ( r )
user , _ , 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 {
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 )
}