Switch to new schema: make it compile
Cette révision appartient à :
Parent
e3a271cca7
révision
a3d13f768a
13 fichiers modifiés avec 101 ajouts et 193 suppressions
|
@ -31,8 +31,8 @@ func GormInit(conf *config.Config) (*gorm.DB, error) {
|
||||||
if config.Environment == "DEVELOPMENT" {
|
if config.Environment == "DEVELOPMENT" {
|
||||||
db.LogMode(true)
|
db.LogMode(true)
|
||||||
// db.DropTable(&model.User{}, "UserFollower")
|
// db.DropTable(&model.User{}, "UserFollower")
|
||||||
db.AutoMigrate(&model.Torrents{}, &model.UsersFollowers{}, &model.User{}, &model.Role{}, &model.Language{}, &model.Comment{})
|
db.AutoMigrate(&model.User{}, &model.UserFollows{})
|
||||||
// db.AutoMigrate(&model.Comment{})
|
db.AutoMigrate(&model.User{}, &model.Torrents{}, &model.Comment{}, &model.OldComment{})
|
||||||
// db.Model(&model.User{}).AddIndex("idx_user_token", "token")
|
// db.Model(&model.User{}).AddIndex("idx_user_token", "token")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,16 +4,28 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Comment is a comment model.
|
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
Id int `json:"id"`
|
Id uint `gorm:"column:comment_id;primary_key"`
|
||||||
Content string `json:"content"`
|
TorrentId uint `gorm:"column:torrent_id"`
|
||||||
UserId int `json:"userId"`
|
UserId uint `gorm:"column:user_id"`
|
||||||
Username string `json:"username"` // this is duplicate but it'll be faster rite?
|
Content string `gorm:"column:content"`
|
||||||
TorrentId int
|
CreatedAt time.Time `gorm:"column:created_at"`
|
||||||
// LikingCount int `json:"likingCount"`
|
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
Torrent *Torrents `gorm:"ForeignKey:TorrentId"`
|
||||||
DeletedAt *time.Time `json:"deletedAt""`
|
User *User `gorm:"ForeignKey:UserId"`
|
||||||
User User `json:"user"`
|
}
|
||||||
|
|
||||||
|
type OldComment struct {
|
||||||
|
TorrentId uint `gorm:"column:torrent_id"`
|
||||||
|
Username string `gorm:"column:username"`
|
||||||
|
Content string `gorm:"column:content"`
|
||||||
|
Date time.Time `gorm:"column:date"`
|
||||||
|
|
||||||
|
Torrent *Torrents `gorm:"ForeignKey:TorrentId"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c OldComment) TableName() string {
|
||||||
|
// cba to renamed this in the db
|
||||||
|
return "comments_old"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
package model
|
|
||||||
|
|
||||||
// Role is a role model for user permission.
|
|
||||||
type Role struct {
|
|
||||||
Id uint `json:"id"`
|
|
||||||
Name string `json:"name",sql:"size:255"`
|
|
||||||
Description string `json:"description",sql:"size:255"`
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
package model
|
|
||||||
|
|
||||||
//user status e.g. verified, filtered, etc
|
|
||||||
type Status struct {
|
|
||||||
Id int `json:"id"`
|
|
||||||
Name string `json:"name",sql:"size:255"`
|
|
||||||
}
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"github.com/ewhal/nyaa/config"
|
"github.com/ewhal/nyaa/config"
|
||||||
"github.com/ewhal/nyaa/util"
|
"github.com/ewhal/nyaa/util"
|
||||||
|
|
||||||
"encoding/json"
|
// "encoding/json"
|
||||||
"html"
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -21,18 +21,23 @@ type Feed struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Torrents struct {
|
type Torrents struct {
|
||||||
Id int `gorm:"column:torrent_id;primary_key"`
|
Id uint `gorm:"column:torrent_id;primary_key"`
|
||||||
Name string `gorm:"column:torrent_name"`
|
Name string `gorm:"column:torrent_name"`
|
||||||
Category int `gorm:"column:category_id"`
|
Hash string `gorm:"column:torrent_hash"`
|
||||||
Sub_Category int `gorm:"column:sub_category_id"`
|
Category int `gorm:"column:category"`
|
||||||
Status int `gorm:"column:status_id"`
|
Sub_Category int `gorm:"column:sub_category"`
|
||||||
Hash string `gorm:"column:torrent_hash"`
|
Status int `gorm:"column:status"`
|
||||||
Date int64 `gorm:"column:date"`
|
Date time.Time `gorm:"column:date"`
|
||||||
Downloads int `gorm:"column:downloads"`
|
UploaderId uint `gorm:"column:uploader"`
|
||||||
Filesize int64 `gorm:"column:filesize"`
|
Downloads int `gorm:"column:downloads"`
|
||||||
Description string `gorm:"column:description"`
|
Stardom int `gorm:"column:stardom"`
|
||||||
OldComments []byte `gorm:"column:comments"`
|
Filesize int64 `gorm:"column:filesize"`
|
||||||
Comments []Comment `gorm:"ForeignKey:TorrentId"`
|
Description string `gorm:"column:description"`
|
||||||
|
WebsiteLink string `gorm:"column:website_link"`
|
||||||
|
|
||||||
|
Uploader *User `gorm:"ForeignKey:UploaderId"`
|
||||||
|
OldComments []OldComment `gorm:"ForeignKey:Id"`
|
||||||
|
Comments []Comment `gorm:"ForeignKey:Id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically
|
/* We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically
|
||||||
|
@ -80,9 +85,9 @@ type TorrentsJson struct {
|
||||||
|
|
||||||
func (t *Torrents) ToJson() TorrentsJson {
|
func (t *Torrents) ToJson() TorrentsJson {
|
||||||
magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...)
|
magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...)
|
||||||
offset := 0
|
//offset := 0
|
||||||
var commentsJson []CommentsJson
|
var commentsJson []CommentsJson
|
||||||
if len(t.OldComments) != 0 {
|
/*if len(t.OldComments) != 0 {
|
||||||
b := []OldCommentsJson{}
|
b := []OldCommentsJson{}
|
||||||
err := json.Unmarshal([]byte(t.OldComments), &b)
|
err := json.Unmarshal([]byte(t.OldComments), &b)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -100,13 +105,13 @@ func (t *Torrents) ToJson() TorrentsJson {
|
||||||
}
|
}
|
||||||
for i, comment := range t.Comments {
|
for i, comment := range t.Comments {
|
||||||
commentsJson[i+offset] = CommentsJson{Content: template.HTML(comment.Content), Username: comment.Username}
|
commentsJson[i+offset] = CommentsJson{Content: template.HTML(comment.Content), Username: comment.Username}
|
||||||
}
|
}*/
|
||||||
res := TorrentsJson{
|
res := TorrentsJson{
|
||||||
Id: strconv.Itoa(t.Id),
|
Id: strconv.FormatUint(uint64(t.Id), 10),
|
||||||
Name: html.UnescapeString(t.Name),
|
Name: html.UnescapeString(t.Name),
|
||||||
Status: t.Status,
|
Status: t.Status,
|
||||||
Hash: t.Hash,
|
Hash: t.Hash,
|
||||||
Date: time.Unix(t.Date, 0).Format(time.RFC3339),
|
Date: t.Date.Format(time.RFC3339),
|
||||||
Filesize: util.FormatFilesize2(t.Filesize),
|
Filesize: util.FormatFilesize2(t.Filesize),
|
||||||
Description: template.HTML(t.Description),
|
Description: template.HTML(t.Description),
|
||||||
Comments: commentsJson,
|
Comments: commentsJson,
|
||||||
|
|
|
@ -7,78 +7,27 @@ import (
|
||||||
// omit is the bool type for omitting a field of struct.
|
// omit is the bool type for omitting a field of struct.
|
||||||
type omit bool
|
type omit bool
|
||||||
|
|
||||||
// User is a user model
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id uint `json:"id"`
|
Id uint `gorm:"column:user_id;primary_key"`
|
||||||
|
Username string `gorm:"column:username"`
|
||||||
Email string `json:"email",sql:"size:255;unique"`
|
Password string `gorm:"column:password"`
|
||||||
Password string `json:"password",sql:"size:255"`
|
Email string `gorm:"column:email"`
|
||||||
Username string `json:"username",sql:"size:255;unique"`
|
Status int `gorm:"column:status"`
|
||||||
Description string `json:"description",sql:"size:100"`
|
CreatedAt time.Time `gorm:"column:created_at"`
|
||||||
Token string `json:"token"`
|
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||||
TokenExpiration time.Time `json:"tokenExperiation"`
|
/*Api*/Token string `gorm:"column:api_token"`
|
||||||
|
//ApiTokenExpiry
|
||||||
// email md5 for gravatar
|
TokenExpiration time.Time `gorm:"column:api_token_expiry"`
|
||||||
Md5 string `json:"md5"`
|
Language string `gorm:"column:language"`
|
||||||
|
|
||||||
// admin
|
|
||||||
Activation bool `json:"activation"`
|
|
||||||
PasswordResetToken string `json:"passwordResetToken"`
|
|
||||||
ActivationToken string `json:"activationToken"`
|
|
||||||
PasswordResetUntil time.Time `json:"passwordResetUntil"`
|
|
||||||
ActivateUntil time.Time `json:"activateUntil"`
|
|
||||||
ActivatedAt time.Time `json:"activatedAt"`
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
|
||||||
DeletedAt *time.Time `json:"deletedAt"`
|
|
||||||
LastLoginAt time.Time `json:"lastLoginAt"`
|
|
||||||
CurrentLoginAt time.Time `json:"currentLoginAt"`
|
|
||||||
LastLoginIp string `json:"lastLoginIp",sql:"size:100"`
|
|
||||||
CurrentLoginIp string `json:"currentLoginIp",sql:"size:100"`
|
|
||||||
|
|
||||||
// Liking
|
|
||||||
LikingCount int `json:"likingCount"`
|
|
||||||
LikedCount int `json:"likedCount"`
|
|
||||||
Likings []User `gorm:"foreignkey:userId;associationforeignkey:follower_id;many2many:users_followers;"`
|
|
||||||
Liked []User `gorm:"foreignkey:follower_id;associationforeignkey:userId;many2many:users_followers;"`
|
|
||||||
|
|
||||||
//Connections []Connection
|
|
||||||
|
|
||||||
Languages string
|
|
||||||
Roles []Role `gorm:"many2many:users_roles;"` // Many To Many, users_roles
|
|
||||||
Torrents []Torrents
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UsersFollowers is a relation table to relate users each other.
|
|
||||||
type UsersFollowers struct {
|
|
||||||
UserID uint `json:"user_id"`
|
|
||||||
FollowerID uint `json:"follower_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// PublicUser is a public user model that contains only a few information for everyone.
|
|
||||||
type PublicUser struct {
|
type PublicUser struct {
|
||||||
*User
|
User *User
|
||||||
Email omit `json:"email,omitempty",sql:"size:255;unique"`
|
|
||||||
Password omit `json:"password,omitempty",sql:"size:255"`
|
|
||||||
Token omit `json:"token,omitempty"`
|
|
||||||
TokenExpiration omit `json:"tokenExperiation,omitempty"`
|
|
||||||
|
|
||||||
// admin
|
|
||||||
Activation omit `json:"activation,omitempty"`
|
|
||||||
PasswordResetToken omit `json:"passwordResetToken,omitempty"`
|
|
||||||
ActivationToken omit `json:"activationToken,omitempty"`
|
|
||||||
PasswordResetUntil omit `json:"passwordResetUntil,omitempty"`
|
|
||||||
ActivateUntil omit `json:"activateUntil,omitempty"`
|
|
||||||
ActivatedAt omit `json:"activatedAt,omitempty"`
|
|
||||||
UpdatedAt omit `json:"updatedAt,omitempty"`
|
|
||||||
DeletedAt omit `json:"deletedAt,omitempty"`
|
|
||||||
LastLoginAt omit `json:"lastLoginAt,omitempty"`
|
|
||||||
CurrentLoginAt omit `json:"currentLoginAt,omitempty"`
|
|
||||||
LastLoginIp omit `json:"lastLoginIp,omitempty",sql:"size:100"`
|
|
||||||
CurrentLoginIp omit `json:"currentLoginIp,omitempty",sql:"size:100"`
|
|
||||||
|
|
||||||
//Connections omit `json:"connections,omitempty"`
|
|
||||||
Languages omit `json:"languages,omitempty"`
|
|
||||||
Roles omit `json:"roles,omitempty"`
|
|
||||||
Torrents omit `json:"articles,omitempty"` //should user torrents not be displayed?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserFollows struct {
|
||||||
|
User User `gorm:"ForeignKey:user_id"`
|
||||||
|
Following User `gorm:"ForeignKey:following"`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
Sub_Category: sub_category,
|
Sub_Category: sub_category,
|
||||||
Status: 1,
|
Status: 1,
|
||||||
Hash: b.Hash,
|
Hash: b.Hash,
|
||||||
Date: time.Now().Unix(),
|
Date: time.Now(),
|
||||||
Filesize: 0,
|
Filesize: 0,
|
||||||
Description: string(b.Description)}
|
Description: string(b.Description)}
|
||||||
db.ORM.Create(&torrent)
|
db.ORM.Create(&torrent)
|
||||||
|
|
|
@ -15,7 +15,7 @@ func RssHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
created_as_time := time.Now()
|
created_as_time := time.Now()
|
||||||
|
|
||||||
if len(torrents) > 0 {
|
if len(torrents) > 0 {
|
||||||
created_as_time = time.Unix(torrents[0].Date, 0)
|
created_as_time = torrents[0].Date
|
||||||
}
|
}
|
||||||
feed := &feeds.Feed{
|
feed := &feeds.Feed{
|
||||||
Title: "Nyaa Pantsu",
|
Title: "Nyaa Pantsu",
|
||||||
|
@ -26,16 +26,15 @@ func RssHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
feed.Items = make([]*feeds.Item, len(torrents))
|
feed.Items = make([]*feeds.Item, len(torrents))
|
||||||
|
|
||||||
for i, _ := range torrents {
|
for i, _ := range torrents {
|
||||||
timestamp_as_time := time.Unix(torrents[0].Date, 0)
|
|
||||||
torrent_json := torrents[i].ToJson()
|
torrent_json := torrents[i].ToJson()
|
||||||
feed.Items[i] = &feeds.Item{
|
feed.Items[i] = &feeds.Item{
|
||||||
// need a torrent view first
|
// need a torrent view first
|
||||||
Id: "https://nyaa.pantsu.cat/view/" + strconv.Itoa(torrents[i].Id),
|
Id: "https://" + config.WebAddress + "/view/" + strconv.FormatUint(uint64(torrents[i].Id), 10),
|
||||||
Title: torrents[i].Name,
|
Title: torrents[i].Name,
|
||||||
Link: &feeds.Link{Href: string(torrent_json.Magnet)},
|
Link: &feeds.Link{Href: string(torrent_json.Magnet)},
|
||||||
Description: "",
|
Description: "",
|
||||||
Created: timestamp_as_time,
|
Created: torrents[0].Date,
|
||||||
Updated: timestamp_as_time,
|
Updated: torrents[0].Date,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,12 +33,12 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
Sub_Category: uploadForm.SubCategoryId,
|
Sub_Category: uploadForm.SubCategoryId,
|
||||||
Status: 1,
|
Status: 1,
|
||||||
Hash: uploadForm.Infohash,
|
Hash: uploadForm.Infohash,
|
||||||
Date: time.Now().Unix(),
|
Date: time.Now(),
|
||||||
Filesize: uploadForm.Filesize, // FIXME: should set to NULL instead of 0
|
Filesize: uploadForm.Filesize, // FIXME: should set to NULL instead of 0
|
||||||
Description: uploadForm.Description}
|
Description: uploadForm.Description}
|
||||||
db.ORM.Create(&torrent)
|
db.ORM.Create(&torrent)
|
||||||
fmt.Printf("%+v\n", torrent)
|
fmt.Printf("%+v\n", torrent)
|
||||||
url, err := Router.Get("view_torrent").URL("id", strconv.Itoa(torrent.Id))
|
url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.Id), 10))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
http.Redirect(w, r, url.String(), 302)
|
http.Redirect(w, r, url.String(), 302)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package router
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ewhal/nyaa/db"
|
"github.com/ewhal/nyaa/db"
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
|
@ -41,14 +42,13 @@ func PostCommentHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
currentUser := GetUser(r)
|
currentUser := GetUser(r)
|
||||||
content := p.Sanitize(r.FormValue("comment"))
|
content := p.Sanitize(r.FormValue("comment"))
|
||||||
|
|
||||||
idNum, err := strconv.Atoi(id)
|
idNum_, err := strconv.Atoi(id)
|
||||||
username := "れんちょん"
|
var idNum uint = uint(idNum_)
|
||||||
userId := 0
|
var userId uint = 0
|
||||||
if (currentUser.Id > 0) {
|
if (currentUser.Id > 0) {
|
||||||
username = currentUser.Username
|
userId = currentUser.Id
|
||||||
userId = int(currentUser.Id)
|
|
||||||
}
|
}
|
||||||
comment := model.Comment{Username: username, UserId: userId, Content: content, TorrentId: idNum}
|
comment := model.Comment{TorrentId: idNum, UserId: userId, Content: content, CreatedAt: time.Now()}
|
||||||
db.ORM.Create(&comment)
|
db.ORM.Create(&comment)
|
||||||
|
|
||||||
url, err := Router.Get("view_torrent").URL("id", id)
|
url, err := Router.Get("view_torrent").URL("id", id)
|
||||||
|
|
|
@ -150,6 +150,6 @@ func CurrentUser(r *http.Request) (model.User, error) {
|
||||||
if db.ORM.Select(config.UserPublicFields+", email").Where("token = ?", token).First(&user).RecordNotFound() {
|
if db.ORM.Select(config.UserPublicFields+", email").Where("token = ?", token).First(&user).RecordNotFound() {
|
||||||
return user, errors.New("User is not found.")
|
return user, errors.New("User is not found.")
|
||||||
}
|
}
|
||||||
db.ORM.Model(&user).Association("Roles").Find(&user.Roles)
|
db.ORM.Model(&user)
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
|
@ -56,7 +57,6 @@ func CreateUserFromForm(registrationForm formStruct.RegistrationForm) (model.Use
|
||||||
var user model.User
|
var user model.User
|
||||||
log.Debugf("registrationForm %+v\n", registrationForm)
|
log.Debugf("registrationForm %+v\n", registrationForm)
|
||||||
modelHelper.AssignValue(&user, ®istrationForm)
|
modelHelper.AssignValue(&user, ®istrationForm)
|
||||||
user.Md5 = crypto.GenerateMD5Hash(user.Email) // Gravatar
|
|
||||||
token, err := crypto.GenerateRandomToken32()
|
token, err := crypto.GenerateRandomToken32()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return user, errors.New("Token not generated.")
|
return user, errors.New("Token not generated.")
|
||||||
|
@ -101,7 +101,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
|
|
||||||
// RetrieveUser retrieves a user.
|
// RetrieveUser retrieves a user.
|
||||||
func RetrieveUser(r *http.Request, id string) (*model.PublicUser, bool, uint, int, error) {
|
func RetrieveUser(r *http.Request, id string) (*model.PublicUser, bool, uint, int, error) {
|
||||||
var user model.User
|
/*var user model.User
|
||||||
var currentUserId uint
|
var currentUserId uint
|
||||||
var isAuthor bool
|
var isAuthor bool
|
||||||
// var publicUser *model.PublicUser
|
// var publicUser *model.PublicUser
|
||||||
|
@ -139,7 +139,8 @@ func RetrieveUser(r *http.Request, id string) (*model.PublicUser, bool, uint, in
|
||||||
|
|
||||||
log.Debugf("user liking %v\n", user.Likings)
|
log.Debugf("user liking %v\n", user.Likings)
|
||||||
log.Debugf("user liked %v\n", user.Liked)
|
log.Debugf("user liked %v\n", user.Liked)
|
||||||
return &model.PublicUser{User: &user}, isAuthor, currentUserId, http.StatusOK, nil
|
return &model.PublicUser{User: &user}, isAuthor, currentUserId, http.StatusOK, nil*/
|
||||||
|
return nil, false, 0, 0, errors.New("NotImpl")
|
||||||
}
|
}
|
||||||
|
|
||||||
// RetrieveUsers retrieves users.
|
// RetrieveUsers retrieves users.
|
||||||
|
@ -155,7 +156,6 @@ func RetrieveUsers() []*model.PublicUser {
|
||||||
|
|
||||||
// UpdateUserCore updates a user. (Applying the modifed data of user).
|
// UpdateUserCore updates a user. (Applying the modifed data of user).
|
||||||
func UpdateUserCore(user *model.User) (int, error) {
|
func UpdateUserCore(user *model.User) (int, error) {
|
||||||
user.Md5 = crypto.GenerateMD5Hash(user.Email)
|
|
||||||
token, err := crypto.GenerateRandomToken32()
|
token, err := crypto.GenerateRandomToken32()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, errors.New("Token not generated.")
|
return http.StatusInternalServerError, errors.New("Token not generated.")
|
||||||
|
@ -165,6 +165,7 @@ func UpdateUserCore(user *model.User) (int, error) {
|
||||||
if db.ORM.Save(user).Error != nil {
|
if db.ORM.Save(user).Error != nil {
|
||||||
return http.StatusInternalServerError, errors.New("User is not updated.")
|
return http.StatusInternalServerError, errors.New("User is not updated.")
|
||||||
}
|
}
|
||||||
|
user.UpdatedAt = time.Now()
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,49 +221,6 @@ func DeleteUser(w http.ResponseWriter, id string) (int, error) {
|
||||||
return status, err
|
return status, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddRoleToUser adds a role to a user.
|
|
||||||
func AddRoleToUser(r *http.Request) (int, error) {
|
|
||||||
var form formStruct.UserRoleForm
|
|
||||||
var user model.User
|
|
||||||
var role model.Role
|
|
||||||
var roles []model.Role
|
|
||||||
modelHelper.BindValueForm(&form, r)
|
|
||||||
|
|
||||||
if db.ORM.First(&user, form.UserId).RecordNotFound() {
|
|
||||||
return http.StatusNotFound, errors.New("User is not found.")
|
|
||||||
}
|
|
||||||
if db.ORM.First(&role, form.RoleId).RecordNotFound() {
|
|
||||||
return http.StatusNotFound, errors.New("Role is not found.")
|
|
||||||
}
|
|
||||||
log.Debugf("user email : %s", user.Email)
|
|
||||||
log.Debugf("Role name : %s", role.Name)
|
|
||||||
db.ORM.Model(&user).Association("Roles").Append(role)
|
|
||||||
db.ORM.Model(&user).Association("Roles").Find(&roles)
|
|
||||||
if db.ORM.Save(&user).Error != nil {
|
|
||||||
return http.StatusInternalServerError, errors.New("Role not appended to user.")
|
|
||||||
}
|
|
||||||
return http.StatusOK, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoveRoleFromUser removes a role from a user.
|
|
||||||
func RemoveRoleFromUser(w http.ResponseWriter, r *http.Request, userId string, roleId string) (int, error) {
|
|
||||||
var user model.User
|
|
||||||
var role model.Role
|
|
||||||
if db.ORM.First(&user, userId).RecordNotFound() {
|
|
||||||
return http.StatusNotFound, errors.New("User is not found.")
|
|
||||||
}
|
|
||||||
if db.ORM.First(&role, roleId).RecordNotFound() {
|
|
||||||
return http.StatusNotFound, errors.New("Role is not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("user : %v\n", user)
|
|
||||||
log.Debugf("role : %v\n", role)
|
|
||||||
if db.ORM.Model(&user).Association("Roles").Delete(role).Error != nil {
|
|
||||||
return http.StatusInternalServerError, errors.New("Role is not deleted from user.")
|
|
||||||
}
|
|
||||||
return http.StatusOK, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RetrieveCurrentUser retrieves a current user.
|
// RetrieveCurrentUser retrieves a current user.
|
||||||
func RetrieveCurrentUser(r *http.Request) (model.User, int, error) {
|
func RetrieveCurrentUser(r *http.Request) (model.User, int, error) {
|
||||||
user, err := CurrentUser(r)
|
user, err := CurrentUser(r)
|
||||||
|
@ -307,8 +265,7 @@ func RetrieveUserForAdmin(id string) (model.User, int, error) {
|
||||||
if db.ORM.First(&user, id).RecordNotFound() {
|
if db.ORM.First(&user, id).RecordNotFound() {
|
||||||
return user, http.StatusNotFound, errors.New("User is not found.")
|
return user, http.StatusNotFound, errors.New("User is not found.")
|
||||||
}
|
}
|
||||||
db.ORM.Model(&user).Association("Languages").Find(&user.Languages)
|
db.ORM.Model(&user)
|
||||||
db.ORM.Model(&user).Association("Roles").Find(&user.Roles)
|
|
||||||
return user, http.StatusOK, nil
|
return user, http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,8 +275,7 @@ func RetrieveUsersForAdmin() []model.User {
|
||||||
var userArr []model.User
|
var userArr []model.User
|
||||||
db.ORM.Find(&users)
|
db.ORM.Find(&users)
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
db.ORM.Model(&user).Association("Languages").Find(&user.Languages)
|
db.ORM.Model(&user)
|
||||||
db.ORM.Model(&user).Association("Roles").Find(&user.Roles)
|
|
||||||
userArr = append(userArr, user)
|
userArr = append(userArr, user)
|
||||||
}
|
}
|
||||||
return userArr
|
return userArr
|
||||||
|
@ -328,7 +284,7 @@ func RetrieveUsersForAdmin() []model.User {
|
||||||
// ActivateUser toggle activation of a user.
|
// ActivateUser toggle activation of a user.
|
||||||
func ActivateUser(r *http.Request, id string) (model.User, int, error) {
|
func ActivateUser(r *http.Request, id string) (model.User, int, error) {
|
||||||
var user model.User
|
var user model.User
|
||||||
var form formStruct.ActivateForm
|
/*var form formStruct.ActivateForm
|
||||||
modelHelper.BindValueForm(&form, r)
|
modelHelper.BindValueForm(&form, r)
|
||||||
if db.ORM.First(&user, id).RecordNotFound() {
|
if db.ORM.First(&user, id).RecordNotFound() {
|
||||||
return user, http.StatusNotFound, errors.New("User is not found.")
|
return user, http.StatusNotFound, errors.New("User is not found.")
|
||||||
|
@ -337,7 +293,8 @@ func ActivateUser(r *http.Request, id string) (model.User, int, error) {
|
||||||
if db.ORM.Save(&user).Error != nil {
|
if db.ORM.Save(&user).Error != nil {
|
||||||
return user, http.StatusInternalServerError, errors.New("User not activated.")
|
return user, http.StatusInternalServerError, errors.New("User not activated.")
|
||||||
}
|
}
|
||||||
return user, http.StatusOK, nil
|
return user, http.StatusOK, nil*/
|
||||||
|
return user, 0, errors.New("NotImpl")
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateUserAuthentication creates user authentication.
|
// CreateUserAuthentication creates user authentication.
|
||||||
|
|
|
@ -3,15 +3,15 @@ package userService
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
// "time"
|
||||||
|
|
||||||
"github.com/ewhal/nyaa/config"
|
"github.com/ewhal/nyaa/config"
|
||||||
"github.com/ewhal/nyaa/db"
|
"github.com/ewhal/nyaa/db"
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
"github.com/ewhal/nyaa/util/crypto"
|
// "github.com/ewhal/nyaa/util/crypto"
|
||||||
"github.com/ewhal/nyaa/util/email"
|
"github.com/ewhal/nyaa/util/email"
|
||||||
"github.com/ewhal/nyaa/util/log"
|
// "github.com/ewhal/nyaa/util/log"
|
||||||
"github.com/ewhal/nyaa/util/timeHelper"
|
// "github.com/ewhal/nyaa/util/timeHelper"
|
||||||
|
|
||||||
"github.com/nicksnyder/go-i18n/i18n"
|
"github.com/nicksnyder/go-i18n/i18n"
|
||||||
)
|
)
|
||||||
|
@ -28,7 +28,7 @@ func SendEmailVerfication(to string, token string, locale string) error {
|
||||||
|
|
||||||
// SendVerificationToUser sends an email verification token to user.
|
// SendVerificationToUser sends an email verification token to user.
|
||||||
func SendVerificationToUser(user model.User) (int, error) {
|
func SendVerificationToUser(user model.User) (int, error) {
|
||||||
var status int
|
/*var status int
|
||||||
var err error
|
var err error
|
||||||
user.ActivateUntil = timeHelper.TwentyFourHoursLater()
|
user.ActivateUntil = timeHelper.TwentyFourHoursLater()
|
||||||
user.ActivationToken, err = crypto.GenerateRandomToken32()
|
user.ActivationToken, err = crypto.GenerateRandomToken32()
|
||||||
|
@ -45,12 +45,12 @@ func SendVerificationToUser(user model.User) (int, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
return http.StatusOK, err
|
return http.StatusOK, err*/
|
||||||
|
return 0, errors.New("NotImpl")
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendVerification sends an email verification token.
|
// SendVerification sends an email verification token.
|
||||||
func SendVerification(r *http.Request) (int, error) {
|
func SendVerification(r *http.Request) (int, error) {
|
||||||
|
|
||||||
var user model.User
|
var user model.User
|
||||||
currentUser, err := CurrentUser(r)
|
currentUser, err := CurrentUser(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -65,7 +65,7 @@ func SendVerification(r *http.Request) (int, error) {
|
||||||
|
|
||||||
// EmailVerification verifies an email of user.
|
// EmailVerification verifies an email of user.
|
||||||
func EmailVerification(token string,w http.ResponseWriter) (int, error) {
|
func EmailVerification(token string,w http.ResponseWriter) (int, error) {
|
||||||
var user model.User
|
/*var user model.User
|
||||||
log.Debugf("verifyEmailForm.ActivationToken : %s", token)
|
log.Debugf("verifyEmailForm.ActivationToken : %s", token)
|
||||||
if db.ORM.Where(&model.User{ActivationToken: token}).First(&user).RecordNotFound() {
|
if db.ORM.Where(&model.User{ActivationToken: token}).First(&user).RecordNotFound() {
|
||||||
return http.StatusNotFound, errors.New("User is not found.")
|
return http.StatusNotFound, errors.New("User is not found.")
|
||||||
|
@ -85,5 +85,6 @@ func EmailVerification(token string,w http.ResponseWriter) (int, error) {
|
||||||
return status, err
|
return status, err
|
||||||
}
|
}
|
||||||
status, err = SetCookie(w, user.Token)
|
status, err = SetCookie(w, user.Token)
|
||||||
return status, err
|
return status, err*/
|
||||||
|
return 0, errors.New("NotImpl")
|
||||||
}
|
}
|
||||||
|
|
Référencer dans un nouveau ticket