2017-05-06 21:21:39 +02:00
package userService
import (
"errors"
2017-05-09 01:56:57 +02:00
"fmt"
2017-05-06 21:21:39 +02:00
"net/http"
"strconv"
2017-05-08 19:26:29 +02:00
"time"
2017-05-06 21:21:39 +02:00
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/db"
"github.com/NyaaPantsu/nyaa/model"
formStruct "github.com/NyaaPantsu/nyaa/service/user/form"
"github.com/NyaaPantsu/nyaa/service/user/permission"
"github.com/NyaaPantsu/nyaa/util/crypto"
"github.com/NyaaPantsu/nyaa/util/log"
2017-06-13 08:01:57 +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-09 01:56:57 +02:00
"golang.org/x/crypto/bcrypt"
2017-05-06 21:21:39 +02:00
)
2017-05-26 12:12:52 +02:00
// NewCurrentUserRetriever create CurrentUserRetriever Struct for languages
2017-05-14 21:45:50 +02:00
func NewCurrentUserRetriever ( ) * CurrentUserRetriever {
return & CurrentUserRetriever { }
}
2017-05-26 12:12:52 +02:00
// CurrentUserRetriever struct for languages
2017-05-14 21:45:50 +02:00
type CurrentUserRetriever struct { }
2017-05-26 12:12:52 +02:00
// RetrieveCurrentUser retrieve current user for languages
2017-05-14 21:45:50 +02:00
func ( * CurrentUserRetriever ) RetrieveCurrentUser ( r * http . Request ) ( model . User , error ) {
user , _ , err := RetrieveCurrentUser ( r )
return user , err
}
2017-05-06 21:21:39 +02:00
// SuggestUsername suggest user's name if user's name already occupied.
func SuggestUsername ( username string ) string {
var count int
var usernameCandidate string
db . ORM . Model ( model . User { } ) . Where ( & model . User { Username : username } ) . Count ( & count )
log . Debugf ( "count Before : %d" , count )
if count == 0 {
return 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
}
var postfix int
for {
usernameCandidate = username + strconv . Itoa ( postfix )
log . Debugf ( "usernameCandidate: %s\n" , usernameCandidate )
db . ORM . Model ( model . User { } ) . Where ( & model . User { Username : usernameCandidate } ) . Count ( & count )
log . Debugf ( "count after : %d\n" , count )
postfix = postfix + 1
if count == 0 {
break
2017-05-06 21:21:39 +02:00
}
}
return usernameCandidate
}
2017-05-08 22:12:57 +02:00
2017-05-26 12:12:52 +02:00
// CheckEmail : check if email is in database
2017-05-07 21:14:32 +02:00
func CheckEmail ( email string ) bool {
2017-05-08 22:12:57 +02:00
if len ( email ) == 0 {
2017-05-09 17:06:21 +02:00
return false
2017-05-08 22:12:57 +02:00
}
2017-05-07 21:14:32 +02:00
var count int
2017-05-08 22:12:57 +02:00
db . ORM . Model ( model . User { } ) . Where ( "email = ?" , email ) . Count ( & count )
2017-05-09 17:06:21 +02:00
if count != 0 {
return true // error: duplicate
2017-05-07 21:14:32 +02:00
}
2017-05-09 17:06:21 +02:00
return false
2017-05-07 21:14:32 +02:00
}
2017-05-08 22:12:57 +02:00
2017-05-06 21:21:39 +02:00
// CreateUserFromForm creates a user from a registration form.
2017-05-07 00:10:40 +02:00
func CreateUserFromForm ( registrationForm formStruct . RegistrationForm ) ( model . User , error ) {
2017-05-06 21:21:39 +02:00
var user model . User
log . Debugf ( "registrationForm %+v\n" , registrationForm )
modelHelper . AssignValue ( & user , & registrationForm )
2017-05-09 17:20:19 +02:00
if user . Email == "" {
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
user . MD5 = ""
2017-05-09 17:20:19 +02:00
} else {
2017-05-10 21:16:30 +02:00
// Despite the email not being verified yet we calculate this for convenience reasons
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
var err error
user . MD5 , err = crypto . GenerateMD5Hash ( user . Email )
if err != nil {
return user , err
}
2017-05-09 17:20:19 +02:00
}
2017-05-10 21:16:30 +02:00
user . Email = "" // unset email because it will be verified later
2017-05-12 12:40:31 +02:00
user . CreatedAt = time . Now ( )
2017-05-21 19:38:39 +02:00
// User settings to default
2017-05-21 20:20:40 +02:00
user . Settings . ToDefault ( )
user . SaveSettings ( )
2017-05-12 12:40:31 +02:00
// currently unused but needs to be set:
2017-05-26 12:12:52 +02:00
user . APIToken , _ = crypto . GenerateRandomToken32 ( )
user . APITokenExpiry = time . Unix ( 0 , 0 )
2017-05-09 17:20:19 +02:00
2017-05-06 21:21:39 +02:00
if db . ORM . Create ( & user ) . Error != nil {
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
return user , errors . New ( "user not created" )
2017-05-06 21:21:39 +02:00
}
2017-05-09 17:20:19 +02:00
2017-05-06 21:21:39 +02:00
return user , nil
}
// CreateUser creates a user.
func CreateUser ( w http . ResponseWriter , r * http . Request ) ( int , error ) {
var user model . User
2017-05-07 00:10:40 +02:00
var registrationForm formStruct . RegistrationForm
2017-05-06 21:21:39 +02:00
var status int
var err error
2017-05-09 01:56:57 +02:00
2017-05-06 21:21:39 +02:00
modelHelper . BindValueForm ( & registrationForm , r )
2017-05-07 21:14:32 +02:00
usernameCandidate := SuggestUsername ( registrationForm . Username )
2017-05-09 01:56:57 +02:00
if usernameCandidate != registrationForm . Username {
2017-05-07 21:14:32 +02:00
return http . StatusInternalServerError , fmt . Errorf ( "Username already taken, you can choose: %s" , usernameCandidate )
}
2017-05-10 22:42:11 +02:00
if registrationForm . Email != "" && CheckEmail ( registrationForm . Email ) {
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
return http . StatusInternalServerError , errors . New ( "email address already in database" )
2017-05-07 21:14:32 +02:00
}
2017-05-06 21:21:39 +02:00
password , err := bcrypt . GenerateFromPassword ( [ ] byte ( registrationForm . Password ) , 10 )
if err != nil {
return http . StatusInternalServerError , err
}
registrationForm . Password = string ( password )
user , err = CreateUserFromForm ( registrationForm )
if err != nil {
return http . StatusInternalServerError , err
}
2017-05-14 21:45:50 +02:00
if registrationForm . Email != "" {
2017-05-10 22:42:11 +02:00
SendVerificationToUser ( user , registrationForm . Email )
}
2017-05-06 22:27:21 +02:00
status , err = RegisterHandler ( w , r )
2017-05-06 21:21:39 +02:00
return status , err
}
// RetrieveUser retrieves a user.
func RetrieveUser ( r * http . Request , id string ) ( * model . PublicUser , bool , uint , int , error ) {
2017-05-08 20:21:11 +02:00
var user model . User
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
var currentUserID uint
2017-05-06 21:21:39 +02:00
var isAuthor bool
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-20 16:26:22 +02:00
if db . ORM . First ( & user , id ) . RecordNotFound ( ) {
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
return nil , isAuthor , currentUserID , http . StatusNotFound , errors . New ( "user not found" )
2017-05-06 21:21:39 +02:00
}
currentUser , err := CurrentUser ( r )
if err == nil {
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
currentUserID = currentUser . ID
isAuthor = currentUser . ID == user . ID
2017-05-06 21:21:39 +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
return & model . PublicUser { User : & user } , isAuthor , currentUserID , http . StatusOK , nil
2017-05-06 21:21:39 +02:00
}
// RetrieveUsers retrieves users.
func RetrieveUsers ( ) [ ] * model . PublicUser {
var users [ ] * model . User
var userArr [ ] * model . PublicUser
for _ , user := range users {
userArr = append ( userArr , & model . PublicUser { User : user } )
}
return userArr
}
// UpdateUserCore updates a user. (Applying the modifed data of user).
func UpdateUserCore ( user * model . User ) ( int , error ) {
2017-05-09 17:20:19 +02:00
if user . Email == "" {
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
user . MD5 = ""
2017-05-09 17:20:19 +02:00
} else {
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
var err error
user . MD5 , err = crypto . GenerateMD5Hash ( user . Email )
if err != nil {
return http . StatusInternalServerError , err
}
2017-05-09 17:20:19 +02:00
}
2017-05-12 12:40:31 +02:00
user . UpdatedAt = time . Now ( )
err := db . ORM . Save ( user ) . Error
2017-05-06 21:21:39 +02:00
if err != nil {
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
return http . StatusInternalServerError , err
2017-05-06 21:21:39 +02:00
}
2017-05-09 17:20:19 +02:00
2017-05-06 21:21:39 +02:00
return http . StatusOK , nil
}
2017-05-27 03:54:54 +02:00
// UpdateRawUser : Function to update a user without updating his associations model
func UpdateRawUser ( user * model . User ) ( int , error ) {
user . UpdatedAt = time . Now ( )
err := db . ORM . Model ( & user ) . UpdateColumn ( & user ) . Error
if err != nil {
return http . StatusInternalServerError , err
}
return http . StatusOK , nil
}
2017-05-06 21:21:39 +02:00
// UpdateUser updates a user.
2017-05-22 00:22:42 +02:00
func UpdateUser ( w http . ResponseWriter , form * formStruct . UserForm , formSet * formStruct . UserSettingsForm , currentUser * model . User , id string ) ( model . User , int , error ) {
2017-05-06 21:21:39 +02:00
var user model . User
if db . ORM . First ( & user , id ) . RecordNotFound ( ) {
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
return user , http . StatusNotFound , errors . New ( "user not found" )
2017-05-09 17:47:06 +02:00
}
2017-05-10 20:24:37 +02:00
log . Infof ( "updateUser" )
2017-05-09 23:21:15 +02:00
if form . Password != "" {
2017-05-09 17:47:06 +02:00
err := bcrypt . CompareHashAndPassword ( [ ] byte ( user . Password ) , [ ] byte ( form . CurrentPassword ) )
if err != nil && ! userPermission . HasAdmin ( currentUser ) {
2017-05-06 21:21:39 +02:00
log . Error ( "Password Incorrect." )
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
return user , http . StatusInternalServerError , errors . New ( "password incorrect" )
2017-05-06 21:21:39 +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
newPassword , err := bcrypt . GenerateFromPassword ( [ ] byte ( form . Password ) , 10 )
if err != nil {
return user , http . StatusInternalServerError , errors . New ( "password not generated" )
}
form . Password = string ( newPassword )
2017-05-09 17:47:06 +02:00
} else { // Then no change of password
form . Password = user . Password
2017-05-06 21:21:39 +02:00
}
2017-05-09 17:47:06 +02:00
if ! userPermission . HasAdmin ( currentUser ) { // We don't want users to be able to modify some fields
form . Status = user . Status
form . Username = user . Username
}
2017-05-14 21:45:50 +02:00
if form . Email != user . Email {
2017-05-12 12:40:31 +02:00
// send verification to new email and keep old
2017-05-10 22:42:11 +02:00
SendVerificationToUser ( user , form . Email )
form . Email = user . Email
}
2017-05-09 23:21:15 +02:00
log . Debugf ( "form %+v\n" , form )
modelHelper . AssignValue ( & user , form )
2017-05-22 00:22:42 +02:00
// We set settings here
user . ParseSettings ( )
user . Settings . Set ( "new_torrent" , formSet . NewTorrent )
user . Settings . Set ( "new_torrent_email" , formSet . NewTorrentEmail )
user . Settings . Set ( "new_comment" , formSet . NewComment )
user . Settings . Set ( "new_comment_email" , formSet . NewCommentEmail )
user . Settings . Set ( "new_responses" , formSet . NewResponses )
user . Settings . Set ( "new_responses_email" , formSet . NewResponsesEmail )
user . Settings . Set ( "new_follower" , formSet . NewFollower )
user . Settings . Set ( "new_follower_email" , formSet . NewFollowerEmail )
user . Settings . Set ( "followed" , formSet . Followed )
user . Settings . Set ( "followed_email" , formSet . FollowedEmail )
user . SaveSettings ( )
2017-05-06 21:21:39 +02:00
status , err := UpdateUserCore ( & user )
2017-05-09 17:47:06 +02:00
return user , status , err
2017-05-06 21:21:39 +02:00
}
// DeleteUser deletes a user.
2017-05-09 17:47:06 +02:00
func DeleteUser ( w http . ResponseWriter , currentUser * model . User , id string ) ( int , error ) {
2017-05-06 21:21:39 +02:00
var user model . User
if db . ORM . First ( & user , id ) . RecordNotFound ( ) {
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
return http . StatusNotFound , errors . New ( "user not found" )
2017-05-06 21:21:39 +02:00
}
2017-05-14 21:45:50 +02:00
if user . ID == 0 {
2017-05-26 12:12:52 +02:00
return http . StatusInternalServerError , errors . New ( "You can't delete that" )
2017-05-10 22:55:44 +02:00
}
2017-05-06 21:21:39 +02:00
if db . ORM . Delete ( & user ) . Error != nil {
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
return http . StatusInternalServerError , errors . New ( "user not deleted" )
2017-05-06 21:21:39 +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 userPermission . CurrentUserIdentical ( currentUser , user . ID ) {
2017-05-09 23:21:15 +02:00
return ClearCookie ( w )
2017-05-09 17:47:06 +02:00
}
2017-05-10 22:55:44 +02:00
2017-05-09 17:47:06 +02:00
return http . StatusOK , nil
2017-05-06 21:21:39 +02:00
}
// RetrieveCurrentUser retrieves a current user.
func RetrieveCurrentUser ( r * http . Request ) ( model . User , int , error ) {
user , err := CurrentUser ( r )
if err != nil {
return user , http . StatusInternalServerError , err
}
return user , http . StatusOK , nil
}
// RetrieveUserByEmail retrieves a user by an email
2017-06-05 15:19:25 +02:00
func RetrieveUserByEmail ( email string ) ( * model . User , string , int , error ) {
2017-05-06 21:21:39 +02:00
var user model . User
2017-05-08 22:33:40 +02:00
if db . ORM . Unscoped ( ) . Where ( "email = ?" , email ) . First ( & user ) . RecordNotFound ( ) {
2017-06-05 15:19:25 +02:00
return & user , email , http . StatusNotFound , errors . New ( "user not found" )
2017-05-06 21:21:39 +02:00
}
2017-06-05 15:19:25 +02:00
return & user , email , http . StatusOK , nil
}
// RetrieveUserByAPIToken retrieves a user by an API token
func RetrieveUserByAPIToken ( apiToken string ) ( * model . User , string , int , error ) {
var user model . User
if db . ORM . Unscoped ( ) . Where ( "api_token = ?" , apiToken ) . First ( & user ) . RecordNotFound ( ) {
return & user , apiToken , http . StatusNotFound , errors . New ( "user not found" )
}
return & user , apiToken , http . StatusOK , nil
2017-05-06 21:21:39 +02:00
}
2017-06-13 08:01:57 +02:00
// RetrieveUserByAPIToken retrieves a user by an API token
func RetrieveUserByAPITokenAndName ( apiToken string , username string ) ( * model . User , string , string , int , error ) {
var user model . User
if db . ORM . Unscoped ( ) . Where ( "api_token = ? AND username = ?" , apiToken , username ) . First ( & user ) . RecordNotFound ( ) {
return & user , apiToken , username , http . StatusNotFound , errors . New ( "user not found" )
}
return & user , apiToken , username , http . StatusOK , nil
}
2017-05-06 21:21:39 +02:00
// RetrieveUsersByEmail retrieves users by an email
2017-06-05 15:19:25 +02:00
func RetrieveUsersByEmail ( email string ) [ ] * model . User {
2017-05-06 21:21:39 +02:00
var users [ ] * model . User
2017-05-08 22:33:40 +02:00
db . ORM . Where ( "email = ?" , email ) . Find ( & users )
2017-06-05 15:19:25 +02:00
return users
2017-05-06 21:21:39 +02:00
}
// RetrieveUserByUsername retrieves a user by username.
2017-06-05 15:19:25 +02:00
func RetrieveUserByUsername ( username string ) ( * model . User , string , int , error ) {
2017-05-06 21:21:39 +02:00
var user model . User
2017-05-08 22:33:40 +02:00
if db . ORM . Where ( "username = ?" , username ) . First ( & user ) . RecordNotFound ( ) {
2017-06-05 15:19:25 +02:00
return & user , username , http . StatusNotFound , errors . New ( "user not found" )
2017-05-06 21:21:39 +02:00
}
2017-06-05 15:19:25 +02:00
return & user , username , http . StatusOK , nil
2017-05-06 21:21:39 +02:00
}
2017-05-26 12:12:52 +02:00
// RetrieveOldUploadsByUsername retrieves olduploads by username
2017-05-13 17:29:21 +02:00
func RetrieveOldUploadsByUsername ( username string ) ( [ ] uint , error ) {
var ret [ ] uint
var tmp [ ] * model . UserUploadsOld
2017-05-20 16:26:22 +02:00
err := db . ORM . Where ( "username = ?" , username ) . Find ( & tmp ) . Error
2017-05-13 17:29:21 +02:00
if err != nil {
return ret , err
}
for _ , tmp2 := range tmp {
2017-05-26 12:12:52 +02:00
ret = append ( ret , tmp2 . TorrentID )
2017-05-13 17:29:21 +02:00
}
return ret , nil
}
2017-05-06 21:21:39 +02:00
// RetrieveUserForAdmin retrieves a user for an administrator.
func RetrieveUserForAdmin ( id string ) ( model . User , int , error ) {
var user model . User
2017-05-20 20:53:05 +02:00
if db . ORM . Preload ( "Notifications" ) . Preload ( "Torrents" ) . Last ( & user , id ) . RecordNotFound ( ) {
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
return user , http . StatusNotFound , errors . New ( "user not found" )
2017-05-06 21:21:39 +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
var liked , likings [ ] model . User
db . ORM . Joins ( "JOIN user_follows on user_follows.user_id=?" , user . ID ) . Where ( "users.user_id = user_follows.following" ) . Group ( "users.user_id" ) . Find ( & likings )
db . ORM . Joins ( "JOIN user_follows on user_follows.following=?" , user . ID ) . Where ( "users.user_id = user_follows.user_id" ) . Group ( "users.user_id" ) . Find ( & liked )
2017-05-27 00:45:18 +02:00
user . Followers = likings
user . Likings = liked
2017-05-06 21:21:39 +02:00
return user , http . StatusOK , nil
}
// RetrieveUsersForAdmin retrieves users for an administrator.
2017-05-10 15:08:38 +02:00
func RetrieveUsersForAdmin ( limit int , offset int ) ( [ ] model . User , int ) {
2017-05-06 21:21:39 +02:00
var users [ ] model . User
2017-05-10 15:08:38 +02:00
var nbUsers int
db . ORM . Model ( & users ) . Count ( & nbUsers )
db . ORM . Preload ( "Torrents" ) . Limit ( limit ) . Offset ( offset ) . Find ( & users )
return users , nbUsers
2017-05-06 21:21:39 +02:00
}
2017-05-27 00:45:18 +02:00
// GetLikings : Gets who is followed by the user
func GetLikings ( user * model . User ) * model . User {
2017-05-20 20:53:05 +02:00
var liked [ ] model . User
db . ORM . Joins ( "JOIN user_follows on user_follows.following=?" , user . ID ) . Where ( "users.user_id = user_follows.user_id" ) . Group ( "users.user_id" ) . Find ( & liked )
2017-05-27 00:45:18 +02:00
user . Likings = liked
2017-05-20 20:53:05 +02:00
return user
}
2017-05-26 12:12:52 +02:00
2017-05-27 00:45:18 +02:00
// GetFollowers : Gets who is following the user
func GetFollowers ( user * model . User ) * model . User {
2017-05-20 20:53:05 +02:00
var likings [ ] model . User
db . ORM . Joins ( "JOIN user_follows on user_follows.user_id=?" , user . ID ) . Where ( "users.user_id = user_follows.following" ) . Group ( "users.user_id" ) . Find ( & likings )
2017-05-27 00:45:18 +02:00
user . Followers = likings
2017-05-20 20:53:05 +02:00
return user
}
2017-05-06 21:21:39 +02:00
// CreateUserAuthentication creates user authentication.
func CreateUserAuthentication ( w http . ResponseWriter , r * http . Request ) ( int , error ) {
2017-05-07 00:10:40 +02:00
var form formStruct . LoginForm
2017-05-06 21:21:39 +02:00
modelHelper . BindValueForm ( & form , r )
2017-06-13 08:01:57 +02:00
user , status , err := CreateUserAuthenticationAPI ( r , & form )
if err != nil {
return status , err
}
status , err = SetCookieHandler ( w , r , user )
return status , err
}
// CreateUserAuthenticationAPI creates user authentication.
func CreateUserAuthenticationAPI ( r * http . Request , form * formStruct . LoginForm ) ( model . User , int , error ) {
2017-05-08 00:21:31 +02:00
username := form . Username
2017-05-06 21:21:39 +02:00
pass := form . Password
2017-06-13 08:01:57 +02:00
user , status , err := checkAuth ( r , username , pass )
return user , status , err
}
func checkAuth ( r * http . Request , email string , pass string ) ( model . User , int , error ) {
var user model . User
if email == "" || pass == "" {
return user , http . StatusNotFound , errors . New ( "No username/password entered" )
}
messages := msg . GetMessages ( r )
// search by email or username
isValidEmail := formStruct . EmailValidation ( email , messages )
messages . ClearErrors ( "email" ) // We need to clear the error added on messages
if isValidEmail {
if db . ORM . Where ( "email = ?" , email ) . First ( & user ) . RecordNotFound ( ) {
return user , http . StatusNotFound , errors . New ( "User not found" )
}
} else {
if db . ORM . Where ( "username = ?" , email ) . First ( & user ) . RecordNotFound ( ) {
return user , http . StatusNotFound , errors . New ( "User not found" )
}
}
err := bcrypt . CompareHashAndPassword ( [ ] byte ( user . Password ) , [ ] byte ( pass ) )
if err != nil {
return user , http . StatusUnauthorized , errors . New ( "Password incorrect" )
}
if user . IsBanned ( ) {
return user , http . StatusUnauthorized , errors . New ( "Account banned" )
}
if user . IsScraped ( ) {
return user , http . StatusUnauthorized , errors . New ( "Account need activation from Moderators, please contact us" )
}
return user , http . StatusOK , nil
2017-05-06 21:21:39 +02:00
}
2017-05-10 03:15:29 +02:00
2017-05-26 12:12:52 +02:00
// SetFollow : Makes a user follow another
2017-05-10 03:15:29 +02:00
func SetFollow ( user * model . User , follower * model . User ) {
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 follower . ID > 0 && user . ID > 0 {
var userFollows = model . UserFollows { UserID : user . ID , FollowerID : follower . ID }
2017-05-10 03:15:29 +02:00
db . ORM . Create ( & userFollows )
}
}
2017-05-26 12:12:52 +02:00
// RemoveFollow : Remove a user following another
2017-05-10 03:15:29 +02:00
func RemoveFollow ( user * model . User , follower * model . User ) {
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 follower . ID > 0 && user . ID > 0 {
var userFollows = model . UserFollows { UserID : user . ID , FollowerID : follower . ID }
2017-05-10 03:15:29 +02:00
db . ORM . Delete ( & userFollows )
}
2017-05-10 04:03:25 +02:00
}